tsdb_hf.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // High frequency data API
  2. #ifndef TSDB_HF_CPP_HPP
  3. #define TSDB_HF_CPP_HPP
  4. #include <cstddef>
  5. #include <cstdint>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <memory>
  9. #include <string_view>
  10. #include "shared.h"
  11. #ifdef _WIN32
  12. #define TSDB_EXPORT __declspec(dllexport)
  13. #else
  14. #define TSDB_EXPORT
  15. #endif
  16. // NOTE: It is recommended to ALWAYS call `close` before dropping the object, otherwise you may
  17. // encounter unexpected behavior, including but not limited to silent data loss, crashes, etc.
  18. namespace tsdb_hf_cpp {
  19. using namespace tsdb_preludes;
  20. struct tsdb_entry;
  21. struct tsdb_entry_stream {
  22. struct impl;
  23. tsdb_entry_stream(std::nullptr_t);
  24. ~tsdb_entry_stream();
  25. tsdb_entry_stream(tsdb_entry_stream&& other);
  26. tsdb_entry_stream& operator=(tsdb_entry_stream rhs) {
  27. std::swap(impl_, rhs.impl_);
  28. return *this;
  29. }
  30. operator bool() const { return impl_ != nullptr; }
  31. /// @brief Closes the stream. Will also commit the stream if it has not been committed yet.
  32. /// It is recommended to always call this method before dropping the object.
  33. bool close();
  34. bool insert_point(double value);
  35. bool insert_points(const double* values, size_t count);
  36. private:
  37. friend tsdb_entry;
  38. tsdb_entry_stream(std::unique_ptr<impl> impl);
  39. bool ensure_open() const;
  40. std::unique_ptr<impl> impl_;
  41. };
  42. /// @brief A time series database.
  43. struct tsdb_entry {
  44. struct impl;
  45. tsdb_entry();
  46. tsdb_entry(std::nullptr_t);
  47. ~tsdb_entry();
  48. tsdb_entry(tsdb_entry&& other);
  49. tsdb_entry& operator=(tsdb_entry rhs) {
  50. std::swap(impl_, rhs.impl_);
  51. return *this;
  52. }
  53. operator bool() const { return impl_ != nullptr; }
  54. /// @brief Closes the database. Will fail if there are open streams. It is recommended to always
  55. /// call this method before dropping the object.
  56. bool close();
  57. bool set_data_dir(string_view data_dir);
  58. /// @brief Sets how many points are stored in a single part.
  59. bool set_part_size(size_t size);
  60. bool set_metric_name(string_view metric_name);
  61. /// @brief Creates a new stream of points. The stream will be automatically committed when
  62. /// the object is dropped.
  63. /// @param name
  64. /// @param timestampOffset The starting timestamp offset (since epoch), in milliseconds.
  65. /// @param interval The interval between points, in nanoseconds.
  66. /// @return An object representing the stream.
  67. tsdb_entry_stream new_stream(std::string name, uint64_t timestampOffset, uint64_t interval);
  68. private:
  69. bool ensure_open() const;
  70. std::unique_ptr<impl> impl_;
  71. };
  72. } // namespace tsdb_hf_cpp
  73. #endif // TSDB_HFCPP_HPP