From 34fd921b89577cee691ba5e69bed6c06554bd736 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Thu, 22 Feb 2024 15:19:27 +0530 Subject: [PATCH] chore: experimenting with callback --- cpp/negentropy.h | 3 ++- cpp/negentropy_wrapper.c | 24 ++++++++++++++---------- cpp/negentropy_wrapper.h | 7 +++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cpp/negentropy.h b/cpp/negentropy.h index d3de53e..9a8e4ee 100644 --- a/cpp/negentropy.h +++ b/cpp/negentropy.h @@ -14,6 +14,7 @@ #include #include #include +#include #include "negentropy/encoding.h" #include "negentropy/types.h" @@ -49,7 +50,7 @@ struct Negentropy { std::string output; output.push_back(PROTOCOL_VERSION); - + std::cout << "storage size" << storage.size() << std::endl; output += splitRange(0, storage.size(), Bound(MAX_U64)); return output; diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index 0499d63..501e86d 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -7,8 +7,6 @@ //This is a C-wrapper for the C++ library that helps in integrating negentropy with nim code. //TODO: Do error handling by catching exceptions -using namespace std; - void* storage_new(const char* db_path, const char* name){ negentropy::storage::BTreeMem* storage; /* @@ -44,18 +42,23 @@ void* negentropy_new(void* storage, uint64_t frameSizeLimit){ return ne; } -const char* negentropy_initiate(void* negentropy){ +void negentropy_initiate(void* negentropy, void (*callback)(const char* buf, size_t len)){ 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() << std::hex << output << std::endl; } catch(negentropy::err e){ //TODO:Find a way to return this error - return NULL; + callback(NULL,0); + return ; } - return output->c_str(); + callback(output.c_str(), output.size()); + //TODO: Avoid copy and use a callback + //memcpy(buf, output, output.size()) + return ; } void negentropy_setinitiator(void* negentropy){ @@ -70,7 +73,7 @@ void negentropy_setinitiator(void* negentropy){ bool storage_insert(void* storage, uint64_t createdAt, buffer* id){ negentropy::storage::BTreeMem* lmdbStorage; lmdbStorage = reinterpret_cast(storage); - std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:" << std::string_view(id->data, id->len) << "length is:"<< id->len << std::endl; + std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:" << std::hex << id->data << " length is:"<< id->len << std::endl; //TODO: Error handling. Is it required? //How does out of memory get handled? return lmdbStorage->insert(createdAt, std::string_view(id->data, id->len)); @@ -122,6 +125,7 @@ const char* reconcile_with_ids(void* negentropy, buffer* query, char* have_ids[ *have_ids_len = haveIds.size(); *need_ids_len = needIds.size(); //TODO: Optimize to not copy and rather return memory reference. + std::cout << "*have_ids_len:" << *have_ids_len << "*need_ids_len:" << *need_ids_len << "output has value" << output->has_value() << std::endl; std::transform(haveIds.begin(), haveIds.end(), have_ids, convert); std::transform(needIds.begin(), needIds.end(), need_ids, convert); @@ -130,11 +134,11 @@ const char* reconcile_with_ids(void* negentropy, buffer* query, char* have_ids[ //TODO:Find a way to return this error return NULL; } - if (output->has_value()) { + if (!output->has_value()) { //TODO: Figure out diff between error and this. return NULL; }else { - + std::cout << "output value" << output->value() << std::endl; return output->value().c_str(); } } diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index bc979aa..0416b54 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -1,4 +1,3 @@ - #ifndef _NEGENTROPY_WRAPPER_H #define _NEGENTROPY_WRAPPER_H @@ -20,7 +19,7 @@ EXTERNC void* storage_new(const char* db_path, const char* name); EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit); -EXTERNC const char* negentropy_initiate(void* negentropy); +EXTERNC void negentropy_initiate(void* negentropy, void *(const char* buf, size_t len)); EXTERNC void negentropy_setinitiator(void* negentropy); @@ -30,8 +29,8 @@ EXTERNC bool storage_erase(void* storage, uint64_t createdAt, buffer* id); EXTERNC const char* reconcile(void* negentropy, buffer* query); -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); +EXTERNC 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); #endif