From c1c779583523e152fe276e111588837498e6a5b5 Mon Sep 17 00:00:00 2001 From: Jurriaan Mous Date: Mon, 6 Jan 2025 21:48:30 +0100 Subject: [PATCH] Expand writebatch methods in C api Added: - rocksdb_writebatch_get_data_size - rocksdb_writebatch_has_put - rocksdb_writebatch_has_delete - rocksdb_writebatch_has_single_delete - rocksdb_writebatch_has_delete_range - rocksdb_writebatch_has_merge - rocksdb_writebatch_has_begin_prepare - rocksdb_writebatch_has_end_prepare - rocksdb_writebatch_has_commit - rocksdb_writebatch_has_rollback - rocksdb_writebatch_set_max_bytes - rocksdb_writebatch_mark_wal_termination_point - rocksdb_writebatch_get_wal_termination_point - rocksdb_writebatch_wi_get_data_size - rocksdb_save_point_get_size - rocksdb_save_point_get_count - rocksdb_save_point_get_content_flags - rocksdb_save_point_clear - rocksdb_save_point_is_cleared - rocksdb_save_point_destroy And tests --- db/c.cc | 84 +++++++++++++++++++++++++++++++++++++++++++++ db/c_test.c | 26 ++++++++++++++ include/rocksdb/c.h | 47 +++++++++++++++++++++++++ 3 files changed, 157 insertions(+) diff --git a/db/c.cc b/db/c.cc index 77b0a6af326..a5aeaa15ebb 100644 --- a/db/c.cc +++ b/db/c.cc @@ -108,6 +108,7 @@ using ROCKSDB_NAMESPACE::Range; using ROCKSDB_NAMESPACE::RateLimiter; using ROCKSDB_NAMESPACE::ReadOptions; using ROCKSDB_NAMESPACE::RestoreOptions; +using ROCKSDB_NAMESPACE::SavePoint; using ROCKSDB_NAMESPACE::SequentialFile; using ROCKSDB_NAMESPACE::Slice; using ROCKSDB_NAMESPACE::SliceParts; @@ -154,6 +155,9 @@ struct rocksdb_iterator_t { struct rocksdb_writebatch_t { WriteBatch rep; }; +struct rocksdb_save_point_t { + SavePoint rep; +}; struct rocksdb_writebatch_wi_t { WriteBatchWithIndex* rep; }; @@ -2344,6 +2348,10 @@ const char* rocksdb_writebatch_data(rocksdb_writebatch_t* b, size_t* size) { return b->rep.Data().c_str(); } +size_t rocksdb_writebatch_get_data_size(rocksdb_writebatch_t* b) { + return b->rep.GetDataSize(); +} + void rocksdb_writebatch_set_save_point(rocksdb_writebatch_t* b) { b->rep.SetSavePoint(); } @@ -2385,6 +2393,58 @@ void rocksdb_writebatch_update_timestamps( })); } +unsigned char rocksdb_writebatch_has_put(rocksdb_writebatch_t* b) { + return b->rep.HasPut(); +} + +unsigned char rocksdb_writebatch_has_delete(rocksdb_writebatch_t* b) { + return b->rep.HasDelete(); +} + +unsigned char rocksdb_writebatch_has_single_delete(rocksdb_writebatch_t* b) { + return b->rep.HasSingleDelete(); +} + +unsigned char rocksdb_writebatch_has_delete_range(rocksdb_writebatch_t* b) { + return b->rep.HasDeleteRange(); +} + +unsigned char rocksdb_writebatch_has_merge(rocksdb_writebatch_t* b) { + return b->rep.HasMerge(); +} + +unsigned char rocksdb_writebatch_has_begin_prepare(rocksdb_writebatch_t* b) { + return b->rep.HasBeginPrepare(); +} + +unsigned char rocksdb_writebatch_has_end_prepare(rocksdb_writebatch_t* b) { + return b->rep.HasEndPrepare(); +} + +unsigned char rocksdb_writebatch_has_commit(rocksdb_writebatch_t* b) { + return b->rep.HasCommit(); +} + +unsigned char rocksdb_writebatch_has_rollback(rocksdb_writebatch_t* b) { + return b->rep.HasRollback(); +} + +void rocksdb_writebatch_set_max_bytes(rocksdb_writebatch_t* b, + size_t max_bytes) { + b->rep.SetMaxBytes(max_bytes); +} + +void rocksdb_writebatch_mark_wal_termination_point(rocksdb_writebatch_t* b) { + b->rep.MarkWalTerminationPoint(); +} + +rocksdb_save_point_t* rocksdb_writebatch_get_wal_termination_point( + rocksdb_writebatch_t* b) { + rocksdb_save_point_t* sp = new rocksdb_save_point_t; + sp->rep = b->rep.GetWalTerminationPoint(); + return sp; +} + void rocksdb_writebatch_wi_update_timestamps( rocksdb_writebatch_wi_t* wb, const char* ts, size_t tslen, void* state, size_t (*get_ts_size)(void*, uint32_t), char** errptr) { @@ -2614,6 +2674,10 @@ const char* rocksdb_writebatch_wi_data(rocksdb_writebatch_wi_t* b, return wb->Data().c_str(); } +size_t rocksdb_writebatch_wi_get_data_size(rocksdb_writebatch_wi_t* b) { + return b->rep->GetDataSize(); +} + void rocksdb_writebatch_wi_set_save_point(rocksdb_writebatch_wi_t* b) { b->rep->SetSavePoint(); } @@ -2728,6 +2792,26 @@ void rocksdb_write_writebatch_wi(rocksdb_t* db, SaveError(errptr, db->rep->Write(options->rep, wb)); } +size_t rocksdb_save_point_get_size(rocksdb_save_point_t* sp) { + return sp->rep.size; +} + +uint32_t rocksdb_save_point_get_count(rocksdb_save_point_t* sp) { + return sp->rep.count; +} + +uint32_t rocksdb_save_point_get_content_flags(rocksdb_save_point_t* sp) { + return sp->rep.content_flags; +} + +void rocksdb_save_point_clear(rocksdb_save_point_t* sp) { sp->rep.clear(); } + +bool rocksdb_save_point_is_cleared(rocksdb_save_point_t* sp) { + return sp->rep.is_cleared(); +} + +void rocksdb_save_point_destroy(rocksdb_save_point_t* sp) { delete sp; } + void rocksdb_load_latest_options( const char* db_path, rocksdb_env_t* env, bool ignore_unknown_options, rocksdb_cache_t* cache, rocksdb_options_t** db_options, diff --git a/db/c_test.c b/db/c_test.c index eb9886ba467..4d851b7852a 100644 --- a/db/c_test.c +++ b/db/c_test.c @@ -1243,6 +1243,10 @@ int main(int argc, char** argv) { rocksdb_writebatch_wi_delete(wbi, "bar", 3); int count = rocksdb_writebatch_wi_count(wbi); CheckCondition(count == 3); + + size_t data_size = rocksdb_writebatch_wi_get_data_size(wbi); + CheckCondition(data_size > 0); + size_t size; char* value; value = rocksdb_writebatch_wi_get_from_batch(wbi, options, "box", 3, &size, @@ -3259,6 +3263,28 @@ int main(int argc, char** argv) { rocksdb_writebatch_put(wb, "bar", 3, "b", 1); rocksdb_writebatch_put(wb, "box", 3, "c", 1); rocksdb_writebatch_delete(wb, "bar", 3); + + CheckCondition(rocksdb_writebatch_has_put(wb)); + CheckCondition(rocksdb_writebatch_has_delete(wb)); + CheckCondition(0 == rocksdb_writebatch_has_single_delete(wb)); + CheckCondition(0 == rocksdb_writebatch_has_delete_range(wb)); + CheckCondition(0 == rocksdb_writebatch_has_merge(wb)); + CheckCondition(0 == rocksdb_writebatch_has_rollback(wb)); + CheckCondition(0 == rocksdb_writebatch_has_commit(wb)); + CheckCondition(0 == rocksdb_writebatch_has_begin_prepare(wb)); + CheckCondition(0 == rocksdb_writebatch_has_end_prepare(wb)); + + // CheckCondition(rocksdb_writebatch_get_data_size(wb) > 0); + + rocksdb_writebatch_mark_wal_termination_point(wb); + rocksdb_save_point_t* sp = rocksdb_writebatch_get_wal_termination_point(wb); + + CheckCondition(rocksdb_save_point_get_size(sp) > 0); + CheckCondition(rocksdb_save_point_get_count(sp) > 0); + CheckCondition(rocksdb_save_point_get_content_flags(sp) > 0); + CheckCondition(!rocksdb_save_point_is_cleared(sp)); + rocksdb_save_point_destroy(sp); + rocksdb_transactiondb_write(txn_db, woptions, wb, &err); rocksdb_writebatch_destroy(wb); CheckTxnDBGet(txn_db, roptions, "box", "c"); diff --git a/include/rocksdb/c.h b/include/rocksdb/c.h index ff452a3546e..6ba2228d77d 100644 --- a/include/rocksdb/c.h +++ b/include/rocksdb/c.h @@ -107,6 +107,7 @@ typedef struct rocksdb_slicetransform_t rocksdb_slicetransform_t; typedef struct rocksdb_snapshot_t rocksdb_snapshot_t; typedef struct rocksdb_writablefile_t rocksdb_writablefile_t; typedef struct rocksdb_writebatch_t rocksdb_writebatch_t; +typedef struct rocksdb_save_point_t rocksdb_save_point_t; typedef struct rocksdb_writebatch_wi_t rocksdb_writebatch_wi_t; typedef struct rocksdb_writeoptions_t rocksdb_writeoptions_t; typedef struct rocksdb_universal_compaction_options_t @@ -872,6 +873,8 @@ extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_iterate_cf( const char* v, size_t vlen)); extern ROCKSDB_LIBRARY_API const char* rocksdb_writebatch_data( rocksdb_writebatch_t*, size_t* size); +extern ROCKSDB_LIBRARY_API size_t +rocksdb_writebatch_get_data_size(rocksdb_writebatch_t*); extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_set_save_point( rocksdb_writebatch_t*); extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_rollback_to_save_point( @@ -882,6 +885,33 @@ extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_update_timestamps( rocksdb_writebatch_t* wb, const char* ts, size_t tslen, void* state, size_t (*get_ts_size)(void*, uint32_t), char** errptr); +extern ROCKSDB_LIBRARY_API unsigned char rocksdb_writebatch_has_put( + rocksdb_writebatch_t*); +extern ROCKSDB_LIBRARY_API unsigned char rocksdb_writebatch_has_delete( + rocksdb_writebatch_t*); +extern ROCKSDB_LIBRARY_API unsigned char rocksdb_writebatch_has_single_delete( + rocksdb_writebatch_t*); +extern ROCKSDB_LIBRARY_API unsigned char rocksdb_writebatch_has_delete_range( + rocksdb_writebatch_t*); +extern ROCKSDB_LIBRARY_API unsigned char rocksdb_writebatch_has_merge( + rocksdb_writebatch_t*); +extern ROCKSDB_LIBRARY_API unsigned char rocksdb_writebatch_has_begin_prepare( + rocksdb_writebatch_t*); +extern ROCKSDB_LIBRARY_API unsigned char rocksdb_writebatch_has_end_prepare( + rocksdb_writebatch_t*); +extern ROCKSDB_LIBRARY_API unsigned char rocksdb_writebatch_has_commit( + rocksdb_writebatch_t*); +extern ROCKSDB_LIBRARY_API unsigned char rocksdb_writebatch_has_rollback( + rocksdb_writebatch_t*); + +extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_set_max_bytes( + rocksdb_writebatch_t* b, size_t max_bytes); + +extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_mark_wal_termination_point( + rocksdb_writebatch_t* b); +extern ROCKSDB_LIBRARY_API rocksdb_save_point_t* +rocksdb_writebatch_get_wal_termination_point(rocksdb_writebatch_t* b); + /* Write batch with index */ extern ROCKSDB_LIBRARY_API rocksdb_writebatch_wi_t* @@ -974,6 +1004,8 @@ extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_wi_iterate( void (*deleted)(void*, const char* k, size_t klen)); extern ROCKSDB_LIBRARY_API const char* rocksdb_writebatch_wi_data( rocksdb_writebatch_wi_t* b, size_t* size); +extern ROCKSDB_LIBRARY_API size_t +rocksdb_writebatch_wi_get_data_size(rocksdb_writebatch_wi_t* b); extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_wi_set_save_point( rocksdb_writebatch_wi_t*); extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_wi_rollback_to_save_point( @@ -1008,6 +1040,21 @@ extern ROCKSDB_LIBRARY_API void rocksdb_writebatch_wi_update_timestamps( rocksdb_writebatch_wi_t* wbwi, const char* ts, size_t tslen, void* state, size_t (*get_ts_size)(void*, uint32_t), char** errptr); +/* Save point */ + +extern ROCKSDB_LIBRARY_API size_t +rocksdb_save_point_get_size(rocksdb_save_point_t* sp); +extern ROCKSDB_LIBRARY_API uint32_t +rocksdb_save_point_get_count(rocksdb_save_point_t* sp); +extern ROCKSDB_LIBRARY_API uint32_t +rocksdb_save_point_get_content_flags(rocksdb_save_point_t* sp); +extern ROCKSDB_LIBRARY_API void rocksdb_save_point_clear( + rocksdb_save_point_t* sp); +extern ROCKSDB_LIBRARY_API bool rocksdb_save_point_is_cleared( + rocksdb_save_point_t* sp); +extern ROCKSDB_LIBRARY_API void rocksdb_save_point_destroy( + rocksdb_save_point_t* sp); + /* Options utils */ // Load the latest rocksdb options from the specified db_path.