mirror of
https://github.com/logos-messaging/negentropy.git
synced 2026-01-05 23:43:11 +00:00
Merge pull request #3 from waku-org/feat/c-wrapper
bug fixes while integrating with nwaku
This commit is contained in:
commit
d4845b95b5
@ -83,6 +83,8 @@ int main(){
|
|||||||
buffer b4 ;
|
buffer b4 ;
|
||||||
b4.len = 153600;
|
b4.len = 153600;
|
||||||
b4.data = (unsigned char*)malloc(153600);
|
b4.data = (unsigned char*)malloc(153600);
|
||||||
|
|
||||||
|
printf("storage size of st2 is %d \n",storage_size(st2));
|
||||||
|
|
||||||
size_t outSize = negentropy_initiate(ngn_inst1, &b4);
|
size_t outSize = negentropy_initiate(ngn_inst1, &b4);
|
||||||
if(outSize == 0){
|
if(outSize == 0){
|
||||||
@ -120,4 +122,4 @@ int main(){
|
|||||||
|
|
||||||
free(b3.data);
|
free(b3.data);
|
||||||
free(b4.data);
|
free(b4.data);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -50,7 +50,7 @@ struct Negentropy {
|
|||||||
|
|
||||||
std::string output;
|
std::string output;
|
||||||
output.push_back(PROTOCOL_VERSION);
|
output.push_back(PROTOCOL_VERSION);
|
||||||
std::cout << "storage size" << storage.size() << std::endl;
|
std::cout << "storage size:" << storage.size() << std::endl;
|
||||||
output += splitRange(0, storage.size(), Bound(MAX_U64));
|
output += splitRange(0, storage.size(), Bound(MAX_U64));
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
|
|||||||
@ -34,6 +34,21 @@ void* storage_new(const char* db_path, const char* name){
|
|||||||
return storage;
|
return storage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void storage_delete(void* storage){
|
||||||
|
negentropy::storage::BTreeMem* lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(storage);
|
||||||
|
delete lmdbStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
int storage_size(void* storage){
|
||||||
|
negentropy::storage::BTreeMem* lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(storage);
|
||||||
|
return lmdbStorage->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
void negentropy_delete(void* negentropy){
|
||||||
|
Negentropy<negentropy::storage::BTreeMem>* ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy);
|
||||||
|
delete ngn_inst;
|
||||||
|
}
|
||||||
|
|
||||||
void* negentropy_new(void* storage, uint64_t frameSizeLimit){
|
void* negentropy_new(void* storage, uint64_t frameSizeLimit){
|
||||||
//TODO: Make these typecasts into macros??
|
//TODO: Make these typecasts into macros??
|
||||||
negentropy::storage::BTreeMem* lmdbStorage;
|
negentropy::storage::BTreeMem* lmdbStorage;
|
||||||
@ -57,9 +72,10 @@ size_t negentropy_initiate(void* negentropy, buffer* out){
|
|||||||
std::string* output = new std::string();
|
std::string* output = new std::string();
|
||||||
try {
|
try {
|
||||||
*output = ngn_inst->initiate();
|
*output = ngn_inst->initiate();
|
||||||
std::cout << "output of initiate is, len:" << output->size() << ", output:";
|
/* std::cout << "output of initiate is, len:" << output->size() << ", output:";
|
||||||
printHexString(std::string_view(*output));
|
printHexString(std::string_view(*output)); */
|
||||||
} catch(negentropy::err e){
|
} catch(negentropy::err e){
|
||||||
|
std::cout << "Exception raised in initiate " << e.what() << std::endl;
|
||||||
//TODO:Find a way to return this error
|
//TODO:Find a way to return this error
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -82,8 +98,8 @@ bool storage_insert(void* storage, uint64_t createdAt, buffer* id){
|
|||||||
lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(storage);
|
lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(storage);
|
||||||
std::string_view data(reinterpret_cast< char const* >(id->data), id->len);
|
std::string_view data(reinterpret_cast< char const* >(id->data), id->len);
|
||||||
|
|
||||||
std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:";
|
/* std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:";
|
||||||
printHexString(data);
|
printHexString(data); */
|
||||||
|
|
||||||
//TODO: Error handling. Is it required?
|
//TODO: Error handling. Is it required?
|
||||||
//How does out of memory get handled?
|
//How does out of memory get handled?
|
||||||
@ -95,8 +111,8 @@ bool storage_erase(void* storage, uint64_t createdAt, buffer* id){
|
|||||||
lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(storage);
|
lmdbStorage = reinterpret_cast<negentropy::storage::BTreeMem*>(storage);
|
||||||
std::string_view data(reinterpret_cast< char const* >(id->data), id->len);
|
std::string_view data(reinterpret_cast< char const* >(id->data), id->len);
|
||||||
|
|
||||||
std::cout << "erasing entry from storage, createdAt:" << createdAt << ",id:";
|
/* std::cout << "erasing entry from storage, createdAt:" << createdAt << ",id:";
|
||||||
printHexString(data);
|
printHexString(data); */
|
||||||
|
|
||||||
//TODO: Error handling
|
//TODO: Error handling
|
||||||
return lmdbStorage->erase(createdAt, data);
|
return lmdbStorage->erase(createdAt, data);
|
||||||
@ -108,10 +124,11 @@ size_t reconcile(void* negentropy, buffer* query, buffer* output){
|
|||||||
std::string* out = new std::string();
|
std::string* out = new std::string();
|
||||||
try {
|
try {
|
||||||
*out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len));
|
*out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len));
|
||||||
std::cout << "reconcile output of reconcile is, len:" << out->size() << ", output:";
|
/* std::cout << "reconcile output of reconcile is, len:" << out->size() << ", output:";
|
||||||
printHexString(std::string_view(*out));
|
printHexString(std::string_view(*out)); */
|
||||||
} catch(negentropy::err e){
|
} catch(negentropy::err e){
|
||||||
//TODO:Find a way to return this error
|
//TODO:Find a way to return this error
|
||||||
|
std::cout << "Exception raised in reconcile " << e.what() << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
memcpy( output->data, out->c_str() ,out->size());
|
memcpy( output->data, out->c_str() ,out->size());
|
||||||
@ -149,7 +166,7 @@ int reconcile_with_ids(void* negentropy, buffer* query,reconcile_cbk cbk, char*
|
|||||||
transform(haveIds, have_ids);
|
transform(haveIds, have_ids);
|
||||||
transform(needIds, need_ids);
|
transform(needIds, need_ids);
|
||||||
} catch(negentropy::err e){
|
} catch(negentropy::err e){
|
||||||
std::cout << "caught error "<< e.what() << std::endl;
|
std::cout << "exception raised in reconcile_with_ids"<< e.what() << std::endl;
|
||||||
//TODO:Find a way to return this error and cleanup partially allocated memory if any
|
//TODO:Find a way to return this error and cleanup partially allocated memory if any
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -175,7 +192,7 @@ int reconcile_with_ids(void* negentropy, buffer* query,reconcile_cbk cbk, char*
|
|||||||
void transform_with_alloc(std::vector<std::string> &from_ids, buffer* to_ids)
|
void transform_with_alloc(std::vector<std::string> &from_ids, buffer* to_ids)
|
||||||
{
|
{
|
||||||
for (int i=0; i < from_ids.size(); i ++){
|
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].data = (unsigned char*) calloc(from_ids[i].size(), sizeof(unsigned char));
|
||||||
to_ids[i].len = from_ids[i].size();
|
to_ids[i].len = from_ids[i].size();
|
||||||
memcpy(to_ids[i].data, from_ids[i].c_str(),to_ids[i].len);
|
memcpy(to_ids[i].data, from_ids[i].c_str(),to_ids[i].len);
|
||||||
}
|
}
|
||||||
@ -189,16 +206,20 @@ void reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* result)
|
|||||||
std::vector<std::string> haveIds, needIds;
|
std::vector<std::string> haveIds, needIds;
|
||||||
try {
|
try {
|
||||||
out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len), haveIds, needIds);
|
out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len), haveIds, needIds);
|
||||||
|
|
||||||
result->have_ids_len = haveIds.size();
|
result->have_ids_len = haveIds.size();
|
||||||
result->need_ids_len = needIds.size();
|
result->need_ids_len = needIds.size();
|
||||||
result->have_ids = (buffer*)malloc(result->have_ids_len*sizeof(buffer));
|
if (haveIds.size() > 0){
|
||||||
result->need_ids = (buffer*)malloc(result->need_ids_len*sizeof(buffer));
|
result->have_ids = (buffer*)calloc(result->have_ids_len, sizeof(buffer));
|
||||||
|
transform_with_alloc(haveIds, result->have_ids);
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << "have_ids_len:" << result->have_ids_len << "need_ids_len:" << result->need_ids_len << std::endl;
|
if (needIds.size() > 0) {
|
||||||
|
result->need_ids = (buffer*)calloc(result->need_ids_len, sizeof(buffer));
|
||||||
|
transform_with_alloc(needIds, result->need_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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){
|
} catch(negentropy::err e){
|
||||||
std::cout << "caught error "<< e.what() << std::endl;
|
std::cout << "caught error "<< e.what() << std::endl;
|
||||||
@ -208,25 +229,35 @@ void reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* result)
|
|||||||
buffer output = {0,NULL};
|
buffer output = {0,NULL};
|
||||||
if (out) {
|
if (out) {
|
||||||
result->output.len = out.value().size();
|
result->output.len = out.value().size();
|
||||||
result->output.data = (unsigned char*)malloc(out.value().size()*sizeof(unsigned char));
|
result->output.data = (unsigned char*)calloc(out.value().size(), sizeof(unsigned char));
|
||||||
result->output.data = (unsigned char*)out.value().c_str();
|
memcpy(result->output.data, (unsigned char*)out.value().c_str(),result->output.len) ;
|
||||||
std::cout << "reconcile_with_ids output of reconcile is, len:" << out.value().size() << ", output:";
|
/* std::cout << "reconcile_with_ids output of reconcile is, len:" << out.value().size() << ", output:";
|
||||||
printHexString(std::string_view(out.value()));
|
printHexString(std::string_view(out.value())); */
|
||||||
|
}else {
|
||||||
|
//std::cout << "reconcile_with_ids_no_cbk output is empty " << std::endl;
|
||||||
|
result->output.len = 0;
|
||||||
|
result->output.data = NULL;
|
||||||
}
|
}
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Note: This function assumes that all relevant heap memory is alloced and just tries to free
|
//Note: This function assumes that all relevant heap memory is alloced and just tries to free
|
||||||
void free_result(result* r){
|
void free_result(result* r){
|
||||||
free((void *) r->output.data);
|
if (r->output.len > 0) {
|
||||||
|
free((void *) r->output.data);
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < r->have_ids_len; i++) {
|
if (r->have_ids_len > 0){
|
||||||
free((void *) r->have_ids[i].data);
|
for (int i = 0; i < r->have_ids_len; i++) {
|
||||||
|
free((void *) r->have_ids[i].data);
|
||||||
|
}
|
||||||
|
free((void *)r->have_ids);
|
||||||
}
|
}
|
||||||
free((void *)r->have_ids);
|
|
||||||
|
|
||||||
for (int i = 0; i < r->need_ids_len; i++) {
|
if (r->need_ids_len > 0) {
|
||||||
free((void *) r->need_ids[i].data);
|
for (int i = 0; i < r->need_ids_len; i++) {
|
||||||
|
free((void *) r->need_ids[i].data);
|
||||||
|
}
|
||||||
|
free((void *)r->need_ids);
|
||||||
}
|
}
|
||||||
free((void *)r->need_ids);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,8 +26,14 @@ typedef struct _result_ {
|
|||||||
|
|
||||||
EXTERNC void* storage_new(const char* db_path, const char* name);
|
EXTERNC void* storage_new(const char* db_path, const char* name);
|
||||||
|
|
||||||
|
EXTERNC void storage_delete(void* storage);
|
||||||
|
|
||||||
|
EXTERNC int storage_size(void* storage);
|
||||||
|
|
||||||
EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit);
|
EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit);
|
||||||
|
|
||||||
|
EXTERNC void negentropy_delete(void* negentropy);
|
||||||
|
|
||||||
EXTERNC size_t negentropy_initiate(void* negentropy, buffer* output);
|
EXTERNC size_t negentropy_initiate(void* negentropy, buffer* output);
|
||||||
|
|
||||||
EXTERNC void negentropy_setinitiator(void* negentropy);
|
EXTERNC void negentropy_setinitiator(void* negentropy);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user