From a736a26532e0311402700f35d37cc10088c5f316 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Tue, 20 Feb 2024 17:27:38 +0530 Subject: [PATCH] fix: update reconcile function logic where out params are passed --- cpp/negentropy_wrapper.c | 30 ++++++++++++++++++++++-------- cpp/negentropy_wrapper.h | 6 +++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index e3269f8..5974aac 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -84,13 +84,13 @@ bool storage_erase(void* storage, uint64_t createdAt, const char* id){ } -const char* reconcile(void* negentropy, const char* query){ +const char* reconcile(void* negentropy, const char* query, uint64_t query_len){ Negentropy *ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); std::string* output = new std::string(); try { - *output = ngn_inst->reconcile(std::string_view(query)); + *output = ngn_inst->reconcile(std::string_view(query, query_len)); } catch(negentropy::err e){ //TODO:Find a way to return this error return NULL; @@ -98,18 +98,32 @@ const char* reconcile(void* negentropy, const char* query){ return output->c_str(); } -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){ +char *convert(const std::string & s) +{ + char *pc = new char[s.size()+1]; + std::strcpy(pc, s.c_str()); + return pc; +} + +const char* reconcile_with_ids(void* negentropy, const char* query, uint64_t query_len, char* have_ids[], + uint64_t *have_ids_len, char* need_ids[], uint64_t *need_ids_len){ Negentropy *ngn_inst; ngn_inst = reinterpret_cast*>(negentropy); std::optional* output; - std::vector haveIds(have_ids, have_ids+have_ids_len); - - std::vector needIds(need_ids, need_ids+need_ids_len); + std::vector haveIds; + std::vector needIds; try { - *output = ngn_inst->reconcile(std::string_view(query), haveIds, needIds); + *output = ngn_inst->reconcile(std::string_view(query, query_len), haveIds, needIds); + + *have_ids_len = haveIds.size(); + *need_ids_len = needIds.size(); + //TODO: Optimize to not copy and rather return memory reference. + + std::transform(haveIds.begin(), haveIds.end(), have_ids, convert); + std::transform(needIds.begin(), needIds.end(), need_ids, convert); + } catch(negentropy::err e){ //TODO:Find a way to return this error return NULL; diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index 8cc58d2..68fcff5 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -23,10 +23,10 @@ EXTERNC bool storage_insert(void* storage, uint64_t createdAt, const char* id); EXTERNC bool storage_erase(void* storage, uint64_t createdAt, const char* id); -EXTERNC const char* reconcile(void* negentropy, const char* query); +EXTERNC const char* reconcile(void* negentropy, const char* query, uint64_t query_len); -EXTERNC 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); +EXTERNC const char* reconcile_with_ids(void* negentropy, const char* query, uint64_t query_len, const char* have_ids[], + uint64_t *have_ids_len, const char* need_ids[], uint64_t *need_ids_len); #endif