1 /******************************************************************************
4 * Company XS Embedded GmbH
5 *****************************************************************************/
6 /******************************************************************************
7 * This Source Code Form is subject to the terms of the
8 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
9 * with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 ******************************************************************************/
12 * @file persistence_client_library_data_access.c
13 * @ingroup Persistence client library
14 * @author Ingo Huerner
15 * @brief Implementation of persistence database access
16 * Library provides an API to access persistent data
20 #include "../include_protected/persistence_client_library_db_access.h"
21 #include "../include_protected/persistence_client_library_rc_table.h"
22 #include "persistence_client_library_custom_loader.h"
23 #include "persistence_client_library_itzam_errors.h"
30 /// definition of a key-value pair stored in the database
31 typedef struct _KeyValuePair_s
33 char m_key[DbKeySize]; /// the key
34 char m_data[DbValueSize]; /// the data
35 unsigned int m_data_size; /// the size of the data
40 // definition of a cursor entry
41 typedef struct _CursorEntry_s
43 itzam_btree_cursor m_cursor;
49 // cursor array handle
50 CursorEntry_s gCursorArray[MaxPersHandle];
53 static int gHandleIdx = 1;
56 int gFreeCursorHandleArray[MaxPersHandle];
58 int gFreeCursorHandleIdxHead = 0;
60 // mutex to controll access to the cursor array
61 pthread_mutex_t gMtx = PTHREAD_MUTEX_INITIALIZER;
65 static itzam_btree gBtree[DbTableSize][PersistencePolicy_LastEntry];
66 static int gBtreeCreated[DbTableSize][PersistencePolicy_LastEntry] = { {0} };
69 itzam_btree* pers_db_open(PersistenceInfo_s* info, const char* dbPath)
72 itzam_btree* btree = NULL;
74 // create array index: index is a combination of resource config table type and group
75 arrayIdx = info->configKey.storage + info->context.ldbid ;
77 if(arrayIdx <= DbTableSize)
79 if(gBtreeCreated[arrayIdx][info->configKey.policy] == 0)
81 itzam_state state = ITZAM_FAILED;
82 state = itzam_btree_open(&gBtree[arrayIdx][info->configKey.policy], dbPath,
83 itzam_comparator_string, error_handler, 0/*recover*/, 0/*read_only*/);
84 if (state != ITZAM_OKAY)
86 fprintf(stderr, "pers_db_open ==> Open Itzam problem: %s\n", STATE_MESSAGES[state]);
88 gBtreeCreated[arrayIdx][info->configKey.policy] = 1;
91 btree = &gBtree[arrayIdx][info->configKey.policy];
95 printf("btree_get ==> invalid storage type\n");
102 void pers_db_close(PersistenceInfo_s* info)
104 int arrayIdx = info->configKey.storage + info->context.ldbid;
106 if(info->configKey.storage <= PersistenceStorage_shared )
108 itzam_state state = ITZAM_FAILED;
109 state = itzam_btree_close(&gBtree[arrayIdx][info->configKey.policy]);
110 if (state != ITZAM_OKAY)
112 fprintf(stderr, "pers_db_close ==> Close Itzam problem: %s\n", STATE_MESSAGES[state]);
114 gBtreeCreated[arrayIdx][info->configKey.policy] = 0;
118 printf("pers_db_close ==> invalid storage type\n");
124 void pers_db_close_all()
128 for(i=0; i<DbTableSize; i++)
130 // close write cached database
131 if(gBtreeCreated[i][PersistencePolicy_wc] == 1)
133 itzam_state state = ITZAM_FAILED;
134 state = itzam_btree_close(&gBtree[i][PersistencePolicy_wc]);
135 if (state != ITZAM_OKAY)
137 fprintf(stderr, "pers_db_close_all ==> Close WC: Itzam problem: %s\n", STATE_MESSAGES[state]);
139 gBtreeCreated[i][PersistencePolicy_wc] = 0;
142 // close write through database
143 if(gBtreeCreated[i][PersistencePolicy_wt] == 1)
145 itzam_state state = ITZAM_FAILED;
146 state = itzam_btree_close(&gBtree[i][PersistencePolicy_wt]);
147 if (state != ITZAM_OKAY)
149 fprintf(stderr, "pers_db_close_all ==> Close WT: Itzam problem: %s\n", STATE_MESSAGES[state]);
151 gBtreeCreated[i][PersistencePolicy_wt] = 0;
158 int pers_db_read_key(char* dbPath, char* key, PersistenceInfo_s* info, unsigned char* buffer, unsigned int buffer_size)
162 if( PersistenceStorage_shared == info->configKey.storage
163 || PersistenceStorage_local == info->configKey.storage)
165 itzam_btree* btree = NULL;
166 itzam_state state = ITZAM_FAILED;
167 KeyValuePair_s search;
169 btree = pers_db_open(info, dbPath);
172 if(itzam_true == itzam_btree_find(btree, key, &search))
174 read_size = search.m_data_size;
175 if(read_size > buffer_size)
177 read_size = buffer_size; // truncate data size to buffer size
179 memcpy(buffer, search.m_data, read_size);
183 read_size = EPERS_NOKEY;
187 // workaround till lifecycle is working correctly
193 read_size = EPERS_NOPRCTABLE;
194 fprintf(stderr, "\npersistence_get_data ==> Open Itzam problem: %s\n", STATE_MESSAGES[state]);
197 else if(PersistenceStorage_custom == info->configKey.storage) // custom storage implementation via custom library
199 int idx = custom_client_name_to_id(dbPath, 1);
200 char workaroundPath[128]; // workaround, because /sys/ can not be accessed on host!!!!
201 snprintf(workaroundPath, 128, "%s%s", "/Data", dbPath );
203 if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_get_data != NULL) )
205 gPersCustomFuncs[idx].custom_plugin_get_data(key, (char*)buffer, buffer_size);
209 read_size = EPERS_NOPLUGINFUNCT;
217 int pers_db_write_key(char* dbPath, char* key, PersistenceInfo_s* info, unsigned char* buffer, unsigned int buffer_size)
221 if( PersistenceStorage_shared == info->configKey.storage
222 || PersistenceStorage_local == info->configKey.storage)
224 write_size = buffer_size;
225 itzam_btree* btree = NULL;
226 itzam_state state = ITZAM_FAILED;
227 KeyValuePair_s insert;
229 btree = pers_db_open(info, dbPath);
233 keySize = (int)strlen((const char*)key);
234 if(keySize < DbKeySize)
237 dataSize = (int)strlen( (const char*)buffer);
238 if(dataSize < DbValueSize)
241 memset(insert.m_key, 0, DbKeySize);
242 memcpy(insert.m_key, key, keySize);
243 if(itzam_true == itzam_btree_find(btree, key, &insert))
245 // key already available, so delete "old" key
246 state = itzam_btree_remove(btree, (const void *)&insert);
250 memset(insert.m_data, 0, DbValueSize);
251 memcpy(insert.m_data, buffer, dataSize);
254 insert.m_data_size = buffer_size;
256 state = itzam_btree_insert(btree,(const void *)&insert);
257 if (state != ITZAM_OKAY)
259 fprintf(stderr, "\npersistence_set_data ==> Insert Itzam problem: %s\n", STATE_MESSAGES[state]);
260 write_size = EPERS_DB_ERROR_INTERNAL;
265 fprintf(stderr, "\npersistence_set_data ==> set_value_to_table_itzam => data to long » size %d | maxSize: %d\n", dataSize, DbKeySize);
266 write_size = EPERS_DB_VALUE_SIZE;
270 // workaround till lifecycle is working correctly
276 fprintf(stderr, "\nset_value_to_table_itzam => key to long » size: %d | maxSize: %d\n", keySize, DbKeySize);
277 write_size = EPERS_DB_KEY_SIZE;
282 write_size = EPERS_NOPRCTABLE;
283 fprintf(stderr, "\npersistence_set_data ==> Open Itzam problem: %s\n", STATE_MESSAGES[state]);
286 else if(PersistenceStorage_custom == info->configKey.storage) // custom storage implementation via custom library
288 int idx = custom_client_name_to_id(dbPath, 1);
289 if((idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_set_data) )
291 gPersCustomFuncs[idx].custom_plugin_set_data(key, (char*)buffer, buffer_size);
295 write_size = EPERS_NOPLUGINFUNCT;
303 int pers_db_get_key_size(char* dbPath, char* key, PersistenceInfo_s* info)
307 if( PersistenceStorage_shared == info->configKey.storage
308 || PersistenceStorage_local == info->configKey.storage)
311 itzam_btree* btree = NULL;
312 itzam_state state = ITZAM_FAILED;
313 KeyValuePair_s search;
315 btree = pers_db_open(info, dbPath);
318 keySize = (int)strlen((const char*)key);
319 if(keySize < DbKeySize)
321 memset(search.m_key,0, DbKeySize);
322 memcpy(search.m_key, key, keySize);
323 if(itzam_true == itzam_btree_find(btree, key, &search))
325 read_size = strlen(search.m_data);
329 read_size = EPERS_NOKEY;
334 fprintf(stderr, "persistence_get_data_size => key to long » size: %d | maxSize: %d\n", keySize, DbKeySize);
335 read_size = EPERS_DB_KEY_SIZE;
338 // workaround till lifecycle is working correctly
344 read_size = EPERS_NOPRCTABLE;
345 fprintf(stderr, "\npersistence_get_data_size ==> Open Itzam problem: %s\n", STATE_MESSAGES[state]);
348 else if(PersistenceStorage_custom == info->configKey.storage) // custom storage implementation via custom library
350 int idx = custom_client_name_to_id(dbPath, 1);
351 if((idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_set_data) )
353 gPersCustomFuncs[idx].custom_plugin_get_size(key);
357 read_size = EPERS_NOPLUGINFUNCT;
365 int pers_db_delete_key(char* dbPath, char* dbKey, PersistenceInfo_s* info)
368 if(PersistenceStorage_custom != info->configKey.storage)
370 itzam_btree* btree = NULL;
371 KeyValuePair_s delete;
373 //printf("delete_key_from_table_itzam => Path: \"%s\" | key: \"%s\" \n", dbPath, key);
374 btree = pers_db_open(info, dbPath);
378 keySize = (int)strlen((const char*)dbKey);
379 if(keySize < DbKeySize)
383 memset(delete.m_key,0, DbKeySize);
384 memcpy(delete.m_key, dbKey, keySize);
385 state = itzam_btree_remove(btree, (const void *)&delete);
386 if (state != ITZAM_OKAY)
388 fprintf(stderr, "persistence_delete_data ==> Remove Itzam problem: %s\n", STATE_MESSAGES[state]);
389 ret = EPERS_DB_ERROR_INTERNAL;
394 fprintf(stderr, "persistence_delete_data => key to long » size: %d | maxSize: %d\n", keySize, DbKeySize);
395 ret = EPERS_DB_KEY_SIZE;
398 // workaround till lifecycle is working correctly
404 fprintf(stderr, "persistence_delete_data => no prct table\n");
405 ret = EPERS_NOPRCTABLE;
408 else // custom storage implementation via custom library
410 int idx = custom_client_name_to_id(dbPath, 1);
411 if((idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_set_data) )
413 gPersCustomFuncs[idx].custom_plugin_delete_data(dbKey);
417 ret = EPERS_NOPLUGINFUNCT;
424 int persistence_reg_notify_on_change(char* dbPath, char* key)
433 //---------------------------------------------------------------------------------------------------------
434 // C U R S O R F U N C T I O N S
435 //---------------------------------------------------------------------------------------------------------
437 int get_cursor_handle()
441 if(pthread_mutex_lock(&gMtx) == 0)
443 if(gFreeCursorHandleIdxHead > 0) // check if we have a free spot in the array before the current max
445 handle = gFreeCursorHandleArray[--gFreeCursorHandleIdxHead];
449 if(gHandleIdx < MaxPersHandle-1)
451 handle = gHandleIdx++; // no free spot before current max, increment handle index
456 printf("get_persistence_handle_idx => Reached maximum of open handles: %d \n", MaxPersHandle);
459 pthread_mutex_unlock(&gMtx);
465 void close_cursor_handle(int handlerDB)
467 if(pthread_mutex_lock(&gMtx) == 0)
469 if(gFreeCursorHandleIdxHead < MaxPersHandle)
471 gFreeCursorHandleArray[gFreeCursorHandleIdxHead++] = handlerDB;
473 pthread_mutex_unlock(&gMtx);
479 int pers_db_cursor_create(char* dbPath)
482 itzam_state state = ITZAM_FAILED;
484 handle = get_cursor_handle();
486 if(handle < MaxPersHandle && handle >= 0)
489 state = itzam_btree_open(&gCursorArray[handle].m_btree, dbPath, itzam_comparator_string, error_handler, 0/*recover*/, 0/*read_only*/);
490 if (state != ITZAM_OKAY)
492 fprintf(stderr, "pers_db_open ==> Open Itzam problem: %s\n", STATE_MESSAGES[state]);
498 state = itzam_btree_cursor_create(&gCursorArray[handle].m_cursor, &gCursorArray[handle].m_btree);
499 if(state == ITZAM_OKAY)
501 gCursorArray[handle].m_empty = 0;
505 gCursorArray[handle].m_empty = 1;
514 int pers_db_cursor_next(unsigned int handlerDB)
517 if(handlerDB < MaxPersHandle && handlerDB >= 0)
519 if(gCursorArray[handlerDB].m_empty != 1)
522 success = itzam_btree_cursor_next(&gCursorArray[handlerDB].m_cursor);
524 if(success == itzam_true)
530 rval = EPERS_LAST_ENTRY_IN_DB;
535 printf("persistence_db_cursor_get_key ==> invalid handle: %u \n", handlerDB);
540 printf("persistence_db_cursor_get_key ==> handle bigger than max » handleDB: %u | max: : %d \n", handlerDB, MaxPersHandle);
547 int pers_db_cursor_get_key(unsigned int handlerDB, char * bufKeyName_out, int bufSize)
550 KeyValuePair_s search;
552 if(handlerDB < MaxPersHandle)
554 if(gCursorArray[handlerDB].m_empty != 1)
557 itzam_btree_cursor_read(&gCursorArray[handlerDB].m_cursor ,(void *)&search);
558 length = strlen(search.m_key);
561 memcpy(bufKeyName_out, search.m_key, length);
566 printf("persistence_db_cursor_get_key ==> buffer to small » keySize: %d | bufSize: %d \n", length, bufSize);
571 printf("persistence_db_cursor_get_key ==> invalid handle: %u \n", handlerDB);
576 printf("persistence_db_cursor_get_key ==> handle bigger than max » handleDB: %u | max: : %d \n", handlerDB, MaxPersHandle);
583 int pers_db_cursor_get_data(unsigned int handlerDB, char * bufData_out, int bufSize)
586 KeyValuePair_s search;
588 if(handlerDB < MaxPersHandle)
590 if(gCursorArray[handlerDB].m_empty != 1)
593 itzam_btree_cursor_read(&gCursorArray[handlerDB].m_cursor ,(void *)&search);
595 length = strlen(search.m_data);
598 memcpy(bufData_out, search.m_data, length);
603 printf("persistence_db_cursor_get_data ==> buffer to small » keySize: %d | bufSize: %d \n", length, bufSize);
608 printf("persistence_db_cursor_get_data ==> invalid handle: %u \n", handlerDB);
613 printf("persistence_db_cursor_get_data ==> handle bigger than max » handleDB: %u | max: : %d \n", handlerDB, MaxPersHandle);
620 int pers_db_cursor_get_data_size(unsigned int handlerDB)
623 KeyValuePair_s search;
625 if(handlerDB < MaxPersHandle)
627 if(gCursorArray[handlerDB].m_empty != 1)
629 itzam_btree_cursor_read(&gCursorArray[handlerDB].m_cursor ,(void *)&search);
630 size = strlen(search.m_data);
634 printf("persistence_db_cursor_get_data ==> invalid handle: %u \n", handlerDB);
639 printf("persistence_db_cursor_get_data ==> handle bigger than max » handleDB: %u | max: : %d \n", handlerDB, MaxPersHandle);
646 int pers_db_cursor_destroy(unsigned int handlerDB)
649 if(handlerDB < MaxPersHandle)
651 itzam_btree_cursor_free(&gCursorArray[handlerDB].m_cursor);
652 gCursorArray[handlerDB].m_empty = 1;
654 itzam_btree_close(&gCursorArray[handlerDB].m_btree);
655 close_cursor_handle(handlerDB);
665 //-----------------------------------------------------------------------------
666 // code to print database content (for debugging)
667 //-----------------------------------------------------------------------------
671 itzam_btree_cursor cursor;
672 state = itzam_btree_cursor_create(&cursor, &btree);
673 if(state == ITZAM_OKAY)
675 printf("==> Database content ==> db size: %d\n", (int)itzam_btree_count(&btree));
678 // get the key pointed to by the cursor
679 state = itzam_btree_cursor_read(&cursor,(void *)&rec);
680 if (state == ITZAM_OKAY)
682 printf(" Key: %s \n ==> data: %s\n", rec.m_key, rec.m_data);
685 fprintf(stderr, "\nItzam problem: %s\n", STATE_MESSAGES[state]);
687 while (itzam_btree_cursor_next(&cursor));
689 state = itzam_btree_cursor_free(&cursor);
692 //-----------------------------------------------------------------------------