collocatordb: add c api
diff --git a/Makefile b/Makefile
index 30684bc..895e252 100644
--- a/Makefile
+++ b/Makefile
@@ -17,6 +17,9 @@
testcdb: testcdb.cc collocatordb.h collocatordb.o Makefile
$(CXX) $(CXXFLAGS) -L. -L/usr/local/lib $@.cc -o$@ collocatordb.o -lrocksdb $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
+c_testcdb: c_testcdb.c collocatordb.h collocatordb.o Makefile
+ $(CC) $(CFLAGS) -L. -L/usr/local/lib $@.c -o$@ collocatordb.o -lstdc++ -lm -lrocksdb $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
+
collocatordb: collocatordb.cc Makefile
$(CXX) $(CXXFLAGS) -L/usr/local/lib $@.cc -o$@ -lrocksdb $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
diff --git a/c_testcdb.c b/c_testcdb.c
new file mode 100644
index 0000000..34f05ec
--- /dev/null
+++ b/c_testcdb.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include "collocatordb.h"
+
+int main() {
+ COLLOCATORS *cdb = open_collocators("/tmp/test2");
+ inc_collocators(cdb, 2000, 2000, 4);
+ inc_collocators(cdb, 2000, 2001, 4);
+ inc_collocators(cdb, 2000, 2002, 4);
+ dump_collocators(cdb, 2000, 0, 0);
+ return 0;
+}
diff --git a/collocatordb.cc b/collocatordb.cc
index cfc5969..d4745fd 100644
--- a/collocatordb.cc
+++ b/collocatordb.cc
@@ -18,6 +18,7 @@
#include "rocksdb/utilities/db_ttl.h"
#include "merge_operators.h"
+
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
#define encodeCollocation(w1, w2, dist) (((uint64_t)dist << 56) | ((uint64_t)w2 << 24) | w1)
#define W1(key) (uint64_t)(key & 0xffffff)
@@ -180,7 +181,7 @@
public:
Collocators(const char *db_name);
-
+
~Collocators();
// public interface of Collocators.
@@ -262,6 +263,7 @@
}
virtual void inc(const uint32_t w1, const uint32_t w2, const uint8_t dist);
+ void dump(uint32_t w1, uint32_t w2, int8_t dist);
// mapped to a rocksdb Merge operation
@@ -332,8 +334,34 @@
return cit;
}
+ void rocksdb::Collocators::dump(uint32_t w1, uint32_t w2, int8_t dist) {
+ auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, w2, dist));
+ for (; it->isValid(); it->Next()) {
+ uint64_t value = it->intValue();
+ uint64_t key = it->intKey();
+ std::cout << "w1:" << W1(key) << ", w2:" << W2(key) << ", dist:" << (int32_t) DIST(key) << " - count:" << value << std::endl;
+ }
+ std::cout << "ready dumping\n";
+ }
+
rocksdb::Slice rocksdb::CollocatorIterator::key() const { return base_iterator_->key(); }
rocksdb::Slice rocksdb::CollocatorIterator::value() const { return base_iterator_->value(); }
rocksdb::Status rocksdb::CollocatorIterator::status() const { return base_iterator_->status(); }
};
+
+typedef rocksdb::Collocators COLLOCATORS;
+
+extern "C" {
+ COLLOCATORS *open_collocators(char *dbname) {
+ return new rocksdb::Collocators(dbname);
+ }
+
+ void inc_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
+ db->inc(w1, w2, dist);
+ }
+
+ void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
+ db->dump(w1, w2, dist);
+ }
+}
diff --git a/collocatordb.h b/collocatordb.h
index 72ab00d..3aea4bd 100644
--- a/collocatordb.h
+++ b/collocatordb.h
@@ -1,12 +1,8 @@
+#ifdef __cplusplus
#include <typeinfo>
-#include <stdint.h>
-#include "rocksdb/cache.h"
-#include "rocksdb/comparator.h"
#include "rocksdb/db.h"
-#include "rocksdb/env.h"
-#include <rocksdb/merge_operator.h>
-#include "rocksdb/utilities/db_ttl.h"
-#include "merge_operators.h"
+#endif
+#include <stdint.h>
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
#define encodeCollocation(w1, w2, dist) (((uint64_t)dist << 56) | ((uint64_t)w2 << 24) | w1)
@@ -14,6 +10,7 @@
#define W2(key) (uint64_t)((key >> 24) & 0xffffff)
#define DIST(key) (int8_t)((uint64_t)((key >> 56) & 0xff))
+#ifdef __cplusplus
namespace rocksdb {
class CollocatorIterator : public Iterator {
public:
@@ -27,12 +24,25 @@
uint64_t intKey();
};
- class Collocators {
- public:
+ extern "C" {
+ class Collocators {
+ public:
Collocators(const char *db_name);
~Collocators();
void inc(const uint32_t w1, const uint32_t w2, const uint8_t dist);
+ void dump(const uint32_t w1, const uint32_t w2, const uint8_t dist);
CollocatorIterator* SeekIterator(uint64_t w1, uint64_t w2, int8_t dist);
- };
-
+ };
+
+ }
}
+
+typedef rocksdb::Collocators COLLOCATORS;
+
+#else
+typedef struct COLLOCATORS COLLOCATORS;
+#endif
+
+extern COLLOCATORS *open_collocators(char *s);
+extern void inc_collocators(COLLOCATORS *db, uint64_t w1, uint64_t w2, int8_t dist);
+extern void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist);