Skip to content

Commit

Permalink
Expand writebatch methods in C api
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jurmous committed Jan 9, 2025
1 parent 44b741e commit 9d272d4
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
84 changes: 84 additions & 0 deletions db/c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions db/c_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
47 changes: 47 additions & 0 deletions include/rocksdb/c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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*
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9d272d4

Please sign in to comment.