#pragma once #if __cplusplus < 201703L #error "C++17 or later is required" #endif #include #ifndef TSDB_CPP_HPP #define TSDB_CPP_HPP #include #include #include #include #include #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 { struct impl; tsdb_batch_insertion(std::nullptr_t); ~tsdb_batch_insertion(); tsdb_batch_insertion(tsdb_batch_insertion&& other); tsdb_batch_insertion& operator=(tsdb_batch_insertion rhs) { std::swap(impl_, rhs.impl_); return *this; } operator bool() const { return impl_ != nullptr; } /// @brief Closes the batch. Will also commit the batch if it has not been committed yet. /// It is recommended to always call this method before dropping the object. bool close(); bool add_point(std::string_view name, double value, long long nanoseconds); private: friend struct tsdb_entry; friend struct tsdb_entry_impl; tsdb_batch_insertion(std::unique_ptr impl); bool ensure_open() const; std::unique_ptr impl_; }; struct tsdb_entry_impl; /// @brief A time series database. struct tsdb_entry { tsdb_entry(std::string_view remote, uint16_t port = 9001); tsdb_entry(std::nullptr_t); ~tsdb_entry(); tsdb_entry(tsdb_entry&& other); tsdb_entry& operator=(tsdb_entry rhs) { std::swap(impl_, rhs.impl_); return *this; } operator bool() const { return impl_ != nullptr; } bool close(); bool create_table(); bool create_memory_table(); bool drop_table(); bool drop_memory_table(); // By default creates a local table (`tsdb_cpp`) and a distributed table (`tsdb_cpp_dist`) with the same schema. bool create_dist_table(std::string_view cluster_name); /// @brief Sets the async insert buffer size. 0 means synchronous insert. /// WARN: With async insertion, you lose the ability to catch exceptions from the server. /// Backpressure is handled in a best-effort manner. bool set_async_insert_buffer_size(size_t size); size_t get_async_insert_buffer_size() const; bool set_active_table(std::string_view table_name); bool set_metric_name(std::string_view metric_name); bool insert_point(const point& p); bool insert_points(const std::vector& points); bool insert_point(std::string_view name, double value, long long nanoseconds); tsdb_batch_insertion new_batch_insert(); private: bool ensure_open() const; std::unique_ptr impl_; }; } // namespace tsdb_cpp #endif // TSDB_CPP_HPP