Merge pull request #427 from haneefmubarak/c-examples

C example
main
Igor Canadi 10 years ago
commit 62d19b7b59
  1. 9
      examples/Makefile
  2. 50
      examples/simple_example.c

@ -2,7 +2,7 @@ include ../build_config.mk
.PHONY: clean
all: simple_example column_families_example compact_files_example
all: simple_example column_families_example compact_files_example simple_example-c
simple_example: simple_example.cc
$(CXX) $(CXXFLAGS) $@.cc -o$@ ../librocksdb.a -I../include -O2 -std=c++11 $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
@ -13,5 +13,8 @@ column_families_example: column_families_example.cc
compact_files_example: compact_files_example.cc
$(CXX) $(CXXFLAGS) $@.cc -o$@ ../librocksdb.a -I../include -O2 -std=c++11 $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
clean: simple_example column_families_example compact_files_example
rm -rf ./simple_example ./column_families_example ./compact_files_example
simple_example-c: simple_example.c
$(CXX) -xc -I../include simple_example.c -o$@ -L.. -lrocksdb -pthread -lsnappy -lbz2 -lz -lrt
clean:
rm -rf ./simple_example ./column_families_example ./compact_files_example ./simple_example-c

@ -0,0 +1,50 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "rocksdb/c.h"
#include <unistd.h> // sysconf() - get CPU count
const 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 # 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;
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);
free(value);
// cleanup
rocksdb_writeoptions_destroy(writeoptions);
rocksdb_readoptions_destroy(readoptions);
rocksdb_options_destroy(options);
rocksdb_close(db);
return 0;
}
Loading…
Cancel
Save