Skip to content

Commit 2a0886b

Browse files
tillrohrmannfacebook-github-bot
authored andcommitted
Expose pinned WriteBatchWithIndex::GetFromBatchAndDB through C bindings (facebook#12970)
Summary: Expose pinned WriteBatchWithIndex::GetFromBatchAndDB through C bindings so that one can read data from the `WriteBatchWithIndex` and db w/o copying the data. This fixes facebook#12969. Pull Request resolved: facebook#12970 Reviewed By: cbi42 Differential Revision: D74586418 Pulled By: jaykorean fbshipit-source-id: a5a4d2e8ce3ddf4c2371fdfdb4e9c3309966a05d
1 parent 9c4b94b commit 2a0886b

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

db/c.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,23 @@ char* rocksdb_writebatch_wi_get_from_batch_and_db(
27222722
return result;
27232723
}
27242724

2725+
rocksdb_pinnableslice_t* rocksdb_writebatch_wi_get_pinned_from_batch_and_db(
2726+
rocksdb_writebatch_wi_t* wbwi, rocksdb_t* db,
2727+
const rocksdb_readoptions_t* options, const char* key, size_t keylen,
2728+
char** errptr) {
2729+
rocksdb_pinnableslice_t* v = new (rocksdb_pinnableslice_t);
2730+
Status s = wbwi->rep->GetFromBatchAndDB(db->rep, options->rep,
2731+
Slice(key, keylen), &v->rep);
2732+
if (!s.ok()) {
2733+
delete (v);
2734+
if (!s.IsNotFound()) {
2735+
SaveError(errptr, s);
2736+
}
2737+
return nullptr;
2738+
}
2739+
return v;
2740+
}
2741+
27252742
char* rocksdb_writebatch_wi_get_from_batch_and_db_cf(
27262743
rocksdb_writebatch_wi_t* wbwi, rocksdb_t* db,
27272744
const rocksdb_readoptions_t* options,
@@ -2743,6 +2760,24 @@ char* rocksdb_writebatch_wi_get_from_batch_and_db_cf(
27432760
return result;
27442761
}
27452762

2763+
rocksdb_pinnableslice_t* rocksdb_writebatch_wi_get_pinned_from_batch_and_db_cf(
2764+
rocksdb_writebatch_wi_t* wbwi, rocksdb_t* db,
2765+
const rocksdb_readoptions_t* options,
2766+
rocksdb_column_family_handle_t* column_family, const char* key,
2767+
size_t keylen, char** errptr) {
2768+
rocksdb_pinnableslice_t* v = new (rocksdb_pinnableslice_t);
2769+
Status s = wbwi->rep->GetFromBatchAndDB(
2770+
db->rep, options->rep, column_family->rep, Slice(key, keylen), &v->rep);
2771+
if (!s.ok()) {
2772+
delete (v);
2773+
if (!s.IsNotFound()) {
2774+
SaveError(errptr, s);
2775+
}
2776+
return nullptr;
2777+
}
2778+
return v;
2779+
}
2780+
27462781
void rocksdb_write_writebatch_wi(rocksdb_t* db,
27472782
const rocksdb_writeoptions_t* options,
27482783
rocksdb_writebatch_wi_t* wbwi, char** errptr) {

db/c_test.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ static void CheckValue(char* err, const char* expected, char** actual,
103103
Free(actual);
104104
}
105105

106+
static void CheckPinnedValue(char* err, const char* expected,
107+
const char** actual, size_t actual_length) {
108+
CheckNoError(err);
109+
CheckEqual(expected, *actual, actual_length);
110+
}
111+
106112
static void CheckGet(rocksdb_t* db, const rocksdb_readoptions_t* options,
107113
const char* key, const char* expected) {
108114
char* err = NULL;
@@ -1245,6 +1251,8 @@ int main(int argc, char** argv) {
12451251
CheckCondition(count == 3);
12461252
size_t size;
12471253
char* value;
1254+
const char* pinned_value;
1255+
rocksdb_pinnableslice_t* p;
12481256
value = rocksdb_writebatch_wi_get_from_batch(wbi, options, "box", 3, &size,
12491257
&err);
12501258
CheckValue(err, "c", &value, size);
@@ -1254,9 +1262,19 @@ int main(int argc, char** argv) {
12541262
value = rocksdb_writebatch_wi_get_from_batch_and_db(wbi, db, roptions,
12551263
"foo", 3, &size, &err);
12561264
CheckValue(err, "hello", &value, size);
1265+
p = rocksdb_writebatch_wi_get_pinned_from_batch_and_db(wbi, db, roptions,
1266+
"foo", 3, &err);
1267+
pinned_value = rocksdb_pinnableslice_value(p, &size);
1268+
CheckPinnedValue(err, "hello", &pinned_value, size);
1269+
rocksdb_pinnableslice_destroy(p);
12571270
value = rocksdb_writebatch_wi_get_from_batch_and_db(wbi, db, roptions,
12581271
"box", 3, &size, &err);
12591272
CheckValue(err, "c", &value, size);
1273+
p = rocksdb_writebatch_wi_get_pinned_from_batch_and_db(wbi, db, roptions,
1274+
"box", 3, &err);
1275+
pinned_value = rocksdb_pinnableslice_value(p, &size);
1276+
CheckPinnedValue(err, "c", &pinned_value, size);
1277+
rocksdb_pinnableslice_destroy(p);
12601278
rocksdb_write_writebatch_wi(db, woptions, wbi, &err);
12611279
CheckNoError(err);
12621280
CheckGet(db, roptions, "foo", "hello");

include/rocksdb/c.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,11 +987,22 @@ extern ROCKSDB_LIBRARY_API char* rocksdb_writebatch_wi_get_from_batch_and_db(
987987
rocksdb_writebatch_wi_t* wbwi, rocksdb_t* db,
988988
const rocksdb_readoptions_t* options, const char* key, size_t keylen,
989989
size_t* vallen, char** errptr);
990+
extern ROCKSDB_LIBRARY_API rocksdb_pinnableslice_t*
991+
rocksdb_writebatch_wi_get_pinned_from_batch_and_db(
992+
rocksdb_writebatch_wi_t* wbwi, rocksdb_t* db,
993+
const rocksdb_readoptions_t* options, const char* key, size_t keylen,
994+
char** errptr);
990995
extern ROCKSDB_LIBRARY_API char* rocksdb_writebatch_wi_get_from_batch_and_db_cf(
991996
rocksdb_writebatch_wi_t* wbwi, rocksdb_t* db,
992997
const rocksdb_readoptions_t* options,
993998
rocksdb_column_family_handle_t* column_family, const char* key,
994999
size_t keylen, size_t* vallen, char** errptr);
1000+
extern ROCKSDB_LIBRARY_API rocksdb_pinnableslice_t*
1001+
rocksdb_writebatch_wi_get_pinned_from_batch_and_db_cf(
1002+
rocksdb_writebatch_wi_t* wbwi, rocksdb_t* db,
1003+
const rocksdb_readoptions_t* options,
1004+
rocksdb_column_family_handle_t* column_family, const char* key,
1005+
size_t keylen, char** errptr);
9951006
extern ROCKSDB_LIBRARY_API void rocksdb_write_writebatch_wi(
9961007
rocksdb_t* db, const rocksdb_writeoptions_t* options,
9971008
rocksdb_writebatch_wi_t* wbwi, char** errptr);

0 commit comments

Comments
 (0)