tsdb.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #ifndef TSDB_CPP_HPP
  3. #define TSDB_CPP_HPP
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <memory>
  7. #include <string_view>
  8. #include <vector>
  9. #ifdef _WIN32
  10. #define TSDB_EXPORT __declspec(dllexport)
  11. #else
  12. #define TSDB_EXPORT
  13. #endif
  14. namespace tsdb_cpp {
  15. /// @brief A point in time series database.
  16. struct point {
  17. const std::string& name_;
  18. double value_;
  19. long long nanoseconds_;
  20. point(const std::string& name, double value, long long nanoseconds) : name_(name), value_(value), nanoseconds_(nanoseconds) {}
  21. };
  22. struct tsdb_batch_insertion {
  23. struct impl;
  24. ~tsdb_batch_insertion();
  25. tsdb_batch_insertion(tsdb_batch_insertion&& other);
  26. /// @brief Closes the batch. Will also commit the batch if it has not been committed yet.
  27. /// It is recommended to always call this method before dropping the object.
  28. void close();
  29. void add_point(std::string_view name, double value, long long nanoseconds);
  30. private:
  31. friend struct tsdb_entry;
  32. friend struct tsdb_entry_impl;
  33. tsdb_batch_insertion(std::unique_ptr<impl> impl);
  34. std::unique_ptr<impl> impl_;
  35. };
  36. struct tsdb_entry_impl;
  37. /// @brief A time series database.
  38. struct tsdb_entry {
  39. tsdb_entry(std::string_view remote, uint16_t port = 9000);
  40. ~tsdb_entry();
  41. tsdb_entry(tsdb_entry&& other);
  42. void close();
  43. void create_table();
  44. void create_memory_table();
  45. void drop_table();
  46. void drop_memory_table();
  47. // By default creates a local table (`tsdb_cpp`) and a distributed table (`tsdb_cpp_dist`) with the same schema.
  48. void create_dist_table(std::string_view cluster_name);
  49. /// @brief Sets the async insert buffer size. 0 means synchronous insert.
  50. /// WARN: With async insertion, you lose the ability to catch exceptions from the server.
  51. /// Backpressure is handled in a best-effort manner.
  52. void set_async_insert_buffer_size(size_t size);
  53. size_t get_async_insert_buffer_size() const;
  54. void set_active_table(std::string_view table_name);
  55. void set_metric_name(std::string_view metric_name);
  56. void insert_point(const point& p);
  57. void insert_points(const std::vector<point>& points);
  58. void insert_point(std::string_view name, double value, long long nanoseconds);
  59. tsdb_batch_insertion new_batch_insert();
  60. private:
  61. std::unique_ptr<tsdb_entry_impl> impl_;
  62. };
  63. } // namespace tsdb_cpp
  64. #endif // TSDB_CPP_HPP