mirror of
https://github.com/logos-messaging/negentropy.git
synced 2026-01-02 14:03:07 +00:00
chore: support a non-callback version of reconcile_with_ids
This commit is contained in:
parent
315e1de72e
commit
f23efbb66c
@ -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, )
|
||||
|
||||
}
|
||||
@ -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<negentropy::storage::BTreeMem*>(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<negentropy::storage::BTreeMem*>(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<negentropy::storage::BTreeMem> *ngn_inst;
|
||||
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(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<std::string> &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<negentropy::storage::BTreeMem> *ngn_inst;
|
||||
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy);
|
||||
|
||||
std::optional<std::string> out;
|
||||
std::vector<std::string> 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);
|
||||
}
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user