From 07a159d82f6372b2755c7176814953040ce8ee53 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Sat, 17 Feb 2024 07:18:22 +0530 Subject: [PATCH] feat: use BTreeMem to start with and also make C wrapper extern --- cpp/negentropy_wrapper.c | 38 +++++++++++++++++++------------------- cpp/negentropy_wrapper.h | 18 +++++++++--------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index c2d18a1..d744fea 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -5,7 +5,7 @@ //TODO: Do error handling by catching exceptions void* storage_new(const char* db_path, const char* name){ - negentropy::storage::BTreeLMDB* storage; + negentropy::storage::BTreeMem* storage; /* auto env = lmdb::env::create(); env.set_max_dbs(64); @@ -15,23 +15,23 @@ void* storage_new(const char* db_path, const char* name){ { auto txn = lmdb::txn::begin(env); - btreeDbi = negentropy::storage::BTreeLMDB::setupDB(txn, name); + btreeDbi = negentropy::storage::BTreeMem::setupDB(txn, name); txn.commit(); } */ //TODO: Finish constructor - //storage = new negentropy::storage::BTreeLMDB(); + storage = new negentropy::storage::BTreeMem(); return storage; } void* negentropy_new(void* storage, uint64_t frameSizeLimit){ //TODO: Make these typecasts into macros?? - negentropy::storage::BTreeLMDB* lmdbStorage; + negentropy::storage::BTreeMem* lmdbStorage; //TODO: reinterpret cast is risky, need to use more safe type conversion. - lmdbStorage = reinterpret_cast(storage); + lmdbStorage = reinterpret_cast(storage); - Negentropy* ne; + Negentropy* ne; try{ - ne = new Negentropy(*lmdbStorage, frameSizeLimit); + ne = new Negentropy(*lmdbStorage, frameSizeLimit); }catch(negentropy::err e){ //TODO:Find a way to return this error return NULL; @@ -40,8 +40,8 @@ void* negentropy_new(void* storage, uint64_t frameSizeLimit){ } const char* negentropy_initiate(void* negentropy){ - Negentropy* ngn_inst; - ngn_inst = reinterpret_cast*>(negentropy); + Negentropy* ngn_inst; + ngn_inst = reinterpret_cast*>(negentropy); std::string* output = new std::string(); try { @@ -54,8 +54,8 @@ const char* negentropy_initiate(void* negentropy){ } void negentropy_setinitiator(void* negentropy){ - Negentropy *ngn_inst; - ngn_inst = reinterpret_cast*>(negentropy); + Negentropy *ngn_inst; + ngn_inst = reinterpret_cast*>(negentropy); ngn_inst->setInitiator(); @@ -63,8 +63,8 @@ void negentropy_setinitiator(void* negentropy){ bool storage_insert(void* storage, uint64_t createdAt, const char* id){ - negentropy::storage::BTreeLMDB* lmdbStorage; - lmdbStorage = reinterpret_cast(storage); + negentropy::storage::BTreeMem* lmdbStorage; + lmdbStorage = reinterpret_cast(storage); //TODO: Error handling. Is it required? //How does out of memory get handled? @@ -73,8 +73,8 @@ bool storage_insert(void* storage, uint64_t createdAt, const char* id){ bool storage_erase(void* storage, uint64_t createdAt, const char* id){ - negentropy::storage::BTreeLMDB* lmdbStorage; - lmdbStorage = reinterpret_cast(storage); + negentropy::storage::BTreeMem* lmdbStorage; + lmdbStorage = reinterpret_cast(storage); //TODO: Error handling return lmdbStorage->erase(createdAt, id); @@ -82,8 +82,8 @@ bool storage_erase(void* storage, uint64_t createdAt, const char* id){ const char* reconcile(void* negentropy, const char* query){ - Negentropy *ngn_inst; - ngn_inst = reinterpret_cast*>(negentropy); + Negentropy *ngn_inst; + ngn_inst = reinterpret_cast*>(negentropy); std::string* output = new std::string(); try { @@ -97,8 +97,8 @@ const char* reconcile(void* negentropy, const char* query){ const char* reconcile_with_ids(void* negentropy, const char* query, const char* have_ids[], uint64_t have_ids_len, const char* need_ids[], uint64_t need_ids_len){ - Negentropy *ngn_inst; - ngn_inst = reinterpret_cast*>(negentropy); + Negentropy *ngn_inst; + ngn_inst = reinterpret_cast*>(negentropy); std::optional* output; std::vector haveIds(have_ids, have_ids+have_ids_len); diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index e18ab5f..d145025 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -3,28 +3,28 @@ #define _NEGENTROPY_WRAPPER_H #include "negentropy.h" -#include "negentropy/storage/BTreeLMDB.h" +#include "negentropy/storage/BTreeMem.h" #define length(array) ((sizeof(array)) / (sizeof(array[0]))) //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. //TODO: Do error handling by catching exceptions -void* storage_new(const char* db_path, const char* name); +extern "C" void* storage_new(const char* db_path, const char* name); -void* negentropy_new(void* storage, uint64_t frameSizeLimit); +extern "C" void* negentropy_new(void* storage, uint64_t frameSizeLimit); -const char* negentropy_initiate(void* negentropy); +extern "C" const char* negentropy_initiate(void* negentropy); -void negentropy_setinitiator(void* negentropy); +extern "C" void negentropy_setinitiator(void* negentropy); -bool storage_insert(void* storage, uint64_t createdAt, const char* id); +extern "C" bool storage_insert(void* storage, uint64_t createdAt, const char* id); -bool storage_erase(void* storage, uint64_t createdAt, const char* id); +extern "C" bool storage_erase(void* storage, uint64_t createdAt, const char* id); -const char* reconcile(void* negentropy, const char* query); +extern "C" const char* reconcile(void* negentropy, const char* query); -const char* reconcile_with_ids(void* negentropy, const char* query, const char* have_ids[], +extern "C" const char* reconcile_with_ids(void* negentropy, const char* query, const char* have_ids[], uint64_t have_ids_len, const char* need_ids[], uint64_t need_ids_len); #endif