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 | |
parent | ab3df06626449c46dc5ab0c546ed2b5bee8b9fd6 (diff) | |
download | persistence-client-library-d2c9088357819fa5e5f30efa8f8de44659df9077.tar.gz |
Implemented default data handling for read
Diffstat (limited to 'src')
-rw-r--r-- | src/persistence_client_library_backup_filelist.c | 2 | ||||
-rw-r--r-- | src/persistence_client_library_data_organization.c | 55 | ||||
-rw-r--r-- | src/persistence_client_library_db_access.c | 125 | ||||
-rw-r--r-- | src/persistence_client_library_handle.c | 2 | ||||
-rw-r--r-- | src/persistence_client_library_prct_access.c | 32 |
5 files changed, 163 insertions, 53 deletions
diff --git a/src/persistence_client_library_backup_filelist.c b/src/persistence_client_library_backup_filelist.c index 7a32e14..7ce72da 100644 --- a/src/persistence_client_library_backup_filelist.c +++ b/src/persistence_client_library_backup_filelist.c @@ -66,7 +66,7 @@ const char gCharLookup[] = char* gpConfigFileMap = 0; -char* gpTokenArray[TOKENARRAYSIZE]; +char* gpTokenArray[TOKENARRAYSIZE] = {0}; int gTokenCounter = 0; unsigned int gConfigFileSize = 0; diff --git a/src/persistence_client_library_data_organization.c b/src/persistence_client_library_data_organization.c index 05ba317..001ded6 100644 --- a/src/persistence_client_library_data_organization.c +++ b/src/persistence_client_library_data_organization.c @@ -22,28 +22,19 @@ #include <stdio.h> #include <stdlib.h> +// rrsource configuration database name const char* gResTableCfg = "/resource-table-cfg.itz"; +/// configurable default database name +const char* gConfigDefault = "/configurable-default-data.itz"; -/// shared cached default database -const char* gSharedCachedDefault = "/cached-default.itz"; -/// shared cached database -const char* gSharedCached = "/cached.itz"; -/// shared write through default database -const char* gSharedWtDefault = "/wt-default.itz"; -/// shared write through database -const char* gSharedWt = "/wt.itz"; - - -/// local cached default database -const char* gLocalCachedDefault = "cached-default.itz"; -/// local cached default database -const char* gLocalCached = "/cached.itz"; -/// local write through default database -const char* gLocalWtDefault = "wt-default.itz"; -/// local write through default database -const char* gLocalWt = "/wt.itz"; +/// default database name +const char* gDefault = "/default-data.itz"; +/// write through database name +const char* gWt = "/wt.itz"; +/// cached database name +const char* gCached = "/cached.itz"; /// directory structure node name defintion @@ -54,21 +45,31 @@ const char* gUser = "/user/"; const char* gSeat = "/seat/"; -/// path prefix for local cached database: /Data/mnt_c/<appId>/<database_name> -const char* gLocalCachePath = "/Data/mnt-c/%s%s"; +/// path prefix for local cached database: /Data/mnt_c/<appId>/ (<database_name> +const char* gLocalCachePath = "/Data/mnt-c/%s"; /// path prefix for local write through database /Data/mnt_wt/<appId>/<database_name> -const char* gLocalWtPath = "/Data/mnt-wt/%s%s"; +const char* gLocalWtPath = "/Data/mnt-wt/%s"; /// path prefix for shared cached database: /Data/mnt_c/Shared/Group/<group_no>/<database_name> -const char* gSharedCachePath = "/Data/mnt-c/%s/Shared_Group_%x%s"; +const char* gSharedCachePath = "/Data/mnt-c/%s/Shared_Group_%x"; /// path prefix for shared write through database: /Data/mnt_wt/Shared/Group/<group_no>/<database_name> -const char* gSharedWtPath = "/Data/mnt-wt/%s/Shared_Group_%x%s"; - +const char* gSharedWtPath = "/Data/mnt-wt/%s/Shared_Group_%x"; /// path prefix for shared public cached database: /Data/mnt_c/Shared/Public//<database_name> -const char* gSharedPublicCachePath = "/Data/mnt-c/%s/Shared_Public%s"; - +const char* gSharedPublicCachePath = "/Data/mnt-c/%s/Shared_Public"; /// path prefix for shared public write through database: /Data/mnt_wt/Shared/Public/<database_name> -const char* gSharedPublicWtPath = "/Data/mnt-wt/%s/Shared_Public%s"; +const char* gSharedPublicWtPath = "/Data/mnt-wt/%s/Shared_Public"; +/// path prefix for local cached database: /Data/mnt_c/<appId>/ (<database_name> +const char* gLocalCachePathKey = "/Data/mnt-c/%s%s"; +/// path prefix for local write through database /Data/mnt_wt/<appId>/<database_name> +const char* gLocalWtPathKey = "/Data/mnt-wt/%s%s"; +/// path prefix for shared cached database: /Data/mnt_c/Shared/Group/<group_no>/<database_name> +const char* gSharedCachePathKey = "/Data/mnt-c/%s/Shared_Group_%x%s"; +/// path prefix for shared write through database: /Data/mnt_wt/Shared/Group/<group_no>/<database_name> +const char* gSharedWtPathKey = "/Data/mnt-wt/%s/Shared_Group_%x%s"; +/// path prefix for shared public cached database: /Data/mnt_c/Shared/Public//<database_name> +const char* gSharedPublicCachePathKey = "/Data/mnt-c/%s/Shared_Public%s"; +/// path prefix for shared public write through database: /Data/mnt_wt/Shared/Public/<database_name> +const char* gSharedPublicWtPathKey = "/Data/mnt-wt/%s/Shared_Public%s"; /// path prefix for local cached files: /Data/mnt_c/<appId>/<user>/<seat>/<resource> const char* gLocalCacheFilePath = "/Data/mnt-c/%s/user/%d/seat/%d/%s"; 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 { diff --git a/src/persistence_client_library_handle.c b/src/persistence_client_library_handle.c index 821e9a3..a6a952d 100644 --- a/src/persistence_client_library_handle.c +++ b/src/persistence_client_library_handle.c @@ -40,7 +40,7 @@ PersistenceFileHandle_s gFileHandleArray[MaxPersHandle]; /// free handle array -int gFreeHandleArray[MaxPersHandle]; +int gFreeHandleArray[MaxPersHandle] = {0}; int gFreeHandleIdxHead = 0; diff --git a/src/persistence_client_library_prct_access.c b/src/persistence_client_library_prct_access.c index f3142b4..c0e450a 100644 --- a/src/persistence_client_library_prct_access.c +++ b/src/persistence_client_library_prct_access.c @@ -86,13 +86,13 @@ itzam_btree* get_resource_cfg_table(PersistenceRCT_e rct, int group) switch(rct) // create db name { case PersistenceRCT_local: - snprintf(filename, DbPathMaxLen, gLocalWtPath, gAppId, gResTableCfg); + snprintf(filename, DbPathMaxLen, gLocalWtPathKey, gAppId, gResTableCfg); break; case PersistenceRCT_shared_public: - snprintf(filename, DbPathMaxLen, gSharedPublicWtPath, gAppId, gResTableCfg); + snprintf(filename, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, gResTableCfg); break; case PersistenceRCT_shared_group: - snprintf(filename, DbPathMaxLen, gSharedWtPath, gAppId, group, gResTableCfg); + snprintf(filename, DbPathMaxLen, gSharedWtPathKey, gAppId, group, gResTableCfg); break; default: DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_resource_cfg_table - error: no valid PersistenceRCT_e")); @@ -283,16 +283,16 @@ int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, c if(PersistencePolicy_wc == dbContext->configKey.policy) { if(dbContext->configKey.type == PersistenceResourceType_key) - snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, gSharedCached); + snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid); else - snprintf(dbPath, DbPathMaxLen, gSharedCachePath, gAppId, dbContext->context.ldbid, dbKey); + snprintf(dbPath, DbPathMaxLen, gSharedCachePathKey, gAppId, dbContext->context.ldbid, dbKey); } else if(PersistencePolicy_wt == dbContext->configKey.policy) { if(dbContext->configKey.type == PersistenceResourceType_key) - snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid, gSharedWt); + snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid); else - snprintf(dbPath, DbPathMaxLen, gSharedWtPath, gAppId, dbContext->context.ldbid, dbKey); + snprintf(dbPath, DbPathMaxLen, gSharedWtPathKey, gAppId, dbContext->context.ldbid, dbKey); } } else @@ -304,16 +304,16 @@ int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, c if(PersistencePolicy_wc == dbContext->configKey.policy) { if(dbContext->configKey.type == PersistenceResourceType_key) - snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, gSharedCached); + snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId); else - snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePath, gAppId, dbKey); + snprintf(dbPath, DbPathMaxLen, gSharedPublicCachePathKey, gAppId, dbKey); } else if(PersistencePolicy_wt == dbContext->configKey.policy) { if(dbContext->configKey.type == PersistenceResourceType_key) - snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, gSharedWt); + snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId); else - snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPath, gAppId, dbKey); + snprintf(dbPath, DbPathMaxLen, gSharedPublicWtPathKey, gAppId, dbKey); } } @@ -326,23 +326,23 @@ int get_db_path_and_key(PersistenceInfo_s* dbContext, const char* resource_id, c if(PersistencePolicy_wc == dbContext->configKey.policy) { if(dbContext->configKey.type == PersistenceResourceType_key) - snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, gLocalCached); + snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId); else - snprintf(dbPath, DbPathMaxLen, gLocalCachePath, gAppId, dbKey); + snprintf(dbPath, DbPathMaxLen, gLocalCachePathKey, gAppId, dbKey); } else if(PersistencePolicy_wt == dbContext->configKey.policy) { if(dbContext->configKey.type == PersistenceResourceType_key) - snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId, gLocalWt); + snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId); else - snprintf(dbPath, DbPathMaxLen, gLocalWtPath, gAppId, dbKey); + snprintf(dbPath, DbPathMaxLen, gLocalWtPathKey, gAppId, dbKey); } storePolicy = PersistenceStorage_local; // we have a local database } //printf("get_db_path_and_key - dbKey : [key ]: %s \n", dbKey); - //printf("get_db_path_and_key - dbPath : [path]: %s\n\n", dbPath); + //printf("get_db_path_and_key - dbPath : [path]: %s\n", dbPath); return storePolicy; } |