chore: modify to use struct and fix makefile

This commit is contained in:
Prem Chaitanya Prathi 2024-02-21 13:45:24 +05:30
parent a736a26532
commit b467a09940
No known key found for this signature in database
3 changed files with 23 additions and 16 deletions

View File

@ -4,7 +4,7 @@
INCS = -I./ -I/opt/homebrew/include/ -I./vendor/lmdbxx/include/ INCS = -I./ -I/opt/homebrew/include/ -I./vendor/lmdbxx/include/
TARGET = libnegentropy.so TARGET = libnegentropy.so
.PHONY: all clean install-deps .PHONY: all clean install-deps precompiled-header shared-lib
all: precompiled-header shared-lib all: precompiled-header shared-lib
@ -16,7 +16,7 @@ install-deps:
precompiled-header: precompiled-header:
g++ --std=c++20 -Wall -fexceptions -g negentropy.h $(INCS) 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/ g++ --std=c++20 $(INCS) -shared -fPIC -o $(TARGET) negentropy_wrapper.c -lcrypto -lssl -L/opt/homebrew/lib/
clean: clean:

View File

@ -1,3 +1,5 @@
#include <iostream>
#include "negentropy.h" #include "negentropy.h"
#include "negentropy/storage/BTreeMem.h" #include "negentropy/storage/BTreeMem.h"
#include "negentropy_wrapper.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. //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code.
//TODO: Do error handling by catching exceptions //TODO: Do error handling by catching exceptions
using namespace std;
void* storage_new(const char* db_path, const char* name){ void* storage_new(const char* db_path, const char* name){
negentropy::storage::BTreeMem* storage; 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; negentropy::storage::BTreeMem* lmdbStorage;
lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(storage); lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(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? //TODO: Error handling. Is it required?
//How does out of memory get handled? //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; negentropy::storage::BTreeMem* lmdbStorage;
lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(storage); lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(storage);
//TODO: Error handling //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<negentropy::storage::BTreeMem> *ngn_inst; Negentropy<negentropy::storage::BTreeMem> *ngn_inst;
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy); ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy);
std::string* output = new std::string(); std::string* output = new std::string();
try { 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){ } catch(negentropy::err e){
//TODO:Find a way to return this error //TODO:Find a way to return this error
return NULL; return NULL;
@ -105,7 +107,7 @@ char *convert(const std::string & s)
return pc; 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){ uint64_t *have_ids_len, char* need_ids[], uint64_t *need_ids_len){
Negentropy<negentropy::storage::BTreeMem> *ngn_inst; Negentropy<negentropy::storage::BTreeMem> *ngn_inst;
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy); ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy);
@ -115,7 +117,7 @@ const char* reconcile_with_ids(void* negentropy, const char* query, uint64_t que
std::vector<std::string> needIds; std::vector<std::string> needIds;
try { 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(); *have_ids_len = haveIds.size();
*need_ids_len = needIds.size(); *need_ids_len = needIds.size();

View File

@ -8,6 +8,11 @@
#define EXTERNC #define EXTERNC
#endif #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. //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code.
//TODO: Do error handling by catching exceptions //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 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); uint64_t *have_ids_len, const char* need_ids[], uint64_t *need_ids_len);
#endif #endif