tsdb.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma once
  2. #include <cstddef>
  3. #ifndef TSDB_CPP_HPP
  4. #define TSDB_CPP_HPP
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <memory>
  8. #include <string>
  9. #include <string_view>
  10. #include <vector>
  11. #include "tsdb.rust.h"
  12. #ifdef _WIN32
  13. #define TSDB_EXPORT __declspec(dllexport)
  14. #else
  15. #define TSDB_EXPORT
  16. #endif
  17. namespace tsdb_cpp {
  18. /// @brief A point in time series database.
  19. struct point {
  20. const std::string& name_;
  21. double value_;
  22. long long nanoseconds_;
  23. point(const std::string& name, double value, long long nanoseconds) : name_(name), value_(value), nanoseconds_(nanoseconds) {}
  24. };
  25. struct tsdb_batch_insertion {
  26. using UniqPtr = std::unique_ptr<TsdbEntryInsertBatch_t, decltype(&rust_tsdb_lf_entry_insert_batch_drop)>;
  27. tsdb_batch_insertion(std::nullptr_t) : impl_(nullptr, rust_tsdb_lf_entry_insert_batch_drop) {}
  28. ~tsdb_batch_insertion() {
  29. if (impl_) {
  30. impl_ = nullptr;
  31. }
  32. }
  33. tsdb_batch_insertion(tsdb_batch_insertion&& other) : impl_(std::move(other.impl_)) {}
  34. tsdb_batch_insertion& operator=(tsdb_batch_insertion rhs) {
  35. std::swap(impl_, rhs.impl_);
  36. return *this;
  37. }
  38. operator bool() const { return impl_ != nullptr; }
  39. bool close() {
  40. if (impl_) {
  41. auto impl = std::move(impl_);
  42. return rust_tsdb_lf_entry_batch_commit_and_close(impl.get());
  43. }
  44. return true;
  45. }
  46. bool add_point(std::string const& name, double value, long long nanoseconds) {
  47. if (!ensure_open()) {
  48. return false;
  49. }
  50. return rust_tsdb_lf_entry_batch_add_point(impl_.get(), name.c_str(), value, nanoseconds);
  51. }
  52. private:
  53. friend struct tsdb_entry;
  54. friend struct tsdb_entry_impl;
  55. tsdb_batch_insertion(UniqPtr impl) : impl_(std::move(impl)) {}
  56. bool ensure_open() const {
  57. if (!impl_) {
  58. rust_log_error_custom("tsdb-LF batch insertion is not open");
  59. return false;
  60. }
  61. return true;
  62. }
  63. UniqPtr impl_;
  64. };
  65. struct tsdb_entry {
  66. using UniqPtr = std::unique_ptr<TsdbEntry_t, decltype(&rust_drop_tsdb_lf_entry)>;
  67. tsdb_entry(const std::string& remote, uint16_t port = 8123) : impl_(nullptr, rust_drop_tsdb_lf_entry) {
  68. impl_ = UniqPtr(rust_create_tsdb_lf_entry(remote.c_str(), port), rust_drop_tsdb_lf_entry);
  69. }
  70. tsdb_entry(std::nullptr_t) : impl_(nullptr, rust_drop_tsdb_lf_entry) {}
  71. ~tsdb_entry() {
  72. if (impl_) {
  73. impl_ = nullptr;
  74. }
  75. }
  76. tsdb_entry(tsdb_entry&& other) : impl_(std::move(other.impl_)) {}
  77. tsdb_entry& operator=(tsdb_entry rhs) {
  78. std::swap(impl_, rhs.impl_);
  79. return *this;
  80. }
  81. operator bool() const { return impl_ != nullptr; }
  82. bool close() {
  83. if (impl_) {
  84. auto impl = std::move(impl_);
  85. return rust_tsdb_lf_entry_end_commit_insert(impl.get());
  86. }
  87. return true;
  88. }
  89. bool set_async_insert_buffer_size(size_t size) {
  90. if (!ensure_open()) {
  91. return false;
  92. }
  93. return rust_tsdb_lf_entry_set_async_insert_buffer_size(impl_.get(), size);
  94. }
  95. size_t get_async_insert_buffer_size() const {
  96. if (!ensure_open()) {
  97. return 0;
  98. }
  99. return rust_tsdb_lf_entry_get_async_insert_buffer_size(impl_.get());
  100. }
  101. bool set_active_table(const std::string& table_name) {
  102. if (!ensure_open()) {
  103. return false;
  104. }
  105. return rust_tsdb_lf_entry_set_active_table(impl_.get(), table_name.c_str());
  106. }
  107. bool set_metric_name(const std::string& metric_name) {
  108. if (!ensure_open()) {
  109. return false;
  110. }
  111. return rust_tsdb_lf_entry_set_metric_name(impl_.get(), metric_name.c_str());
  112. }
  113. bool insert_point(const point& p) {
  114. if (!ensure_open()) {
  115. return false;
  116. }
  117. return rust_tsdb_lf_entry_insert_point(impl_.get(), p.name_.c_str(), p.value_, p.nanoseconds_);
  118. }
  119. bool insert_points(const std::vector<point>& points) {
  120. if (!ensure_open()) {
  121. return false;
  122. }
  123. // for (const auto& p : points) {
  124. // if (!insert_point(p)) {
  125. // return false;
  126. // }
  127. // }
  128. struct InsertState {
  129. const std::vector<point>& points;
  130. uint64_t index;
  131. } state{points, 0};
  132. return rust_tsdb_lf_entry_insert_points_with_callback(
  133. impl_.get(),
  134. [](void* opaque) -> FfiMyRow {
  135. auto state = static_cast<InsertState*>(opaque);
  136. if (state->index >= state->points.size()) {
  137. return {{(uint8_t*)"", 0}, 0, 0};
  138. }
  139. auto& p = state->points[state->index];
  140. state->index++;
  141. return {{(uint8_t*)p.name_.c_str(), p.name_.size()}, p.value_, p.nanoseconds_};
  142. },
  143. &state);
  144. }
  145. bool insert_point(const std::string& name, double value, long long nanoseconds) {
  146. if (!ensure_open()) {
  147. return false;
  148. }
  149. return rust_tsdb_lf_entry_insert_point(impl_.get(), name.c_str(), value, nanoseconds);
  150. }
  151. tsdb_batch_insertion new_batch_insert() {
  152. if (!ensure_open()) {
  153. return tsdb_batch_insertion(nullptr);
  154. }
  155. return tsdb_batch_insertion({rust_tsdb_lf_entry_new_insert_batch(impl_.get()), rust_tsdb_lf_entry_insert_batch_drop});
  156. }
  157. private:
  158. bool ensure_open() const {
  159. if (!impl_) {
  160. rust_log_error_custom("tsdb-LF entry is not open");
  161. return false;
  162. }
  163. return true;
  164. }
  165. UniqPtr impl_;
  166. };
  167. } // namespace tsdb_cpp
  168. #endif // TSDB_CPP_HPP