From 25df24254b77911b474012a98cfaedbeae4ded6e Mon Sep 17 00:00:00 2001 From: Stanislav Tkach Date: Thu, 10 Aug 2017 13:40:57 -0700 Subject: [PATCH] Add column families related functions (C API) Summary: (#2564) Closes https://github.com/facebook/rocksdb/pull/2669 Differential Revision: D5594151 Pulled By: yiwu-arbug fbshipit-source-id: 67ae9446342f3323d6ecad8e811f4158da194270 --- db/c.cc | 85 ++++++++++++++++++++++++++++++++++++++++++++- db/c_test.c | 31 +++++++++++++++++ include/rocksdb/c.h | 34 ++++++++++++++++++ 3 files changed, 149 insertions(+), 1 deletion(-) diff --git a/db/c.cc b/db/c.cc index 68213e48c4..6952660a1b 100644 --- a/db/c.cc +++ b/db/c.cc @@ -3249,6 +3249,17 @@ void rocksdb_transaction_options_set_max_write_batch_size( opt->rep.max_write_batch_size = size; } +rocksdb_column_family_handle_t* rocksdb_transactiondb_create_column_family( + rocksdb_transactiondb_t* txn_db, + const rocksdb_options_t* column_family_options, + const char* column_family_name, char** errptr) { + rocksdb_column_family_handle_t* handle = new rocksdb_column_family_handle_t; + SaveError(errptr, txn_db->rep->CreateColumnFamily( + ColumnFamilyOptions(column_family_options->rep), + std::string(column_family_name), &(handle->rep))); + return handle; +} + rocksdb_transactiondb_t* rocksdb_transactiondb_open( const rocksdb_options_t* options, const rocksdb_transactiondb_options_t* txn_db_options, const char* name, @@ -3325,6 +3336,27 @@ char* rocksdb_transaction_get(rocksdb_transaction_t* txn, return result; } +char* rocksdb_transaction_get_cf(rocksdb_transaction_t* txn, + const rocksdb_readoptions_t* options, + rocksdb_column_family_handle_t* column_family, + const char* key, size_t klen, size_t* vlen, + char** errptr) { + char* result = nullptr; + std::string tmp; + Status s = + txn->rep->Get(options->rep, column_family->rep, Slice(key, klen), &tmp); + if (s.ok()) { + *vlen = tmp.size(); + result = CopyString(tmp); + } else { + *vlen = 0; + if (!s.IsNotFound()) { + SaveError(errptr, s); + } + } + return result; +} + // Read a key outside a transaction char* rocksdb_transactiondb_get( rocksdb_transactiondb_t* txn_db, @@ -3347,6 +3379,26 @@ char* rocksdb_transactiondb_get( return result; } +char* rocksdb_transactiondb_get_cf( + rocksdb_transactiondb_t* txn_db, const rocksdb_readoptions_t* options, + rocksdb_column_family_handle_t* column_family, const char* key, + size_t keylen, size_t* vallen, char** errptr) { + char* result = nullptr; + std::string tmp; + Status s = txn_db->rep->Get(options->rep, column_family->rep, + Slice(key, keylen), &tmp); + if (s.ok()) { + *vallen = tmp.size(); + result = CopyString(tmp); + } else { + *vallen = 0; + if (!s.IsNotFound()) { + SaveError(errptr, s); + } + } + return result; +} + // Put a key inside a transaction void rocksdb_transaction_put(rocksdb_transaction_t* txn, const char* key, size_t klen, const char* val, size_t vlen, @@ -3354,6 +3406,14 @@ void rocksdb_transaction_put(rocksdb_transaction_t* txn, const char* key, SaveError(errptr, txn->rep->Put(Slice(key, klen), Slice(val, vlen))); } +void rocksdb_transaction_put_cf(rocksdb_transaction_t* txn, + rocksdb_column_family_handle_t* column_family, + const char* key, size_t klen, const char* val, + size_t vlen, char** errptr) { + SaveError(errptr, txn->rep->Put(column_family->rep, Slice(key, klen), + Slice(val, vlen))); +} + //Put a key outside a transaction void rocksdb_transactiondb_put(rocksdb_transactiondb_t* txn_db, const rocksdb_writeoptions_t* options, @@ -3363,6 +3423,16 @@ void rocksdb_transactiondb_put(rocksdb_transactiondb_t* txn_db, txn_db->rep->Put(options->rep, Slice(key, klen), Slice(val, vlen))); } +void rocksdb_transactiondb_put_cf(rocksdb_transactiondb_t* txn_db, + const rocksdb_writeoptions_t* options, + rocksdb_column_family_handle_t* column_family, + const char* key, size_t keylen, + const char* val, size_t vallen, + char** errptr) { + SaveError(errptr, txn_db->rep->Put(options->rep, column_family->rep, + Slice(key, keylen), Slice(val, vallen))); +} + //Write batch into transaction db void rocksdb_transactiondb_write( rocksdb_transactiondb_t* db, @@ -3372,13 +3442,18 @@ void rocksdb_transactiondb_write( SaveError(errptr, db->rep->Write(options->rep, &batch->rep)); } - // Delete a key inside a transaction void rocksdb_transaction_delete(rocksdb_transaction_t* txn, const char* key, size_t klen, char** errptr) { SaveError(errptr, txn->rep->Delete(Slice(key, klen))); } +void rocksdb_transaction_delete_cf( + rocksdb_transaction_t* txn, rocksdb_column_family_handle_t* column_family, + const char* key, size_t klen, char** errptr) { + SaveError(errptr, txn->rep->Delete(column_family->rep, Slice(key, klen))); +} + // Delete a key outside a transaction void rocksdb_transactiondb_delete(rocksdb_transactiondb_t* txn_db, const rocksdb_writeoptions_t* options, @@ -3386,6 +3461,14 @@ void rocksdb_transactiondb_delete(rocksdb_transactiondb_t* txn_db, SaveError(errptr, txn_db->rep->Delete(options->rep, Slice(key, klen))); } +void rocksdb_transactiondb_delete_cf( + rocksdb_transactiondb_t* txn_db, const rocksdb_writeoptions_t* options, + rocksdb_column_family_handle_t* column_family, const char* key, + size_t keylen, char** errptr) { + SaveError(errptr, txn_db->rep->Delete(options->rep, column_family->rep, + Slice(key, keylen))); +} + // Create an iterator inside a transaction rocksdb_iterator_t* rocksdb_transaction_create_iterator( rocksdb_transaction_t* txn, const rocksdb_readoptions_t* options) { diff --git a/db/c_test.c b/db/c_test.c index 209af13ac7..95d2736074 100644 --- a/db/c_test.c +++ b/db/c_test.c @@ -348,6 +348,20 @@ static void CheckTxnDBGet( Free(&val); } +static void CheckTxnDBGetCF(rocksdb_transactiondb_t* txn_db, + const rocksdb_readoptions_t* options, + rocksdb_column_family_handle_t* column_family, + const char* key, const char* expected) { + char* err = NULL; + size_t val_len; + char* val; + val = rocksdb_transactiondb_get_cf(txn_db, options, column_family, key, + strlen(key), &val_len, &err); + CheckNoError(err); + CheckEqual(expected, val, val_len); + Free(&val); +} + int main(int argc, char** argv) { rocksdb_t* db; rocksdb_comparator_t* cmp; @@ -1432,6 +1446,23 @@ int main(int argc, char** argv) { CheckNoError(err); CheckTxnDBGet(txn_db, roptions, "bar", NULL); + // Column families. + rocksdb_column_family_handle_t* cfh; + cfh = rocksdb_transactiondb_create_column_family(txn_db, options, + "txn_db_cf", &err); + CheckNoError(err); + + rocksdb_transactiondb_put_cf(txn_db, woptions, cfh, "cf_foo", 6, "cf_hello", + 8, &err); + CheckNoError(err); + CheckTxnDBGetCF(txn_db, roptions, cfh, "cf_foo", "cf_hello"); + + rocksdb_transactiondb_delete_cf(txn_db, woptions, cfh, "cf_foo", 6, &err); + CheckNoError(err); + CheckTxnDBGetCF(txn_db, roptions, cfh, "cf_foo", NULL); + + rocksdb_column_family_handle_destroy(cfh); + // close and destroy rocksdb_transaction_destroy(txn); rocksdb_transactiondb_close(txn_db); diff --git a/include/rocksdb/c.h b/include/rocksdb/c.h index 3d3c175f91..077030775f 100644 --- a/include/rocksdb/c.h +++ b/include/rocksdb/c.h @@ -1256,6 +1256,12 @@ extern ROCKSDB_LIBRARY_API void rocksdb_delete_file_in_range_cf( /* Transactions */ +extern ROCKSDB_LIBRARY_API rocksdb_column_family_handle_t* +rocksdb_transactiondb_create_column_family( + rocksdb_transactiondb_t* txn_db, + const rocksdb_options_t* column_family_options, + const char* column_family_name, char** errptr); + extern ROCKSDB_LIBRARY_API rocksdb_transactiondb_t* rocksdb_transactiondb_open( const rocksdb_options_t* options, const rocksdb_transactiondb_options_t* txn_db_options, const char* name, @@ -1286,18 +1292,37 @@ extern ROCKSDB_LIBRARY_API char* rocksdb_transaction_get( rocksdb_transaction_t* txn, const rocksdb_readoptions_t* options, const char* key, size_t klen, size_t* vlen, char** errptr); +extern ROCKSDB_LIBRARY_API char* rocksdb_transaction_get_cf( + rocksdb_transaction_t* txn, const rocksdb_readoptions_t* options, + rocksdb_column_family_handle_t* column_family, const char* key, size_t klen, + size_t* vlen, char** errptr); + extern ROCKSDB_LIBRARY_API char* rocksdb_transactiondb_get( rocksdb_transactiondb_t* txn_db, const rocksdb_readoptions_t* options, const char* key, size_t klen, size_t* vlen, char** errptr); +extern ROCKSDB_LIBRARY_API char* rocksdb_transactiondb_get_cf( + rocksdb_transactiondb_t* txn_db, const rocksdb_readoptions_t* options, + rocksdb_column_family_handle_t* column_family, const char* key, + size_t keylen, size_t* vallen, char** errptr); + extern ROCKSDB_LIBRARY_API void rocksdb_transaction_put( rocksdb_transaction_t* txn, const char* key, size_t klen, const char* val, size_t vlen, char** errptr); +extern ROCKSDB_LIBRARY_API void rocksdb_transaction_put_cf( + rocksdb_transaction_t* txn, rocksdb_column_family_handle_t* column_family, + const char* key, size_t klen, const char* val, size_t vlen, char** errptr); + extern ROCKSDB_LIBRARY_API void rocksdb_transactiondb_put( rocksdb_transactiondb_t* txn_db, const rocksdb_writeoptions_t* options, const char* key, size_t klen, const char* val, size_t vlen, char** errptr); +extern ROCKSDB_LIBRARY_API void rocksdb_transactiondb_put_cf( + rocksdb_transactiondb_t* txn_db, const rocksdb_writeoptions_t* options, + rocksdb_column_family_handle_t* column_family, const char* key, + size_t keylen, const char* val, size_t vallen, char** errptr); + extern ROCKSDB_LIBRARY_API void rocksdb_transactiondb_write( rocksdb_transactiondb_t* txn_db, const rocksdb_writeoptions_t* options, rocksdb_writebatch_t *batch, char** errptr); @@ -1305,10 +1330,19 @@ extern ROCKSDB_LIBRARY_API void rocksdb_transactiondb_write( extern ROCKSDB_LIBRARY_API void rocksdb_transaction_delete( rocksdb_transaction_t* txn, const char* key, size_t klen, char** errptr); +extern ROCKSDB_LIBRARY_API void rocksdb_transaction_delete_cf( + rocksdb_transaction_t* txn, rocksdb_column_family_handle_t* column_family, + const char* key, size_t klen, char** errptr); + extern ROCKSDB_LIBRARY_API void rocksdb_transactiondb_delete( rocksdb_transactiondb_t* txn_db, const rocksdb_writeoptions_t* options, const char* key, size_t klen, char** errptr); +extern ROCKSDB_LIBRARY_API void rocksdb_transactiondb_delete_cf( + rocksdb_transactiondb_t* txn_db, const rocksdb_writeoptions_t* options, + rocksdb_column_family_handle_t* column_family, const char* key, + size_t keylen, char** errptr); + extern ROCKSDB_LIBRARY_API rocksdb_iterator_t* rocksdb_transaction_create_iterator(rocksdb_transaction_t* txn, const rocksdb_readoptions_t* options);