#pragma once #include #ifndef TSDB_CPP_HPP #define TSDB_CPP_HPP #include #include #include #include #include #include #include "tsdb.rust.h" #ifdef _WIN32 #define TSDB_EXPORT __declspec(dllexport) #else #define TSDB_EXPORT #endif namespace tsdb_cpp { /// @brief A point in time series database. struct point { const std::string& name_; double value_; long long nanoseconds_; point(const std::string& name, double value, long long nanoseconds) : name_(name), value_(value), nanoseconds_(nanoseconds) {} }; struct tsdb_batch_insertion { using UniqPtr = std::unique_ptr; tsdb_batch_insertion(std::nullptr_t) : impl_(nullptr, rust_tsdb_lf_entry_insert_batch_drop) {} ~tsdb_batch_insertion() { if (impl_) { impl_ = nullptr; } } tsdb_batch_insertion(tsdb_batch_insertion&& other) : impl_(std::move(other.impl_)) {} tsdb_batch_insertion& operator=(tsdb_batch_insertion rhs) { std::swap(impl_, rhs.impl_); return *this; } operator bool() const { return impl_ != nullptr; } bool close() { if (impl_) { auto impl = std::move(impl_); return rust_tsdb_lf_entry_batch_commit_and_close(impl.get()); } return true; } bool add_point(std::string const& name, double value, long long nanoseconds) { if (!ensure_open()) { return false; } return rust_tsdb_lf_entry_batch_add_point(impl_.get(), name.c_str(), value, nanoseconds); } private: friend struct tsdb_entry; friend struct tsdb_entry_impl; tsdb_batch_insertion(UniqPtr impl) : impl_(std::move(impl)) {} bool ensure_open() const { if (!impl_) { rust_log_error_custom("tsdb-LF batch insertion is not open"); return false; } return true; } UniqPtr impl_; }; struct tsdb_entry { using UniqPtr = std::unique_ptr; tsdb_entry(const std::string& remote, uint16_t port = 8123) : impl_(nullptr, rust_drop_tsdb_lf_entry) { impl_ = UniqPtr(rust_create_tsdb_lf_entry(remote.c_str(), port), rust_drop_tsdb_lf_entry); } tsdb_entry(std::nullptr_t) : impl_(nullptr, rust_drop_tsdb_lf_entry) {} ~tsdb_entry() { if (impl_) { impl_ = nullptr; } } tsdb_entry(tsdb_entry&& other) : impl_(std::move(other.impl_)) {} tsdb_entry& operator=(tsdb_entry rhs) { std::swap(impl_, rhs.impl_); return *this; } operator bool() const { return impl_ != nullptr; } bool close() { if (impl_) { auto impl = std::move(impl_); return rust_tsdb_lf_entry_end_commit_insert(impl.get()); } return true; } bool set_async_insert_buffer_size(size_t size) { if (!ensure_open()) { return false; } return rust_tsdb_lf_entry_set_async_insert_buffer_size(impl_.get(), size); } size_t get_async_insert_buffer_size() const { if (!ensure_open()) { return 0; } return rust_tsdb_lf_entry_get_async_insert_buffer_size(impl_.get()); } bool set_active_table(const std::string& table_name) { if (!ensure_open()) { return false; } return rust_tsdb_lf_entry_set_active_table(impl_.get(), table_name.c_str()); } bool set_metric_name(const std::string& metric_name) { if (!ensure_open()) { return false; } return rust_tsdb_lf_entry_set_metric_name(impl_.get(), metric_name.c_str()); } bool insert_point(const point& p) { if (!ensure_open()) { return false; } return rust_tsdb_lf_entry_insert_point(impl_.get(), p.name_.c_str(), p.value_, p.nanoseconds_); } bool insert_points(const std::vector& points) { if (!ensure_open()) { return false; } // for (const auto& p : points) { // if (!insert_point(p)) { // return false; // } // } struct InsertState { const std::vector& points; uint64_t index; } state{points, 0}; return rust_tsdb_lf_entry_insert_points_with_callback( impl_.get(), [](void* opaque) -> FfiMyRow { auto state = static_cast(opaque); if (state->index >= state->points.size()) { return {{(uint8_t*)"", 0}, 0, 0}; } auto& p = state->points[state->index]; state->index++; return {{(uint8_t*)p.name_.c_str(), p.name_.size()}, p.value_, p.nanoseconds_}; }, &state); } bool insert_point(const std::string& name, double value, long long nanoseconds) { if (!ensure_open()) { return false; } return rust_tsdb_lf_entry_insert_point(impl_.get(), name.c_str(), value, nanoseconds); } tsdb_batch_insertion new_batch_insert() { if (!ensure_open()) { return tsdb_batch_insertion(nullptr); } return tsdb_batch_insertion({rust_tsdb_lf_entry_new_insert_batch(impl_.get()), rust_tsdb_lf_entry_insert_batch_drop}); } private: bool ensure_open() const { if (!impl_) { rust_log_error_custom("tsdb-LF entry is not open"); return false; } return true; } UniqPtr impl_; }; } // namespace tsdb_cpp #endif // TSDB_CPP_HPP