diff options
author | Ingo Huerner <ingo.huerner@xse.de> | 2015-02-10 15:43:04 +0100 |
---|---|---|
committer | Ingo Huerner <ingo.huerner@xse.de> | 2015-02-10 15:43:04 +0100 |
commit | 84f52fadc2153ef8ab5d3c41547e11005ca0213e (patch) | |
tree | 3322007553903d1412c97a1043022017b97b59c4 | |
parent | cb586635de541f0516f52a522a0c8a045477203d (diff) | |
download | persistence-client-library-84f52fadc2153ef8ab5d3c41547e11005ca0213e.tar.gz |
Some optimization; fixed a problem with unregister/register notifications
-rw-r--r-- | include/persistence_client_library.h | 5 | ||||
-rw-r--r-- | src/persistence_client_library_backup_filelist.c | 7 | ||||
-rw-r--r-- | src/persistence_client_library_custom_loader.c | 17 | ||||
-rw-r--r-- | src/persistence_client_library_db_access.c | 60 | ||||
-rw-r--r-- | src/persistence_client_library_db_access.h | 4 | ||||
-rw-r--r-- | src/persistence_client_library_handle.c | 11 | ||||
-rw-r--r-- | src/persistence_client_library_key.c | 2 | ||||
-rw-r--r-- | src/persistence_client_library_tree_helper.c | 9 | ||||
-rw-r--r-- | src/persistence_client_library_tree_helper.h | 12 | ||||
-rw-r--r-- | src/rbtree.c | 4 | ||||
-rw-r--r-- | src/rbtree.h | 2 | ||||
-rw-r--r-- | test/persistence_client_library_dbus_test.c | 9 |
12 files changed, 88 insertions, 54 deletions
diff --git a/include/persistence_client_library.h b/include/persistence_client_library.h index 5ab5b4b..0df405e 100644 --- a/include/persistence_client_library.h +++ b/include/persistence_client_library.h @@ -211,8 +211,6 @@ extern "C" { * The function will be called within process using always the same appname! * It is not allowed call this function within a process using different appnames! * - * @attention This function is currently N O T part of the GENIVI compliance specification - * * @param appname application name, the name must be a unique name in the system * @param shutdownMode shutdown mode ::PCL_SHUTDOWN_TYPE_FAST or ::PCL_SHUTDOWN_TYPE_NORMAL ::PCL_SHUTDOWN_TYPE_NONE * @@ -235,8 +233,6 @@ int pclInitLibrary(const char* appname, int shutdownMode); * if it's needed your should know what you do and why exactly you need to do this in * this way. * - * @attention This function is currently N O T part of the GENIVI compliance specification - * * @return positive value: success; * On error a negative value will be returned. */ @@ -250,7 +246,6 @@ int pclDeinitLibrary(void); * This function can be called if to flush and write back the data form cache to memory device. * The function is only available if PCL_SHUTDOWN_TYPE_NONE has been used in pclInitLibrary. * - * @attention This function is currently N O T part of the GENIVI compliance specification * @attention In order to prevent misuse of this function the cancel shutdown request * can only be called 3 times per lifecycle. * The function called by an application with the parameter ::PCL_SHUTDOWN_CANCEL diff --git a/src/persistence_client_library_backup_filelist.c b/src/persistence_client_library_backup_filelist.c index 9629a57..fdb7718 100644 --- a/src/persistence_client_library_backup_filelist.c +++ b/src/persistence_client_library_backup_filelist.c @@ -36,6 +36,9 @@ static char* gpTokenArray[TOKENARRAYSIZE] = {0}; +/// the rb tree +static jsw_rbtree_t *gRb_tree_bl = NULL; + // local function prototypes static int need_backup_key(unsigned int key); static int pclRecoverFromBackup(int backupFd, const char* original); @@ -538,7 +541,7 @@ int pclCalcCrc32Csum(int fd, char crc32sum[]) if(fstat(fd, &statBuf) != -1) { - buf = malloc((unsigned int)statBuf.st_size); + buf = malloc(statBuf.st_size); if(buf != 0) { @@ -583,7 +586,7 @@ int pclGetPosixPermission(PersistencePermission_e permission) { int posixPerm = -1; - switch( (int)permission) + switch(permission) { case PersistencePermission_ReadWrite: posixPerm = O_RDWR; diff --git a/src/persistence_client_library_custom_loader.c b/src/persistence_client_library_custom_loader.c index e7ef848..64c62a6 100644 --- a/src/persistence_client_library_custom_loader.c +++ b/src/persistence_client_library_custom_loader.c @@ -677,20 +677,5 @@ int load_custom_plugins(plugin_callback_async_t pfInitCompletedCB) void invalidate_custom_plugin(int idx) { - gPersCustomFuncs[idx].handle = NULL; - gPersCustomFuncs[idx].custom_plugin_init = NULL; - gPersCustomFuncs[idx].custom_plugin_deinit = NULL; - gPersCustomFuncs[idx].custom_plugin_handle_open = NULL; - gPersCustomFuncs[idx].custom_plugin_handle_close = NULL; - gPersCustomFuncs[idx].custom_plugin_handle_get_data = NULL; - gPersCustomFuncs[idx].custom_plugin_handle_set_data = NULL; - gPersCustomFuncs[idx].custom_plugin_get_data = NULL; - gPersCustomFuncs[idx].custom_plugin_set_data = NULL; - gPersCustomFuncs[idx].custom_plugin_delete_data = NULL; - gPersCustomFuncs[idx].custom_plugin_get_status_notification_clbk = NULL; - gPersCustomFuncs[idx].custom_plugin_handle_get_size = NULL; - gPersCustomFuncs[idx].custom_plugin_get_size = NULL; - gPersCustomFuncs[idx].custom_plugin_create_backup = NULL; - gPersCustomFuncs[idx].custom_plugin_get_backup = NULL; - gPersCustomFuncs[idx].custom_plugin_restore_backup = NULL; + memset(&gPersCustomFuncs[idx], 0, sizeof(Pers_custom_functs_s)); } diff --git a/src/persistence_client_library_db_access.c b/src/persistence_client_library_db_access.c index 438e7e8..cd9476a 100644 --- a/src/persistence_client_library_db_access.c +++ b/src/persistence_client_library_db_access.c @@ -21,6 +21,8 @@ #include "persistence_client_library_custom_loader.h" #include "persistence_client_library_dbus_service.h" #include "persistence_client_library_prct_access.h" +#include "persistence_client_library_tree_helper.h" +#include "crc32.h" #include <persComErrors.h> @@ -28,10 +30,13 @@ + /// btree array static int gHandlesDB[DbTableSize][PersistenceDB_LastEntry]; static int gHandlesDBCreated[DbTableSize][PersistenceDB_LastEntry] = { {0} }; +/// tree to store notification information +static jsw_rbtree_t *gNotificationTree = NULL; static int database_get(PersistenceInfo_s* info, const char* dbPath, int dbType) { @@ -612,7 +617,7 @@ int persistence_delete_data(char* dbPath, char* key, const char* resource_id, Pe -int persistence_notify_on_change(const char* key, unsigned int ldbid, unsigned int user_no, unsigned int seat_no, +int persistence_notify_on_change(const char* resource_id, const char* dbKey, unsigned int ldbid, unsigned int user_no, unsigned int seat_no, pclChangeNotifyCallback_t callback, PersNotifyRegPolicy_e regPolicy) { int rval = 0; @@ -620,6 +625,9 @@ int persistence_notify_on_change(const char* key, unsigned int ldbid, unsigned i if(regPolicy < Notify_lastEntry) { MainLoopData_u data; + key_value_s* foundItem = NULL; + key_value_s* searchItem = NULL; + unsigned int hashKey = pclCrc32(0, (unsigned char*)dbKey, strlen(dbKey)); data.message.cmd = (uint32_t)CMD_REG_NOTIFY_SIGNAL; data.message.params[0] = ldbid; @@ -627,15 +635,57 @@ int persistence_notify_on_change(const char* key, unsigned int ldbid, unsigned i data.message.params[2] = seat_no; data.message.params[3] = regPolicy; - snprintf(data.message.string, PERS_DB_MAX_LENGTH_KEY_NAME, "%s", key); + snprintf(data.message.string, PERS_DB_MAX_LENGTH_KEY_NAME, "%s", resource_id); + + // check if the tree has already been created + if(gNotificationTree == NULL) + { + gNotificationTree = jsw_rbnew(key_val_cmp, key_val_dup, key_val_rel); + } + + // search if item is already stored in the tree + searchItem = malloc(sizeof(key_value_s)); + searchItem->key = hashKey; + foundItem = (key_value_s*)jsw_rbfind(gNotificationTree, searchItem); if(regPolicy == Notify_register) { - gChangeNotifyCallback = callback; // assign callback + if(foundItem == NULL) // item not found add it, else already added so nothing to do + { + key_value_s* item = malloc(sizeof(key_value_s)); // assign key and value to the rbtree item + if(item != NULL) + { + item->key = hashKey; + // we don't need the path name here, we just need to know that this key is available in the tree + item->value = ""; + (void)jsw_rbinsert(gNotificationTree, item); + free(item); + + gChangeNotifyCallback = callback; // assign callback + } + else + { + rval = -1; + } + } + free(foundItem); } else if(regPolicy == Notify_unregister) { - gChangeNotifyCallback = NULL; // remove callback + if(foundItem != NULL) // item already in the tree remove it, if not found nothing to do + { + // remove from tree + jsw_rberase(gNotificationTree, foundItem); + + if(jsw_rbsize(gNotificationTree) == 0) // if no other notification is stored in the tree, remove callback + { + gChangeNotifyCallback = NULL; // remove callback + } + } + else + { + free(foundItem); + } } if(-1 == deliverToMainloop(&data)) @@ -643,6 +693,8 @@ int persistence_notify_on_change(const char* key, unsigned int ldbid, unsigned i DLT_LOG(gPclDLTContext, DLT_LOG_ERROR, DLT_STRING("notifyOnChange - Err to write to pipe"), DLT_INT(errno)); rval = -1; } + + free(searchItem); } else { diff --git a/src/persistence_client_library_db_access.h b/src/persistence_client_library_db_access.h index 5a8eb35..86331ce 100644 --- a/src/persistence_client_library_db_access.h +++ b/src/persistence_client_library_db_access.h @@ -163,7 +163,7 @@ void database_close_all(); /** * @brief register or unregister for change notifications of a key * - * @param key the database key to register on + * @param resource_id the database resource_id to register on * @param ldbid logical database ID of the resource to monitor * @param user_no the user ID; user_no=0 can not be used as user-ID beacause '0' is defined as System/node * @param seat_no the seat number @@ -172,7 +172,7 @@ void database_close_all(); * * @return 0 of registration was successful; -1 if registration fails */ -int persistence_notify_on_change(const char* key, unsigned int ldbid, unsigned int user_no, unsigned int seat_no, +int persistence_notify_on_change(const char* resource_id, const char* dbKey, unsigned int ldbid, unsigned int user_no, unsigned int seat_no, pclChangeNotifyCallback_t callback, PersNotifyRegPolicy_e regPolicy); diff --git a/src/persistence_client_library_handle.c b/src/persistence_client_library_handle.c index 47b16ea..09311ae 100644 --- a/src/persistence_client_library_handle.c +++ b/src/persistence_client_library_handle.c @@ -28,6 +28,17 @@ pthread_mutex_t gFileHandleAccessMtx = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t gOssFileHandleAccessMtx = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t gMtx = PTHREAD_MUTEX_INITIALIZER; +/// tree to store key handle information +static jsw_rbtree_t *gKeyHandleTree = NULL; + +/// tree to store file handle information +static jsw_rbtree_t *gFileHandleTree = NULL; + + +static jsw_rbtree_t *gOssFileHandleTree = NULL; + + + // open file descriptor handle array int gOpenFdArray[MaxPersHandle] = { [0 ...MaxPersHandle-1] = 0 }; diff --git a/src/persistence_client_library_key.c b/src/persistence_client_library_key.c index 8c1a8c0..3591f91 100644 --- a/src/persistence_client_library_key.c +++ b/src/persistence_client_library_key.c @@ -591,7 +591,7 @@ int regNotifyOnChange(unsigned int ldbid, const char* resource_id, unsigned int if( (dbContext.configKey.storage != PersistenceStorage_local) && (dbContext.configKey.type == PersistenceResourceType_key) ) { - rval = persistence_notify_on_change(resource_id, ldbid, user_no, seat_no, callback, regPolicy); + rval = persistence_notify_on_change(resource_id, dbKey, ldbid, user_no, seat_no, callback, regPolicy); } else { diff --git a/src/persistence_client_library_tree_helper.c b/src/persistence_client_library_tree_helper.c index b5c6457..83bb895 100644 --- a/src/persistence_client_library_tree_helper.c +++ b/src/persistence_client_library_tree_helper.c @@ -20,19 +20,10 @@ -jsw_rbtree_t *gKeyHandleTree = NULL; - -jsw_rbtree_t *gFileHandleTree = NULL; - -jsw_rbtree_t *gOssFileHandleTree = NULL; - -jsw_rbtree_t *gRb_tree_bl = NULL; - /** * File handle helper functions */ - // compare function for tree item int fh_key_val_cmp(const void *p1, const void *p2) { diff --git a/src/persistence_client_library_tree_helper.h b/src/persistence_client_library_tree_helper.h index 290a5ce..f42e635 100644 --- a/src/persistence_client_library_tree_helper.h +++ b/src/persistence_client_library_tree_helper.h @@ -27,18 +27,6 @@ #include "rbtree.h" -/// tree to store key handle information -extern jsw_rbtree_t *gKeyHandleTree; - -/// tree to store file handle information -extern jsw_rbtree_t *gFileHandleTree; - -/// tree to store OSS file handle information -extern jsw_rbtree_t *gOssFileHandleTree; - -/// the rb tree -extern jsw_rbtree_t *gRb_tree_bl; - /// key handle data union definition typedef union KeyHandleData_u_ diff --git a/src/rbtree.c b/src/rbtree.c index 2b511c7..87359ff 100644 --- a/src/rbtree.c +++ b/src/rbtree.c @@ -480,7 +480,7 @@ int jsw_rberase ( jsw_rbtree_t *tree, void *data ) return 1; } -#if 0 + /** <summary> Gets the number of nodes in a red black tree @@ -492,7 +492,7 @@ size_t jsw_rbsize ( jsw_rbtree_t *tree ) { return tree->size; } - +#if 0 /** <summary> Create a new traversal object diff --git a/src/rbtree.h b/src/rbtree.h index c674d99..7d30512 100644 --- a/src/rbtree.h +++ b/src/rbtree.h @@ -81,7 +81,7 @@ void jsw_rbdelete ( jsw_rbtree_t *tree ); void *jsw_rbfind ( jsw_rbtree_t *tree, void *data ); int jsw_rbinsert ( jsw_rbtree_t *tree, void *data ); int jsw_rberase ( jsw_rbtree_t *tree, void *data ); -//size_t jsw_rbsize ( jsw_rbtree_t *tree ); +size_t jsw_rbsize ( jsw_rbtree_t *tree ); /* Traversal functions */ //jsw_rbtrav_t *jsw_rbtnew ( void ); diff --git a/test/persistence_client_library_dbus_test.c b/test/persistence_client_library_dbus_test.c index ceac126..d6bd472 100644 --- a/test/persistence_client_library_dbus_test.c +++ b/test/persistence_client_library_dbus_test.c @@ -77,8 +77,17 @@ int main(int argc, char *argv[]) getchar(); ret = pclKeyUnRegisterNotifyOnChange(0x20, "links/last_link2", 2/*user_no*/, 1/*seat_no*/, &myChangeCallback); + printf("UnReg => last_link2: %d\n", ret); + printf("Press enter to proceed\n"); + getchar(); + ret = pclKeyUnRegisterNotifyOnChange(0x20, "links/last_link3", 3/*user_no*/, 2/*seat_no*/, &myChangeCallback); + printf("UnReg => last_link3: %d\n", ret); + printf("Press enter to proceed\n"); + getchar(); + ret = pclKeyUnRegisterNotifyOnChange(0x20, "links/last_link4", 4/*user_no*/, 1/*seat_no*/, &myChangeCallback); + printf("UnReg => last_link4: %d\n", ret); printf("Press enter to register to notifications\n"); getchar(); |