tsdb.h 2.9 KB

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