From f23efbb66c1f7a4d165bc2f001dbf2a0b252821c Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Wed, 6 Mar 2024 05:45:33 +0530 Subject: [PATCH] chore: support a non-callback version of reconcile_with_ids --- cpp/example/test.c | 21 ++++++++++---- cpp/negentropy_wrapper.c | 62 ++++++++++++++++++++++++++++++++++++++-- cpp/negentropy_wrapper.h | 11 +++++++ 3 files changed, 85 insertions(+), 9 deletions(-) diff --git a/cpp/example/test.c b/cpp/example/test.c index 0fb7211..f3511db 100644 --- a/cpp/example/test.c +++ b/cpp/example/test.c @@ -102,13 +102,22 @@ int main(){ printf("reconcile returned with output of len %zu \n", outSize); b3.len = outSize; - outSize = reconcile_with_ids(ngn_inst1, &b3, &rec_callback); + //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); + + for (int i=0; i < res.need_ids_len ; i++) { + printf("need ID at %d :", i); + printHexBuffer(res.need_ids[i]); + } + + for (int j=0; j < res.have_ids_len ; j++) { + printf("need ID at %d :", j); + printHexBuffer(res.have_ids[j]); + } free(b3.data); free(b4.data); - //b3.len = len; - //b3.data = (char*)malloc(len); - - //reconcile_with_ids(ngn_inst1, &b3, ) - } \ No newline at end of file diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index f624a7f..5a14c37 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -74,7 +74,6 @@ void negentropy_setinitiator(void* negentropy){ } - bool storage_insert(void* storage, uint64_t createdAt, buffer* id){ negentropy::storage::BTreeMem* lmdbStorage; lmdbStorage = reinterpret_cast(storage); @@ -88,7 +87,6 @@ bool storage_insert(void* storage, uint64_t createdAt, buffer* id){ return lmdbStorage->insert(createdAt, data); } - bool storage_erase(void* storage, uint64_t createdAt, buffer* id){ negentropy::storage::BTreeMem* lmdbStorage; lmdbStorage = reinterpret_cast(storage); @@ -101,7 +99,6 @@ bool storage_erase(void* storage, uint64_t createdAt, buffer* id){ return lmdbStorage->erase(createdAt, data); } - size_t reconcile(void* negentropy, buffer* query, buffer* output){ Negentropy *ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); @@ -171,3 +168,62 @@ int reconcile_with_ids(void* negentropy, buffer* query,reconcile_cbk cbk, char* free(need_ids); return 0; } + +void transform_with_alloc(std::vector &from_ids, buffer* to_ids) +{ + for (int i=0; i < from_ids.size(); i ++){ + to_ids[i].data = (unsigned char*) malloc(from_ids[i].size()*sizeof(unsigned char)); + to_ids[i].len = from_ids[i].size(); + memcpy(to_ids[i].data, from_ids[i].c_str(),to_ids[i].len); + } +} + +void reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* result){ + Negentropy *ngn_inst; + ngn_inst = reinterpret_cast*>(negentropy); + + std::optional out; + std::vector haveIds, needIds; + try { + out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len), haveIds, needIds); + + result->have_ids_len = haveIds.size(); + result->need_ids_len = needIds.size(); + result->have_ids = (buffer*)malloc(result->have_ids_len*sizeof(buffer)); + result->need_ids = (buffer*)malloc(result->need_ids_len*sizeof(buffer)); + + std::cout << "have_ids_len:" << result->have_ids_len << "need_ids_len:" << result->need_ids_len << std::endl; + + transform_with_alloc(haveIds, result->have_ids); + transform_with_alloc(needIds, result->need_ids); + + } catch(negentropy::err e){ + std::cout << "caught error "<< e.what() << std::endl; + //TODO:Find a way to return this error and cleanup partially allocated memory if any + return ; + } + buffer output = {0,NULL}; + if (out) { + result->output.len = out.value().size(); + result->output.data = (unsigned char*)malloc(out.value().size()*sizeof(unsigned char)); + result->output.data = (unsigned char*)out.value().c_str(); + std::cout << "reconcile_with_ids output of reconcile is, len:" << out.value().size() << ", output:"; + printHexString(std::string_view(out.value())); + } + return ; +} + +//Note: This function assumes that all relevant heap memory is alloced and just tries to free +void free_result(result* r){ + free((void *) r->output.data); + + for (int i = 0; i < r->have_ids_len; i++) { + free((void *) r->have_ids[i].data); + } + free((void *)r->have_ids); + + for (int i = 0; i < r->need_ids_len; i++) { + free((void *) r->need_ids[i].data); + } + free((void *)r->need_ids); +} \ No newline at end of file diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index f3d0eac..dd3fd29 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -13,6 +13,14 @@ typedef struct _buffer_{ unsigned char* data; }buffer; +typedef struct _result_ { + buffer output; + uint64_t have_ids_len; + uint64_t need_ids_len; + buffer* have_ids; + buffer* need_ids; +} result; + //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. //TODO: Do error handling by catching exceptions @@ -34,6 +42,9 @@ EXTERNC typedef void (*reconcile_cbk)(buffer* have_ids, uint64_t have_ids_len, b EXTERNC int reconcile_with_ids(void* negentropy, buffer* query, reconcile_cbk cbk, char* outptr); +EXTERNC void reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* result); + +EXTERNC void free_result(result* result); #endif