diff options
author | Ingo Huerner <ingo.huerner@xse.de> | 2013-08-21 15:25:46 +0200 |
---|---|---|
committer | Ingo Huerner <ingo.huerner@xse.de> | 2013-08-21 15:25:46 +0200 |
commit | d2c9088357819fa5e5f30efa8f8de44659df9077 (patch) | |
tree | 5db8d538db7664629d130c84206cdd35528d5f88 /src/persistence_client_library_db_access.c | |
parent | ab3df06626449c46dc5ab0c546ed2b5bee8b9fd6 (diff) | |
download | persistence-client-library-d2c9088357819fa5e5f30efa8f8de44659df9077.tar.gz |
Implemented default data handling for read
Diffstat (limited to 'src/persistence_client_library_db_access.c')
-rw-r--r-- | src/persistence_client_library_db_access.c | 125 |
1 files changed, 117 insertions, 8 deletions
diff --git a/src/persistence_client_library_db_access.c b/src/persistence_client_library_db_access.c index 3424d94..aab7a1f 100644 --- a/src/persistence_client_library_db_access.c +++ b/src/persistence_client_library_db_access.c @@ -33,7 +33,7 @@ /// definition of a key-value pair stored in the database typedef struct _KeyValuePair_s { - char m_key[DbKeySize]; /// the key + char m_key[DbKeySize]; /// the key char m_data[DbValueSize]; /// the data unsigned int m_data_size; /// the size of the data } @@ -73,6 +73,38 @@ static int gBtreeCreated[DbTableSize][PersistencePolicy_LastEntry] = { {0} }; int pers_send_Notification_Signal(const char* key, PersistenceDbContext_s* context, unsigned int reason); + + + + +int pers_db_open_default(itzam_btree* btree, PersistenceInfo_s* info, const char* dbPath, int configDefault) +{ + itzam_state state = ITZAM_FAILED; + char path[DbPathMaxLen] = {0}; + + if(1 == configDefault) + { + snprintf(path, DbPathMaxLen, "%s%s", dbPath, gConfigDefault); + } + else if(0 == configDefault) + { + snprintf(path, DbPathMaxLen, "%s%s", dbPath, gDefault); + } + else + { + return -1; // invalid + } + + state = itzam_btree_open(btree, path, itzam_comparator_string, error_handler, 0/*recover*/, 0/*read_only*/); + if (state != ITZAM_OKAY) + { + DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_db_open_default ==> itzam_btree_open => Itzam problem"), DLT_STRING(STATE_MESSAGES[state])); + } + + return 1; +} + + itzam_btree* pers_db_open(PersistenceInfo_s* info, const char* dbPath) { int arrayIdx = 0; @@ -87,7 +119,22 @@ itzam_btree* pers_db_open(PersistenceInfo_s* info, const char* dbPath) if(gBtreeCreated[arrayIdx][info->configKey.policy] == 0) { itzam_state state = ITZAM_FAILED; - state = itzam_btree_open(&gBtree[arrayIdx][info->configKey.policy], dbPath, + char path[DbPathMaxLen] = {0}; + + if(PersistencePolicy_wt == info->configKey.policy) + { + snprintf(path, DbPathMaxLen, "%s%s", dbPath, gWt); + } + else if(PersistencePolicy_wc == info->configKey.policy) + { + snprintf(path, DbPathMaxLen, "%s%s", dbPath, gCached); + } + else + { + return btree; + } + + state = itzam_btree_open(&gBtree[arrayIdx][info->configKey.policy], path, itzam_comparator_string, error_handler, 0/*recover*/, 0/*read_only*/); if (state != ITZAM_OKAY) { @@ -162,7 +209,6 @@ void pers_db_close_all() } - int pers_db_read_key(char* dbPath, char* key, PersistenceInfo_s* info, unsigned char* buffer, unsigned int buffer_size) { int read_size = -1; @@ -171,7 +217,11 @@ int pers_db_read_key(char* dbPath, char* key, PersistenceInfo_s* info, unsigned || PersistenceStorage_local == info->configKey.storage) { itzam_btree* btree = NULL; + itzam_btree btreeDefault; + itzam_btree btreeConfDefault; KeyValuePair_s search; + int keyFound = 0; + itzam_state state = ITZAM_FAILED; btree = pers_db_open(info, dbPath); if(btree != NULL) @@ -184,17 +234,76 @@ int pers_db_read_key(char* dbPath, char* key, PersistenceInfo_s* info, unsigned read_size = buffer_size; // truncate data size to buffer size } memcpy(buffer, search.m_data, read_size); + keyFound = 1; } - else + } + + + // 1. check if _configurable_ default data is available + // -------------------------------- + if(keyFound == 0) + { + if(pers_db_open_default(&btreeConfDefault, info, dbPath, 1) != -1) { - read_size = EPERS_NOKEY; + if(itzam_true == itzam_btree_find(&btreeConfDefault, key, &search)) // read db + { + read_size = search.m_data_size; + if(read_size > buffer_size) + { + read_size = buffer_size; // truncate data size to buffer size + } + memcpy(buffer, search.m_data, read_size); + + keyFound = 1; + } + else + { + DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_db_read_key ==> 2. resource not found in default config => search in default db"), DLT_STRING(key)); + } + + state = itzam_btree_close(&btreeConfDefault); + if (state != ITZAM_OKAY) + { + DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_db_read_key ==> default: itzam_btree_close => Itzam problem"), DLT_STRING(STATE_MESSAGES[state])); + } } } - else + + // 2. check if default data is available + // -------------------------------- + if(keyFound == 0) { - DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_db_read_key ==>no resource config table"), DLT_STRING(dbPath), DLT_STRING(key) ); - read_size = EPERS_NOPRCTABLE; + if(pers_db_open_default(&btreeDefault, info, dbPath, 0) != -1) + { + if(itzam_true == itzam_btree_find(&btreeDefault, key, &search)) // read db + { + read_size = search.m_data_size; + if(read_size > buffer_size) + { + read_size = buffer_size; // truncate data size to buffer size + } + memcpy(buffer, search.m_data, read_size); + } + else + { + DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_db_read_key ==> 3. reasoure not found in both default db's"), DLT_STRING(key) ); + read_size = EPERS_NOKEY; // the key is not available neither in regular db nor in the default db's + } + + state = itzam_btree_close(&btreeDefault); + if (state != ITZAM_OKAY) + { + DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_db_read_key ==> default: itzam_btree_close => Itzam problem"), DLT_STRING(STATE_MESSAGES[state])); + } + } + else + { + DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pers_db_read_key ==>no resource config table"), DLT_STRING(dbPath), DLT_STRING(key) ); + read_size = EPERS_NOPRCTABLE; + } } + + } else if(PersistenceStorage_custom == info->configKey.storage) // custom storage implementation via custom library { |