fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.4 KiB
50 lines
1.4 KiB
10 years ago
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
10 years ago
|
#include <stdlib.h>
|
||
10 years ago
|
#include <assert.h>
|
||
|
|
||
|
#include "rocksdb/c.h"
|
||
|
|
||
|
#include <unistd.h> // sysconf() - get CPU count
|
||
|
|
||
10 years ago
|
const char DBPath[] = "/tmp/rocksdb_simple_example";
|
||
10 years ago
|
|
||
10 years ago
|
int main(int argc, char **argv) {
|
||
10 years ago
|
rocksdb_t *db;
|
||
10 years ago
|
rocksdb_options_t *options = rocksdb_options_create();
|
||
|
// Optimize RocksDB. This is the easiest way to
|
||
|
// get RocksDB to perform well
|
||
10 years ago
|
long cpus = sysconf(_SC_NPROCESSORS_ONLN); // get # of online cores
|
||
|
rocksdb_options_increase_parallelism(options, (int)(cpus));
|
||
10 years ago
|
rocksdb_options_optimize_level_style_compaction(options, 0);
|
||
10 years ago
|
// create the DB if it's not already present
|
||
10 years ago
|
rocksdb_options_set_create_if_missing(options, 1);
|
||
10 years ago
|
|
||
|
// open DB
|
||
10 years ago
|
char *err = NULL;
|
||
10 years ago
|
db = rocksdb_open(options, DBPath, &err);
|
||
|
assert(!err);
|
||
10 years ago
|
|
||
|
// Put key-value
|
||
10 years ago
|
rocksdb_writeoptions_t *writeoptions = rocksdb_writeoptions_create();
|
||
10 years ago
|
const char key[] = "key";
|
||
10 years ago
|
const char *value = "value";
|
||
10 years ago
|
rocksdb_put(db, writeoptions, key, strlen (key), value, \
|
||
|
strlen (value), &err);
|
||
|
assert(!err);
|
||
10 years ago
|
// Get value
|
||
10 years ago
|
rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create();
|
||
10 years ago
|
size_t len;
|
||
10 years ago
|
value = rocksdb_get(db, readoptions, key, strlen (key), &len, &err);
|
||
|
assert(!err);
|
||
|
assert(strcmp(value, "value") == 0);
|
||
10 years ago
|
|
||
10 years ago
|
// cleanup
|
||
10 years ago
|
rocksdb_writeoptions_destroy(writeoptions);
|
||
|
rocksdb_readoptions_destroy(readoptions);
|
||
|
rocksdb_options_destroy(options);
|
||
|
rocksdb_close(db);
|
||
10 years ago
|
|
||
|
return 0;
|
||
|
}
|