From 311a21a22bdb6d80e5c4ba5e3d2f550e0062b2cb Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Wed, 1 May 2024 14:02:23 +0530 Subject: [PATCH] feat: wrapper API's using subranges (#5) * feat: wrapper API's using subranges * chore: address review comments --- cpp/Makefile | 2 +- cpp/example/test.c | 22 +++++-- ...tropy_wrapper.c => negentropy_wrapper.cpp} | 59 +++++++++++-------- cpp/negentropy_wrapper.h | 35 ++++++----- 4 files changed, 71 insertions(+), 47 deletions(-) rename cpp/{negentropy_wrapper.c => negentropy_wrapper.cpp} (89%) diff --git a/cpp/Makefile b/cpp/Makefile index 39f0719..44bf143 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -17,7 +17,7 @@ precompiled-header: g++ -O0 --std=c++20 -Wall -fexceptions -g negentropy.h $(INCS) shared-lib: - g++ -O0 -g -std=c++20 $(INCS) -shared -fPIC -o $(TARGET) negentropy_wrapper.c -lcrypto -lssl -L/opt/homebrew/lib/ + g++ -O0 -g -std=c++20 $(INCS) -shared -fPIC -o $(TARGET) negentropy_wrapper.cpp -lcrypto -lssl -L/opt/homebrew/lib/ clean: rm -f $(TARGET) negentropy.h.gch libnegentropy.so diff --git a/cpp/example/test.c b/cpp/example/test.c index c4efd44..92e88d8 100644 --- a/cpp/example/test.c +++ b/cpp/example/test.c @@ -8,6 +8,8 @@ #include #include "../negentropy_wrapper.h" +#define MAX_FRAME_SIZE 153600 + void printHexBuffer(buffer buf){ for (uint64_t i = 0; i < buf.len; ++i) { printf("%0hhx", buf.data[i]); @@ -83,23 +85,28 @@ int main(){ void* subrange = subrange_new(st2, 0 , UINT64_MAX); if (subrange == NULL){ perror("failed to init subrange"); + return -1; } - printf("subrange init successful"); + printf("subrange init successful with size %d \n ", subrange_size(subrange) ); + void* subrange1 = subrange_new(st1, 0 , UINT64_MAX); if (subrange == NULL){ perror("failed to init subrange"); + return -1; } - printf("subrange init successful"); + printf("subrange init successful with size %d \n ", subrange_size(subrange1) ); - void* ngn_inst1 = negentropy_new(subrange1, 153600); + void* ngn_inst1 = negentropy_new(subrange1, MAX_FRAME_SIZE); if(ngn_inst1 == NULL){ perror("failed to create negentropy instance"); + return -1; } - void* ngn_inst2 = negentropy_new(subrange, 153600); + void* ngn_inst2 = negentropy_new(subrange, MAX_FRAME_SIZE); if(ngn_inst2 == NULL){ perror("failed to create negentropy instance"); + return -1; } @@ -107,6 +114,7 @@ int main(){ int ret1 = negentropy_subrange_initiate(ngn_inst1, &res); if(ret1 < 0){ perror("failed to initiate negentropy instance"); + return -1; } printf("initiated negentropy successfully with output of len %llu \n", res.output.len); b4.len = res.output.len; @@ -148,6 +156,12 @@ int main(){ free(b4.data); free_result(&res1); + ret = storage_insert(st1, time(NULL), &b2); + if (ret){ + printf("inserted hash successfully in st1\n"); + } + printf("\n storage size after adding 1 more elem is %d, subrange size is %d \n", storage_size(st1), subrange_size(subrange1)); + subrange_delete(subrange); subrange_delete(subrange1); diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.cpp similarity index 89% rename from cpp/negentropy_wrapper.c rename to cpp/negentropy_wrapper.cpp index 409751f..57aca5d 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.cpp @@ -60,6 +60,7 @@ void* negentropy_new(void* storage, uint64_t frameSizeLimit){ try{ ne = new Negentropy(*lmdbStorage, frameSizeLimit); }catch(negentropy::err e){ + //TODO: Error handling return NULL; } return ne; @@ -70,24 +71,24 @@ int negentropy_initiate(void* negentropy, result* result){ Negentropy* ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); - std::string* output = new std::string(); + std::string output; try { - *output = ngn_inst->initiate(); + output = ngn_inst->initiate(); /* std::cout << "output of initiate is, len:" << output->size() << ", output:"; printHexString(std::string_view(*output)); */ } catch(negentropy::err e){ //std::cout << "Exception raised in initiate " << e.what() << std::endl; + //TODO: Error handling return -1; } - if (output->size() > 0 ){ - result->output.len = output->size(); - result->output.data = (unsigned char*)calloc(output->size(), sizeof(unsigned char)); - memcpy(result->output.data, (unsigned char*)output->c_str(),result->output.len) ; + if (output.size() > 0 ){ + result->output.len = output.size(); + result->output.data = (unsigned char*)calloc(output.size(), sizeof(unsigned char)); + memcpy(result->output.data, (unsigned char*)output.c_str(),result->output.len) ; }else { result->output.len = 0; result->output.data = NULL; } - delete output; return 0; } @@ -127,9 +128,9 @@ bool storage_erase(void* storage, uint64_t createdAt, buffer* id){ int reconcile(void* negentropy, buffer* query, result* result){ Negentropy *ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); - std::string* out = new std::string(); + std::string out; try { - *out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len)); + out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len)); /* std::cout << "reconcile output of reconcile is, len:" << out->size() << ", output:"; printHexString(std::string_view(*out)); */ } catch(negentropy::err e){ @@ -140,10 +141,10 @@ int reconcile(void* negentropy, buffer* query, result* result){ strcpy(result->error,e.what()); return -1; } - if (out->size() > 0 ){ - result->output.len = out->size(); - result->output.data = (unsigned char*)calloc(out->size(), sizeof(unsigned char)); - memcpy(result->output.data, (unsigned char*)out->c_str(),result->output.len) ; + if (out.size() > 0 ){ + result->output.len = out.size(); + result->output.data = (unsigned char*)calloc(out.size(), sizeof(unsigned char)); + memcpy(result->output.data, (unsigned char*)out.c_str(),result->output.len) ; }else { result->output.len = 0; result->output.data = NULL; @@ -292,6 +293,7 @@ void* subrange_new(void* storage, uint64_t startTimeStamp, uint64_t endTimeStamp try { subRange = new negentropy::storage::SubRange(*st, negentropy::Bound(startTimeStamp), negentropy::Bound(endTimeStamp)); } catch (negentropy::err e){ + //TODO: Error handling return NULL; } return subRange; @@ -302,6 +304,11 @@ void subrange_delete(void* range){ delete subRange; } +int subrange_size(void* range){ + negentropy::storage::SubRange* subrange = reinterpret_cast(range); + return subrange->size(); +} + void negentropy_subrange_delete(void* negentropy){ Negentropy* ngn_inst = reinterpret_cast*>(negentropy); delete ngn_inst; @@ -317,6 +324,7 @@ void* negentropy_subrange_new(void* subrange, uint64_t frameSizeLimit){ try{ ne = new Negentropy(*sub_range, frameSizeLimit); }catch(negentropy::err e){ + //TODO: Error handling return NULL; } return ne; @@ -327,24 +335,23 @@ int negentropy_subrange_initiate(void* negentropy, result* result){ Negentropy* ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); - std::string* output = new std::string(); + std::string output; try { - *output = ngn_inst->initiate(); + output = ngn_inst->initiate(); /* std::cout << "output of initiate is, len:" << output->size() << ", output:"; printHexString(std::string_view(*output)); */ } catch(negentropy::err e){ //std::cout << "Exception raised in initiate " << e.what() << std::endl; return -1; } - if (output->size() > 0 ){ - result->output.len = output->size(); - result->output.data = (unsigned char*)calloc(output->size(), sizeof(unsigned char)); - memcpy(result->output.data, (unsigned char*)output->c_str(),result->output.len) ; + if (output.size() > 0 ){ + result->output.len = output.size(); + result->output.data = (unsigned char*)calloc(output.size(), sizeof(unsigned char)); + memcpy(result->output.data, (unsigned char*)output.c_str(),result->output.len) ; }else { result->output.len = 0; result->output.data = NULL; } - delete output; return 0; } @@ -359,9 +366,9 @@ void negentropy_subrange_setinitiator(void* negentropy){ int reconcile_subrange(void* negentropy, buffer* query, result* result){ Negentropy *ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); - std::string* out = new std::string(); + std::string out; try { - *out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len)); + out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len)); /* std::cout << "reconcile output of reconcile is, len:" << out->size() << ", output:"; printHexString(std::string_view(*out)); */ } catch(negentropy::err e){ @@ -372,10 +379,10 @@ int reconcile_subrange(void* negentropy, buffer* query, result* result){ strcpy(result->error,e.what()); return -1; } - if (out->size() > 0 ){ - result->output.len = out->size(); - result->output.data = (unsigned char*)calloc(out->size(), sizeof(unsigned char)); - memcpy(result->output.data, (unsigned char*)out->c_str(),result->output.len) ; + if (out.size() > 0 ){ + result->output.len = out.size(); + result->output.data = (unsigned char*)calloc(out.size(), sizeof(unsigned char)); + memcpy(result->output.data, (unsigned char*)out.c_str(),result->output.len) ; }else { result->output.len = 0; result->output.data = NULL; diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index 32c9ba6..6cccab7 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -31,22 +31,6 @@ EXTERNC void storage_delete(void* storage); EXTERNC int storage_size(void* storage); -//SubRange methods -EXTERNC void* subrange_new(void* storage, uint64_t startTimeStamp, uint64_t endTimeStamp); - -EXTERNC void subrange_delete(void* range); - -EXTERNC void* negentropy_subrange_new(void* subrange, uint64_t frameSizeLimit); - -EXTERNC void negentropy_subrange_delete(void* negentropy); - -EXTERNC int negentropy_subrange_initiate(void* negentropy, result* result); - -EXTERNC int reconcile_subrange(void* negentropy, buffer* query, result* result); - -EXTERNC int reconcile_with_ids_subrange_no_cbk(void* negentropy, buffer* query, result* result); - -//End of SubRange methods EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit); EXTERNC void negentropy_delete(void* negentropy); @@ -69,5 +53,24 @@ EXTERNC int reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* EXTERNC void free_result(result* result); +//SubRange methods +EXTERNC void* subrange_new(void* storage, uint64_t startTimeStamp, uint64_t endTimeStamp); + +EXTERNC void subrange_delete(void* range); + +EXTERNC void* negentropy_subrange_new(void* subrange, uint64_t frameSizeLimit); + +EXTERNC void negentropy_subrange_delete(void* negentropy); + +EXTERNC int negentropy_subrange_initiate(void* negentropy, result* result); + +EXTERNC int reconcile_subrange(void* negentropy, buffer* query, result* result); + +EXTERNC int reconcile_with_ids_subrange_no_cbk(void* negentropy, buffer* query, result* result); + +EXTERNC int subrange_size(void* storage); + +//End of SubRange methods + #endif