Skip to content
/ ctdb Public

ctdb is a simple & lightweight key-value database, based on "copy-on-write"(Append-only) Compressed Trie.

License

Notifications You must be signed in to change notification settings

souce/ctdb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ctdb

ctdb is a simple & lightweight key-value database, based on "copy-on-write"(Append-only) Compressed Trie.

Supported operations

  • Add a new key/value pair to the database.
  • Delete an existing key from the database.
  • Querying the database by a specific key.
  • Traverse the database by a specific prefix.
  • Support for transactions.

Quick start

make iter; ./iter

make simple; ./simple

make trans; rm ./test.db; ./trans

make vacuum; rm ./*.db; ./vacuum

example

put & del:

struct ctdb *db = ctdb_open("./test.db");

struct ctdb_transaction *trans = ctdb_transaction_begin(db);
ctdb_put(trans, "apple", 5, "apple_value", 11);
ctdb_put(trans, "app", 3, "app_value", 9);
ctdb_put(trans, "application", 11, "application_value", 17);

ctdb_del(trans, "app", 3);

ctdb_transaction_commit(trans);
//ctdb_transaction_rollback(trans);

ctdb_transaction_free(trans);
ctdb_close(db);

get:

struct ctdb *db = ctdb_open("./test.db");

struct ctdb_transaction *trans = ctdb_transaction_begin(db);
struct ctdb_leaf leaf = ctdb_get(trans, "app", 3);
//seek(db->fd, leaf.value_pos, SEEK_SET)
//read(db->fd, buffer, leaf.value_len)) or sendfile(client_fd, db->fd, &off, leaf.value_len)

ctdb_transaction_free(trans);
ctdb_close(db);

traverse:

struct ctdb *db = ctdb_open("./test.db");
struct ctdb_transaction *trans = ctdb_transaction_begin(db);

//traversing data starting with "app"
int res = CTDB_FOREACH(trans, "app", 3, 
            (int fd, char *key, uint8_t key_len, struct ctdb_leaf leaf){
                printf("key:%.*s value_len:%u\n", key_len, key, leaf.value_len);
                //return CTDB_ERR; //stop traversal
                return CTDB_OK; //continue
            }
        );

ctdb_transaction_free(trans);
ctdb_close(db);

vacuum:

struct ctdb *db = ctdb_open("./test.db");
struct ctdb_transaction *trans = ctdb_transaction_begin(db);
//...
//copy the data to a new file and compress it
struct ctdb *new_db = ctdb_open("./test_tmp.db");
assert(CTDB_OK == ctdb_vacuum(trans, new_db));
ctdb_transaction_free(&trans);
ctdb_close(&new_db);
ctdb_close(&db);

About

ctdb is a simple & lightweight key-value database, based on "copy-on-write"(Append-only) Compressed Trie.

Resources

License

Stars

Watchers

Forks