rocksdb/examples/simple_example.c

49 lines
1.4 KiB
C
Raw Normal View History

2014-11-27 22:49:19 +01:00
#include <stdio.h>
#include <string.h>
2014-11-30 06:42:42 +01:00
#include <stdlib.h>
2014-11-27 22:49:19 +01:00
#include <assert.h>
#include "rocksdb/c.h"
#include <unistd.h> // sysconf() - get CPU count
2014-11-27 22:53:04 +01:00
const char DBPath[] = "/tmp/rocksdb_simple_example";
2014-11-27 22:49:19 +01:00
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 = NULL;
2014-11-27 22:49:19 +01:00
db = rocksdb_open (options, DBPath, &err);
assert (!err);
2014-11-27 22:49:19 +01:00
// 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);
2014-11-27 22:49:19 +01:00
// Get value
rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create ();
size_t len;
value = rocksdb_get (db, readoptions, key, strlen (key), &len, &err);
assert (!err);
2014-11-27 22:49:19 +01:00
assert (strcmp (value, "value") == 0);
2014-11-30 06:42:42 +01:00
free (value);
2014-11-27 22:49:19 +01:00
// cleanup
rocksdb_writeoptions_destroy (writeoptions);
rocksdb_readoptions_destroy (readoptions);
rocksdb_options_destroy (options);
2014-11-27 22:49:19 +01:00
rocksdb_close (db);
return 0;
}