Skip to content
Snippets Groups Projects
Commit db409656 authored by Aleksandr Lysikov's avatar Aleksandr Lysikov
Browse files

add delete from mempool

parent 929eded6
No related branches found
No related tags found
No related merge requests found
......@@ -2,12 +2,18 @@
#include <string.h>
#include <stdio.h>
#include <pthread.h>
// for access from several streams
static pthread_mutex_t ldb_mutex = PTHREAD_MUTEX_INITIALIZER;
int dap_chain_global_db_init(const char *a_storage_path)
{
if(a_storage_path)
{
pthread_mutex_lock(&ldb_mutex);
int res = dap_db_init(a_storage_path);
pthread_mutex_unlock(&ldb_mutex);
return res;
}
return -1;
......@@ -15,7 +21,9 @@ int dap_chain_global_db_init(const char *a_storage_path)
void dap_chain_global_db_deinit(void)
{
pthread_mutex_lock(&ldb_mutex);
dap_db_deinit();
pthread_mutex_unlock(&ldb_mutex);
}
/**
......@@ -29,7 +37,9 @@ char * dap_chain_global_db_get(const char *a_key)
return NULL;
char query[32 + strlen(a_key)];
sprintf(query, "(cn=%s)(objectClass=addr_leased)", a_key);
pthread_mutex_lock(&ldb_mutex);
pdap_store_obj_t store_data = dap_db_read_data(query, &count);
pthread_mutex_unlock(&ldb_mutex);
if(count == 1 && store_data && !strcmp(store_data->key, a_key))
str = (store_data->value) ? strdup(store_data->value) : NULL;
dab_db_free_pdap_store_obj_t(store_data, count);
......@@ -44,13 +54,33 @@ bool dap_chain_global_db_set(const char *a_key, const char *a_value)
pdap_store_obj_t store_data = DAP_NEW_Z_SIZE(dap_store_obj_t, sizeof(struct dap_store_obj));
store_data->key = (char*) a_key;
store_data->value = (char*) a_value;
pthread_mutex_lock(&ldb_mutex);
int res = dap_db_merge(store_data, 1);
pthread_mutex_unlock(&ldb_mutex);
DAP_DELETE(store_data);
if(!res)
return true;
return false;
}
/**
* Delete entry from base
*/
bool dap_chain_global_db_del(const char *a_key)
{
char *str = NULL;
if(!a_key)
return NULL;
pdap_store_obj_t store_data = DAP_NEW_Z_SIZE(dap_store_obj_t, sizeof(struct dap_store_obj));
store_data->key = (char*) a_key;
pthread_mutex_lock(&ldb_mutex);
int res = dap_db_delete(store_data, 1);
pthread_mutex_unlock(&ldb_mutex);
DAP_DELETE(store_data);
if(!res)
return true;
return false;
}
/**
* Read the entire database into an array of size bytes
*
......@@ -62,7 +92,9 @@ uint8_t* dap_chain_global_db_load(size_t *data_size)
const char *query = "(objectClass=addr_leased)";
int count = 0;
// Read data
pthread_mutex_lock(&ldb_mutex);
pdap_store_obj_t store_obj = dap_db_read_data(query, &count);
pthread_mutex_unlock(&ldb_mutex);
// Serialization data
dap_store_obj_pkt_t *pkt = dap_store_packet_multiple(store_obj, count);
dab_db_free_pdap_store_obj_t(store_obj, count);
......@@ -96,7 +128,9 @@ bool dap_chain_global_db_save(uint8_t* data, size_t data_size)
DAP_DELETE(pkt);
if(store_data)
{
pthread_mutex_lock(&ldb_mutex);
int res = dap_db_merge(store_data, count);
pthread_mutex_unlock(&ldb_mutex);
dab_db_free_pdap_store_obj_t(store_data, count);
if(!res)
return true;
......
......@@ -17,6 +17,11 @@ char* dap_chain_global_db_get(const char *a_key);
*/
bool dap_chain_global_db_set(const char *a_key, const char *a_value);
/**
* Delete entry from base
*/
bool dap_chain_global_db_del(const char *a_key);
/**
* Read the entire database into an array of size bytes
*
......
......@@ -7,6 +7,9 @@
#define LOG_TAG "dap_global_db"
#define TDB_PREFIX_LEN 7
static struct ldb_context *ldb = NULL;
static TALLOC_CTX *mem_ctx = NULL;
//static int dap_store_len = 0; // initialized only when reading from local db
static char *dap_db_path = NULL;
......@@ -66,6 +69,7 @@ int dap_db_init(const char *path)
log_it(L_ERROR, "Couldn't initialize LDB context");
return -2;
}
return -1;
}
int dap_db_add_msg(struct ldb_message *msg)
......@@ -96,6 +100,24 @@ int dap_db_add_msg(struct ldb_message *msg)
log_it(L_INFO, "Entry %s added", ldb_dn_get_linearized(msg->dn));
return 0;
}
return -1;
}
int dap_db_del_msg(struct ldb_dn *ldn)
{
ldb_transaction_start(ldb);
int status = ldb_delete(ldb, ldn);
if(status != LDB_SUCCESS) {
log_it(L_ERROR, "LDB deleting error: %s", ldb_errstring(ldb));
ldb_transaction_cancel(ldb);
return -2;
}
else {
ldb_transaction_commit(ldb);
log_it(L_INFO, "Entry %s deleted", ldb_dn_get_linearized(ldn));
return 0;
}
return -1;
}
/* path is supposed to have been obtained by smth like
......@@ -215,6 +237,8 @@ pdap_store_obj_t dap_db_read_file_data(const char *path)
/*
* Add multiple entries received from remote node to local database.
* Since we don't know the size, it must be supplied too
*
* dap_store_size the count records
*/
int dap_db_merge(pdap_store_obj_t store_obj, int dap_store_size)
{
......@@ -253,6 +277,42 @@ int dap_db_merge(pdap_store_obj_t store_obj, int dap_store_size)
return ret;
}
/*
* Delete multiple entries from local database.
*
* dap_store_size the count records
*/
int dap_db_delete(pdap_store_obj_t store_obj, int dap_store_size)
{
int ret = 0;
if(store_obj == NULL) {
log_it(L_ERROR, "Invalid Dap store objects passed");
return -1;
}
if(ldb_connect(ldb, dap_db_path, 0, NULL) != LDB_SUCCESS) {
log_it(L_ERROR, "Couldn't connect to database");
return -2;
}
log_it(L_INFO, "We're delete %d records from database", dap_store_size);
struct ldb_message *msg;
int q;
if(dap_store_size == 0) {
dap_store_size = 1;
}
for(q = 0; q < dap_store_size; q++) {
char dn[128];
memset(dn, '\0', 128);
strcat(dn, "cn=");
strcat(dn, store_obj[q].key);
strcat(dn, ",ou=addrs_leased,dc=kelvin_nodes");
struct ldb_dn *ldn = ldb_dn_new(mem_ctx, ldb, dn);
ret += dap_db_del_msg(ldn); // accumulation error codes
talloc_free(ldn);
}
return ret;
}
/* serialization */
/*dap_store_obj_pkt_t *dap_store_packet_single(pdap_store_obj_t store_obj)
{
......
......@@ -14,21 +14,19 @@ typedef struct dap_store_obj {
typedef struct dap_store_obj_pkt {
/*uint8_t type;
uint8_t sec_size;
uint8_t grp_size;
uint8_t name_size;*/
uint8_t sec_size;
uint8_t grp_size;
uint8_t name_size;*/
size_t data_size;
uint8_t data[];
}__attribute__((packed)) dap_store_obj_pkt_t;
static struct ldb_context *ldb = NULL;
static TALLOC_CTX *mem_ctx = NULL;
int dap_db_init(const char*);
void dap_db_deinit(void);
int dap_db_add_msg(struct ldb_message *);
int dap_db_merge(pdap_store_obj_t, int);
int dap_db_delete(pdap_store_obj_t, int);
pdap_store_obj_t dap_db_read_data(const char *query, int *count);
pdap_store_obj_t dap_db_read_file_data(const char *); // state of emergency only, if LDB database is inaccessible
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment