rocksdb/examples/simple_example.c

51 lines
1.4 KiB
C
Raw Normal View History

2014-11-27 21:49:19 +00:00
#include <stdio.h>
#include <string.h>
2014-11-30 05:42:42 +00:00
#include <stdlib.h>
2014-11-27 21:49:19 +00:00
#include <assert.h>
#include "rocksdb/c.h"
#include <unistd.h> // sysconf() - get CPU count
2014-11-27 21:53:04 +00:00
const char DBPath[] = "/tmp/rocksdb_simple_example";
2014-11-27 21:49:19 +00:00
2014-12-18 14:48:46 +00:00
int main(int argc, char **argv) {
2014-11-27 21:49:19 +00:00
rocksdb_t *db;
2014-12-18 14:48:46 +00:00
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 # of online cores
rocksdb_options_increase_parallelism(options, cpus);
rocksdb_options_optimize_level_style_compaction(options, 0);
2014-11-27 21:49:19 +00:00
// create the DB if it's not already present
2014-12-18 14:48:46 +00:00
rocksdb_options_set_create_if_missing(options, 1);
2014-11-27 21:49:19 +00:00
// open DB
char *err = NULL;
2014-12-18 14:48:46 +00:00
db = rocksdb_open(options, DBPath, &err);
assert(!err);
2014-11-27 21:49:19 +00:00
// Put key-value
2014-12-18 14:48:46 +00:00
rocksdb_writeoptions_t *writeoptions = rocksdb_writeoptions_create();
2014-11-27 21:49:19 +00:00
const char key[] = "key";
char *value = "value";
2014-12-18 14:48:46 +00:00
rocksdb_put(db, writeoptions, key, strlen (key), value, \
strlen (value), &err);
assert(!err);
2014-11-27 21:49:19 +00:00
// Get value
2014-12-18 14:48:46 +00:00
rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create();
2014-11-27 21:49:19 +00:00
size_t len;
2014-12-18 14:48:46 +00:00
value = rocksdb_get(db, readoptions, key, strlen (key), &len, &err);
assert(!err);
assert(strcmp(value, "value") == 0);
free(value);
2014-11-27 21:49:19 +00:00
// cleanup
2014-12-18 14:48:46 +00:00
rocksdb_writeoptions_destroy(writeoptions);
rocksdb_readoptions_destroy(readoptions);
rocksdb_options_destroy(options);
rocksdb_close(db);
2014-11-27 21:49:19 +00:00
return 0;
}