diff options
Diffstat (limited to 'src/key-value-store/pers_low_level_db_access.c')
-rw-r--r-- | src/key-value-store/pers_low_level_db_access.c | 1637 |
1 files changed, 992 insertions, 645 deletions
diff --git a/src/key-value-store/pers_low_level_db_access.c b/src/key-value-store/pers_low_level_db_access.c index 1ca2b27..71f49ce 100644 --- a/src/key-value-store/pers_low_level_db_access.c +++ b/src/key-value-store/pers_low_level_db_access.c @@ -1,4 +1,4 @@ - /****************************************************************************** +/****************************************************************************** * Project Persistency * (c) copyright 2014 * Company XS Embedded GmbH @@ -7,21 +7,21 @@ * This Source Code Form is subject to the terms of the * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed * with this file, You can obtain one at http://mozilla.org/MPL/2.0/. -******************************************************************************/ - /** + ******************************************************************************/ +/** * @file pers_low_level_db_access.c * @author Simon Disch * @brief Implementation of persComDbAccess.h * @see */ - #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #include <sys/types.h> +#include <sys/mman.h> #include <unistd.h> #include "./database/kissdb.h" #include "./hashtable/qlibc.h" @@ -32,20 +32,40 @@ #include "persComDbAccess.h" #include "persComRct.h" #include "pers_low_level_db_access_if.h" -#include "dlt.h" +#include <dlt.h> #include <errno.h> #include <sys/time.h> +#include <sys/shm.h> + + +/* #define PFS_TEST */ + /* L&T context */ #define LT_HDR "[persComLLDB]" +DltContext persComLldbDLTCtx; +DLT_DECLARE_CONTEXT (persComLldbDLTCtx) + + + +///// library constructor +//void pco_library_init(void) __attribute__((constructor)); +//void pco_library_init() { +// printf ("\n pco_library_init() constructor \n"); +//} +// +///// library destructor +//void pco_library_destroy(void) __attribute__((destructor)); +//void pco_library_destroy() { +// printf ("\n pco_library_destroy() destructor \n"); +//} -DLT_DECLARE_CONTEXT(persComLldbDLTCtx); /* ---------------------- local definition ---------------------------- */ /* max number of open handlers per process */ #define PERS_LLDB_NO_OF_STATIC_HANDLES 16 #define PERS_LLDB_MAX_STATIC_HANDLES (PERS_LLDB_NO_OF_STATIC_HANDLES-1) -#define PERS_STATUS_KEY_NOT_IN_CACHE -10 //!< key not in cache +#define PERS_STATUS_KEY_NOT_IN_CACHE -10 /* /!< key not in cache */ typedef struct { @@ -56,7 +76,7 @@ typedef struct typedef enum pers_lldb_cache_flag_e { CachedDataDelete = 0, /* Resource-Configuration-Table */ - CachedDataWrite, /* Local/Shared DB */ + CachedDataWrite /* Local/Shared DB */ } pers_lldb_cache_flag_e; typedef struct @@ -85,7 +105,7 @@ typedef struct typedef struct lldb_handles_list_el_s_ { lldb_handler_s sHandle; - struct lldb_handles_list_el_s_ * pNext; + struct lldb_handles_list_el_s_* pNext; } lldb_handles_list_el_s; typedef struct @@ -98,7 +118,8 @@ typedef struct static const char ListItemsSeparator = '\0'; /* shared by all the threads within a process */ -static lldb_handlers_s g_sHandlers = { { { 0 } } }; +static lldb_handlers_s g_sHandlers; // initialize to 0 and NULL +//static lldb_handlers_s g_sHandlers = { { { 0 } } }; static pthread_mutex_t g_mutexLldb = PTHREAD_MUTEX_INITIALIZER; /* ---------------------- local macros --------------------------------- */ @@ -112,24 +133,26 @@ static sint_t GetKeySizeFromKissLocalDB(sint_t dbHandler, pconststr_t key); static sint_t GetDataFromKissLocalDB(sint_t dbHandler, pconststr_t key, pstr_t buffer_out, sint_t bufSize); static sint_t GetDataFromKissRCT(sint_t dbHandler, pconststr_t key, PersistenceConfigurationKey_s* pConfig); static sint_t SetDataInKissLocalDB(sint_t dbHandler, pconststr_t key, pconststr_t data, sint_t dataSize); -static sint_t SetDataInKissRCT(sint_t dbHandler, pconststr_t key, PersistenceConfigurationKey_s const * pConfig); +static sint_t SetDataInKissRCT(sint_t dbHandler, pconststr_t key, PersistenceConfigurationKey_s const* pConfig); static sint_t writeBackKissDB(KISSDB* db, lldb_handler_s* pLldbHandler); static sint_t writeBackKissRCT(KISSDB* db, lldb_handler_s* pLldbHandler); static sint_t getListandSize(KISSDB* db, pstr_t buffer, sint_t size, bool_t bOnlySizeNeeded, pers_lldb_purpose_e purpose); -static sint_t putToCache(KISSDB* db, sint_t dataSize, char* tmp_key, void* insert_cached_data); -static sint_t getFromCache(KISSDB* db, void* tmp_key, void* readBuffer, sint_t bufsize, bool_t sizeOnly); -static sint_t getFromDatabaseFile(KISSDB* db, void* tmp_key, void* readBuffer, pers_lldb_purpose_e purpose, sint_t bufsize, bool_t sizeOnly); +static sint_t putToCache(KISSDB* db, sint_t dataSize, char* metaKey, void* cachedData); +static sint_t deleteFromCache(KISSDB* db, char* metaKey); +static sint_t getFromCache(KISSDB* db, void* metaKey, void* readBuffer, sint_t bufsize, bool_t sizeOnly); +static sint_t getFromDatabaseFile(KISSDB* db, void* metaKey, void* readBuffer, sint_t bufsize); /* access to resources shared by the threads within a process */ static bool_t lldb_handles_Lock(void); static bool_t lldb_handles_Unlock(void); static lldb_handler_s* lldb_handles_FindInUseHandle(sint_t dbHandler); static lldb_handler_s* lldb_handles_FindAvailableHandle(void); -static void lldb_handles_InitHandle(lldb_handler_s* psHandle_inout, pers_lldb_purpose_e ePurpose, str_t const * dbPathname); +static void lldb_handles_InitHandle(lldb_handler_s* psHandle_inout, pers_lldb_purpose_e ePurpose, str_t const* dbPathname); static bool_t lldb_handles_DeinitHandle(sint_t dbHandler); static int createCache(KISSDB* db); static int openCache(KISSDB* db); +//static int addCache(KISSDB* db); static int closeCache(KISSDB* db); /** @@ -142,15 +165,22 @@ static int closeCache(KISSDB* db); * * \return >=0 for success, negative value otherway (see pers_error_codes.h) */ -sint_t pers_lldb_open(str_t const * dbPathname, pers_lldb_purpose_e ePurpose, bool_t bForceCreationIfNotPresent) +sint_t pers_lldb_open(str_t const* dbPathname, pers_lldb_purpose_e ePurpose, bool_t bForceCreationIfNotPresent) { - sint_t returnValue = PERS_COM_FAILURE; bool_t bCanContinue = true; - lldb_handler_s* pLldbHandler = NIL; bool_t bLocked = false; - int mode = KISSDB_OPEN_MODE_RDWR; + char linkBuffer[256] = { 0 }; + const char* path; + int error = 0; + int kdbState = 0; + int openMode = KISSDB_OPEN_MODE_RDWR; //default is open existing in RDWR + int writeMode = KISSDB_WRITE_MODE_WC; //default is write cached + lldb_handler_s* pLldbHandler = NIL; + sint_t returnValue = PERS_COM_FAILURE; static bool_t bFirstCall = true; + path = dbPathname; + if (bFirstCall) { pid_t pid = getpid(); @@ -161,12 +191,14 @@ sint_t pers_lldb_open(str_t const * dbPathname, pers_lldb_purpose_e ePurpose, bo /* init DLT */ (void) snprintf(dltContextID, sizeof(dltContextID), "Pers_%04d", pid); DLT_REGISTER_CONTEXT(persComLldbDLTCtx, dltContextID, "PersCommonLLDB"); - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("register context PersCommonLLDB ContextID="); DLT_STRING(dltContextID)); + //DLT_SET_APPLICATION_LL_TS_LIMIT(DLT_LOG_DEBUG, DLT_TRACE_STATUS_OFF); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("register context PersCommonLLDB ContextID="); DLT_STRING(dltContextID)); } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING("Begin opening:"); DLT_STRING("<<"); DLT_STRING(dbPathname); DLT_STRING(">>, "); ((PersLldbPurpose_RCT == ePurpose) ? DLT_STRING("RCT, ") : DLT_STRING("DB, ")); ((true == bForceCreationIfNotPresent) ? DLT_STRING("forced, ") : DLT_STRING("unforced, ")); DLT_STRING(" ... ")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING("Begin opening:"); DLT_STRING("<"); DLT_STRING(dbPathname); DLT_STRING(">, "); + ((PersLldbPurpose_RCT == ePurpose) ? DLT_STRING("RCT, ") : DLT_STRING("DB, ")); ((true == bForceCreationIfNotPresent) ? DLT_STRING("forced, ") : DLT_STRING("unforced, "))); if (lldb_handles_Lock()) { @@ -179,58 +211,158 @@ sint_t pers_lldb_open(str_t const * dbPathname, pers_lldb_purpose_e ePurpose, bo } } else + { bCanContinue = false; + } if (bCanContinue) { - int kissdb_state = 0; - size_t datasize = - (PersLldbPurpose_RCT == ePurpose) ? sizeof(PersistenceConfigurationKey_s) : sizeof(Data_LocalDB_s); - size_t keysize = - (PersLldbPurpose_RCT == ePurpose) ? PERS_RCT_MAX_LENGTH_RESOURCE_ID : PERS_DB_MAX_LENGTH_KEY_NAME; + size_t datasize = (PersLldbPurpose_RCT == ePurpose) ? sizeof(PersistenceConfigurationKey_s) : + PERS_DB_MAX_SIZE_KEY_DATA; + size_t keysize = (PersLldbPurpose_RCT == ePurpose) ? PERS_RCT_MAX_LENGTH_RESOURCE_ID : + PERS_DB_MAX_LENGTH_KEY_NAME; - if (bForceCreationIfNotPresent & (1 << 0) ) //check bit 0 - mode = KISSDB_OPEN_MODE_RWCREAT; + if (bForceCreationIfNotPresent & (1 << 0)) //check bit 0 0x0 (open) 0x1 (create) + { + openMode = KISSDB_OPEN_MODE_RWCREAT; //bit 0 is set + } + if(bForceCreationIfNotPresent & (1 << 1)) //check bit 1 + { + //bit 1 is set -> writeThrough mode 0x2 (open) 0x3 (create) + writeMode = KISSDB_WRITE_MODE_WT; + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, DLT_STRING(LT_HDR), DLT_STRING(__FUNCTION__), DLT_STRING("Opening in write through mode:"), DLT_STRING("<"), + DLT_STRING(dbPathname), DLT_STRING(">, ")); + } + if( bForceCreationIfNotPresent & (1 << 2)) //check bit 2 + { + openMode = KISSDB_OPEN_MODE_RDONLY; //bit 2 is set 0x4 + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, DLT_STRING(LT_HDR), DLT_STRING(__FUNCTION__), DLT_STRING("Opening in read only mode:"), DLT_STRING("<"), + DLT_STRING(dbPathname), DLT_STRING(">, ")); + } -#ifdef __writeThrough - if(bForceCreationIfNotPresent & (1 << 1)) //check bit 1 - printf("cached \n"); + if (1 == checkIsLink(dbPathname, linkBuffer)) + { + path = linkBuffer; + } else - printf("uncached \n"); -#endif + { + path = dbPathname; + } - kissdb_state = KISSDB_open(&pLldbHandler->kissDb, dbPathname, mode, 256, keysize, datasize); - if (kissdb_state != 0) + //printKdb(&pLldbHandler->kissDb); + + if (pLldbHandler->kissDb.alreadyOpen == Kdb_false) //check if this instance has already opened the db before { - DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, - DLT_STRING("KISSDB_open: "); DLT_STRING("<<"); DLT_STRING(dbPathname); DLT_STRING(">>, "); DLT_STRING(" retval=<"); DLT_INT(kissdb_state); DLT_STRING(">"), DLT_STRING(strerror(errno))); + pLldbHandler->kissDb.semName = kdbGetShmName("-sem", path); + if (NULL == pLldbHandler->kissDb.semName) + { + return -1; + } + pLldbHandler->kissDb.kdbSem = sem_open(pLldbHandler->kissDb.semName, O_CREAT | O_EXCL, 0644, 1); + error = errno; //store errno -> (errno could be modified by following DLT_LOG) + if (pLldbHandler->kissDb.kdbSem == SEM_FAILED) //open failed + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(": first sem_open() with mode O_CREAT | O_EXCL failed with: "); DLT_STRING(strerror(error))); + if (error == EEXIST) + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(": semaphore already exists: "); DLT_STRING(strerror(error))); + //try to open existing semaphore + pLldbHandler->kissDb.kdbSem = sem_open(pLldbHandler->kissDb.semName, O_CREAT, 0644, 0); + error = errno; + if (pLldbHandler->kissDb.kdbSem == SEM_FAILED) //open failed + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(": sem_open() for existing semaphore failed with error: "); DLT_STRING(strerror(error))); + return -1; + } + } + else + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":sem_open() failed:"); DLT_STRING(strerror(error))); + return -1; + } + } + } + if (-1 == sem_wait(pLldbHandler->kissDb.kdbSem)) + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, DLT_STRING(__FUNCTION__); DLT_STRING(": sem_wait() in open failed: "), + DLT_STRING(strerror(errno))); + } + kdbState = KISSDB_open(&pLldbHandler->kissDb, path, openMode, writeMode, HASHTABLE_SLOT_COUNT, keysize, datasize); + if (kdbState != 0) + { + if (kdbState == KISSDB_ERROR_WRONG_DATABASE_VERSION) + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING("KISSDB_open: "); DLT_STRING("<"); DLT_STRING(path); DLT_STRING(">, "); DLT_STRING("database to be opened has wrong version! retval=<"); DLT_INT(kdbState); DLT_STRING(">")); + } + else + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, + DLT_STRING("KISSDB_open: "); DLT_STRING("<"); DLT_STRING(path); DLT_STRING(">, "); DLT_STRING(" retval=<"); DLT_INT(kdbState); DLT_STRING(">"), + DLT_STRING(strerror(errno))); + } bCanContinue = false; } - if (bCanContinue) + } + if (kdbState == 0) + { + pLldbHandler->kissDb.shared->refCount++; //increment reference to opened databases + if (-1 == sem_post(pLldbHandler->kissDb.kdbSem)) //release semaphore { - lldb_handles_InitHandle(pLldbHandler, ePurpose, dbPathname); - returnValue = pLldbHandler->dbHandler; + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, DLT_STRING(__FUNCTION__); DLT_STRING(": End of open -> sem_post() failed: "),DLT_STRING(strerror(errno))); } - else + } + else + { + cleanKdbStruct(&pLldbHandler->kissDb); + + //in case of cleanup failure for semaphores, release semaphore + if (pLldbHandler->kissDb.kdbSem != NULL) { - /* clean up */ - returnValue = PERS_COM_FAILURE; - (void) lldb_handles_DeinitHandle(pLldbHandler->dbHandler); + if (-1 == sem_post(pLldbHandler->kissDb.kdbSem)) //release semaphore + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, DLT_STRING(__FUNCTION__); DLT_STRING(": End of open -> sem_post() in cleanup failed: "), + DLT_STRING(strerror(errno))); + } + } + + if(pLldbHandler->kissDb.semName != NULL) + { + free(pLldbHandler->kissDb.semName); + pLldbHandler->kissDb.semName = NULL; } } + if (bCanContinue) + { + lldb_handles_InitHandle(pLldbHandler, ePurpose, path); + returnValue = pLldbHandler->dbHandler; + } + else + { + /* clean up */ + returnValue = PERS_COM_FAILURE; + (void) lldb_handles_DeinitHandle(pLldbHandler->dbHandler); + } if (bLocked) + { (void) lldb_handles_Unlock(); - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING("End of open for:"); DLT_STRING("<<"); DLT_STRING(dbPathname); DLT_STRING(">>, "); ((PersLldbPurpose_RCT == ePurpose) ? DLT_STRING("RCT, ") : DLT_STRING("DB, ")); ((true == bForceCreationIfNotPresent) ? DLT_STRING("forced, ") : DLT_STRING("unforced, ")); DLT_STRING("retval=<"); DLT_INT(returnValue); DLT_STRING(">")); - - return returnValue; -} + } + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, DLT_STRING(LT_HDR), DLT_STRING(__FUNCTION__), DLT_STRING("End of open for:"), DLT_STRING("<"), + DLT_STRING(dbPathname), DLT_STRING(">, "), ((PersLldbPurpose_RCT == ePurpose) ? DLT_STRING("RCT, ") : DLT_STRING("DB, ")), + ((true == bForceCreationIfNotPresent) ? DLT_STRING("forced, ") : DLT_STRING("unforced, ")); DLT_STRING("retval=<"), DLT_INT(returnValue), + DLT_STRING(">")); + return returnValue; +} /** - * \close a key-value database + * \brief close a key-value database * \note : DB type is identified from dbPathname (based on extension) * * \param handlerDB [in] handler obtained with pers_lldb_open @@ -239,19 +371,24 @@ sint_t pers_lldb_open(str_t const * dbPathname, pers_lldb_purpose_e ePurpose, bo */ sint_t pers_lldb_close(sint_t handlerDB) { - int kissdb_state = 0; - sint_t returnValue = PERS_COM_SUCCESS; - lldb_handler_s* pLldbHandler = NIL; +#ifdef PFS_TEST + printf("START: pers_lldb_close for PID: %d \n", getpid()); +#endif + bool_t bLocked = false; + int kdbState = 0; + lldb_handler_s* pLldbHandler = NIL; + sint_t returnValue = PERS_COM_SUCCESS; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(handlerDB); DLT_STRING("...")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(handlerDB)); #ifdef __showTimeMeasurements long long duration = 0; long long KdbDuration = 0; long long writeDuration = 0; - struct timespec writeStart, writeEnd, kdbStart, kdbEnd, writebackStart, writebackEnd; + struct timespec writeStart, writeEnd, kdbStart, kdbEnd, writebackStart, + writebackEnd; duration = 0; KdbDuration = 0; writeDuration = 0; @@ -266,118 +403,168 @@ sint_t pers_lldb_close(sint_t handlerDB) pLldbHandler = lldb_handles_FindInUseHandle(handlerDB); if (NIL == pLldbHandler) + { returnValue = PERS_COM_FAILURE; + } } } else + { returnValue = PERS_COM_ERR_INVALID_PARAM; + } if (PERS_COM_SUCCESS == returnValue) { - //persist cached data to flash memory KISSDB* db = &pLldbHandler->kissDb; + if (-1 == sem_wait(db->kdbSem)) + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, DLT_STRING(__FUNCTION__); DLT_STRING(": sem_wait() in close failed: "), + DLT_STRING(strerror(errno))); + } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Closing database =<<"); DLT_STRING(pLldbHandler->dbPathname); DLT_STRING(">>, ")); - + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Closing database =<"); DLT_STRING(pLldbHandler->dbPathname); DLT_STRING(">, ")); - Kdb_wrlock(&db->shmem_info->cache_rwlock); + Kdb_wrlock(&db->shared->rwlock); //lock acces to shared status information - if (db->shmem_info->cache_initialised == Kdb_true) + if (db->shared->refCount > 0) { - if (db->shmem_creator == Kdb_true) + db->shared->refCount--; + } + if (db->shared->cacheCreated == Kdb_true) + { + if (db->shared->refCount == 0) { - //open existing cache in existing shared memory - if (db->shmem_cached_fd <= 0) + if (openCache(db) != 0) { - if (openCache(db) != 0) - { - Kdb_unlock(&db->shmem_info->cache_rwlock); - return PERS_COM_FAILURE; - } + Kdb_unlock(&db->shared->rwlock); + return PERS_COM_FAILURE; } #ifdef __showTimeMeasurements clock_gettime(CLOCK_ID, &writebackStart); #endif - if (pLldbHandler->ePurpose == PersLldbPurpose_DB) //write back to local database - writeBackKissDB(&pLldbHandler->kissDb, pLldbHandler); - else if (pLldbHandler->ePurpose == PersLldbPurpose_RCT) //write back to RCT database + + if (db->shared->openMode != KISSDB_OPEN_MODE_RDONLY) { - writeBackKissRCT(&pLldbHandler->kissDb, pLldbHandler); +#ifdef PFS_TEST + printf(" START: writeback of %d slots\n", pLldbHandler->kissDb.tbl->data->usedslots); +#endif + if (pLldbHandler->ePurpose == PersLldbPurpose_DB) //write back to local database + { + writeBackKissDB(&pLldbHandler->kissDb, pLldbHandler); + } + else + { + if (pLldbHandler->ePurpose == PersLldbPurpose_RCT) //write back to RCT database + { + writeBackKissRCT(&pLldbHandler->kissDb, pLldbHandler); + } + } +#ifdef PFS_TEST + printf(" END: writeback \n"); +#endif } + #ifdef __showTimeMeasurements clock_gettime(CLOCK_ID, &writebackEnd); #endif - if (db->shmem_info->cache_initialised) + //release reference object + db->tbl[0]->free(db->tbl[0]); + db->tbl[0] = NULL; + if (closeCache(db) != 0) { - db->tbl->free(db->tbl); - if (closeCache(db) != 0) - { - Kdb_unlock(&db->shmem_info->cache_rwlock); - return PERS_COM_FAILURE; - } + Kdb_unlock(&db->shared->rwlock); + return PERS_COM_FAILURE; + } + db->sharedCache = NULL; + if(db->sharedCacheFd) + { + close(db->sharedCacheFd); + db->sharedCacheFd = -1; + } + } + else //not the last instance, just unmap shared cache and free the name + { + //release reference object + db->tbl[0]->free(db->tbl[0]); + db->tbl[0] = NULL; + freeKdbShmemPtr(db->sharedCache, PERS_CACHE_MEMSIZE); + db->sharedCache = NULL; + if(db->sharedCacheFd) + { + close(db->sharedCacheFd); + db->sharedCacheFd = -1; } } } - Kdb_unlock(&db->shmem_info->cache_rwlock); + //no cache exists + Kdb_unlock(&db->shared->rwlock); #ifdef __showTimeMeasurements clock_gettime(CLOCK_ID, &kdbStart); #endif - kissdb_state = KISSDB_close(&pLldbHandler->kissDb); + kdbState = KISSDB_close(&pLldbHandler->kissDb); #ifdef __showTimeMeasurements clock_gettime(CLOCK_ID, &kdbEnd); #endif - if (kissdb_state == 0) + if (kdbState == 0) { if (!lldb_handles_DeinitHandle(pLldbHandler->dbHandler)) + { returnValue = PERS_COM_FAILURE; + } } else { - switch (kissdb_state) + switch (kdbState) { - case KISSDB_ERROR_UNMAP_SHM: + case KISSDB_ERROR_CLOSE_SHM: { DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING("KISSDB_close: "); DLT_STRING("Could not unmap shared memory object, retval=<"); DLT_INT(kissdb_state); DLT_STRING(">")); + DLT_STRING("KISSDB_close: "); DLT_STRING("Could not close shared memory object, retval=<"); DLT_INT(kdbState); DLT_STRING(">")); break; } - case KISSDB_ERROR_CLOSE_SHM: + default: { DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING("KISSDB_close: "); DLT_STRING("Could not close shared memory object, retval=<"); DLT_INT(kissdb_state); DLT_STRING(">")); + DLT_STRING("KISSDB_close: "); DLT_STRING("Could not close database, retval=<"); DLT_INT(kdbState); DLT_STRING(">")); break; } - default: - break; } returnValue = PERS_COM_FAILURE; } } + if (bLocked) + { (void) lldb_handles_Unlock(); + } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(handlerDB); DLT_STRING(" retval=<"); DLT_INT(returnValue); DLT_STRING(">")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(handlerDB); DLT_STRING(" retval=<"); DLT_INT(returnValue); DLT_STRING(">")); #ifdef __showTimeMeasurements clock_gettime(CLOCK_ID, &writeEnd); writeDuration += getNsDuration(&writebackStart, &writebackEnd); - printf("Writeback to flash duration for %s => %f ms\n", pLldbHandler->dbPathname, (double)((double)writeDuration/NANO2MIL)); + printf("Writeback to flash duration for %s => %f ms\n", + pLldbHandler->dbPathname, (double)((double)writeDuration/NANO2MIL)); KdbDuration += getNsDuration(&kdbStart, &kdbEnd); - printf("KISSDB_close duration for %s => %f ms\n", pLldbHandler->dbPathname, (double)((double)KdbDuration/NANO2MIL)); + printf("KISSDB_close duration for %s => %f ms\n", pLldbHandler->dbPathname, + (double)((double)KdbDuration/NANO2MIL)); duration += getNsDuration(&writeStart, &writeEnd); - printf("Overall Close duration for %s => %f ms\n", pLldbHandler->dbPathname, (double)((double)duration/NANO2MIL)); + printf("Overall Close duration for %s => %f ms\n", pLldbHandler->dbPathname, + (double)((double)duration/NANO2MIL)); #endif - return returnValue; -} - +#ifdef PFS_TEST + printf("END: pers_lldb_close for PID: %d \n", getpid()); +#endif + return returnValue; +} /** * \writeback cache of RCT key-value database @@ -385,48 +572,59 @@ sint_t pers_lldb_close(sint_t handlerDB) */ static sint_t writeBackKissRCT(KISSDB* db, lldb_handler_s* pLldbHandler) { - int kissdb_state = 0; + char* metaKey; + char* ptr; int idx = 0; - sint_t returnValue = PERS_COM_SUCCESS; - //lldb_handler_s* pLldbHandler = NIL; + int kdbState = 0; + int32_t bytesDeleted = 0; + int32_t bytesWritten = 0; pers_lldb_cache_flag_e eFlag; - char* ptr; qnobj_t obj; - char tmp_key[PERS_RCT_MAX_LENGTH_RESOURCE_ID]; + sint_t returnValue = PERS_COM_SUCCESS; + + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("START writeback for RCT: "), + DLT_STRING(pLldbHandler->dbPathname)); - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("START writeback for RCT: "), DLT_STRING(db->shmem_ht_name) ); + setMemoryAddress(db->sharedCache, db->tbl[0]); - while (db->tbl->getnext(db->tbl, &obj, &idx) == true) + while (db->tbl[0]->getnext(db->tbl[0], &obj, &idx) == true) { ptr = obj.data; eFlag = (pers_lldb_cache_flag_e) *(int*) ptr; ptr += 2 * (sizeof(int)); - (void) strncpy(tmp_key, obj.name, PERS_RCT_MAX_LENGTH_RESOURCE_ID); + metaKey = obj.name; //check how data should be persisted switch (eFlag) { case CachedDataDelete: //data must be deleted from file { - kissdb_state = KISSDB_delete(&pLldbHandler->kissDb, tmp_key); - if (kissdb_state != 0) + kdbState = KISSDB_delete(&pLldbHandler->kissDb, metaKey, &bytesDeleted); + if (kdbState != 0) { - if (kissdb_state == 1) + if (kdbState == 1) + { DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: RCT key=<"); DLT_STRING(tmp_key); DLT_STRING(">, "); DLT_STRING("not found in database file, retval=<"); DLT_INT(kissdb_state); DLT_STRING(">")); + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: RCT key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("not found in database file, retval=<"); DLT_INT(kdbState); + DLT_STRING(">")); + } else + { DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: RCT key=<");DLT_STRING(tmp_key); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(kissdb_state); DLT_STRING(">")); + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: RCT key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(kdbState); DLT_STRING(">")); + } } break; } case CachedDataWrite: //data must be written to file { - kissdb_state = KISSDB_put(&pLldbHandler->kissDb, tmp_key, ptr); - if (kissdb_state != 0) + kdbState = KISSDB_put(&pLldbHandler->kissDb, metaKey, ptr, sizeof(PersistenceConfigurationKey_s), &bytesWritten); + if (kdbState != 0) + { DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_put: RCT key=<");DLT_STRING(tmp_key); DLT_STRING(">, "); DLT_STRING("Error: Writing back to file failed with retval=<"); DLT_INT(kissdb_state); DLT_STRING(">")); + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_put: RCT key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("Writing back to file failed with retval=<"); + DLT_INT(kdbState); DLT_STRING(">")); + } break; } default: @@ -436,39 +634,40 @@ static sint_t writeBackKissRCT(KISSDB* db, lldb_handler_s* pLldbHandler) free(obj.data); } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("END writeback for RCT: "), DLT_STRING(db->shmem_ht_name) ); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("END writeback for RCT: "), + DLT_STRING(pLldbHandler->dbPathname)); return returnValue; } - - - /** - * \writeback cache of local DB key-value database - * \return 0 for success, negative value otherway (see pers_error_codes.h) + * Write back the data in cache to database file + * @param db + * @param pLldbHandler + * @return 0 for success, negative value otherway (see pers_error_codes.h) */ static sint_t writeBackKissDB(KISSDB* db, lldb_handler_s* pLldbHandler) { - int kissdb_state = 0; + char* metaKey; + char* ptr; + Data_LocalDB_s insert = {{0},0}; + int datasize = 0; int idx = 0; - sint_t returnValue = PERS_COM_SUCCESS; - //lldb_handler_s* pLldbHandler = NIL; + int kdbState = 0; + int32_t bytesDeleted = 0; + int32_t bytesWritten = 0; pers_lldb_cache_flag_e eFlag; - char* ptr; qnobj_t obj; + sint_t returnValue = PERS_COM_SUCCESS; - char tmp_key[PERS_DB_MAX_LENGTH_KEY_NAME]; - Data_LocalDB_s insert = { { 0 } }; - int datasize = 0; + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("START writeback for DB: "), + DLT_STRING(pLldbHandler->dbPathname)); - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("START writeback for DB: "), DLT_STRING(db->shmem_ht_name) ); + setMemoryAddress(db->sharedCache, db->tbl[0]); - while (db->tbl->getnext(db->tbl, &obj, &idx) == true) + while (db->tbl[0]->getnext(db->tbl[0], &obj, &idx) == true) { //get flag and datasize ptr = obj.data; @@ -476,7 +675,7 @@ static sint_t writeBackKissDB(KISSDB* db, lldb_handler_s* pLldbHandler) ptr += sizeof(int); datasize = *(int*) ptr; //pointer in obj.data to datasize ptr += sizeof(int); //pointer in obj.data to data - (void) strncpy(tmp_key, obj.name, PERS_DB_MAX_LENGTH_KEY_NAME); + metaKey = obj.name; //check how data should be persisted switch (eFlag) @@ -484,15 +683,21 @@ static sint_t writeBackKissDB(KISSDB* db, lldb_handler_s* pLldbHandler) case CachedDataDelete: //data must be deleted from file { //delete key-value pair from database file - kissdb_state = KISSDB_delete(&pLldbHandler->kissDb, tmp_key); - if (kissdb_state != 0) + kdbState = KISSDB_delete(&pLldbHandler->kissDb, metaKey, &bytesDeleted); + if (kdbState != 0) { - if (kissdb_state == 1) + if (kdbState == 1) + { DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: key=<"); DLT_STRING(tmp_key); DLT_STRING(">, "); DLT_STRING("not found in database file, retval=<"); DLT_INT(kissdb_state); DLT_STRING(">")); + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("not found in database file, retval=<"); DLT_INT(kdbState); + DLT_STRING(">")); + } else + { DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: key=<");DLT_STRING(tmp_key); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(kissdb_state); DLT_STRING(">"); DLT_STRING("Error Message: ");DLT_STRING(strerror(errno))); + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(kdbState); DLT_STRING(">"); + DLT_STRING("Error Message: "); DLT_STRING(strerror(errno))); + } } break; } @@ -500,11 +705,13 @@ static sint_t writeBackKissDB(KISSDB* db, lldb_handler_s* pLldbHandler) { (void) memcpy(insert.m_data, ptr, datasize); insert.m_dataSize = datasize; - - kissdb_state = KISSDB_put(&pLldbHandler->kissDb, tmp_key, &insert); //store data followed by datasize - if (kissdb_state != 0) + kdbState = KISSDB_put(&pLldbHandler->kissDb, metaKey, &insert, insert.m_dataSize, &bytesWritten); //store data followed by datasize + if (kdbState != 0) + { DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_put: key=<");DLT_STRING(tmp_key); DLT_STRING(">, "); DLT_STRING("Error: Writing back to file failed with retval=<"); DLT_INT(kissdb_state); DLT_STRING(">"); DLT_STRING("Error Message: ");DLT_STRING(strerror(errno))); + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_put: key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("Writing back to file failed with retval=<"); + DLT_INT(kdbState); DLT_STRING(">"); DLT_STRING("Error Message: "); DLT_STRING(strerror(errno))); + } break; } default: @@ -513,17 +720,12 @@ static sint_t writeBackKissDB(KISSDB* db, lldb_handler_s* pLldbHandler) free(obj.name); free(obj.data); } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("END writeback for DB: "), DLT_STRING(db->shmem_ht_name) ); + + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("END writeback for DB: "), + DLT_STRING(pLldbHandler->dbPathname)); return returnValue; } - - - - - - /** * \brief write a key-value pair into database * \note : DB type is identified from dbPathname (based on extension) @@ -537,15 +739,10 @@ static sint_t writeBackKissDB(KISSDB* db, lldb_handler_s* pLldbHandler) * * \return 0 for success, negative value otherway (see pers_error_codes.h) */ -sint_t pers_lldb_write_key(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t const * key, str_t const * data, - sint_t dataSize) +sint_t pers_lldb_write_key(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t const* key, str_t const* data, sint_t dataSize) { sint_t eErrorCode = PERS_COM_SUCCESS; - //int i =0; - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - // DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Datatest="); DLT_RAW(data,dataSize); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>"); DLT_STRING(" Data Size=<<"); DLT_INT(dataSize); DLT_STRING(">>...")); - switch (ePurpose) { case PersLldbPurpose_DB: @@ -555,7 +752,7 @@ sint_t pers_lldb_write_key(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t } case PersLldbPurpose_RCT: { - eErrorCode = SetDataInKissRCT(handlerDB, key, (PersistenceConfigurationKey_s const *) data); + eErrorCode = SetDataInKissRCT(handlerDB, key, (PersistenceConfigurationKey_s const*) data); break; } default: @@ -579,8 +776,7 @@ sint_t pers_lldb_write_key(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t * * \return read size, or negative value in case of error (see pers_error_codes.h) */ -sint_t pers_lldb_read_key(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t const * key, pstr_t dataBuffer_out, - sint_t bufSize) +sint_t pers_lldb_read_key(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t const* key, pstr_t dataBuffer_out, sint_t bufSize) { sint_t eErrorCode = PERS_COM_SUCCESS; @@ -614,7 +810,7 @@ sint_t pers_lldb_read_key(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t * \param key [in] key's name * \return size of the value corresponding to the key, or negative value in case of error (see pers_error_codes.h) */ -sint_t pers_lldb_get_key_size(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t const * key) +sint_t pers_lldb_get_key_size(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t const* key) { sint_t eErrorCode = PERS_COM_SUCCESS; @@ -645,7 +841,7 @@ sint_t pers_lldb_get_key_size(sint_t handlerDB, pers_lldb_purpose_e ePurpose, st * * \return 0 for success, negative value otherway (see pers_error_codes.h) */ -sint_t pers_lldb_delete_key(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t const * key) +sint_t pers_lldb_delete_key(sint_t handlerDB, pers_lldb_purpose_e ePurpose, str_t const* key) { sint_t eErrorCode = PERS_COM_SUCCESS; @@ -738,29 +934,19 @@ sint_t pers_lldb_get_keys_list(sint_t handlerDB, pers_lldb_purpose_e ePurpose, p break; } } - return eErrorCode; } -//TODO add write through compatibility static sint_t DeleteDataFromKissDB(sint_t dbHandler, pconststr_t key) { bool_t bCanContinue = true; - sint_t delete_size = PERS_COM_FAILURE; - lldb_handler_s* pLldbHandler = NIL; bool_t bLocked = false; - char m_data[sizeof(Data_LocalDB_s)] = {0}; - pers_lldb_cache_flag_e eFlag; - void *val; - char *ptr; - int status = PERS_COM_FAILURE; - int datasize = 0; - Kdb_bool not_found = Kdb_false; - size_t size = 0; - Data_Cached_s data_cached = { 0 }; + int kdbState = 0; + lldb_handler_s* pLldbHandler = NIL; + sint_t bytesDeleted = PERS_COM_FAILURE; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>...")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">")); if ((dbHandler >= 0) && (NIL != key)) { @@ -769,135 +955,69 @@ static sint_t DeleteDataFromKissDB(sint_t dbHandler, pconststr_t key) bLocked = true; pLldbHandler = lldb_handles_FindInUseHandle(dbHandler); if (NIL == pLldbHandler) + { bCanContinue = false; + } } } else + { bCanContinue = false; + } + if (bCanContinue) { - KISSDB* db = &pLldbHandler->kissDb; - - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - // DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Working on DB: "), DLT_STRING(db->shmem_ht_name) ); - - Kdb_wrlock(&db->shmem_info->cache_rwlock); - - char tmp_key[PERS_DB_MAX_LENGTH_KEY_NAME]; - (void) strncpy(tmp_key, key, PERS_DB_MAX_LENGTH_KEY_NAME); - data_cached.eFlag = CachedDataDelete; - data_cached.m_dataSize = 0; - - //if cache not already created - if (db->shmem_info->cache_initialised == Kdb_false) - { - if (createCache(db) != 0) - { - Kdb_unlock(&db->shmem_info->cache_rwlock); - return PERS_COM_FAILURE; - } - } - else //open cache + Kdb_wrlock(&pLldbHandler->kissDb.shared->rwlock); + if ( KISSDB_WRITE_MODE_WC == pLldbHandler->kissDb.shared->writeMode) { - if (openCache(db) != 0) - { - Kdb_unlock(&db->shmem_info->cache_rwlock); - return PERS_COM_FAILURE; - } + bytesDeleted = deleteFromCache(&pLldbHandler->kissDb, (char*) key); } - val = db->tbl->get(db->tbl, tmp_key, &size); - if (NULL != val) //check if key to be deleted is in Cache + else //write through { - ptr = val; - eFlag = (pers_lldb_cache_flag_e) *(int*) ptr; - ptr += sizeof(int); - datasize = *(int*) ptr; - //Mark data in cache as deleted - if (eFlag != CachedDataDelete) - { - if (db->tbl->put(db->tbl, tmp_key, &data_cached, sizeof(pers_lldb_cache_flag_e) + sizeof(int)) == false) //do not store any data - { - DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":");DLT_STRING("Failed to mark data in cache as deleted")); - delete_size = PERS_COM_ERR_NOT_FOUND; - not_found = Kdb_true; - } - else - delete_size = datasize; - } - } - else //check if key to be deleted is in database file - { - //get dataSize - status = KISSDB_get(&pLldbHandler->kissDb, tmp_key, m_data); - if (status == 0) + kdbState = KISSDB_delete(&pLldbHandler->kissDb, key, &bytesDeleted); + if (kdbState != 0) { - ptr = m_data; - ptr += PERS_DB_MAX_SIZE_KEY_DATA; - datasize = *(int*) ptr; - //put information about the key to be deleted in cache (deletion in file happens at system shutdown) - if (db->tbl->put(db->tbl, tmp_key, &data_cached, sizeof(pers_lldb_cache_flag_e) + sizeof(int)) == false) + if (kdbState == 1) { - DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":");DLT_STRING("Failed to mark existing data as deleted")); - delete_size = PERS_COM_ERR_NOT_FOUND; - } - else - delete_size = datasize; - } - else - { - if (status == 1) - { - not_found = Kdb_true; DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("not found, retval=<"); DLT_INT(status); DLT_STRING(">")); + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("not found in database file, retval=<"); DLT_INT(kdbState); + DLT_STRING(">")); } else { DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(tmp_key); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(status); DLT_STRING(">")); + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(kdbState); DLT_STRING(">"); + DLT_STRING("Error Message: "); DLT_STRING(strerror(errno))); } } } - - if (not_found == Kdb_true) //key not found, - delete_size = PERS_COM_ERR_NOT_FOUND; - Kdb_unlock(&db->shmem_info->cache_rwlock); + Kdb_unlock(&pLldbHandler->kissDb.shared->rwlock); } if (bLocked) + { (void) lldb_handles_Unlock(); + } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>, "); DLT_STRING("retval=<"); DLT_INT(delete_size); DLT_STRING(">")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("retval=<"); + DLT_INT(bytesDeleted); DLT_STRING(">")); - return delete_size; + return bytesDeleted; } - - -//TODO add write through compatibility static sint_t DeleteDataFromKissRCT(sint_t dbHandler, pconststr_t key) { bool_t bCanContinue = true; - sint_t delete_size = PERS_COM_FAILURE; - lldb_handler_s* pLldbHandler = NIL; bool_t bLocked = false; - char m_data[sizeof(Data_LocalDB_s)] = {0}; - pers_lldb_cache_flag_e eFlag; - void *val; - char *ptr; - int status = PERS_COM_FAILURE; - int datasize = 0; - Kdb_bool not_found = Kdb_false; - size_t size = 0; - Data_Cached_s data_cached = { 0 }; + int kdbState = 0; + lldb_handler_s* pLldbHandler = NIL; + sint_t bytesDeleted = PERS_COM_FAILURE; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>...")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">")); if ((dbHandler >= 0) && (NIL != key)) { @@ -906,123 +1026,67 @@ static sint_t DeleteDataFromKissRCT(sint_t dbHandler, pconststr_t key) bLocked = true; pLldbHandler = lldb_handles_FindInUseHandle(dbHandler); if (NIL == pLldbHandler) + { bCanContinue = false; + } } } else + { bCanContinue = false; + } if (bCanContinue) { - KISSDB* db = &pLldbHandler->kissDb; - - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - // DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Working on DB: "), DLT_STRING(db->shmem_ht_name) ); - - Kdb_wrlock(&db->shmem_info->cache_rwlock); - - char tmp_key[PERS_RCT_MAX_LENGTH_RESOURCE_ID]; - (void) strncpy(tmp_key, key, PERS_RCT_MAX_LENGTH_RESOURCE_ID); - data_cached.eFlag = CachedDataDelete; - data_cached.m_dataSize = 0; - - //if cache not already created - if (db->shmem_info->cache_initialised == Kdb_false) + Kdb_wrlock(&pLldbHandler->kissDb.shared->rwlock); + if ( KISSDB_WRITE_MODE_WC == pLldbHandler->kissDb.shared->writeMode) { - if (createCache(db) != 0) - { - Kdb_unlock(&db->shmem_info->cache_rwlock); - return PERS_COM_FAILURE; - } + bytesDeleted = deleteFromCache(&pLldbHandler->kissDb, (char*) key); } - else //open cache + else //write through { - if (openCache(db) != 0) - { - Kdb_unlock(&db->shmem_info->cache_rwlock); - return PERS_COM_FAILURE; - } - } - //get dataSize - val = db->tbl->get(db->tbl, tmp_key, &size); - if (NULL != val) //check if key to be deleted is in Cache - { - ptr = val; - eFlag = (pers_lldb_cache_flag_e) *(int*) ptr; - ptr += sizeof(int); - datasize = *(int*) ptr; - - //Mark data in cache as deleted - if (eFlag != CachedDataDelete) + kdbState = KISSDB_delete(&pLldbHandler->kissDb, key, &bytesDeleted); + if (kdbState != 0) { - if (db->tbl->put(db->tbl, tmp_key, &data_cached, sizeof(pers_lldb_cache_flag_e) + sizeof(int)) == false) + if (kdbState == 1) { - DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":");DLT_STRING("Failed to mark RCT data in cache as deleted")); - delete_size = PERS_COM_ERR_NOT_FOUND; - not_found = Kdb_true; + DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("not found in database file, retval=<"); DLT_INT(kdbState); + DLT_STRING(">")); } else - delete_size = datasize; - } - } - else //check if key to be deleted is in database file - { - status = KISSDB_get(&pLldbHandler->kissDb, tmp_key, m_data); - if (status == 0) - { - //Data to be deleted is not in cache, but was found in local database - //put information about the key to be deleted in cache (deletion in file happens at system shutdown) - if (db->tbl->put(db->tbl, tmp_key, &data_cached, sizeof(pers_lldb_cache_flag_e) + sizeof(int)) == false) { DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":");DLT_STRING("Failed to mark existing RCT data as deleted")); - delete_size = PERS_COM_ERR_NOT_FOUND; + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_delete: key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(kdbState); DLT_STRING(">"); + DLT_STRING("Error Message: "); DLT_STRING(strerror(errno))); } - else - delete_size = sizeof(PersistenceConfigurationKey_s); - } - else - { - if (status == 1) - { - not_found = Kdb_true; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("not found, retval=<"); DLT_INT(status); DLT_STRING(">")); - } - else - DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(status); DLT_STRING(">")); } } - if (not_found == Kdb_true) - delete_size = PERS_COM_ERR_NOT_FOUND; - - Kdb_unlock(&db->shmem_info->cache_rwlock); + Kdb_unlock(&pLldbHandler->kissDb.shared->rwlock); } if (bLocked) + { (void) lldb_handles_Unlock(); + } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>, "); DLT_STRING("retval=<"); DLT_INT(delete_size); DLT_STRING(">")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("handlerDB="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("retval=<"); + DLT_INT(bytesDeleted); DLT_STRING(">")); - return delete_size; + return bytesDeleted; } - - -//TODO add write through compatibility static sint_t GetAllKeysFromKissLocalDB(sint_t dbHandler, pstr_t buffer, sint_t size) { bool_t bCanContinue = true; - sint_t result = 0; + bool_t bLocked = false; bool_t bOnlySizeNeeded = (NIL == buffer); lldb_handler_s* pLldbHandler = NIL; - bool_t bLocked = false; + sint_t result = 0; - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - //DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("buffer="); DLT_UINT((uint_t)buffer); DLT_STRING("size="); DLT_INT(size); DLT_STRING("...")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("buffer="); DLT_UINT((uint_t)buffer); DLT_STRING("size="); DLT_INT(size)); if (dbHandler >= 0) { @@ -1038,7 +1102,8 @@ static sint_t GetAllKeysFromKissLocalDB(sint_t dbHandler, pstr_t buffer, sint_t else { if (PersLldbPurpose_DB != pLldbHandler->ePurpose) - {/* this would be very bad */ + { + /* this would be very bad */ bCanContinue = false; result = PERS_COM_FAILURE; } @@ -1055,34 +1120,38 @@ static sint_t GetAllKeysFromKissLocalDB(sint_t dbHandler, pstr_t buffer, sint_t if (bCanContinue) { if ((buffer != NIL) && (size > 0)) + { (void) memset(buffer, 0, (size_t) size); + } - Kdb_wrlock(&pLldbHandler->kissDb.shmem_info->cache_rwlock); + Kdb_wrlock(&pLldbHandler->kissDb.shared->rwlock); result = getListandSize(&pLldbHandler->kissDb, buffer, size, bOnlySizeNeeded, PersLldbPurpose_DB); - Kdb_unlock(&pLldbHandler->kissDb.shmem_info->cache_rwlock); + Kdb_unlock(&pLldbHandler->kissDb.shared->rwlock); if (result < 0) + { result = PERS_COM_FAILURE; + } } if (bLocked) + { (void) lldb_handles_Unlock(); + } - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - //DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("retval=<"); DLT_INT(result); DLT_STRING(">")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("retval=<"); DLT_INT(result); DLT_STRING(">")); return result; } - -//TODO add write through compatibility static sint_t GetAllKeysFromKissRCT(sint_t dbHandler, pstr_t buffer, sint_t size) { bool_t bCanContinue = true; - sint_t result = 0; + bool_t bLocked = false; bool_t bOnlySizeNeeded = (NIL == buffer); lldb_handler_s* pLldbHandler = NIL; - bool_t bLocked = false; + sint_t result = 0; - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - //DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("buffer="); DLT_UINT((uint_t)buffer); DLT_STRING("size="); DLT_INT(size); DLT_STRING("...")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("buffer="); DLT_UINT((uint_t)buffer); DLT_STRING("size="); DLT_INT(size)); if (dbHandler >= 0) { @@ -1098,7 +1167,8 @@ static sint_t GetAllKeysFromKissRCT(sint_t dbHandler, pstr_t buffer, sint_t size else { if (PersLldbPurpose_RCT != pLldbHandler->ePurpose) - {/* this would be very bad */ + { + /* this would be very bad */ bCanContinue = false; result = PERS_COM_FAILURE; } @@ -1115,34 +1185,41 @@ static sint_t GetAllKeysFromKissRCT(sint_t dbHandler, pstr_t buffer, sint_t size if (bCanContinue) { if ((buffer != NIL) && (size > 0)) + { (void) memset(buffer, 0, (size_t) size); - Kdb_wrlock(&pLldbHandler->kissDb.shmem_info->cache_rwlock); + } + Kdb_wrlock(&pLldbHandler->kissDb.shared->rwlock); result = getListandSize(&pLldbHandler->kissDb, buffer, size, bOnlySizeNeeded, PersLldbPurpose_RCT); - Kdb_unlock(&pLldbHandler->kissDb.shmem_info->cache_rwlock); + Kdb_unlock(&pLldbHandler->kissDb.shared->rwlock); if (result < 0) + { result = PERS_COM_FAILURE; + } } if (bLocked) + { (void) lldb_handles_Unlock(); + } - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - //DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("retval=<"); DLT_INT(result); DLT_STRING(">")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("retval=<"); DLT_INT(result); DLT_STRING(">")); return result; } - -//TODO add write through compatibility static sint_t SetDataInKissLocalDB(sint_t dbHandler, pconststr_t key, pconststr_t data, sint_t dataSize) { bool_t bCanContinue = true; - sint_t size_written = PERS_COM_FAILURE; - lldb_handler_s* pLldbHandler = NIL; bool_t bLocked = false; - Data_Cached_s data_cached = { 0 }; + Data_Cached_s dataCached = { 0 }; + int kdbState = 0; + lldb_handler_s* pLldbHandler = NIL; + sint_t bytesWritten = PERS_COM_FAILURE; + - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>, "); DLT_STRING("size<<"); DLT_INT(dataSize); DLT_STRING(">> ...")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("size<"); + DLT_INT(dataSize); DLT_STRING(">")); if ((dbHandler >= 0) && (NIL != key) && (NIL != data) && (dataSize > 0)) { @@ -1153,14 +1230,15 @@ static sint_t SetDataInKissLocalDB(sint_t dbHandler, pconststr_t key, pconststr_ if (NIL == pLldbHandler) { bCanContinue = false; - size_written = PERS_COM_ERR_INVALID_PARAM; + bytesWritten = PERS_COM_ERR_INVALID_PARAM; } else { if (PersLldbPurpose_DB != pLldbHandler->ePurpose) - {/* this would be very bad */ + { + /* this would be very bad */ bCanContinue = false; - size_written = PERS_COM_FAILURE; + bytesWritten = PERS_COM_FAILURE; } } } @@ -1168,43 +1246,59 @@ static sint_t SetDataInKissLocalDB(sint_t dbHandler, pconststr_t key, pconststr_ else { bCanContinue = false; - size_written = PERS_COM_ERR_INVALID_PARAM; + bytesWritten = PERS_COM_ERR_INVALID_PARAM; } if (bCanContinue) { - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - // DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Working on DB: "), DLT_STRING(pLldbHandler->dbPathname) ); + char* metaKey = (char*) key; + dataCached.eFlag = CachedDataWrite; + dataCached.m_dataSize = dataSize; + (void) memcpy(dataCached.m_data, data, (size_t) dataSize); - //TODO add write through (call KissDB_put) - char tmp_key[PERS_DB_MAX_LENGTH_KEY_NAME]; - (void) strncpy(tmp_key, key, PERS_DB_MAX_LENGTH_KEY_NAME); - data_cached.eFlag = CachedDataWrite; - data_cached.m_dataSize = dataSize; - (void) memcpy(data_cached.m_data, data, (size_t) dataSize); - size_written = putToCache(&pLldbHandler->kissDb, dataSize, (char*) &tmp_key, &data_cached); + Kdb_wrlock(&pLldbHandler->kissDb.shared->rwlock); + if ( KISSDB_WRITE_MODE_WC == pLldbHandler->kissDb.shared->writeMode) + { + bytesWritten = putToCache(&pLldbHandler->kissDb, dataSize, (char*) metaKey, &dataCached); + } + else + { + if (KISSDB_OPEN_MODE_RDONLY != pLldbHandler->kissDb.shared->openMode) + { + kdbState = KISSDB_put(&pLldbHandler->kissDb, metaKey, dataCached.m_data, dataCached.m_dataSize, &bytesWritten); + if (kdbState != 0) + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_put: key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("WriteThrough to file failed with retval=<"); DLT_INT(bytesWritten); DLT_STRING(">")); + } + } + } + Kdb_unlock(&pLldbHandler->kissDb.shared->rwlock); } if (bLocked) + { (void) lldb_handles_Unlock(); + } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>, "); DLT_STRING("size<<"); DLT_INT(dataSize); DLT_STRING(">>, "); DLT_STRING("retval=<"); DLT_INT(size_written); DLT_STRING(">")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("size<"); + DLT_INT(dataSize); DLT_STRING(">, "); DLT_STRING("retval=<"); DLT_INT(bytesWritten); DLT_STRING(">")); - return size_written; + return bytesWritten; } -//TODO add write through compatibility -static sint_t SetDataInKissRCT(sint_t dbHandler, pconststr_t key, PersistenceConfigurationKey_s const * pConfig) +static sint_t SetDataInKissRCT(sint_t dbHandler, pconststr_t key, PersistenceConfigurationKey_s const* pConfig) { bool_t bCanContinue = true; - sint_t size_written = PERS_COM_FAILURE; - lldb_handler_s* pLldbHandler = NIL; bool_t bLocked = false; - Data_Cached_RCT_s data_cached = { 0 }; + Data_Cached_RCT_s dataCached = { 0 }; + int kdbState = 0; + lldb_handler_s* pLldbHandler = NIL; + sint_t bytesWritten = PERS_COM_FAILURE; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>...")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">")); if ((dbHandler >= 0) && (NIL != key) && (NIL != pConfig)) { @@ -1215,14 +1309,15 @@ static sint_t SetDataInKissRCT(sint_t dbHandler, pconststr_t key, PersistenceCon if (NIL == pLldbHandler) { bCanContinue = false; - size_written = PERS_COM_ERR_INVALID_PARAM; + bytesWritten = PERS_COM_ERR_INVALID_PARAM; } else { if (PersLldbPurpose_RCT != pLldbHandler->ePurpose) - {/* this would be very bad */ + { + /* this would be very bad */ bCanContinue = false; - size_written = PERS_COM_FAILURE; + bytesWritten = PERS_COM_FAILURE; } /* to not use DLT while mutex locked */ } @@ -1231,42 +1326,59 @@ static sint_t SetDataInKissRCT(sint_t dbHandler, pconststr_t key, PersistenceCon else { bCanContinue = false; - size_written = PERS_COM_ERR_INVALID_PARAM; + bytesWritten = PERS_COM_ERR_INVALID_PARAM; } if (bCanContinue) { - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - // DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Working on DB: "), DLT_STRING(pLldbHandler->dbPathname) ); - - //TODO add RCT write through (call KissDB_put) int dataSize = sizeof(PersistenceConfigurationKey_s); - char tmp_key[PERS_RCT_MAX_LENGTH_RESOURCE_ID]; - (void) strncpy(tmp_key, key, PERS_RCT_MAX_LENGTH_RESOURCE_ID); - data_cached.eFlag = CachedDataWrite; - data_cached.m_dataSize = dataSize; - (void) memcpy(data_cached.m_data, pConfig, (size_t) dataSize); - size_written = putToCache(&pLldbHandler->kissDb, dataSize, (char*) &tmp_key, &data_cached); + char* metaKey = (char*) key; + dataCached.eFlag = CachedDataWrite; + dataCached.m_dataSize = dataSize; + (void) memcpy(dataCached.m_data, pConfig, (size_t) dataSize); + + + Kdb_wrlock(&pLldbHandler->kissDb.shared->rwlock); + if ( KISSDB_WRITE_MODE_WC == pLldbHandler->kissDb.shared->writeMode) + { + bytesWritten = putToCache(&pLldbHandler->kissDb, dataSize, (char*) metaKey, &dataCached); + } + else + { + + if (KISSDB_OPEN_MODE_RDONLY != pLldbHandler->kissDb.shared->openMode) + { + kdbState = KISSDB_put(&pLldbHandler->kissDb, metaKey, dataCached.m_data, dataCached.m_dataSize, &bytesWritten); + if (kdbState != 0) + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_put: RCT key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("WriteThrough to file failed with retval=<"); DLT_INT(bytesWritten); DLT_STRING(">")); + } + } + } + Kdb_unlock(&pLldbHandler->kissDb.shared->rwlock); + } if (bLocked) + { (void) lldb_handles_Unlock(); + } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>, "); DLT_STRING("retval=<"); DLT_INT(size_written); DLT_STRING(">")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("retval=<"); + DLT_INT(bytesWritten); DLT_STRING(">")); - return size_written; + return bytesWritten; } - -//TODO add write through compatibility static sint_t GetKeySizeFromKissLocalDB(sint_t dbHandler, pconststr_t key) { bool_t bCanContinue = true; - sint_t size_read = PERS_COM_FAILURE; - lldb_handler_s* pLldbHandler = NIL; bool_t bLocked = false; + lldb_handler_s* pLldbHandler = NIL; + sint_t bytesRead = PERS_COM_FAILURE; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">> ...")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">")); if ((dbHandler >= 0) && (NIL != key)) { @@ -1277,14 +1389,15 @@ static sint_t GetKeySizeFromKissLocalDB(sint_t dbHandler, pconststr_t key) if (NIL == pLldbHandler) { bCanContinue = false; - size_read = PERS_COM_ERR_INVALID_PARAM; + bytesRead = PERS_COM_ERR_INVALID_PARAM; } else { if (PersLldbPurpose_DB != pLldbHandler->ePurpose) - {/* this would be very bad */ + { + /* this would be very bad */ bCanContinue = false; - size_read = PERS_COM_FAILURE; + bytesRead = PERS_COM_FAILURE; } } } @@ -1292,38 +1405,47 @@ static sint_t GetKeySizeFromKissLocalDB(sint_t dbHandler, pconststr_t key) else { bCanContinue = false; - size_read = PERS_COM_ERR_INVALID_PARAM; + bytesRead = PERS_COM_ERR_INVALID_PARAM; } if (bCanContinue) { - char tmp_key[PERS_DB_MAX_LENGTH_KEY_NAME]; - (void) strncpy(tmp_key, key, PERS_DB_MAX_LENGTH_KEY_NAME); - size_read = getFromCache(&pLldbHandler->kissDb, &tmp_key, NULL, 0, true); - if (size_read == PERS_STATUS_KEY_NOT_IN_CACHE) - size_read = getFromDatabaseFile(&pLldbHandler->kissDb, &tmp_key, NULL, PersLldbPurpose_DB, 0, true); + Kdb_wrlock(&pLldbHandler->kissDb.shared->rwlock); + if ( KISSDB_WRITE_MODE_WC == pLldbHandler->kissDb.shared->writeMode) + { + bytesRead = getFromCache(&pLldbHandler->kissDb, (char*) key, NULL, 0, true); + if (bytesRead == PERS_STATUS_KEY_NOT_IN_CACHE) + { + bytesRead = getFromDatabaseFile(&pLldbHandler->kissDb, (char*) key, NULL, 0); + } + } + else + { + bytesRead = getFromDatabaseFile(&pLldbHandler->kissDb, (char*) key, NULL, 0); + } + Kdb_unlock(&pLldbHandler->kissDb.shared->rwlock); } if (bLocked) + { (void) lldb_handles_Unlock(); - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>, "); DLT_STRING("retval=<"); DLT_INT(size_read); DLT_STRING(">")); + } + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("retval=<"); + DLT_INT(bytesRead); DLT_STRING(">")); - return size_read; + return bytesRead; } - - - -//TODO add write through compatibility /* return no of bytes read, or negative value in case of error */ static sint_t GetDataFromKissLocalDB(sint_t dbHandler, pconststr_t key, pstr_t buffer_out, sint_t bufSize) { bool_t bCanContinue = true; - sint_t size_read = PERS_COM_FAILURE; - lldb_handler_s* pLldbHandler = NIL; bool_t bLocked = false; + lldb_handler_s* pLldbHandler = NIL; + sint_t bytesRead = PERS_COM_FAILURE; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>, "); DLT_STRING("bufsize=<<"); DLT_INT(bufSize); DLT_STRING(">> ... ")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("bufsize=<"); + DLT_INT(bufSize); DLT_STRING(">")); if ((dbHandler >= 0) && (NIL != key) && (NIL != buffer_out) && (bufSize > 0)) { @@ -1334,14 +1456,15 @@ static sint_t GetDataFromKissLocalDB(sint_t dbHandler, pconststr_t key, pstr_t b if (NIL == pLldbHandler) { bCanContinue = false; - size_read = PERS_COM_ERR_INVALID_PARAM; + bytesRead = PERS_COM_ERR_INVALID_PARAM; } else { if (PersLldbPurpose_DB != pLldbHandler->ePurpose) - {/* this would be very bad */ + { + /* this would be very bad */ bCanContinue = false; - size_read = PERS_COM_FAILURE; + bytesRead = PERS_COM_FAILURE; } /* to not use DLT while mutex locked */ } @@ -1350,42 +1473,47 @@ static sint_t GetDataFromKissLocalDB(sint_t dbHandler, pconststr_t key, pstr_t b else { bCanContinue = false; - size_read = PERS_COM_ERR_INVALID_PARAM; + bytesRead = PERS_COM_ERR_INVALID_PARAM; } if (bCanContinue) { - - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - // DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Working on DB: "), DLT_STRING(pLldbHandler->dbPathname) ); - - char tmp_key[PERS_DB_MAX_LENGTH_KEY_NAME]; - (void) strncpy(tmp_key, key, PERS_DB_MAX_LENGTH_KEY_NAME); - size_read = getFromCache(&pLldbHandler->kissDb, &tmp_key, buffer_out, bufSize, false); - //if key is not already in cache - if (size_read == PERS_STATUS_KEY_NOT_IN_CACHE) - size_read = getFromDatabaseFile(&pLldbHandler->kissDb, &tmp_key, buffer_out, PersLldbPurpose_DB, bufSize, - false); + Kdb_wrlock(&pLldbHandler->kissDb.shared->rwlock); + if ( KISSDB_WRITE_MODE_WC == pLldbHandler->kissDb.shared->writeMode) + { + bytesRead = getFromCache(&pLldbHandler->kissDb, (char*) key, buffer_out, bufSize, false); + //if key is not already in cache + if (bytesRead == PERS_STATUS_KEY_NOT_IN_CACHE) + { + bytesRead = getFromDatabaseFile(&pLldbHandler->kissDb, (char*) key, buffer_out, bufSize); + } + } + else //write through mode -> only read from file + { + bytesRead = getFromDatabaseFile(&pLldbHandler->kissDb, (char*) key, buffer_out, bufSize); + } + Kdb_unlock(&pLldbHandler->kissDb.shared->rwlock); } if (bLocked) + { (void) lldb_handles_Unlock(); + } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>, "); DLT_STRING("bufsize=<<"); DLT_INT(bufSize); DLT_STRING(">>, "); DLT_STRING("retval=<"); DLT_INT(size_read); DLT_STRING(">")); - return size_read; + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("bufsize=<"); + DLT_INT(bufSize); DLT_STRING(">, "); DLT_STRING("retval=<"); DLT_INT(bytesRead); DLT_STRING(">")); + return bytesRead; } - -//TODO add write through compatibility static sint_t GetDataFromKissRCT(sint_t dbHandler, pconststr_t key, PersistenceConfigurationKey_s* pConfig) { bool_t bCanContinue = true; - sint_t size_read = PERS_COM_FAILURE; - lldb_handler_s* pLldbHandler = NIL; bool_t bLocked = false; + lldb_handler_s* pLldbHandler = NIL; + sint_t bytesRead = PERS_COM_FAILURE; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">> ...")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">")); if ((dbHandler >= 0) && (NIL != key) && (NIL != pConfig)) { @@ -1396,14 +1524,15 @@ static sint_t GetDataFromKissRCT(sint_t dbHandler, pconststr_t key, PersistenceC if (NIL == pLldbHandler) { bCanContinue = false; - size_read = PERS_COM_ERR_INVALID_PARAM; + bytesRead = PERS_COM_ERR_INVALID_PARAM; } else { if (PersLldbPurpose_RCT != pLldbHandler->ePurpose) - {/* this would be very bad */ + { + /* this would be very bad */ bCanContinue = false; - size_read = PERS_COM_FAILURE; + bytesRead = PERS_COM_FAILURE; } /* to not use DLT while mutex locked */ } @@ -1412,34 +1541,39 @@ static sint_t GetDataFromKissRCT(sint_t dbHandler, pconststr_t key, PersistenceC else { bCanContinue = false; - size_read = PERS_COM_ERR_INVALID_PARAM; + bytesRead = PERS_COM_ERR_INVALID_PARAM; } //read RCT if (bCanContinue) { - //DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - // DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Working on DB: "), DLT_STRING(pLldbHandler->dbPathname) ); - - char tmp_key[PERS_RCT_MAX_LENGTH_RESOURCE_ID]; - (void) strncpy(tmp_key, key, PERS_RCT_MAX_LENGTH_RESOURCE_ID); - - size_read = getFromCache(&pLldbHandler->kissDb, &tmp_key, pConfig, sizeof(PersistenceConfigurationKey_s), false); - if (size_read == PERS_STATUS_KEY_NOT_IN_CACHE) - size_read = getFromDatabaseFile(&pLldbHandler->kissDb, &tmp_key, pConfig, PersLldbPurpose_RCT, - sizeof(PersistenceConfigurationKey_s), false); + Kdb_wrlock(&pLldbHandler->kissDb.shared->rwlock); + if ( KISSDB_WRITE_MODE_WC == pLldbHandler->kissDb.shared->writeMode) + { + bytesRead = getFromCache(&pLldbHandler->kissDb, (char*) key, pConfig, sizeof(PersistenceConfigurationKey_s), false); + if (bytesRead == PERS_STATUS_KEY_NOT_IN_CACHE) + { + bytesRead = getFromDatabaseFile(&pLldbHandler->kissDb, (char*) key, pConfig, sizeof(PersistenceConfigurationKey_s)); + } + } + else + { + bytesRead = getFromDatabaseFile(&pLldbHandler->kissDb, (char*) key, pConfig, sizeof(PersistenceConfigurationKey_s)); + } + Kdb_unlock(&pLldbHandler->kissDb.shared->rwlock); } if (bLocked) + { (void) lldb_handles_Unlock(); + } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<<"); DLT_STRING(key); DLT_STRING(">>, "); DLT_STRING("retval=<"); DLT_INT(size_read); DLT_STRING(">")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler="); DLT_INT(dbHandler); DLT_STRING("key=<"); DLT_STRING(key); DLT_STRING(">, "); DLT_STRING("retval=<"); + DLT_INT(bytesRead); DLT_STRING(">")); - return size_read; + return bytesRead; } - - static bool_t lldb_handles_Lock(void) { bool_t bEverythingOK = true; @@ -1448,7 +1582,7 @@ static bool_t lldb_handles_Lock(void) { bEverythingOK = false; DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("pthread_mutex_lock failed with error=<"); DLT_INT(siErr); DLT_STRING(">")); + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("pthread_mutex_lock failed with error=<"); DLT_INT(siErr); DLT_STRING(">")); } return bEverythingOK; } @@ -1462,7 +1596,7 @@ static bool_t lldb_handles_Unlock(void) { bEverythingOK = false; DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("pthread_mutex_unlock failed with error=<"); DLT_INT(siErr); DLT_STRING(">")); + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("pthread_mutex_unlock failed with error=<"); DLT_INT(siErr); DLT_STRING(">")); } return bEverythingOK; } @@ -1493,8 +1627,9 @@ static lldb_handler_s* lldb_handles_FindInUseHandle(sint_t dbHandler) } } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING((NIL!=pHandler) ? "Found handler <" : "ERROR can't find handler <"); DLT_INT(dbHandler); DLT_STRING(">"); DLT_STRING((NIL!=pHandler) ? (dbHandler <= PERS_LLDB_MAX_STATIC_HANDLES ? "in static area" : "in dynamic list") : "")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING((NIL!=pHandler) ? "Found handler <" : "ERROR can't find handler <"); DLT_INT(dbHandler); DLT_STRING(">"); + DLT_STRING((NIL!=pHandler) ? (dbHandler <= PERS_LLDB_MAX_STATIC_HANDLES ? "in static area" : "in dynamic list") : "")); return pHandler; } @@ -1511,6 +1646,7 @@ static lldb_handler_s* lldb_handles_FindAvailableHandle(void) { if (!g_sHandlers.asStaticHandles[siIndex].bIsAssigned) { + //INITIALIZE KISSDB struct /* index setting should be done only once at the initialization of the static array * Anyway, doing it here is more consistent */ g_sHandlers.asStaticHandles[siIndex].dbHandler = siIndex; @@ -1529,8 +1665,7 @@ static lldb_handler_s* lldb_handles_FindAvailableHandle(void) if (NIL == psListElemNew) { bCanContinue = false; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("malloc failed")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("malloc failed")); } else { @@ -1597,14 +1732,15 @@ static lldb_handler_s* lldb_handles_FindAvailableHandle(void) } } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING((NIL!=pHandler) ? "Found availble handler <" : "ERROR can't find available handler <"); DLT_INT((NIL!=pHandler) ? pHandler->dbHandler : (-1)); DLT_STRING(">"); DLT_STRING((NIL!=pHandler) ? (pHandler->dbHandler <= PERS_LLDB_MAX_STATIC_HANDLES ? "in static area" : "in dynamic list") : "")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING((NIL!=pHandler) ? "Found availble handler <" : "ERROR can't find available handler <"); + DLT_INT((NIL!=pHandler) ? pHandler->dbHandler : (-1)); DLT_STRING(">"); + DLT_STRING((NIL!=pHandler) ? (pHandler->dbHandler <= PERS_LLDB_MAX_STATIC_HANDLES ? "in static area" : "in dynamic list") : "")); return pHandler; } -static void lldb_handles_InitHandle(lldb_handler_s* psHandle_inout, pers_lldb_purpose_e ePurpose, - str_t const * dbPathname) +static void lldb_handles_InitHandle(lldb_handler_s* psHandle_inout, pers_lldb_purpose_e ePurpose, str_t const* dbPathname) { psHandle_inout->bIsAssigned = true; psHandle_inout->ePurpose = ePurpose; @@ -1669,43 +1805,40 @@ static bool_t lldb_handles_DeinitHandle(sint_t dbHandler) } } - DLT_LOG(persComLldbDLTCtx, DLT_LOG_INFO, - DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler=<"); DLT_INT(dbHandler); DLT_STRING("> "); DLT_STRING(bEverythingOK ? (dbHandler <= PERS_LLDB_MAX_STATIC_HANDLES ? "deinit handler in static area" : "deinit handler in dynamic list") : "ERROR - handler not found")); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(LT_HDR); DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("dbHandler=<"); DLT_INT(dbHandler); DLT_STRING("> "); + DLT_STRING(bEverythingOK ? (dbHandler <= PERS_LLDB_MAX_STATIC_HANDLES ? "deinit handler in static area" : "deinit handler in dynamic list") : "ERROR - handler not found")); return bEverythingOK; } - - -sint_t getFromCache(KISSDB* db, void* tmp_key, void* readBuffer, sint_t bufsize, bool_t sizeOnly) +sint_t getFromCache(KISSDB* db, void* metaKey, void* readBuffer, sint_t bufsize, bool_t sizeOnly) { - Kdb_bool cache_empty, key_deleted, key_not_found; - key_deleted = cache_empty = key_not_found = Kdb_false; - sint_t size_read = 0; - size_t size = 0; - int datasize = 0; char* ptr; + int datasize = 0; + Kdb_bool cacheEmpty, keyDeleted, keyNotFound; pers_lldb_cache_flag_e eFlag; + sint_t bytesRead = 0; + size_t size = 0; + void* val; - //if cache not already created - Kdb_wrlock(&db->shmem_info->cache_rwlock); + keyDeleted = cacheEmpty = keyNotFound = Kdb_false; - if (db->shmem_info->cache_initialised == Kdb_true) + //if cache already created + if (db->shared->cacheCreated == Kdb_true) { - //open existing cache in existing shared memory - if (db->shmem_cached_fd <= 0) + if (openCache(db) != 0) { - if (openCache(db) != 0) - { - Kdb_unlock(&db->shmem_info->cache_rwlock); - return PERS_COM_FAILURE; - } + return PERS_COM_FAILURE; } - void* val = db->tbl->get(db->tbl, tmp_key, &size); + + setMemoryAddress(db->sharedCache, db->tbl[0]); + + val = db->tbl[0]->get(db->tbl[0], metaKey, &size); if (val == NULL) { - size_read = PERS_COM_ERR_NOT_FOUND; - key_not_found = Kdb_true; + bytesRead = PERS_COM_ERR_NOT_FOUND; + keyNotFound = Kdb_true; } else { @@ -1719,112 +1852,89 @@ sint_t getFromCache(KISSDB* db, void* tmp_key, void* readBuffer, sint_t bufsize, ptr = ptr + sizeof(pers_lldb_cache_flag_e); datasize = *(int*) ptr; ptr = ptr + sizeof(int); //move pointer to beginning of data - size_read = datasize; + bytesRead = datasize; //get data if needed if (!sizeOnly) { if (bufsize < datasize) { - Kdb_unlock(&db->shmem_info->cache_rwlock); return PERS_COM_FAILURE; } else + { (void) memcpy(readBuffer, ptr, datasize); + } } } else { - size_read = PERS_COM_ERR_NOT_FOUND; - key_deleted = Kdb_true; + bytesRead = PERS_COM_ERR_NOT_FOUND; + keyDeleted = Kdb_true; } free(val); } } else - cache_empty = Kdb_true; + { + cacheEmpty = Kdb_true; + } //only read from file if key was not found in cache and if key was not marked as deleted in cache - if ((cache_empty == Kdb_true && key_deleted == Kdb_false) || key_not_found == Kdb_true) + if ((cacheEmpty == Kdb_true && keyDeleted == Kdb_false) || keyNotFound == Kdb_true) { - Kdb_unlock(&db->shmem_info->cache_rwlock); return PERS_STATUS_KEY_NOT_IN_CACHE; //key not found in cache } else { - Kdb_unlock(&db->shmem_info->cache_rwlock); - return size_read; + return bytesRead; } } - - -sint_t getFromDatabaseFile(KISSDB* db, void* tmp_key, void* readBuffer, pers_lldb_purpose_e purpose, sint_t bufsize, - bool_t sizeOnly) +sint_t getFromDatabaseFile(KISSDB* db, void* metaKey, void* readBuffer, sint_t bufsize) { - sint_t size_read = 0; - int datasize = 0; - char* ptr; - char m_data[sizeof(Data_LocalDB_s)] = { 0 }; //temporary buffer that gets filled with read in KISSDB_get + int kdbState = 0; + sint_t bytesRead = 0; + uint32_t size = 0; - int kissdb_status = KISSDB_get(db, tmp_key, m_data); - if (kissdb_status == 0) + kdbState = KISSDB_get(db, metaKey, readBuffer, bufsize, &size); + if (kdbState == 0) { - if (purpose == PersLldbPurpose_DB) - { - ptr = m_data; - ptr += PERS_DB_MAX_SIZE_KEY_DATA; - datasize = *(int*) ptr; - if (!sizeOnly) - { - if (bufsize < datasize) - return PERS_COM_FAILURE; - else - (void) memcpy(readBuffer, m_data, datasize); - } - size_read = datasize; - } - else - { - if (!sizeOnly) - { - if (bufsize < datasize) - return PERS_COM_FAILURE; - else - (void) memcpy(readBuffer, m_data, sizeof(PersistenceConfigurationKey_s)); - } - size_read = sizeof(PersistenceConfigurationKey_s); - } + bytesRead = size; } else { - if (kissdb_status == 1) + if (kdbState == 1) { DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(tmp_key); DLT_STRING(">, "); DLT_STRING("not found, retval=<"); DLT_INT(kissdb_status); DLT_STRING(">")); - size_read = PERS_COM_ERR_NOT_FOUND; + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("not found, retval=<"); DLT_INT(kdbState); DLT_STRING(">")); + bytesRead = PERS_COM_ERR_NOT_FOUND; } else { DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(tmp_key); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(kissdb_status); DLT_STRING(">")); + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("Error with retval=<"); DLT_INT(kdbState); DLT_STRING(">")); } - size_read = PERS_COM_ERR_NOT_FOUND; + bytesRead = PERS_COM_ERR_NOT_FOUND; } - return size_read; + return bytesRead; } -sint_t putToCache(KISSDB* db, sint_t dataSize, char* tmp_key, void* insert_cached_data) +sint_t putToCache(KISSDB* db, sint_t dataSize, char* metaKey, void* cachedData) { - sint_t size_written = 0; - Kdb_wrlock(&db->shmem_info->cache_rwlock); + sint_t bytesWritten = 0; + + //DO NOT ALLOW WRITING TO CACHE IF DATABASE IS OPENED IN READONLY MODE + if(KISSDB_OPEN_MODE_RDONLY == db->shared->openMode ) + { + return PERS_COM_ERR_READONLY; + } //if cache not already created - if (db->shmem_info->cache_initialised == Kdb_false) + if (db->shared->cacheCreated == Kdb_false) { if (createCache(db) != 0) { - Kdb_unlock(&db->shmem_info->cache_rwlock); return PERS_COM_FAILURE; } } @@ -1832,110 +1942,295 @@ sint_t putToCache(KISSDB* db, sint_t dataSize, char* tmp_key, void* insert_cache { if (openCache(db) != 0) { - Kdb_unlock(&db->shmem_info->cache_rwlock); return PERS_COM_FAILURE; } } - + // update db->sharedCache pointer (process adress range mapping can be different after remap) (use + //printf("setMemoryAddress(db->sharedCache, db->tbl[0] = %p \n", db->sharedCache ); + setMemoryAddress(db->sharedCache, db->tbl[0]); //address to first hashtable //put in cache - if (db->tbl->put(db->tbl, tmp_key, insert_cached_data, - sizeof(pers_lldb_cache_flag_e) + sizeof(int) + (size_t) dataSize) == false) //store flag , datasize and data as value in cache + if (db->tbl[0]->put(db->tbl[0], metaKey, cachedData, sizeof(pers_lldb_cache_flag_e) + sizeof(int) + (size_t) dataSize) == + false) //store flag , datasize and data as value in cache { - size_written = PERS_COM_FAILURE; - DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":");DLT_STRING("Error: Failed to put data into cache")); + bytesWritten = PERS_COM_FAILURE; + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Failed to put data into cache: "); DLT_STRING(strerror(errno))); + +#if 0 + int k = 0; + //if additional caches were created by another process -> get referemnce to them and init the local tbl struct + if(db->cacheReferenced < db->shared->cacheCount) + { + printf("additional caches found! \n"); + //init local tbl2 array (internally a malloc occurs) + for(k=1; k < db->shared->cacheCount; k++ ) + { + printf("qhasharr init cache no.= %d with zero \n",k); + db->tbl[k] = qhasharr(db->sharedCache + (k * PERS_CACHE_MEMSIZE) , 0); + db->cacheReferenced++; + } + } + + //reset addresses for all caches mapped to this process space + for(k=0; k < db->shared->cacheCount; k++ ) + { + printf("setMemoryAddress k= %d -- putToCache ptr: %p \n", k, db->sharedCache + (k * PERS_CACHE_MEMSIZE)); + setMemoryAddress(db->sharedCache + (k * PERS_CACHE_MEMSIZE ), db->tbl[k]); + } + + int putOk = 0; + k=0; + //try to insert data until empty slot or same key is found in all of the caches + while (k < db->shared->cacheCount) //store flag , datasize and data as value in cache + { + printf("start while k = %d, cacheCount= %d\n", k, db->shared->cacheCount); + if( db->tbl[k]->put(db->tbl[k], metaKey, cachedData, sizeof(pers_lldb_cache_flag_e) + sizeof(int) + (size_t) dataSize) == true) + { + putOk = 1; + printf("INSERT OK \n"); + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("INSERT INTO Cache Nr: "); DLT_INT(k); DLT_STRING("worked : ")); + break; + } + else + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("INSERT INTO Cache Nr: "); DLT_INT(k); DLT_STRING("failed : "); DLT_STRING(strerror(errno))); + } + printf("end while k = %d \n", k); + k++; + } + + + + //if all caches are full -> add new cache and insert data here + if (putOk == 0) + { + printf("data->maxslots 1 = %d \n", db->tbl[0]->data->maxslots); + printf("start adding new cache \n"); + addCache(db); //add additional cache + printf("end adding new cache \n"); + printf("data->maxslots 2 = %d \n", db->tbl[0]->data->maxslots); + + printf("Put not ok -> try to use added cache no. = %d \n", db->shared->cacheCount -1); + + if (db->tbl[db->shared->cacheCount - 1]->put(db->tbl[db->shared->cacheCount - 1], metaKey, cachedData, + sizeof(pers_lldb_cache_flag_e) + sizeof(int) + (size_t) dataSize) == false) //store flag , datasize and data as value in cache + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("INSERT NEW DATA INTO RESIZE OF CACHE FAILED : "); DLT_STRING(strerror(errno))); + + } + else + DLT_LOG(persComLldbDLTCtx, DLT_LOG_DEBUG, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("INSERT NEW DATA INTO RESIZE OF CACHE WORKS : ")); + } + printf("END ----------- \n\n"); +#endif } else - size_written = dataSize; - - Kdb_unlock(&db->shmem_info->cache_rwlock); - return size_written; + { + bytesWritten = dataSize; // return only size of data that has to be stored + } + return bytesWritten; } +sint_t deleteFromCache(KISSDB* db, char* metaKey) +{ + char* ptr; + Data_Cached_s dataCached = { 0 }; + int datasize = 0; + int status = PERS_COM_FAILURE; + Kdb_bool found = Kdb_true; + pers_lldb_cache_flag_e eFlag; + sint_t bytesDeleted = 0; + size_t size = 0; + void* val; + + //DO NOT ALLOW WRITING TO CACHE IF DATABASE IS OPENED IN READONLY MODE + if (KISSDB_OPEN_MODE_RDONLY != db->shared->openMode) + { + dataCached.eFlag = CachedDataDelete; + dataCached.m_dataSize = 0; + + //if cache not already created + if (db->shared->cacheCreated == Kdb_false) + { + if (createCache(db) != 0) + { + return PERS_COM_FAILURE; + } + } + else //open cache + { + if (openCache(db) != 0) + { + return PERS_COM_FAILURE; + } + } + + setMemoryAddress(db->sharedCache, db->tbl[0]); + + val = db->tbl[0]->get(db->tbl[0], metaKey, &size); + if (NULL != val) //check if key to be deleted is in Cache + { + ptr = val; + eFlag = (pers_lldb_cache_flag_e) *(int*) ptr; + ptr += sizeof(int); + datasize = *(int*) ptr; + + //Mark data in cache as deleted + if (eFlag != CachedDataDelete) + { + if (db->tbl[0]->put(db->tbl[0], metaKey, &dataCached, sizeof(pers_lldb_cache_flag_e) + sizeof(int)) == false) //do not store any data + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Failed to mark data in cache as deleted")); + bytesDeleted = PERS_COM_ERR_NOT_FOUND; + found = Kdb_false; + } + else + { + bytesDeleted = datasize; + } + } + } + else //check if key to be deleted is in database file + { + //get dataSize + uint32_t size; + status = KISSDB_get(db, metaKey, NULL, 0, &size); + if (status == 0) + { + if (db->tbl[0]->put(db->tbl[0], metaKey, &dataCached, sizeof(pers_lldb_cache_flag_e) + sizeof(int)) == false) + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Failed to mark existing data as deleted")); + bytesDeleted = PERS_COM_ERR_NOT_FOUND; + } + else + { + bytesDeleted = size; + } + } + else + { + if (status == 1) + { + found = Kdb_false; + DLT_LOG(persComLldbDLTCtx, DLT_LOG_WARN, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("not found, retval=<"); DLT_INT(status); DLT_STRING(">")); + } + else + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, + DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("KISSDB_get: key=<"); DLT_STRING(metaKey); DLT_STRING(">, "); DLT_STRING("failed with retval=<"); DLT_INT(status); DLT_STRING(">")); + } + } + } + + if (found == Kdb_false) + { + bytesDeleted = PERS_COM_ERR_NOT_FOUND; + } + } + else + { + bytesDeleted = PERS_COM_ERR_READONLY; + } + return bytesDeleted; +} + sint_t getListandSize(KISSDB* db, pstr_t buffer, sint_t size, bool_t bOnlySizeNeeded, pers_lldb_purpose_e purpose) { - KISSDB_Iterator dbi; - int keycount_file = 0, keycount_cache = 0, result = 0, x = 0, idx = 0, max = 0, used = 0, obj_count, keylength = 0; + char* memory = NULL; + char* ptr; char** tmplist = NULL; + int keyCountFile = 0, keyCountCache = 0, result = 0, x = 0, idx = 0, max = 0, used = 0, objCount = 0; + KISSDB_Iterator dbi; + pers_lldb_cache_flag_e eFlag; + qhasharr_t* tbl; qnobj_t obj; sint_t availableSize = size; - char* ptr; - char* memory = NULL; void* pt; - pers_lldb_cache_flag_e eFlag; - if (purpose == PersLldbPurpose_RCT) - keylength = PERS_RCT_MAX_LENGTH_RESOURCE_ID; - else - keylength = PERS_DB_MAX_LENGTH_KEY_NAME; + int keylength = (purpose == PersLldbPurpose_RCT) ? PERS_RCT_MAX_LENGTH_RESOURCE_ID : PERS_DB_MAX_LENGTH_KEY_NAME; + char kbuf[PERS_DB_MAX_LENGTH_KEY_NAME]; //open existing cache if present and look for keys - if (db->shmem_info->cache_initialised == Kdb_true) + if (db->shared->cacheCreated == Kdb_true) { if (openCache(db) != 0) { - Kdb_unlock(&db->shmem_info->cache_rwlock); return PERS_COM_FAILURE; } else { - obj_count = db->tbl->size(db->tbl, &max, &used); - if (obj_count > 0) + setMemoryAddress(db->sharedCache, db->tbl[0]); + + objCount = db->tbl[0]->size(db->tbl[0], &max, &used); + if (objCount > 0) { - tmplist = malloc(sizeof(char*) * obj_count); + tmplist = malloc(sizeof(char*) * objCount); if (tmplist != NULL) { - while (db->tbl->getnext(db->tbl, &obj, &idx) == true) + while (db->tbl[0]->getnext(db->tbl[0], &obj, &idx) == true) { + size_t keyLen = strlen(obj.name); pt = obj.data; eFlag = (pers_lldb_cache_flag_e) *(int*) pt; if (eFlag != CachedDataDelete) { - tmplist[keycount_cache] = (char*) malloc(strlen(obj.name) + 1); - (void) strncpy(tmplist[keycount_cache], obj.name, strlen(obj.name)); - ptr = tmplist[keycount_cache]; - ptr[strlen(obj.name)] = '\0'; - keycount_cache++; + tmplist[keyCountCache] = (char*) malloc(keyLen + 1); + (void) strncpy(tmplist[keyCountCache], obj.name, keyLen); + ptr = tmplist[keyCountCache]; + ptr[keyLen] = '\0'; + keyCountCache++; } } } else + { return PERS_COM_ERR_MALLOC; + } } } } //look for keys in database file + //Initialise database iterator KISSDB_Iterator_init(db, &dbi); - char kbuf[keylength]; - //get number of keys, stored in database file while (KISSDB_Iterator_next(&dbi, &kbuf, NULL) > 0) - keycount_file++; + { + keyCountFile++; + } - if ((keycount_cache + keycount_file) > 0) + if ((keyCountCache + keyCountFile) > 0) { - int memsize = qhasharr_calculate_memsize(keycount_cache + keycount_file); + int memsize = qhasharr_calculate_memsize(keyCountCache + keyCountFile); //create hashtable that stores the list of keys without duplicates memory = malloc(memsize); if (memory != NULL) { memset(memory, 0, memsize); - qhasharr_t *tbl = qhasharr(memory, memsize); + tbl = qhasharr(memory, memsize); if (tbl == NULL) + { return PERS_COM_ERR_MALLOC; - + } //put keys in cache to a hashtable - for (x = 0; x < keycount_cache; x++) + for (x = 0; x < keyCountCache; x++) { if (tbl->put(tbl, tmplist[x], "0", 1) == true) { if (tmplist[x] != NULL) + { free(tmplist[x]); + } } } free(tmplist); @@ -1947,7 +2242,9 @@ sint_t getListandSize(KISSDB* db, pstr_t buffer, sint_t size, bool_t bOnlySizeNe { size_t keyLen = strnlen(kbuf, sizeof(kbuf)); if (keyLen > 0) + { tbl->put(tbl, kbuf, "0", 1); + } } //count needed size for buffer / copy keys to buffer @@ -1972,82 +2269,132 @@ sint_t getListandSize(KISSDB* db, pstr_t buffer, sint_t size, bool_t bOnlySizeNe free(memory); } else + { return PERS_COM_ERR_MALLOC; + } } return result; } - - - int createCache(KISSDB* db) { - Kdb_bool shmem_creator; + Kdb_bool shmCreator; int status = -1; - db->shmem_cached_fd = kdbShmemOpen(db->shmem_cached_name, PERS_CACHE_MEMSIZE, &shmem_creator); - if (db->shmem_cached_fd != -1) + db->sharedCacheFd = kdbShmemOpen(db->cacheName, PERS_CACHE_MEMSIZE, &shmCreator); + if (db->sharedCacheFd != -1) { - db->shmem_cached = (void*) getKdbShmemPtr(db->shmem_cached_fd, PERS_CACHE_MEMSIZE); - if (db->shmem_cached != ((void *) -1)) - { - db->tbl = qhasharr(db->shmem_cached, PERS_CACHE_MEMSIZE); - if (db->tbl != NULL) + db->sharedCache = (void*) getKdbShmemPtr(db->sharedCacheFd, PERS_CACHE_MEMSIZE); + if (db->sharedCache != ((void*) -1)) + { + // for dynamic cache -> create reference into array db->tbl[0] = qhasharr(db->sharedCache, PERS_CACHE_MEMSIZE); + /* + * Add a function called addCache that resizes shared memory with the size of PERS_CACHE_MEMSIZE + * Then init the additional cache -> db->tbl[n] = qhasharr(db->sharedCache + (n * PERS_CACHE_MEMSIZE) , PERS_CACHE_MEMSIZE) + * Other processes must recognize additional created caches and reopen them with -> db->tbl[0 - n] = qhasharr(db->sharedCache + (n * PERS_CACHE_MEMSIZE) , 0); + * Store the count of created caches in shared information + */ + db->tbl[0] = qhasharr(db->sharedCache, PERS_CACHE_MEMSIZE); + if (db->tbl[0] != NULL) { status = 0; - db->shmem_info->cache_initialised = Kdb_true; + db->shared->cacheCreated = Kdb_true; } } } if (status != 0) - DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":");DLT_STRING("Error: Failed to create cache")); + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, DLT_STRING(__FUNCTION__); DLT_STRING(":"); DLT_STRING("Failed to create cache"); DLT_STRING(strerror(errno))); + } return status; } - int openCache(KISSDB* db) { - Kdb_bool shmem_creator; + Kdb_bool shmCreator; int status = -1; //only open shared memory again if filedescriptor is not initialised yet - if (db->shmem_cached_fd <= 0) //not shared filedescriptor + if (db->sharedCacheFd <= 0) //not shared filedescriptor { - db->shmem_cached_fd = kdbShmemOpen(db->shmem_cached_name, PERS_CACHE_MEMSIZE, &shmem_creator); - if (db->shmem_cached_fd != -1) + db->sharedCacheFd = kdbShmemOpen(db->cacheName, PERS_CACHE_MEMSIZE, &shmCreator); + if (db->sharedCacheFd != -1) { - db->shmem_cached = (void*) getKdbShmemPtr(db->shmem_cached_fd, PERS_CACHE_MEMSIZE); - if (db->shmem_cached != ((void *) -1)) + db->sharedCache = (void*) getKdbShmemPtr(db->sharedCacheFd, PERS_CACHE_MEMSIZE); + if (db->sharedCache != ((void*) -1)) { // use existent hash-table - db->tbl = qhasharr(db->shmem_cached, 0); - if (db->tbl != NULL) + db->tbl[0] = qhasharr(db->sharedCache, 0); + if (db->tbl[0] != NULL) + { status = 0; + } } } } else + { status = 0; + } if (status != 0) - DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":");DLT_STRING("Error: Failed to open cache")); + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, DLT_STRING(__FUNCTION__), DLT_STRING(":"), DLT_STRING("Failed to open cache")); + } return status; } +#if 0 +int addCache(KISSDB* db) +{ + //ftruncate db->sharedCacheFd (oldsize + additionalspace) + + //printf("start ftruncate new cache db->tbl[0]->data->maxslots = %d\n", db->tbl[0]->data->maxslots); + if( ftruncate(db->sharedCacheFd, db->shared->cacheSize + PERS_CACHE_MEMSIZE) < 0) + { + printf("Cache resize failed: %s \n", strerror(errno)); + } + //printf("end ftruncate new cache db->tbl[0]->data->maxslots = %d\n", db->tbl[0]->data->maxslots); + + //mremap oldsize , newsize + //store new cache pointer for this process in db->sharedCache + db->sharedCache = mremap(db->sharedCache, db->shared->cacheSize , db->shared->cacheSize + PERS_CACHE_MEMSIZE, MREMAP_MAYMOVE ); + if (db->sharedCache == MAP_FAILED) + { + printf("cacheresize MAP_FAILED \n"); + } + //printf("end mremap new cache db->tbl[0]->data->maxslots = %d\n", db->tbl[0]->data->maxslots); + //printf("adding cache into db->tbl[%d],----- ptr: %p \n",db->shared->cacheCount, db->sharedCache + (db->shared->cacheCount * PERS_CACHE_MEMSIZE)); + + db->tbl[db->shared->cacheCount] = qhasharr(db->sharedCache + (db->shared->cacheCount * PERS_CACHE_MEMSIZE) , PERS_CACHE_MEMSIZE); + db->cacheReferenced++; + //printf("here 1 \n "); + + //printf("end qhasharr new cache db->tbl[0]->data->maxslots = %d\n", db->tbl[0]->data->maxslots); + + //store new size in shared memory + db->shared->cacheSize += PERS_CACHE_MEMSIZE; + db->shared->cacheCount++; + // add check if cache was resized (check local mapped size versus shared mapped size) for all processes when cache is accessed and do a remap for new size without truncation + return 0; +} +#endif + + int closeCache(KISSDB* db) { int status = -1; - if (kdbShmemClose(db->shmem_cached_fd, db->shmem_cached_name) != Kdb_false) + if (kdbShmemClose(db->sharedCacheFd, db->cacheName) != Kdb_false) { - free(db->shmem_cached_name); //free memory for name obtained by kdbGetShmName() function - if (freeKdbShmemPtr(db->shmem_cached, PERS_CACHE_MEMSIZE) != Kdb_false) + if (freeKdbShmemPtr(db->sharedCache, PERS_CACHE_MEMSIZE) != Kdb_false) + { status = 0; + } } if (status != 0) - DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, - DLT_STRING(__FUNCTION__); DLT_STRING(":");DLT_STRING("Error: Failed to close cache")); + { + DLT_LOG(persComLldbDLTCtx, DLT_LOG_ERROR, DLT_STRING(__FUNCTION__), DLT_STRING(":"), DLT_STRING("Failed to close cache")); + } return status; } |