chore: experimenting with callback

This commit is contained in:
Prem Chaitanya Prathi 2024-02-22 15:19:27 +05:30
parent b467a09940
commit 34fd921b89
No known key found for this signature in database
3 changed files with 19 additions and 15 deletions

View File

@ -14,6 +14,7 @@
#include <stdexcept>
#include <optional>
#include <bit>
#include <iostream>
#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;

View File

@ -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<negentropy::storage::BTreeMem>* ngn_inst;
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(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<negentropy::storage::BTreeMem*>(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();
}
}

View File

@ -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