From 593b0de6734171f73a2eac76b0ca8987cdd6d0de Mon Sep 17 00:00:00 2001 From: vikonix Date: Mon, 5 May 2025 12:50:21 +0200 Subject: [PATCH] add creation mode --- quasardb/cluster.cpp | 2 +- quasardb/detail/writer.cpp | 26 ++++++++++++++++++++++++-- quasardb/detail/writer.hpp | 19 ++++++++++++++++++- quasardb/writer.hpp | 7 +++++-- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/quasardb/cluster.cpp b/quasardb/cluster.cpp index a2f24987..ebfd2ac4 100644 --- a/quasardb/cluster.cpp +++ b/quasardb/cluster.cpp @@ -70,7 +70,7 @@ void cluster::close() _handle->close(); } } - catch (qdb::invalid_handle_exception const & e) + catch (qdb::invalid_handle_exception const & /*e*/) { // This can happen if, for example, we call close() after an error occured; in those // circumstances, we fully expect the connection to already be invalid, and we should diff --git a/quasardb/detail/writer.cpp b/quasardb/detail/writer.cpp index e730bc26..20ca3ad0 100644 --- a/quasardb/detail/writer.cpp +++ b/quasardb/detail/writer.cpp @@ -220,7 +220,8 @@ void staged_table::prepare_table_data(qdb_exp_batch_push_table_data_t & table_da void staged_table::prepare_batch(qdb_exp_batch_push_mode_t mode, detail::deduplicate_options const & deduplicate_options, qdb_ts_range_t * ranges, - qdb_exp_batch_push_table_t & batch) + qdb_exp_batch_push_table_t & batch, + qdb_exp_batch_creation_mode_t creation) { batch.name = _table_name.c_str(); @@ -235,7 +236,7 @@ void staged_table::prepare_batch(qdb_exp_batch_push_mode_t mode, batch.where_duplicate = nullptr; batch.where_duplicate_count = 0; batch.deduplication_mode = qdb_exp_batch_deduplication_mode_disabled; - batch.creation = qdb_exp_batch_dont_create; + batch.creation = creation; enum detail::deduplication_mode_t mode_ = deduplicate_options.mode_; @@ -311,6 +312,27 @@ void staged_table::prepare_batch(qdb_exp_batch_push_mode_t mode, return ret; } +/* static */ py::kwargs detail::batch_creation_mode::ensure(py::kwargs kwargs) +{ + if (kwargs.contains(batch_creation_mode::kw_creation_mode) == false) + { + kwargs[batch_creation_mode::kw_creation_mode] = batch_creation_mode::default_creation_mode; + } + + return kwargs; +} + +/* static */ qdb_exp_batch_creation_mode_t detail::batch_creation_mode::from_kwargs( + py::kwargs const & kwargs) +{ + assert(kwargs.contains(batch_creation_mode::kw_creation_mode)); + + // By default no flags are set + qdb_exp_batch_creation_mode_t ret = qdb_exp_batch_dont_create; + + return py::cast(kwargs[batch_creation_mode::kw_creation_mode]); +} + /* static */ qdb_exp_batch_options_t detail::batch_options::from_kwargs(py::kwargs const & kwargs) { auto kwargs_ = detail::batch_push_mode::ensure(kwargs); diff --git a/quasardb/detail/writer.hpp b/quasardb/detail/writer.hpp index e549c025..92a69f64 100644 --- a/quasardb/detail/writer.hpp +++ b/quasardb/detail/writer.hpp @@ -189,7 +189,8 @@ class staged_table void prepare_batch(qdb_exp_batch_push_mode_t mode, detail::deduplicate_options const & deduplicate_options, qdb_ts_range_t * ranges, - qdb_exp_batch_push_table_t & batch); + qdb_exp_batch_push_table_t & batch, + qdb_exp_batch_creation_mode_t creation); static inline void _set_deduplication_mode( enum detail::deduplication_mode_t mode, bool columns, qdb_exp_batch_push_table_t & out) @@ -487,6 +488,22 @@ struct batch_options static qdb_exp_batch_options_t from_kwargs(py::kwargs const & kwargs); }; +struct batch_creation_mode +{ + static constexpr char const * kw_creation_mode = "creation_mode"; + static constexpr qdb_exp_batch_creation_mode_t default_creation_mode = qdb_exp_batch_dont_create; + + /** + * Ensures all options are always explicitly set, according to the defaults above. + */ + static py::kwargs ensure(py::kwargs kwargs); + + /** + * Returns the push mode, throws error if push mode is not set. + */ + static qdb_exp_batch_creation_mode_t from_kwargs(py::kwargs const & kwargs); +}; + struct batch_truncate_ranges { diff --git a/quasardb/writer.hpp b/quasardb/writer.hpp index 9f88a04b..6822e973 100644 --- a/quasardb/writer.hpp +++ b/quasardb/writer.hpp @@ -174,6 +174,7 @@ class writer // Ensure some default variables that are set kwargs = detail::batch_push_flags::ensure(kwargs); + kwargs = detail::batch_creation_mode::ensure(kwargs); std::vector truncate_ranges{}; @@ -202,7 +203,8 @@ class writer truncate_ranges_ = truncate_ranges.data(); } - int cur = 0; + auto creation_mode = detail::batch_creation_mode::from_kwargs(kwargs); + int cur = 0; for (auto pos = idx.begin(); pos != idx.end(); ++pos) { @@ -214,7 +216,8 @@ class writer options.mode, // deduplicate_options, // truncate_ranges_, // - batch_table); + batch_table, // + creation_mode); if (batch_table.data.column_count == 0) [[unlikely]] {