From 0a9a7e753c499db59a62a3232ce2f275e2028339 Mon Sep 17 00:00:00 2001 From: Haneef Mubarak Date: Thu, 27 Nov 2014 13:49:19 -0800 Subject: [PATCH] added C version of simple_example --- examples/simple_example.c | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/simple_example.c diff --git a/examples/simple_example.c b/examples/simple_example.c new file mode 100644 index 000000000..a64861346 --- /dev/null +++ b/examples/simple_example.c @@ -0,0 +1,42 @@ +#include +#include +#include + +#include "rocksdb/c.h" + +#include // sysconf() - get CPU count + +char DBPath[] = "/tmp/rocksdb_simple_example"; + +int main (int argc, char **argv) { + rocksdb_t *db; + rocksdb_options_t *options = rocksdb_options_create (); + // Optimize RocksDB. This is the easiest way to get RocksDB to perform well + int cpus = sysconf (_SC_NPROCESSORS_ONLN); // get number of online cores + rocksdb_options_increase_parallelism (options, cpus); + rocksdb_options_optimize_level_style_compaction (options, 0); + // create the DB if it's not already present + rocksdb_options_set_create_if_missing (options, 1); + + // open DB + char *err; + db = rocksdb_open (options, DBPath, &err); +// assert (!err); + + // Put key-value + rocksdb_writeoptions_t *writeoptions = rocksdb_writeoptions_create (); + const char key[] = "key"; + char *value = "value"; + rocksdb_put (db, writeoptions, key, strlen (key), value, strlen (value), &err); +// assert (!err); + // Get value + rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create (); + size_t len; + value = rocksdb_get (db, readoptions, key, strlen (key), &len, &err); +// assert (!err); + assert (strcmp (value, "value") == 0); + + rocksdb_close (db); + + return 0; +}