diff --git a/cpp/Makefile b/cpp/Makefile index b0553fd..d7626fc 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -4,7 +4,7 @@ INCS = -I./ -I/opt/homebrew/include/ -I./vendor/lmdbxx/include/ TARGET = libnegentropy.so -.PHONY: all clean install-deps +.PHONY: all clean install-deps precompiled-header shared-lib all: precompiled-header shared-lib @@ -16,7 +16,7 @@ install-deps: precompiled-header: g++ --std=c++20 -Wall -fexceptions -g negentropy.h $(INCS) -shared-lib: negentropy.h.gch +shared-lib: g++ --std=c++20 $(INCS) -shared -fPIC -o $(TARGET) negentropy_wrapper.c -lcrypto -lssl -L/opt/homebrew/lib/ clean: diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index 5974aac..0499d63 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -1,3 +1,5 @@ +#include + #include "negentropy.h" #include "negentropy/storage/BTreeMem.h" #include "negentropy_wrapper.h" @@ -5,7 +7,7 @@ //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. //TODO: Do error handling by catching exceptions - +using namespace std; void* storage_new(const char* db_path, const char* name){ negentropy::storage::BTreeMem* storage; @@ -65,32 +67,32 @@ void negentropy_setinitiator(void* negentropy){ } -bool storage_insert(void* storage, uint64_t createdAt, const char* id){ +bool storage_insert(void* storage, uint64_t createdAt, buffer* id){ negentropy::storage::BTreeMem* lmdbStorage; lmdbStorage = reinterpret_cast(storage); - + std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:" << std::string_view(id->data, id->len) << "length is:"<< id->len << std::endl; //TODO: Error handling. Is it required? //How does out of memory get handled? - return lmdbStorage->insert(createdAt, id); + return lmdbStorage->insert(createdAt, std::string_view(id->data, id->len)); } -bool storage_erase(void* storage, uint64_t createdAt, const char* id){ +bool storage_erase(void* storage, uint64_t createdAt, buffer* id){ negentropy::storage::BTreeMem* lmdbStorage; lmdbStorage = reinterpret_cast(storage); //TODO: Error handling - return lmdbStorage->erase(createdAt, id); + return lmdbStorage->erase(createdAt, std::string_view(id->data, id->len)); } -const char* reconcile(void* negentropy, const char* query, uint64_t query_len){ +const char* reconcile(void* negentropy, buffer* query){ Negentropy *ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); std::string* output = new std::string(); try { - *output = ngn_inst->reconcile(std::string_view(query, query_len)); + *output = ngn_inst->reconcile(std::string_view(query->data, query->len)); } catch(negentropy::err e){ //TODO:Find a way to return this error return NULL; @@ -105,7 +107,7 @@ char *convert(const std::string & s) return pc; } -const char* reconcile_with_ids(void* negentropy, const char* query, uint64_t query_len, char* have_ids[], +const char* reconcile_with_ids(void* negentropy, buffer* query, char* have_ids[], uint64_t *have_ids_len, char* need_ids[], uint64_t *need_ids_len){ Negentropy *ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); @@ -115,7 +117,7 @@ const char* reconcile_with_ids(void* negentropy, const char* query, uint64_t que std::vector needIds; try { - *output = ngn_inst->reconcile(std::string_view(query, query_len), haveIds, needIds); + *output = ngn_inst->reconcile(std::string_view(query->data, query->len), haveIds, needIds); *have_ids_len = haveIds.size(); *need_ids_len = needIds.size(); diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index 68fcff5..bc979aa 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -8,6 +8,11 @@ #define EXTERNC #endif +typedef struct _buffer_{ + uint64_t len ; + char* data; +}buffer; + //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. //TODO: Do error handling by catching exceptions @@ -19,13 +24,13 @@ EXTERNC const char* negentropy_initiate(void* negentropy); EXTERNC void negentropy_setinitiator(void* negentropy); -EXTERNC bool storage_insert(void* storage, uint64_t createdAt, const char* id); +EXTERNC bool storage_insert(void* storage, uint64_t createdAt, buffer* id); -EXTERNC bool storage_erase(void* storage, uint64_t createdAt, const char* id); +EXTERNC bool storage_erase(void* storage, uint64_t createdAt, buffer* id); -EXTERNC const char* reconcile(void* negentropy, const char* query, uint64_t query_len); +EXTERNC const char* reconcile(void* negentropy, buffer* query); -EXTERNC const char* reconcile_with_ids(void* negentropy, const char* query, uint64_t query_len, const char* have_ids[], +EXTERNC const char* reconcile_with_ids(void* negentropy, buffer* query, const char* have_ids[], uint64_t *have_ids_len, const char* need_ids[], uint64_t *need_ids_len); #endif