chore: make callback functional with nim integration

This commit is contained in:
Prem Chaitanya Prathi 2024-03-13 13:58:13 +05:30
parent 1411f20219
commit 1e1988967d
No known key found for this signature in database
3 changed files with 48 additions and 36 deletions

View File

@ -15,17 +15,31 @@ void printHexBuffer(buffer buf){
printf("\n");
}
void rec_callback(buffer* have_ids, uint64_t have_ids_len, buffer* need_ids, uint64_t need_ids_len, buffer* output){
printf("needIds count:%llu , haveIds count: %llu \n",need_ids_len, have_ids_len);
for (int i=0; i < need_ids_len ; i++) {
void rec_callback(result* res, void *data){
result* myRes = (result*)data;
printf("needIds count:%llu , haveIds count: %llu \n",res->need_ids_len, res->have_ids_len);
myRes->need_ids_len = res->need_ids_len;
myRes->have_ids_len = res->have_ids_len;
if ( res->need_ids_len > 0 ){
myRes->need_ids = (buffer*)calloc(myRes->need_ids_len, sizeof(buffer*));
}
if ( res->have_ids_len > 0 ){
myRes->have_ids = (buffer*)calloc(myRes->have_ids_len, sizeof(buffer*));
}
for (int i=0; i < res->need_ids_len ; i++) {
printf("need ID at %d :", i);
printHexBuffer(need_ids[i]);
printHexBuffer(res->need_ids[i]);
myRes->need_ids[i].data = (unsigned char*)calloc(res->need_ids[i].len, sizeof(unsigned char));
memcpy(myRes->need_ids[i].data,res->need_ids[i].data,res->need_ids[i].len);
myRes->need_ids[i].len = res->need_ids[i].len;
}
for (int j=0; j < have_ids_len ; j++) {
for (int j=0; j < res->have_ids_len ; j++) {
printf("need ID at %d :", j);
printHexBuffer(have_ids[j]);
printHexBuffer(res->have_ids[j]);
memcpy(myRes->have_ids[j].data,res->have_ids[j].data,res->have_ids[j].len);
myRes->have_ids[j].len = res->have_ids[j].len;
}
}
@ -104,21 +118,21 @@ int main(){
printf("reconcile returned with output of len %zu \n", outSize);
b3.len = outSize;
//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);
//reconcile_with_ids_no_cbk(ngn_inst1, &b3, &res);
reconcile_with_ids(ngn_inst1, &b3, &rec_callback, (void*)&res);
printf("final 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("final 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);

View File

@ -143,51 +143,49 @@ void transform(std::vector<std::string> &from_ids, buffer* to_ids)
}
}
int reconcile_with_ids(void* negentropy, buffer* query,reconcile_cbk cbk, char* outptr){
int reconcile_with_ids(void* negentropy, buffer* query, reconcile_cbk cbk, void* userData){
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;
uint64_t have_ids_len, need_ids_len;
buffer* have_ids;
buffer* need_ids;
result myResult = {0};
try {
out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len), haveIds, needIds);
have_ids_len = haveIds.size();
need_ids_len = needIds.size();
have_ids = (buffer*)malloc(have_ids_len*sizeof(buffer));
need_ids = (buffer*)malloc(need_ids_len*sizeof(buffer));
myResult.have_ids_len = haveIds.size();
myResult.need_ids_len = needIds.size();
myResult.have_ids = new buffer[myResult.have_ids_len];
myResult.need_ids = new buffer[myResult.need_ids_len];
std::cout << "have_ids_len:" << myResult.have_ids_len << "need_ids_len:" << myResult.need_ids_len << std::endl;
std::cout << "have_ids_len:" << have_ids_len << "need_ids_len:" << need_ids_len << std::endl;
transform(haveIds, have_ids);
transform(needIds, need_ids);
transform(haveIds, myResult.have_ids);
transform(needIds, myResult.need_ids);
} catch(negentropy::err e){
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
return -1;
}
buffer output = {0,NULL};
if (out) {
output.len = out.value().size();
output.data = (unsigned char*)out.value().c_str();
myResult.output.len = out.value().size();
myResult.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()));
}else{
myResult.output.len = 0;
myResult.output.data = NULL;
}
std::cout << "invoking callback" << std::endl;
std::flush(std::cout);
cbk(have_ids, have_ids_len, need_ids, need_ids_len, &output, outptr);
cbk(&myResult, userData);
std::cout << "invoked callback" << std::endl;
std::flush(std::cout);
free(have_ids);
free(need_ids);
delete myResult.have_ids;
delete myResult.need_ids;
return 0;
}
}
void transform_with_alloc(std::vector<std::string> &from_ids, buffer* to_ids)
{

View File

@ -25,7 +25,7 @@ typedef struct _result_ {
//TODO: Do error handling by catching exceptions
EXTERNC void* storage_new(const char* db_path, const char* name);
//TODO: Expose subrange that can be created from new storage.
EXTERNC void storage_delete(void* storage);
EXTERNC int storage_size(void* storage);
@ -44,9 +44,9 @@ EXTERNC bool storage_erase(void* storage, uint64_t createdAt, buffer* id);
EXTERNC size_t reconcile(void* negentropy, buffer* query, buffer* output);
EXTERNC typedef void (*reconcile_cbk)(buffer* have_ids, uint64_t have_ids_len, buffer* need_ids, uint64_t need_ids_len, buffer* output, char* outptr );
EXTERNC typedef void (*reconcile_cbk)(result *result , void* userData);
EXTERNC int reconcile_with_ids(void* negentropy, buffer* query, reconcile_cbk cbk, char* outptr);
EXTERNC int reconcile_with_ids(void* negentropy, buffer* query, reconcile_cbk cbk, void* userData);
EXTERNC void reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* result);