diff --git a/modules/global-db/dap_chain_global_db.c b/modules/global-db/dap_chain_global_db.c index 6f55fe6c1bd424f022758298cf2e45895e989a03..b10bd0088ad5a4bc590bbe431ef9ba02ebdf90a5 100644 --- a/modules/global-db/dap_chain_global_db.c +++ b/modules/global-db/dap_chain_global_db.c @@ -51,11 +51,13 @@ // for access from several streams //static pthread_mutex_t ldb_mutex_ = PTHREAD_MUTEX_INITIALIZER; +//Do nothing static inline void lock() { //pthread_mutex_lock(&ldb_mutex_); } +//Do nothing static inline void unlock() { //pthread_mutex_unlock(&ldb_mutex_); @@ -127,7 +129,8 @@ dap_list_t *dap_chain_db_get_sync_extra_groups() } /** - * Clean struct dap_global_db_obj_t + * @brief Deallocates memory of key and value members of a item. + * @param obj A pointer to the item */ void dap_chain_global_db_obj_clean(dap_global_db_obj_t *obj) { @@ -140,7 +143,8 @@ void dap_chain_global_db_obj_clean(dap_global_db_obj_t *obj) } /** - * Delete struct dap_global_db_obj_t + * @brief Deallocates memory of a item. + * @param obj A pointer to the item */ void dap_chain_global_db_obj_delete(dap_global_db_obj_t *obj) { @@ -149,7 +153,9 @@ void dap_chain_global_db_obj_delete(dap_global_db_obj_t *obj) } /** - * Delete mass of struct dap_global_db_obj_t + * @brief Deallocates memory of array of items. + * @param objs an array of items + * @param a_count A aount of items in the array. */ void dap_chain_global_db_objs_delete(dap_global_db_obj_t *objs, size_t a_count) { @@ -160,9 +166,9 @@ void dap_chain_global_db_objs_delete(dap_global_db_obj_t *objs, size_t a_count) } /** - * @brief dap_chain_global_db_init - * @param g_config - * @return + * @brief Initialise a datebase. Befor using must call this fuction. + * @param g_config a pointer to a congiguration struct + * @return 0 if success, otherwise error, */ int dap_chain_global_db_init(dap_config_t * g_config) { @@ -181,7 +187,7 @@ int dap_chain_global_db_init(dap_config_t * g_config) } /** - * @brief dap_chain_global_db_deinit + * @brief Deinitialise a datebase. */ void dap_chain_global_db_deinit(void) { @@ -207,8 +213,8 @@ void dap_chain_global_db_deinit(void) } /** - * @brief dap_chain_global_db_flush - * @return + * @brief Flushes a datebase. + * @return 0 */ int dap_chain_global_db_flush(void){ lock(); @@ -218,9 +224,10 @@ int dap_chain_global_db_flush(void){ } /** - * Get entry from base - * - * return dap_store_obj_t* + * @brief Gets an item from a datebase by a_key and a_group + * @param a_key a key + * @param a_group the group name + * @return If successful, a pointer to an array of items, otherwise NULL. */ void* dap_chain_global_db_obj_get(const char *a_key, const char *a_group) { @@ -341,11 +348,10 @@ static bool global_db_gr_del_del(char *a_key, const char *a_group) /** - * @brief global_db_gr_del_get_timestamp - * Get timestamp of the deleted entry - * @param a_group group name, for example "kelvin-testnet.nodes" - * @param a_key key, looked like "0x8FAFBD00B..." - * @return time_t + * @brief Gets time stamp of the deleted item by a_group and a_key + * @param a_group a group name, for example "kelvin-testnet.nodes" + * @param a_key a key, looked like "0x8FAFBD00B..." + * @return If successful, a time stamp, otherwise 0. */ time_t global_db_gr_del_get_timestamp(const char *a_group, char *a_key) { @@ -372,11 +378,9 @@ time_t global_db_gr_del_get_timestamp(const char *a_group, char *a_key) } /** - * @brief dap_chain_global_db_del - * call dap_chain_global_db_gr_del - * @param a_key - * @return true - * @return false + * @brief Call dap_chain_global_db_gr_del(a_key, GROUP_LOCAL_GENERAL). + * @param a_key key + * @return True or false */ bool dap_chain_global_db_del(char *a_key) { @@ -385,10 +389,9 @@ bool dap_chain_global_db_del(char *a_key) /** - * @brief dap_chain_global_db_get_last - * Read last item in global_db - * @param a_group - * @return dap_store_obj_t* array (note:not Null-terminated string) on NULL in case of an error + * @brief Gets a last item from a datebase by a_group. + * @param a_group a group name + * @return If successful, a pointer to item; otherwise NULL. */ dap_store_obj_t* dap_chain_global_db_get_last(const char *a_group) { @@ -400,10 +403,12 @@ dap_store_obj_t* dap_chain_global_db_get_last(const char *a_group) } /** - * Read the entire database with condition into an array of size bytes - * - * @param data_size[out] size of output array - * @return array (note:not Null-terminated string) on NULL in case of an error + * @brief Gets items from a database by a_group_name and a_first_id. + * @param a_group a group name + * @param a_first_id a first id + * @param a_objs_count[in] a count items to read, if 0 - no limits + * @param a_objs_count[out] a count items were read + * @return If successful, a pointer to items; otherwise NULL. */ dap_store_obj_t* dap_chain_global_db_cond_load(const char *a_group, uint64_t a_first_id, size_t *a_objs_count) { @@ -415,10 +420,11 @@ dap_store_obj_t* dap_chain_global_db_cond_load(const char *a_group, uint64_t a_f } /** - * Read the entire database into an array of size bytes - * - * @param data_size[out] size of output array - * @return array (note:not Null-terminated string) on NULL in case of an error + * @brief Gets all data from a database by a_group + * @param a_group a group name + * @param a_data_size[in] a poiter to return a count of data + * @param a_data_size[out] a count of data + * @return If successful, a pointer to data; otherwise NULL. */ dap_global_db_obj_t* dap_chain_global_db_gr_load(const char *a_group, size_t *a_data_size_out) { @@ -445,13 +451,16 @@ dap_global_db_obj_t* dap_chain_global_db_gr_load(const char *a_group, size_t *a_ return l_data; } +/** + * @brief Calls a dap_chain_global_db_gr_load(GROUP_LOCAL_GENERAL, a_data_size_out). + */ dap_global_db_obj_t* dap_chain_global_db_load(size_t *a_data_size_out) { return dap_chain_global_db_gr_load(GROUP_LOCAL_GENERAL, a_data_size_out); } /** - * @brief extract_group_mask + * @brief Finds item by a_group * @param a_group * @return */ @@ -510,11 +519,11 @@ void dap_global_db_obj_track_history(void* a_store_data) /** - * @brief dap_chain_global_db_gr_set - * @param a_key - * @param a_value - * @param a_value_len - * @param a_group + * @brief Adds a value to a datebase. + * @param a_key a key + * @param a_value a value to be added + * @param a_value_len length of value. If a_value_len=-1, counts it in function + * @param a_group a group name * @details Set one entry to base. IMPORTANT: a_key and a_value should be passed without free after (it will be released by gdb itself) * @return */ @@ -547,13 +556,19 @@ bool dap_chain_global_db_gr_set(char *a_key, void *a_value, size_t a_value_len, return !l_res; } +/** + * @breif Calls dap_chain_global_db_gr_set(a_key, a_value, a_value_len, GROUP_LOCAL_GENERAL) + */ bool dap_chain_global_db_set( char *a_key, void *a_value, size_t a_value_len) { return dap_chain_global_db_gr_set(a_key, a_value, a_value_len, GROUP_LOCAL_GENERAL); } /** - * Delete entry from base + * @brief Deletes item from a datebase by a a_key and a_group. + * @param a_key a key + * @param a_group a group name + * @return True or false. */ bool dap_chain_global_db_gr_del(char *a_key,const char *a_group) { @@ -581,9 +596,10 @@ bool dap_chain_global_db_gr_del(char *a_key,const char *a_group) } /** - * Write to the database from an array of data_size bytes - * - * @return + * @brief Saves(deletes) items to a database from an array of data_size bytes + * @param a_store_data a pointer to data + * @param a_objs_count a count of items + * @return True or false. */ bool dap_chain_global_db_obj_save(void* a_store_data, size_t a_objs_count) { @@ -623,6 +639,13 @@ bool dap_chain_global_db_obj_save(void* a_store_data, size_t a_objs_count) return !l_res; } +/** + * @brief Saves items in a datebase by a_group. + * @param a_objs a pointer to items + * @param a_objs_count a count of items + * @param a_group a group name + * @return If successful, true; otherwise false. + */ bool dap_chain_global_db_gr_save(dap_global_db_obj_t* a_objs, size_t a_objs_count, const char *a_group) { dap_store_obj_t *l_store_data = DAP_NEW_Z_SIZE(dap_store_obj_t, a_objs_count * sizeof(struct dap_store_obj)); @@ -643,15 +666,22 @@ bool dap_chain_global_db_gr_save(dap_global_db_obj_t* a_objs, size_t a_objs_coun return l_res; } +/** + * @brief Saves items in a database. + * @param a_objs a pointer to items + * @param a_objs_count a count of items + * @return If successful, true; otherwise false. + */ bool dap_chain_global_db_save(dap_global_db_obj_t* a_objs, size_t a_objs_count) { return dap_chain_global_db_gr_save(a_objs, a_objs_count, GROUP_LOCAL_GENERAL); } /** - * Calc hash for data - * - * return hash or NULL + * @brief Calcs a hash string for data + * @param data a pointer to data + * @param data_size a size of data + * @return A hash string or NULL. */ char* dap_chain_global_db_hash(const uint8_t *data, size_t data_size) { diff --git a/modules/global-db/dap_chain_global_db_driver.c b/modules/global-db/dap_chain_global_db_driver.c index 61f325ef74e12339735160500b0b9555b57214e8..fba9c6dd17d67ba9343cc6d00c116259eb9b8f38 100644 --- a/modules/global-db/dap_chain_global_db_driver.c +++ b/modules/global-db/dap_chain_global_db_driver.c @@ -43,6 +43,7 @@ #define LOG_TAG "db_driver" +// A selected datebase driver. static char *s_used_driver = NULL; //#define USE_WRITE_BUFFER @@ -72,10 +73,11 @@ static void* func_write_buf(void * arg); static dap_db_driver_callbacks_t s_drv_callback; /** - * Select driver - * driver_name may be "ldb", "sqlite" - * - * return 0 OK, <0 Error + * @brief Initializes a database driver. Befor using the driver must call this fuction. + * @param driver_name a string determining a type of datebase driver. + * Maybe "Ñdb", "sqlite" ("sqlite3"), "pgsql", "mdbx" + * @param a_filename_db a name of a datebase file + * @return If successful, 0 , otherwise <0. */ int dap_db_driver_init(const char *a_driver_name, const char *a_filename_db) { @@ -127,9 +129,8 @@ int dap_db_driver_init(const char *a_driver_name, const char *a_filename_db) } /** - * Shutting down the db library + * @brief Deinitializes a database driver. Must call this function after using the driver. */ - void dap_db_driver_deinit(void) { #ifdef USE_WRITE_BUFFER @@ -168,11 +169,21 @@ void dap_db_driver_deinit(void) } } +/** + * @brief Flush data to disk + * @return 0 + */ int dap_db_driver_flush(void) { return s_drv_callback.flush(); } +/** + * @brief Copies a_store_count items from a_store_obj to return. + * @param a_store_obj a pointer to source items + * @param a_store_count a count of items + * @return A pointer to the copied items. + */ dap_store_obj_t* dap_store_obj_copy(dap_store_obj_t *a_store_obj, size_t a_store_count) { if(!a_store_obj || !a_store_count) @@ -190,6 +201,12 @@ dap_store_obj_t* dap_store_obj_copy(dap_store_obj_t *a_store_obj, size_t a_store return l_store_obj; } +/** + * @brief Deallocates memory of items. + * @param a_store_obj a pointer to items + * @param a_store_count a count of items + * @return (none) + */ void dap_store_obj_free(dap_store_obj_t *a_store_obj, size_t a_store_count) { if(!a_store_obj) @@ -204,9 +221,10 @@ void dap_store_obj_free(dap_store_obj_t *a_store_obj, size_t a_store_count) } /** - * Calc hash for data - * - * return hash or NULL + * @brief Calcs hash string for data + * @param data a pointer to data + * @param data_size a size of data + * @return Hash string or NULL. */ char* dap_chain_global_db_driver_hash(const uint8_t *data, size_t data_size) { @@ -226,8 +244,11 @@ char* dap_chain_global_db_driver_hash(const uint8_t *data, size_t data_size) } /** - * Wait data to write buffer - * return 0 - Ok, 1 - timeout + * @brief Wait data to write buffer + * @param a_mutex a mutex + * @param a_cond condition + * @param l_timeout_ms timeout in ms, if -1 endless waiting + * @return 0 - Ok, 1 - timeout */ static int wait_data(pthread_mutex_t *a_mutex, pthread_cond_t *a_cond, int l_timeout_ms) { @@ -257,7 +278,11 @@ static int wait_data(pthread_mutex_t *a_mutex, pthread_cond_t *a_cond, int l_tim } #ifdef USE_WRITE_BUFFER -// return 0 if buffer empty, 1 data present +/ +/** + * @brief Checks buffer is fill + * @return 0 if buffer empty, 1 data present + */ static bool check_fill_buf(void) { dap_list_t *l_list_begin; @@ -275,7 +300,9 @@ static bool check_fill_buf(void) return l_ret; } -// wait apply write buffer +/** + * @brief Waits apply write buffer. + */ static void wait_write_buf() { // printf("** Start wait data\n"); @@ -289,7 +316,10 @@ static void wait_write_buf() // printf("** End wait data\n"); } -// save data from buffer to database +/** + * @brief Saves data from buffer to database + * @return 0 + */ static int save_write_buf(void) { dap_list_t *l_list_end; @@ -348,7 +378,11 @@ static int save_write_buf(void) return 0; } -// thread for save data from buffer to database +/** + * @brief thread for save data from buffer to database + * @param arg + * @return NULL + */ static void* func_write_buf(void * arg) { while(1) { @@ -366,6 +400,12 @@ static void* func_write_buf(void * arg) } #endif //USE_WRITE_BUFFER +/** + * @brief Applies items to datebase + * @param a_store an array of items + * @param a_store_count a count of items + * @return + */ int dap_chain_global_db_driver_appy(pdap_store_obj_t a_store_obj, size_t a_store_count) { //dap_store_obj_t *l_store_obj = dap_store_obj_copy(a_store_obj, a_store_count); @@ -426,6 +466,12 @@ int dap_chain_global_db_driver_appy(pdap_store_obj_t a_store_obj, size_t a_store } +/** + * @brief Adds items to a database. + * @param a_store_obj items to be added + * @param a_store_count a count added items + * @return If sucseesful, 1, otherwise 0 or <0. + */ int dap_chain_global_db_driver_add(pdap_store_obj_t a_store_obj, size_t a_store_count) { for(size_t i = 0; i < a_store_count; i++) @@ -433,6 +479,12 @@ int dap_chain_global_db_driver_add(pdap_store_obj_t a_store_obj, size_t a_store_ return dap_chain_global_db_driver_appy(a_store_obj, a_store_count); } +/** + * @brief Deletes items from a database. + * @param a_store_obj items to be deleted + * @param a_store_count a count deleted items + * @return If sucseesful, 1, otherwise 0 or <0. + */ int dap_chain_global_db_driver_delete(pdap_store_obj_t a_store_obj, size_t a_store_count) { for(size_t i = 0; i < a_store_count; i++) @@ -441,10 +493,10 @@ int dap_chain_global_db_driver_delete(pdap_store_obj_t a_store_obj, size_t a_sto } /** - * Read the number of items - * - * a_group - group name - * a_id - from this id + * @brief Gets a count of items in a database by a_group and id. + * @param a_group the group name + * @param a_id id + * @return The count of items. */ size_t dap_chain_global_db_driver_count(const char *a_group, uint64_t id) { @@ -456,11 +508,11 @@ size_t dap_chain_global_db_driver_count(const char *a_group, uint64_t id) } /** - * Get group matching the pattern + * @brief Gets a list of group names matching the pattern. * Check whether the groups match the pattern a_group_mask, which is a shell wildcard pattern - * patterns: [] {} [!] * ? - * https://en.wikipedia.org/wiki/Glob_(programming) - * a_group_mask - group mask + * patterns: [] {} [!] * ? https://en.wikipedia.org/wiki/Glob_(programming). + * @param a_group_mask the group mask + * @return If successful, the list of group names, otherwise NULL. */ dap_list_t* dap_chain_global_db_driver_get_groups_by_mask(const char *a_group_mask) { @@ -472,9 +524,9 @@ dap_list_t* dap_chain_global_db_driver_get_groups_by_mask(const char *a_group_ma /** - * Read last items - * - * a_group - group name + * @brief Reads last items in the database. + * @param a_group the group name + * @return If successful, a pointer to the item, otherwise NULL. */ dap_store_obj_t* dap_chain_global_db_driver_read_last(const char *a_group) { @@ -490,13 +542,12 @@ dap_store_obj_t* dap_chain_global_db_driver_read_last(const char *a_group) } /** - * Read several items - * - * a_group - group name - * a_key - key name, may by NULL, it means reading the whole group - * a_id - from this id - * a_count_out[in], how many items to read, 0 - no limits - * a_count_out[out], how many items was read + * @brief Reads several items from a database by a_group and id. + * @param a_group the group name + * @param a_id id + * @param a_count_out[in] a count of items to read, if 0 - no limits + * @param a_count_out[out] a count of items were read + * @return If successful, a pointer to an array of items, otherwise NULL. */ dap_store_obj_t* dap_chain_global_db_driver_cond_read(const char *a_group, uint64_t id, size_t *a_count_out) { @@ -512,12 +563,13 @@ dap_store_obj_t* dap_chain_global_db_driver_cond_read(const char *a_group, uint6 } /** - * Read several items - * - * a_group - group name - * a_key - key name, may by NULL, it means reading the whole group - * a_count_out[in], how many items to read, 0 - no limits - * a_count_out[out], how many items was read + * @brief Reads several items from a database by a_group and a_key. + * If a_key is NULL, reads whole group. + * @param a_group the group name + * @param a_key key name, may by NULL. it means reading the whole group + * @param a_count_out[in] a count of items to read, if 0 - no limits + * @param a_count_out[out] a count of items was read + * @return If successful, a pointer to an array of items, otherwise NULL. */ dap_store_obj_t* dap_chain_global_db_driver_read(const char *a_group, const char *a_key, size_t *a_count_out) { @@ -533,10 +585,10 @@ dap_store_obj_t* dap_chain_global_db_driver_read(const char *a_group, const char } /** - * Check an element in the database - * - * a_group - group name - * a_key - key name + * @brief Checks an element in a database by a_group and a_key. + * @param a_group a group namme + * @param a_key a key + * @return True if is, 0 otherwise. */ bool dap_chain_global_db_driver_is(const char *a_group, const char *a_key) { diff --git a/modules/global-db/dap_chain_global_db_driver_cdb.c b/modules/global-db/dap_chain_global_db_driver_cdb.c index daa16fef21e67113a78094968fdf49dff23ffb35..5fc9ac4ebaada90577c6c7b68cccf6b7562e1207 100644 --- a/modules/global-db/dap_chain_global_db_driver_cdb.c +++ b/modules/global-db/dap_chain_global_db_driver_cdb.c @@ -38,6 +38,7 @@ #define LOG_TAG "dap_chain_global_db_cdb" +/** Struct for a item */ typedef struct _obj_arg { pdap_store_obj_t o; uint64_t q; @@ -45,6 +46,7 @@ typedef struct _obj_arg { uint64_t id; } obj_arg, *pobj_arg; +/** Struct for a CDB instanse */ typedef struct _cdb_instance { CDB *cdb; char *local_group; @@ -52,11 +54,26 @@ typedef struct _cdb_instance { UT_hash_handle hh; } cdb_instance, *pcdb_instance; +/** A path to a CDB file. */ static char *s_cdb_path = NULL; +/** A pointer to a CDB instance. */ static pcdb_instance s_cdb = NULL; +/** A mutex for working with a CDB instanse. */ static pthread_mutex_t cdb_mutex = PTHREAD_MUTEX_INITIALIZER; +/** A read-write lock for working with a CDB instanse. */ static pthread_rwlock_t cdb_rwlock = PTHREAD_RWLOCK_INITIALIZER; +/** + * @brief Serialize key and val to a item + * key -> key + * val[0..8] => id + * val[..] => value_len + * val[..] => value + * val[..] => timestamp + * @param a_obj a pointer to a item + * @param key a key + * @param val a serialize string + */ static void cdb_serialize_val_to_dap_store_obj(pdap_store_obj_t a_obj, const char *key, const char *val) { if (!key || !val) { a_obj = NULL; @@ -74,6 +91,7 @@ static void cdb_serialize_val_to_dap_store_obj(pdap_store_obj_t a_obj, const cha a_obj->timestamp = (time_t)dap_hex_to_uint(val + offset, sizeof(time_t)); } +/** A callback function designed for finding a last item */ bool dap_cdb_get_last_obj_iter_callback(void *arg, const char *key, int ksize, const char *val, int vsize, uint32_t expire, uint64_t oid) { UNUSED(ksize); UNUSED(val); @@ -88,6 +106,7 @@ bool dap_cdb_get_last_obj_iter_callback(void *arg, const char *key, int ksize, c return true; } +//** A callback function designed for finding a some items */ bool dap_cdb_get_some_obj_iter_callback(void *arg, const char *key, int ksize, const char *val, int vsize, uint32_t expire, uint64_t oid) { UNUSED(ksize); UNUSED(val); @@ -103,6 +122,7 @@ bool dap_cdb_get_some_obj_iter_callback(void *arg, const char *key, int ksize, c return true; } +//** A callback function designed for finding a some items by conditionals */ bool dap_cdb_get_cond_obj_iter_callback(void *arg, const char *key, int ksize, const char *val, int vsize, uint32_t expire, uint64_t oid) { UNUSED(ksize); UNUSED(val); @@ -121,6 +141,7 @@ bool dap_cdb_get_cond_obj_iter_callback(void *arg, const char *key, int ksize, c return true; } +//** A callback function designed for countng items*/ bool dap_cdb_get_count_iter_callback(void *arg, const char *key, int ksize, const char *val, int vsize, uint32_t expire, uint64_t oid) { UNUSED(ksize); UNUSED(val); @@ -138,6 +159,14 @@ bool dap_cdb_get_count_iter_callback(void *arg, const char *key, int ksize, cons return true; } +/** + * @brief Initiates a CDB with main hash table size: 1000000, + * record cache: 128Mb, index page cache: 1024Mb. + * @param a_group a group name + * @param a_flags should be combination of CDB_CREAT / CDB_TRUNC / CDB_PAGEWARMUP + CDB_PAGEWARMUP + * @return A pointer to CDB, if success. NULL, if error. + */ pcdb_instance dap_cdb_init_group(char *a_group, int a_flags) { pcdb_instance l_cdb_i = NULL; pthread_mutex_lock(&cdb_mutex); @@ -198,6 +227,12 @@ ERR: return NULL; } +/** + * @brief Initiates a CDB with callback fuctions. + * @param a_cdb_path a path to CDB. Saved in s_cdb_path + * @param a_drv_callback a struct for callback functions + * @return 0 if success, -1 if Ñouldn't open db directory, -2 if dap_cdb_init_group() returns NULL. + */ int dap_db_driver_cdb_init(const char *a_cdb_path, dap_db_driver_callbacks_t *a_drv_callback) { s_cdb_path = dap_strdup(a_cdb_path); if(s_cdb_path[strlen(s_cdb_path)] == '/') { @@ -251,6 +286,11 @@ int dap_db_driver_cdb_init(const char *a_cdb_path, dap_db_driver_callbacks_t *a_ return CDB_SUCCESS; } +/** + * @brief Gets CDB by a_group. + * @param a_group a group name + * @return if CDB is found, a pointer to CDB, otherwise NULL. + */ pcdb_instance dap_cdb_get_db_by_group(const char *a_group) { pcdb_instance l_cdb_i = NULL; pthread_rwlock_rdlock(&cdb_rwlock); @@ -259,6 +299,11 @@ pcdb_instance dap_cdb_get_db_by_group(const char *a_group) { return l_cdb_i; } +/** + * @brief Creates a directory on the path s_cdb_path/a_group. + * @param a_group the group name + * @return 0 + */ int dap_cdb_add_group(const char *a_group) { char l_cdb_path[strlen(s_cdb_path) + strlen(a_group) + 2]; memset(l_cdb_path, '\0', sizeof(l_cdb_path)); @@ -271,6 +316,10 @@ int dap_cdb_add_group(const char *a_group) { return 0; } +/** + * @brief Deinitialize CDB. + * @return 0 + */ int dap_db_driver_cdb_deinit() { pcdb_instance cur_cdb, tmp; pthread_rwlock_wrlock(&cdb_rwlock); @@ -285,6 +334,10 @@ int dap_db_driver_cdb_deinit() { return CDB_SUCCESS; } +/** + * @brief Flushing CDB to the disk. + * @return 0 + */ int dap_db_driver_cdb_flush(void) { int ret = 0; log_it(L_DEBUG, "Flushing CDB to disk"); @@ -298,6 +351,11 @@ int dap_db_driver_cdb_flush(void) { return ret; } +/** + * @brief Read last store item from CDB. + * @param a_group a group name + * @return If successful, a pointer to item, otherwise NULL. + */ dap_store_obj_t *dap_db_driver_cdb_read_last_store_obj(const char* a_group) { if (!a_group) { return NULL; @@ -319,6 +377,12 @@ dap_store_obj_t *dap_db_driver_cdb_read_last_store_obj(const char* a_group) { return l_arg.o; } +/** + * @brief Checks if CDB has a_key + * @param a_group the group name + * @param a_key the key + * @return true or false + */ bool dap_db_driver_cdb_is_obj(const char *a_group, const char *a_key) { bool l_ret = false; @@ -338,6 +402,13 @@ bool dap_db_driver_cdb_is_obj(const char *a_group, const char *a_key) return l_ret; } +/** + * @brief Gets items from CDB by a_group and a_key. If a_key=NULL then gets a_count_out items. + * @param a_group the group name + * @param a_key the key or NULL + * @param a_count_out IN. Count of read items. OUT Count of items was read + * @return If successful, pointer to items; otherwise NULL. + */ dap_store_obj_t *dap_db_driver_cdb_read_store_obj(const char *a_group, const char *a_key, size_t *a_count_out) { if (!a_group) { return NULL; @@ -390,6 +461,14 @@ dap_store_obj_t *dap_db_driver_cdb_read_store_obj(const char *a_group, const cha return l_obj; } +/** + * @brief Gets items from CDB by a_group and a_id. + * @param a_group the group name + * @param a_id id + * @param a_count_out[in] a count of items + * @param a_count[out] a count of items were got + * @return If successful, pointer to items, otherwise NULL. + */ dap_store_obj_t* dap_db_driver_cdb_read_cond_store_obj(const char *a_group, uint64_t a_id, size_t *a_count_out) { if (!a_group) { return NULL; @@ -436,6 +515,13 @@ dap_store_obj_t* dap_db_driver_cdb_read_cond_store_obj(const char *a_group, uint return l_arg.o; } + +/** + * @brief Reads count of items in CDB by a_group and a_id. + * @param a_group the group name + * @param a_id id + * @return If successful, count of store items; otherwise 0. + */ size_t dap_db_driver_cdb_read_count_store(const char *a_group, uint64_t a_id) { if (!a_group) { @@ -458,7 +544,9 @@ size_t dap_db_driver_cdb_read_count_store(const char *a_group, uint64_t a_id) } /** - * Check whether the groups match the pattern a_group_mask, which is a shell wildcard pattern + * @brief Check whether the groups match the pattern a_group_mask, which is a shell wildcard pattern. + * @param a_group_mask the mask + * @return If successful, pointer to dap_list with group names; otherwise NULL. */ dap_list_t* dap_db_driver_cdb_get_groups_by_mask(const char *a_group_mask) { @@ -476,6 +564,12 @@ dap_list_t* dap_db_driver_cdb_get_groups_by_mask(const char *a_group_mask) return l_ret_list; } + +/** + * @brief Adds or deletes item in CDB depending on a_store_obj->type. + * @param a_store_obj a pointer to the item + * @return 0 if success, <0 error. + */ int dap_db_driver_cdb_apply_store_obj(pdap_store_obj_t a_store_obj) { if(!a_store_obj || !a_store_obj->group) { return -1; diff --git a/modules/global-db/dap_chain_global_db_remote.c b/modules/global-db/dap_chain_global_db_remote.c index 6dab70cdf0d4ab2ea8264ac2402ab917a5cadfeb..8bed4f838c9c191b7a8d2dbc0cd68d7558ff674c 100644 --- a/modules/global-db/dap_chain_global_db_remote.c +++ b/modules/global-db/dap_chain_global_db_remote.c @@ -14,6 +14,12 @@ // default time of node address expired in hours #define NODE_TIME_EXPIRED_DEFAULT 720 +/** + * @brief Sets current node adress + * @param a_address a current node adress + * @param a_net_name a net name + * @return True if success, otherwise false + */ static bool dap_db_set_cur_node_addr_common(uint64_t a_address, char *a_net_name, time_t a_expire_time) { if(!a_net_name)