From f871ff9cd727fa4c304992322846a6aa99b6f1d4 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Wed, 13 Mar 2024 15:43:05 +0530 Subject: [PATCH] chore: optimize initiate and reconcile --- cpp/example/test.c | 51 +++++++++++++++++++++++----------------- cpp/negentropy_wrapper.c | 31 +++++++++++++++++------- cpp/negentropy_wrapper.h | 4 ++-- 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/cpp/example/test.c b/cpp/example/test.c index 7d77e93..f8cbaa6 100644 --- a/cpp/example/test.c +++ b/cpp/example/test.c @@ -81,45 +81,52 @@ int main(){ } buffer b4 ; - b4.len = 153600; - b4.data = (unsigned char*)malloc(153600); + b4.len = 0; + b4.data = (unsigned char*)malloc(37*sizeof(unsigned char)); printf("storage size of st2 is %d \n",storage_size(st2)); - - size_t outSize = negentropy_initiate(ngn_inst1, &b4); - if(outSize == 0){ + result res; + int ret1 = negentropy_initiate(ngn_inst1, &res); + if(ret1 < 0){ perror("failed to initiate negentropy instance"); } - printf("initiated negentropy successfully with output of len %zu \n", outSize); - b4.len = outSize; + printf("initiated negentropy successfully with output of len %llu \n", res.output.len); + b4.len = res.output.len; + memcpy(b4.data, res.output.data, res.output.len); + free_result(&res); buffer b3 ; - b3.len = 153600; - b3.data = (unsigned char*)malloc(153600); + b3.len = 0; + b3.data = (unsigned char*)malloc(69*sizeof(unsigned char)); - outSize = reconcile(ngn_inst2, &b4, &b3); - if(outSize == 0){ - perror("nothing to reconcile"); + ret1 = reconcile(ngn_inst2, &b4, &res); + if(ret1 < 0){ + perror("error from reconcile"); } - printf("reconcile returned with output of len %zu \n", outSize); - b3.len = outSize; - + if (res.output.len == 0){ + perror("nothing to reconcile"); + } + printf("reconcile returned with output of len %llu \n", res.output.len); + b3.len = res.output.len; + memcpy(b3.data, res.output.data, res.output.len); + free_result(&res); //outSize = reconcile_with_ids(ngn_inst1, &b3, &rec_callback); - result res; - reconcile_with_ids_no_cbk(ngn_inst1, &b3, &res); - printf("needIds count:%llu , haveIds count: %llu \n",res.need_ids_len, res.have_ids_len); + result res1; + reconcile_with_ids_no_cbk(ngn_inst1, &b3, &res1); + printf("needIds count:%llu , haveIds count: %llu \n",res1.need_ids_len, res1.have_ids_len); - for (int i=0; i < res.need_ids_len ; i++) { + for (int i=0; i < res1.need_ids_len ; i++) { printf("need ID at %d :", i); - printHexBuffer(res.need_ids[i]); + printHexBuffer(res1.need_ids[i]); } - for (int j=0; j < res.have_ids_len ; j++) { + for (int j=0; j < res1.have_ids_len ; j++) { printf("need ID at %d :", j); - printHexBuffer(res.have_ids[j]); + printHexBuffer(res1.have_ids[j]); } free(b3.data); free(b4.data); + free_result(&res1); } diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index 697b645..25d1a60 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -65,7 +65,7 @@ void* negentropy_new(void* storage, uint64_t frameSizeLimit){ return ne; } -size_t negentropy_initiate(void* negentropy, buffer* out){ +int negentropy_initiate(void* negentropy, result* result){ Negentropy* ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); @@ -77,12 +77,18 @@ size_t negentropy_initiate(void* negentropy, buffer* out){ } catch(negentropy::err e){ std::cout << "Exception raised in initiate " << e.what() << std::endl; //TODO:Find a way to return this error - return 0; + 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) ; + }else { + result->output.len = 0; + result->output.data = NULL; } - memcpy( out->data, output->c_str() ,output->size()); - size_t outlen = output->size(); delete output; - return outlen; + return 0; } void negentropy_setinitiator(void* negentropy){ @@ -118,7 +124,7 @@ bool storage_erase(void* storage, uint64_t createdAt, buffer* id){ return lmdbStorage->erase(createdAt, data); } -size_t reconcile(void* negentropy, buffer* query, buffer* output){ +int reconcile(void* negentropy, buffer* query, result* result){ Negentropy *ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); std::string* out = new std::string(); @@ -129,10 +135,17 @@ size_t reconcile(void* negentropy, buffer* query, buffer* output){ } catch(negentropy::err e){ //TODO:Find a way to return this error std::cout << "Exception raised in reconcile " << e.what() << std::endl; - return 0; + return -1; } - memcpy( output->data, out->c_str() ,out->size()); - return out->size(); + 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; + } + return 0; } void transform(std::vector &from_ids, buffer* to_ids) diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index 985bfa2..61d3d37 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -34,7 +34,7 @@ EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit); EXTERNC void negentropy_delete(void* negentropy); -EXTERNC size_t negentropy_initiate(void* negentropy, buffer* output); +EXTERNC int negentropy_initiate(void* negentropy, result* result); EXTERNC void negentropy_setinitiator(void* negentropy); @@ -42,7 +42,7 @@ EXTERNC bool storage_insert(void* storage, uint64_t createdAt, buffer* id); EXTERNC bool storage_erase(void* storage, uint64_t createdAt, buffer* id); -EXTERNC size_t reconcile(void* negentropy, buffer* query, buffer* output); +EXTERNC int reconcile(void* negentropy, buffer* query, result* result); EXTERNC typedef void (*reconcile_cbk)(buffer* have_ids, uint64_t have_ids_len, buffer* need_ids, uint64_t need_ids_len, buffer* output, char* outptr );