Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rocksdb_create_column_families_with_options to C API. #12896

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions db/c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,34 @@ rocksdb_column_family_handle_t** rocksdb_create_column_families(
return c_handles;
}

rocksdb_column_family_handle_t** rocksdb_create_column_families_with_options(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve of changing the parameter order so as not to allow confusion of options[] and options* ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @alanpaxton, I don't understand what are you saying. Can you please describe it more?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are different rocksdb_create_column_famil[y|ies] methods with different orders of parameters. I was confused by the different orders of parameters. But I think your function has the "correct" order of parameters for what it does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the other APIs don't have method comments, but maybe we could start ? In particular state that this method takes a set of options per CF.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but wasn't be better to add doc comments to c.h file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, at least in this case where we are documenting the API. Thanks for doing that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we implement a TTL variant, or is that enough scope ? I prefer to restrict the scope but worth thinking about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first C API. I would like to start small and then we can add more later. Once API is out, it's hard to remove it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

rocksdb_t* db, int num_column_families,
const char* const* column_family_names,
const rocksdb_options_t* const* column_family_options, size_t* lencfs,
char** errptr) {
std::vector<ColumnFamilyHandle*> handles;

std::vector<ColumnFamilyDescriptor> column_families;
column_families.reserve(num_column_families);
for (int i = 0; i < num_column_families; i++) {
column_families.emplace_back(
std::string(column_family_names[i]),
ColumnFamilyOptions(column_family_options[i]->rep));
}
SaveError(errptr, db->rep->CreateColumnFamilies(column_families, &handles));

*lencfs = handles.size();
rocksdb_column_family_handle_t** c_handles =
static_cast<rocksdb_column_family_handle_t**>(
malloc(sizeof(rocksdb_column_family_handle_t*) * handles.size()));
for (size_t i = 0; i != handles.size(); ++i) {
c_handles[i] = new rocksdb_column_family_handle_t;
c_handles[i]->rep = handles[i];
c_handles[i]->immortal = false;
}
return c_handles;
}

void rocksdb_create_column_families_destroy(
rocksdb_column_family_handle_t** list) {
free(list);
Expand Down
74 changes: 74 additions & 0 deletions db/c_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,79 @@ int main(int argc, char** argv) {
CheckGet(db, roptions, "bar", "fake");
}

StartPhase("CF with option");
{
rocksdb_close(db);
rocksdb_destroy_db(options, dbname, &err);
CheckNoError(err);

rocksdb_options_t* db_options = rocksdb_options_create();
rocksdb_options_set_create_if_missing(db_options, 1);
db = rocksdb_open(db_options, dbname, &err);
CheckNoError(err);

char** cf_names = (char**)malloc(2 * sizeof(char*));
cf_names[0] = "cf1";
cf_names[1] = "cf2";
size_t cflen;

rocksdb_options_t* cf1_options = rocksdb_options_create();
rocksdb_options_set_paranoid_checks(cf1_options, true);
rocksdb_options_set_create_if_missing(cf1_options, true);

rocksdb_options_t* cf2_options = rocksdb_options_create();
rocksdb_options_set_max_bytes_for_level_base(cf2_options, 4 * 1024);
rocksdb_options_set_create_if_missing(cf2_options, true);

const rocksdb_options_t* cf_options[2] = {cf1_options, cf2_options};
rocksdb_column_family_handle_t** column_family_handle =
rocksdb_create_column_families_with_options(
db, 2, (const char* const*)cf_names, cf_options, &cflen, &err);
free(cf_names);
CheckNoError(err);
assert(cflen == 2);

rocksdb_writeoptions_t* write_options = rocksdb_writeoptions_create();

rocksdb_put_cf(db, write_options, column_family_handle[0], "cf1", 3, "val1",
4, &err);
CheckNoError(err);

rocksdb_put_cf(db, write_options, column_family_handle[1], "cf2", 3, "val2",
4, &err);
CheckNoError(err);

rocksdb_writeoptions_destroy(write_options);

size_t val_len;

rocksdb_readoptions_t* read_options = rocksdb_readoptions_create();

char* value1 = rocksdb_get_cf(db, read_options, column_family_handle[0],
"cf1", 3, &val_len, &err);
CheckNoError(err);
CheckEqual("val1", value1, val_len);
Free(&value1);

char* value2 = rocksdb_get_cf(db, read_options, column_family_handle[1],
"cf2", 3, &val_len, &err);
CheckNoError(err);
CheckEqual("val2", value2, val_len);
Free(&value2);

rocksdb_readoptions_destroy(read_options);

rocksdb_column_family_handle_destroy(column_family_handle[0]);
rocksdb_column_family_handle_destroy(column_family_handle[1]);

rocksdb_create_column_families_destroy(column_family_handle);

rocksdb_options_destroy(cf1_options);
rocksdb_options_destroy(cf2_options);

rocksdb_options_destroy(db_options);
}

StartPhase("columnfamilies");
{
rocksdb_close(db);
Expand Down Expand Up @@ -3710,6 +3783,7 @@ int main(int argc, char** argv) {
cfh1 = list_cfh[0];
cfh2 = list_cfh[1];
rocksdb_create_column_families_destroy(list_cfh);

txn = rocksdb_optimistictransaction_begin(otxn_db, woptions, otxn_options,
NULL);
rocksdb_transaction_put_cf(txn, cfh1, "key_cf1", 7, "val_cf1", 7, &err);
Expand Down
28 changes: 28 additions & 0 deletions include/rocksdb/c.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,34 @@ rocksdb_create_column_families(rocksdb_t* db,
const char* const* column_family_names,
size_t* lencfs, char** errptr);

/**
* @brief Creates multiple column families in the specified RocksDB database
* with the given options for each column family.
*
* @param db A pointer to the RocksDB database instance.
* @param num_column_families The number of column families to create.
* @param column_family_names An array of null-terminated strings, each
* representing the name of a column family to create.
* @param column_family_options An array of pointers to rocksdb_options_t
* structures, each specifying the options for the corresponding column family.
* @param lencfs A pointer to a size_t variable where the length of the created
* column families will be stored.
* @param errptr A pointer to a char pointer. If an error occurs, a descriptive
* error message will be stored in the memory pointed to by errptr. The caller
* must free this memory using rocksdb_free when it is no longer needed.
*
* @return An array of pointers to rocksdb_column_family_handle_t structures,
* each representing a handle to the created column families. The length of this
* array is stored in the variable pointed to by lencfs. If an error occurs,
* NULL is returned and errptr is set to a descriptive error message.
*/
extern ROCKSDB_LIBRARY_API rocksdb_column_family_handle_t**
rocksdb_create_column_families_with_options(
rocksdb_t* db, int num_column_families,
const char* const* column_family_names,
const rocksdb_options_t* const* column_family_options, size_t* lencfs,
char** errptr);

extern ROCKSDB_LIBRARY_API void rocksdb_create_column_families_destroy(
rocksdb_column_family_handle_t** list);

Expand Down
Loading