105 lines
2.7 KiB
C
105 lines
2.7 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "libp2p/db/datastore.h"
|
|
#include "libp2p/os/utils.h"
|
|
|
|
int alloc_and_assign(char** result, const char* string) {
|
|
*result = malloc(strlen(string)+1);
|
|
if (*result == NULL)
|
|
return 0;
|
|
strcpy(*result, string);
|
|
return 1;
|
|
}
|
|
|
|
/***
|
|
* initialize the structure of the datastore
|
|
* @param datastore the struct to initialize
|
|
* @returns true(1) on success
|
|
*/
|
|
int libp2p_datastore_init(struct Datastore* datastore, const char* config_root) {
|
|
unsigned long stringLength = strlen(config_root) + 12;
|
|
datastore->path = malloc(sizeof(char) * stringLength);
|
|
os_utils_filepath_join(config_root, "datastore", datastore->path, stringLength);
|
|
alloc_and_assign(&datastore->type, "lmdb");
|
|
alloc_and_assign(&datastore->storage_max, "10GB");
|
|
datastore->storage_gc_watermark = 90;
|
|
alloc_and_assign(&datastore->gc_period, "1h");
|
|
datastore->hash_on_read = 0;
|
|
datastore->bloom_filter_size = 0;
|
|
datastore->no_sync = 0;
|
|
return 1;
|
|
}
|
|
|
|
/***
|
|
* initialize the structure of the datastore
|
|
* @param datastore the struct to initialize
|
|
* @returns true(1) on success
|
|
*/
|
|
int libp2p_datastore_new(struct Datastore** datastore) {
|
|
*datastore = malloc(sizeof(struct Datastore));
|
|
if (*datastore == NULL)
|
|
return 0;
|
|
(*datastore)->path = NULL;
|
|
(*datastore)->datastore_context = NULL;
|
|
(*datastore)->type = NULL;
|
|
(*datastore)->storage_max = NULL;
|
|
(*datastore)->gc_period = NULL;
|
|
(*datastore)->params = NULL;
|
|
return 1;
|
|
}
|
|
|
|
/***
|
|
* deallocate the memory and clear resources from a datastore_init
|
|
* @param datastore the struct to deallocate
|
|
* @returns true(1)
|
|
*/
|
|
int libp2p_datastore_free(struct Datastore* datastore) {
|
|
if (datastore != NULL)
|
|
{
|
|
if (datastore->path != NULL)
|
|
free(datastore->path);
|
|
if (datastore->type != NULL)
|
|
free(datastore->type);
|
|
if (datastore->storage_max != NULL)
|
|
free(datastore->storage_max);
|
|
if (datastore->gc_period != NULL)
|
|
free(datastore->gc_period);
|
|
if (datastore->params != NULL)
|
|
free(datastore->params);
|
|
if (datastore->datastore_context != NULL)
|
|
datastore->datastore_close(datastore);
|
|
free(datastore);
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
struct DatastoreRecord* libp2p_datastore_record_new() {
|
|
struct DatastoreRecord* rec = (struct DatastoreRecord*) malloc(sizeof(struct DatastoreRecord));
|
|
if (rec != NULL) {
|
|
rec->key = NULL;
|
|
rec->key_size = 0;
|
|
rec->timestamp = 0;
|
|
rec->value = NULL;
|
|
rec->value_size = 0;
|
|
}
|
|
return rec;
|
|
}
|
|
|
|
int libp2p_datastore_record_free(struct DatastoreRecord* rec) {
|
|
if (rec != NULL) {
|
|
if (rec->key != NULL) {
|
|
free(rec->key);
|
|
rec->key = NULL;
|
|
}
|
|
rec->key_size = 0;
|
|
if (rec->value != NULL) {
|
|
free(rec->value);
|
|
rec->value = NULL;
|
|
}
|
|
rec->value_size = 0;
|
|
rec->timestamp = 0;
|
|
free(rec);
|
|
}
|
|
return 1;
|
|
}
|