feat: wrapper API's using subranges (#5)

* feat: wrapper API's using subranges

* chore: address review comments
This commit is contained in:
Prem Chaitanya Prathi 2024-05-01 14:02:23 +05:30 committed by GitHub
parent b63c834614
commit 311a21a22b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 71 additions and 47 deletions

View File

@ -17,7 +17,7 @@ precompiled-header:
g++ -O0 --std=c++20 -Wall -fexceptions -g negentropy.h $(INCS)
shared-lib:
g++ -O0 -g -std=c++20 $(INCS) -shared -fPIC -o $(TARGET) negentropy_wrapper.c -lcrypto -lssl -L/opt/homebrew/lib/
g++ -O0 -g -std=c++20 $(INCS) -shared -fPIC -o $(TARGET) negentropy_wrapper.cpp -lcrypto -lssl -L/opt/homebrew/lib/
clean:
rm -f $(TARGET) negentropy.h.gch libnegentropy.so

View File

@ -8,6 +8,8 @@
#include <string>
#include "../negentropy_wrapper.h"
#define MAX_FRAME_SIZE 153600
void printHexBuffer(buffer buf){
for (uint64_t i = 0; i < buf.len; ++i) {
printf("%0hhx", buf.data[i]);
@ -83,23 +85,28 @@ int main(){
void* subrange = subrange_new(st2, 0 , UINT64_MAX);
if (subrange == NULL){
perror("failed to init subrange");
return -1;
}
printf("subrange init successful");
printf("subrange init successful with size %d \n ", subrange_size(subrange) );
void* subrange1 = subrange_new(st1, 0 , UINT64_MAX);
if (subrange == NULL){
perror("failed to init subrange");
return -1;
}
printf("subrange init successful");
printf("subrange init successful with size %d \n ", subrange_size(subrange1) );
void* ngn_inst1 = negentropy_new(subrange1, 153600);
void* ngn_inst1 = negentropy_new(subrange1, MAX_FRAME_SIZE);
if(ngn_inst1 == NULL){
perror("failed to create negentropy instance");
return -1;
}
void* ngn_inst2 = negentropy_new(subrange, 153600);
void* ngn_inst2 = negentropy_new(subrange, MAX_FRAME_SIZE);
if(ngn_inst2 == NULL){
perror("failed to create negentropy instance");
return -1;
}
@ -107,6 +114,7 @@ int main(){
int ret1 = negentropy_subrange_initiate(ngn_inst1, &res);
if(ret1 < 0){
perror("failed to initiate negentropy instance");
return -1;
}
printf("initiated negentropy successfully with output of len %llu \n", res.output.len);
b4.len = res.output.len;
@ -148,6 +156,12 @@ int main(){
free(b4.data);
free_result(&res1);
ret = storage_insert(st1, time(NULL), &b2);
if (ret){
printf("inserted hash successfully in st1\n");
}
printf("\n storage size after adding 1 more elem is %d, subrange size is %d \n", storage_size(st1), subrange_size(subrange1));
subrange_delete(subrange);
subrange_delete(subrange1);

View File

@ -60,6 +60,7 @@ void* negentropy_new(void* storage, uint64_t frameSizeLimit){
try{
ne = new Negentropy<negentropy::storage::BTreeMem>(*lmdbStorage, frameSizeLimit);
}catch(negentropy::err e){
//TODO: Error handling
return NULL;
}
return ne;
@ -70,24 +71,24 @@ int negentropy_initiate(void* negentropy, result* result){
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() << ", output:";
printHexString(std::string_view(*output)); */
} catch(negentropy::err e){
//std::cout << "Exception raised in initiate " << e.what() << std::endl;
//TODO: Error handling
return -1;
}
if (output->size() > 0 ){
result->output.len = output->size();
result->output.data = (unsigned char*)calloc(output->size(), sizeof(unsigned char));
memcpy(result->output.data, (unsigned char*)output->c_str(),result->output.len) ;
if (output.size() > 0 ){
result->output.len = output.size();
result->output.data = (unsigned char*)calloc(output.size(), sizeof(unsigned char));
memcpy(result->output.data, (unsigned char*)output.c_str(),result->output.len) ;
}else {
result->output.len = 0;
result->output.data = NULL;
}
delete output;
return 0;
}
@ -127,9 +128,9 @@ bool storage_erase(void* storage, uint64_t createdAt, buffer* id){
int reconcile(void* negentropy, buffer* query, result* result){
Negentropy<negentropy::storage::BTreeMem> *ngn_inst;
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::BTreeMem>*>(negentropy);
std::string* out = new std::string();
std::string out;
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:";
printHexString(std::string_view(*out)); */
} catch(negentropy::err e){
@ -140,10 +141,10 @@ int reconcile(void* negentropy, buffer* query, result* result){
strcpy(result->error,e.what());
return -1;
}
if (out->size() > 0 ){
result->output.len = out->size();
result->output.data = (unsigned char*)calloc(out->size(), sizeof(unsigned char));
memcpy(result->output.data, (unsigned char*)out->c_str(),result->output.len) ;
if (out.size() > 0 ){
result->output.len = out.size();
result->output.data = (unsigned char*)calloc(out.size(), sizeof(unsigned char));
memcpy(result->output.data, (unsigned char*)out.c_str(),result->output.len) ;
}else {
result->output.len = 0;
result->output.data = NULL;
@ -292,6 +293,7 @@ void* subrange_new(void* storage, uint64_t startTimeStamp, uint64_t endTimeStamp
try {
subRange = new negentropy::storage::SubRange(*st, negentropy::Bound(startTimeStamp), negentropy::Bound(endTimeStamp));
} catch (negentropy::err e){
//TODO: Error handling
return NULL;
}
return subRange;
@ -302,6 +304,11 @@ void subrange_delete(void* range){
delete subRange;
}
int subrange_size(void* range){
negentropy::storage::SubRange* subrange = reinterpret_cast<negentropy::storage::SubRange*>(range);
return subrange->size();
}
void negentropy_subrange_delete(void* negentropy){
Negentropy<negentropy::storage::SubRange>* ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::SubRange>*>(negentropy);
delete ngn_inst;
@ -317,6 +324,7 @@ void* negentropy_subrange_new(void* subrange, uint64_t frameSizeLimit){
try{
ne = new Negentropy<negentropy::storage::SubRange>(*sub_range, frameSizeLimit);
}catch(negentropy::err e){
//TODO: Error handling
return NULL;
}
return ne;
@ -327,24 +335,23 @@ int negentropy_subrange_initiate(void* negentropy, result* result){
Negentropy<negentropy::storage::SubRange>* ngn_inst;
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::SubRange>*>(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() << ", output:";
printHexString(std::string_view(*output)); */
} catch(negentropy::err e){
//std::cout << "Exception raised in initiate " << e.what() << std::endl;
return -1;
}
if (output->size() > 0 ){
result->output.len = output->size();
result->output.data = (unsigned char*)calloc(output->size(), sizeof(unsigned char));
memcpy(result->output.data, (unsigned char*)output->c_str(),result->output.len) ;
if (output.size() > 0 ){
result->output.len = output.size();
result->output.data = (unsigned char*)calloc(output.size(), sizeof(unsigned char));
memcpy(result->output.data, (unsigned char*)output.c_str(),result->output.len) ;
}else {
result->output.len = 0;
result->output.data = NULL;
}
delete output;
return 0;
}
@ -359,9 +366,9 @@ void negentropy_subrange_setinitiator(void* negentropy){
int reconcile_subrange(void* negentropy, buffer* query, result* result){
Negentropy<negentropy::storage::SubRange> *ngn_inst;
ngn_inst = reinterpret_cast<Negentropy<negentropy::storage::SubRange>*>(negentropy);
std::string* out = new std::string();
std::string out;
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:";
printHexString(std::string_view(*out)); */
} catch(negentropy::err e){
@ -372,10 +379,10 @@ int reconcile_subrange(void* negentropy, buffer* query, result* result){
strcpy(result->error,e.what());
return -1;
}
if (out->size() > 0 ){
result->output.len = out->size();
result->output.data = (unsigned char*)calloc(out->size(), sizeof(unsigned char));
memcpy(result->output.data, (unsigned char*)out->c_str(),result->output.len) ;
if (out.size() > 0 ){
result->output.len = out.size();
result->output.data = (unsigned char*)calloc(out.size(), sizeof(unsigned char));
memcpy(result->output.data, (unsigned char*)out.c_str(),result->output.len) ;
}else {
result->output.len = 0;
result->output.data = NULL;

View File

@ -31,22 +31,6 @@ EXTERNC void storage_delete(void* storage);
EXTERNC int storage_size(void* storage);
//SubRange methods
EXTERNC void* subrange_new(void* storage, uint64_t startTimeStamp, uint64_t endTimeStamp);
EXTERNC void subrange_delete(void* range);
EXTERNC void* negentropy_subrange_new(void* subrange, uint64_t frameSizeLimit);
EXTERNC void negentropy_subrange_delete(void* negentropy);
EXTERNC int negentropy_subrange_initiate(void* negentropy, result* result);
EXTERNC int reconcile_subrange(void* negentropy, buffer* query, result* result);
EXTERNC int reconcile_with_ids_subrange_no_cbk(void* negentropy, buffer* query, result* result);
//End of SubRange methods
EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit);
EXTERNC void negentropy_delete(void* negentropy);
@ -69,5 +53,24 @@ EXTERNC int reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result*
EXTERNC void free_result(result* result);
//SubRange methods
EXTERNC void* subrange_new(void* storage, uint64_t startTimeStamp, uint64_t endTimeStamp);
EXTERNC void subrange_delete(void* range);
EXTERNC void* negentropy_subrange_new(void* subrange, uint64_t frameSizeLimit);
EXTERNC void negentropy_subrange_delete(void* negentropy);
EXTERNC int negentropy_subrange_initiate(void* negentropy, result* result);
EXTERNC int reconcile_subrange(void* negentropy, buffer* query, result* result);
EXTERNC int reconcile_with_ids_subrange_no_cbk(void* negentropy, buffer* query, result* result);
EXTERNC int subrange_size(void* storage);
//End of SubRange methods
#endif