From 0a9a7e753c499db59a62a3232ce2f275e2028339 Mon Sep 17 00:00:00 2001 From: Haneef Mubarak Date: Thu, 27 Nov 2014 13:49:19 -0800 Subject: [PATCH 1/7] added C version of simple_example --- examples/simple_example.c | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/simple_example.c diff --git a/examples/simple_example.c b/examples/simple_example.c new file mode 100644 index 000000000..a64861346 --- /dev/null +++ b/examples/simple_example.c @@ -0,0 +1,42 @@ +#include +#include +#include + +#include "rocksdb/c.h" + +#include // sysconf() - get CPU count + +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 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; + 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); + + rocksdb_close (db); + + return 0; +} From 9c34d5e36152f067c1f4068e3f9fdd8405de636e Mon Sep 17 00:00:00 2001 From: Haneef Mubarak Date: Thu, 27 Nov 2014 13:53:04 -0800 Subject: [PATCH 2/7] fix type in C simple example --- examples/simple_example.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_example.c b/examples/simple_example.c index a64861346..ecb4c8f37 100644 --- a/examples/simple_example.c +++ b/examples/simple_example.c @@ -6,7 +6,7 @@ #include // sysconf() - get CPU count -char DBPath[] = "/tmp/rocksdb_simple_example"; +const char DBPath[] = "/tmp/rocksdb_simple_example"; int main (int argc, char **argv) { rocksdb_t *db; From d7f5ccb0c27fa5cd869a6bf822d887a2dd956f69 Mon Sep 17 00:00:00 2001 From: Haneef Mubarak Date: Thu, 27 Nov 2014 15:06:12 -0800 Subject: [PATCH 3/7] add c example to makefile and fix "make clean" --- examples/Makefile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index ce43785e0..10d5fe190 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -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: + $(CXX) $(CXXFLAGS) -xc $@.c -o$@ ../librocksdb.a -I../include -O2 $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS) + +clean: + rm -rf ./simple_example ./column_families_example ./compact_files_example ./simple_example-c From ac4ed1e305dda3d030bf384d8570957107303a9d Mon Sep 17 00:00:00 2001 From: Haneef Mubarak Date: Thu, 27 Nov 2014 15:20:55 -0800 Subject: [PATCH 4/7] fix examples/makefile for C example --- examples/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index 10d5fe190..abeee2488 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -13,8 +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) -simple_example-c: - $(CXX) $(CXXFLAGS) -xc $@.c -o$@ ../librocksdb.a -I../include -O2 $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS) +simple_example-c: simple_example.c + $(CXX) -xc -I../include simple_example.c -L.. -lrocksdb -pthread -lsnappy -lbz2 -lz -lrt clean: rm -rf ./simple_example ./column_families_example ./compact_files_example ./simple_example-c From c6f31a289303794f990b78baac104c1b761ffb32 Mon Sep 17 00:00:00 2001 From: Haneef Mubarak Date: Sat, 29 Nov 2014 21:42:42 -0800 Subject: [PATCH 5/7] minor memory leak in C example --- examples/Makefile | 2 +- examples/simple_example.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/Makefile b/examples/Makefile index abeee2488..96c8bc3cf 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -14,7 +14,7 @@ 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) simple_example-c: simple_example.c - $(CXX) -xc -I../include simple_example.c -L.. -lrocksdb -pthread -lsnappy -lbz2 -lz -lrt + $(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 diff --git a/examples/simple_example.c b/examples/simple_example.c index ecb4c8f37..59848902a 100644 --- a/examples/simple_example.c +++ b/examples/simple_example.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "rocksdb/c.h" @@ -35,6 +36,7 @@ int main (int argc, char **argv) { value = rocksdb_get (db, readoptions, key, strlen (key), &len, &err); // assert (!err); assert (strcmp (value, "value") == 0); + free (value); rocksdb_close (db); From 91c58752fa4bc32cfd51189b3f35edde77816ff4 Mon Sep 17 00:00:00 2001 From: Haneef Mubarak Date: Wed, 17 Dec 2014 02:06:36 -0800 Subject: [PATCH 6/7] error detection and memory leaks in c example --- examples/simple_example.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/simple_example.c b/examples/simple_example.c index 59848902a..29de5629b 100644 --- a/examples/simple_example.c +++ b/examples/simple_example.c @@ -20,24 +20,28 @@ int main (int argc, char **argv) { rocksdb_options_set_create_if_missing (options, 1); // open DB - char *err; + char *err = NULL; db = rocksdb_open (options, DBPath, &err); -// assert (!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); + 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 (!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; From 28424d734bee62ff1999b17e6938c42abfd8bdc9 Mon Sep 17 00:00:00 2001 From: Haneef Mubarak Date: Thu, 18 Dec 2014 06:48:46 -0800 Subject: [PATCH 7/7] style fixes in c example --- examples/simple_example.c | 44 ++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/examples/simple_example.c b/examples/simple_example.c index 29de5629b..e982cce89 100644 --- a/examples/simple_example.c +++ b/examples/simple_example.c @@ -9,40 +9,42 @@ const char DBPath[] = "/tmp/rocksdb_simple_example"; -int main (int argc, char **argv) { +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); + 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); + rocksdb_options_set_create_if_missing(options, 1); // open DB char *err = NULL; - db = rocksdb_open (options, DBPath, &err); - assert (!err); + db = rocksdb_open(options, DBPath, &err); + assert(!err); // Put key-value - rocksdb_writeoptions_t *writeoptions = rocksdb_writeoptions_create (); + 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); + rocksdb_put(db, writeoptions, key, strlen (key), value, \ + strlen (value), &err); + assert(!err); // Get value - rocksdb_readoptions_t *readoptions = rocksdb_readoptions_create (); + 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); + 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); + rocksdb_writeoptions_destroy(writeoptions); + rocksdb_readoptions_destroy(readoptions); + rocksdb_options_destroy(options); + rocksdb_close(db); return 0; }