rocksdb/examples/c_simple_example.c

50 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
2014-12-18 15:48:46 +01:00
int main(int argc, char **argv) {
2014-11-27 22:49:19 +01:00
rocksdb_t *db;
2014-12-18 15:48:46 +01:00
rocksdb_options_t *options = rocksdb_options_create();
// Optimize RocksDB. This is the easiest way to
// get RocksDB to perform well
2014-12-23 17:32:30 +01:00
long cpus = sysconf(_SC_NPROCESSORS_ONLN); // get # of online cores
rocksdb_options_increase_parallelism(options, (int)(cpus));
2014-12-18 15:48:46 +01:00
rocksdb_options_optimize_level_style_compaction(options, 0);
2014-11-27 22:49:19 +01:00
// create the DB if it's not already present
2014-12-18 15:48:46 +01:00
rocksdb_options_set_create_if_missing(options, 1);
2014-11-27 22:49:19 +01:00
// open DB
char *err = NULL;
2014-12-18 15:48:46 +01:00
db = rocksdb_open(options, DBPath, &err);
assert(!err);
2014-11-27 22:49:19 +01:00
// Put key-value
2014-12-18 15:48:46 +01:00
rocksdb_writeoptions_t *writeoptions = rocksdb_writeoptions_create();
2014-11-27 22:49:19 +01:00
const char key[] = "key";
2014-12-23 17:32:30 +01:00
const char *value = "value";
2014-12-18 15:48:46 +01:00
rocksdb_put(db, writeoptions, key, strlen (key), value, \
Fixed memory issue in c_simple_example Valgrind report prior to this fix: ==20829== Memcheck, a memory error detector ==20829== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==20829== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info ==20829== Command: ./c_simple_example ==20829== ==20829== Invalid read of size 1 ==20829== at 0x4C2F1C8: strcmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20829== by 0x422522: main (in /home/user/rocksgit/transfer/rocksdb-git/examples/c_simple_example) ==20829== Address 0x5f60df5 is 0 bytes after a block of size 5 alloc'd ==20829== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==20829== by 0x4226D5: CopyString (c.cc:498) ==20829== by 0x423032: rocksdb_get (c.cc:730) ==20829== by 0x4224EB: main (in /home/user/rocksgit/transfer/rocksdb-git/examples/c_simple_example) ==20829== ==20829== ==20829== HEAP SUMMARY: ==20829== in use at exit: 77 bytes in 5 blocks ==20829== total heap usage: 4,491 allocs, 4,486 frees, 839,216 bytes allocated ==20829== ==20829== LEAK SUMMARY: ==20829== definitely lost: 5 bytes in 1 blocks ==20829== indirectly lost: 0 bytes in 0 blocks ==20829== possibly lost: 0 bytes in 0 blocks ==20829== still reachable: 72 bytes in 4 blocks ==20829== suppressed: 0 bytes in 0 blocks ==20829== Rerun with --leak-check=full to see details of leaked memory ==20829== ==20829== For counts of detected and suppressed errors, rerun with: -v ==20829== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
2015-01-09 09:22:49 +01:00
strlen (value) + 1, &err);
2014-12-18 15:48:46 +01:00
assert(!err);
2014-11-27 22:49:19 +01:00
// Get value
2014-12-18 15:48:46 +01:00
rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create();
2014-11-27 22:49:19 +01:00
size_t len;
2014-12-18 15:48:46 +01:00
value = rocksdb_get(db, readoptions, key, strlen (key), &len, &err);
assert(!err);
assert(strcmp(value, "value") == 0);
2014-11-27 22:49:19 +01:00
// cleanup
2014-12-18 15:48:46 +01:00
rocksdb_writeoptions_destroy(writeoptions);
rocksdb_readoptions_destroy(readoptions);
rocksdb_options_destroy(options);
rocksdb_close(db);
2014-11-27 22:49:19 +01:00
return 0;
}