123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #pragma once
- #if __cplusplus < 201703L
- #error "C++17 or later is required"
- #endif
- #include <cstddef>
- #ifndef TSDB_CPP_HPP
- #define TSDB_CPP_HPP
- #include <cstdio>
- #include <cstring>
- #include <memory>
- #include <string_view>
- #include <vector>
- #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> impl);
- bool ensure_open() const;
- std::unique_ptr<impl> 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<point>& 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<tsdb_entry_impl> impl_;
- };
- } // namespace tsdb_cpp
- #endif // TSDB_CPP_HPP
|