123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #pragma once
- #include <cstddef>
- #ifndef TSDB_CPP_HPP
- #define TSDB_CPP_HPP
- #include <cstdio>
- #include <cstring>
- #include <memory>
- #include <string>
- #include <string_view>
- #include <vector>
- #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<TsdbEntryInsertBatch_t, decltype(&rust_tsdb_lf_entry_insert_batch_drop)>;
- 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<TsdbEntry_t, decltype(&rust_drop_tsdb_lf_entry)>;
- 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<point>& points) {
- if (!ensure_open()) {
- return false;
- }
- // for (const auto& p : points) {
- // if (!insert_point(p)) {
- // return false;
- // }
- // }
- struct InsertState {
- const std::vector<point>& 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<InsertState*>(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
|