diff options
author | ihuerner <ihuerner@b9707d8c-442e-47db-affb-152dabb2260b> | 2012-11-08 12:05:22 +0000 |
---|---|---|
committer | ihuerner <ihuerner@b9707d8c-442e-47db-affb-152dabb2260b> | 2012-11-08 12:05:22 +0000 |
commit | 63960e261f58104a772dbebd01e8dc3b64337277 (patch) | |
tree | 986fe4d584d36005c1b447e2df0f5739ac822458 | |
parent | 1bdfb2ca95777e6d582c68d32ee16263ad0909c6 (diff) | |
download | persistence-client-library-63960e261f58104a772dbebd01e8dc3b64337277.tar.gz |
New release 0.2.3, for changes see ChangeLog
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | include/persistence_client_library_error_def.h | 52 | ||||
-rw-r--r-- | src/persistence_client_library_access_helper.c | 10 | ||||
-rw-r--r-- | src/persistence_client_library_data_access.c | 257 | ||||
-rw-r--r-- | src/persistence_client_library_data_access.h | 72 | ||||
-rw-r--r-- | src/persistence_client_library_dbus_service.c | 3 | ||||
-rw-r--r-- | src/persistence_client_library_handle.c | 15 | ||||
-rw-r--r-- | src/persistence_client_library_key.c | 5 | ||||
-rw-r--r-- | test/data/Data.tar.gz | bin | 76540 -> 121017 bytes | |||
-rw-r--r-- | test/persistence_client_library_test.c | 205 |
10 files changed, 552 insertions, 74 deletions
@@ -1,3 +1,10 @@ +Revision 0.2.3 (rev 1793) +- Fixed handle problem +- added cursor functions +- key do not need to start with a '/' anymore +- added multi-threading support to dbus mainloop + + Revision 0.2.2 (rev 1759) - Updated test data - Added shared data support diff --git a/include/persistence_client_library_error_def.h b/include/persistence_client_library_error_def.h index a7140d6..dc4cad9 100644 --- a/include/persistence_client_library_error_def.h +++ b/include/persistence_client_library_error_def.h @@ -34,57 +34,59 @@ */ // common error, for this error errno will be set -#define EPERS_COMMON -1 +#define EPERS_COMMON (-1) /// file system is locked -#define EPERS_LOCKFS -2 +#define EPERS_LOCKFS (-2) /// filesystem is currently locked #define EPERS_MAP_LOCKFS ((void *) -2) /// bad storage policy -#define EPERS_BADPOL -3 +#define EPERS_BADPOL (-3) /// open handle limit reached -#define EPERS_MAXHANDLE -4 +#define EPERS_MAXHANDLE (-4) /// max buffer limit for persistence data -#define EPERS_BUFLIMIT -5 +#define EPERS_BUFLIMIT (-5) /// persistence resource configuration table not found -#define EPERS_NOPRCTABLE -6 +#define EPERS_NOPRCTABLE (-6) /// key not found -#define EPERS_NOKEY -7 +#define EPERS_NOKEY (-7) /// no data for key -#define EPERS_NOKEYDATA -8 +#define EPERS_NOKEYDATA (-8) /// write of data failed -#define EPERS_SETDTAFAILED -9 +#define EPERS_SETDTAFAILED (-9) /// failed to open file -#define EPERS_OPENFILE -10 +#define EPERS_OPENFILE (-10) /// invalid buffer or key -#define EPERS_DESER_BUFORKEY -11 +#define EPERS_DESER_BUFORKEY (-11) /// can't allocat memory for deserialization of keyvalue -#define EPERS_DESER_ALLOCMEM -12 +#define EPERS_DESER_ALLOCMEM (-12) /// no ploicy avaliable in data to serialize -#define EPERS_DESER_POLICY -13 +#define EPERS_DESER_POLICY (-13) /// no store type avaliable in data to serialize -#define EPERS_DESER_STORE -14 +#define EPERS_DESER_STORE (-14) /// no permission avaliable in data to serialize -#define EPERS_DESER_PERM -15 +#define EPERS_DESER_PERM (-15) /// no max size avaliable in data to serialize -#define EPERS_DESER_MAXSIZE -16 +#define EPERS_DESER_MAXSIZE (-16) /// no responsibility avaliable in data to serialize -#define EPERS_DESER_RESP -17 +#define EPERS_DESER_RESP (-17) /// out of array bounds -#define EPERS_OUTOFBOUNDS -18 +#define EPERS_OUTOFBOUNDS (-18) /// failed to map config file -#define EPERS_CONFIGMAPFAILED -19 +#define EPERS_CONFIGMAPFAILED (-19) /// config file if not available -#define EPERS_CONFIGNOTAVAILABLE -20 +#define EPERS_CONFIGNOTAVAILABLE (-20) /// can't stat config file -#define EPERS_CONFIGNOSTAT -21 +#define EPERS_CONFIGNOSTAT (-21) /// plugin functin not found -#define EPERS_NOPLUGINFCNT -22 +#define EPERS_NOPLUGINFCNT (-22) /// dlopen error -#define EPERS_DLOPENERROR -23 +#define EPERS_DLOPENERROR (-23) /// plugin function not loaded -#define EPERS_NOPLUGINFUNCT -24 +#define EPERS_NOPLUGINFUNCT (-24) /// file remove error -#define EPERS_FILEREMOVE -25 +#define EPERS_FILEREMOVE (-25) +/// err code to signalize last entry in DB +#define EPERS_LAST_ENTRY_IN_DB (-26) #endif /* PERSISTENCE_CLIENT_LIBRARY_ERROR_DEF_H */ diff --git a/src/persistence_client_library_access_helper.c b/src/persistence_client_library_access_helper.c index cea96b6..f7454ee 100644 --- a/src/persistence_client_library_access_helper.c +++ b/src/persistence_client_library_access_helper.c @@ -373,7 +373,7 @@ int get_db_path_and_key(unsigned char ldbid, char* resource_id, unsigned char us // // Node is added in front of the resource ID as the key string. // - snprintf(dbKey, dbKeyMaxLen, "%s%s", gNode, resource_id); + snprintf(dbKey, dbKeyMaxLen, "%s/%s", gNode, resource_id); } else { @@ -383,12 +383,12 @@ int get_db_path_and_key(unsigned char ldbid, char* resource_id, unsigned char us if(seat_no == 0) { // /User/<user_no_parameter> is added in front of the resource ID as the key string. - snprintf(dbKey, dbKeyMaxLen, "%s%d%s", gUser, user_no, resource_id); + snprintf(dbKey, dbKeyMaxLen, "%s%d/%s", gUser, user_no, resource_id); } else { // /User/<user_no_parameter>/Seat/<seat_no_parameter> is added in front of the resource ID as the key string. - snprintf(dbKey, dbKeyMaxLen, "%s%d%s%d%s", gUser, user_no, gSeat, seat_no, resource_id); + snprintf(dbKey, dbKeyMaxLen, "%s%d%s%d/%s", gUser, user_no, gSeat, seat_no, resource_id); } } storePolicy = PersistenceStorage_local; @@ -404,11 +404,11 @@ int get_db_path_and_key(unsigned char ldbid, char* resource_id, unsigned char us if(seat_no != 0) { - snprintf(dbKey, dbKeyMaxLen, "/%x%s%d%s%d%s", ldbid, gUser, user_no, gSeat, seat_no, resource_id); + snprintf(dbKey, dbKeyMaxLen, "/%x%s%d%s%d/%s", ldbid, gUser, user_no, gSeat, seat_no, resource_id); } else { - snprintf(dbKey, dbKeyMaxLen, "/%x%s%d%s", ldbid, gUser, user_no, resource_id); + snprintf(dbKey, dbKeyMaxLen, "/%x%s%d/%s", ldbid, gUser, user_no, resource_id); } storePolicy = PersistenceStorage_local; } diff --git a/src/persistence_client_library_data_access.c b/src/persistence_client_library_data_access.c index 9f89254..cce0ac0 100644 --- a/src/persistence_client_library_data_access.c +++ b/src/persistence_client_library_data_access.c @@ -49,6 +49,30 @@ typedef struct _KeyValuePair_s KeyValuePair_s; + +typedef struct _CursorEntry_s +{ + itzam_btree_cursor m_cursor; + int m_empty; +} +CursorEntry_s; + + +CursorEntry_s gCursorArray[maxPersHandle]; + +/// handle index +static int gHandleIdx = 1; + +static int gInitialized = 0; + +/// free handle array +int gFreeCursorHandleArray[maxPersHandle]; + +int gFreeCursorHandleIdxHead = 0; + +pthread_mutex_t gMtx; + + /// btree array static itzam_btree gBtree[2]; static int gBtreeCreated[] = { 0, 0 }; @@ -366,6 +390,239 @@ int persistence_reg_notify_on_change(char* dbPath, char* key) } +//--------------------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------------------- + +int get_cursor_handle() +{ + int handle = 0; + + if(gInitialized == 0) + { + gInitialized = 1; + pthread_mutex_init(&gMtx, 0); + } + + if(pthread_mutex_lock(&gMtx) == 0) + { + if(gFreeCursorHandleIdxHead > 0) // check if we have a free spot in the array before the current max + { + handle = gFreeCursorHandleArray[--gFreeCursorHandleIdxHead]; + } + else + { + if(gHandleIdx < maxPersHandle-1) + { + handle = gHandleIdx++; // no free spot before current max, increment handle index + } + else + { + handle = -1; + printf("get_persistence_handle_idx => Reached maximum of open handles: %d \n", maxPersHandle); + } + } + pthread_mutex_unlock(&gMtx); + } + + return handle; +} + + +void close_cursor_handle(int handlerDB) +{ + if(pthread_mutex_lock(&gMtx) == 0) + { + if(gFreeCursorHandleIdxHead < maxPersHandle) + { + gFreeCursorHandleArray[gFreeCursorHandleIdxHead++] = handlerDB; + } + pthread_mutex_unlock(&gMtx); + } +} + + +int persistence_db_cursor_create(char* dbPath, PersistenceStorage_e storage) +{ + int handle = -1; + itzam_btree* btree = NULL; + + btree = database_get(storage, dbPath); + if(btree != NULL) + { + itzam_state state; + handle = get_cursor_handle(); + state = itzam_btree_cursor_create(&gCursorArray[handle].m_cursor, btree); + if(state == ITZAM_OKAY) + { + gCursorArray[handle].m_empty = 0; + } + else + { + gCursorArray[handle].m_empty = 1; + } + } + + + return handle; +} + + + +int persistence_db_cursor_next(int handlerDB) +{ + int rval = -1; + if(handlerDB < maxPersHandle) + { + if(gCursorArray[handlerDB].m_empty != 1) + { + itzam_bool success; + success = itzam_btree_cursor_next(&gCursorArray[handlerDB].m_cursor); + + if(success == itzam_true) + { + rval = 0; + } + else + { + rval = EPERS_LAST_ENTRY_IN_DB; + } + } + else + { + printf("persistence_db_cursor_get_key ==> invalid handle: %d \n", handlerDB); + } + } + else + { + printf("persistence_db_cursor_get_key ==> handle bigger than max » handleDB: %d | max: : %d \n", handlerDB, maxPersHandle); + } + + return rval; +} + + + +int persistence_db_cursor_get_key(int handlerDB, char * bufKeyName_out, int bufSize) +{ + int rval = -1; + KeyValuePair_s search; + + if(handlerDB < maxPersHandle) + { + if(gCursorArray[handlerDB].m_empty != 1) + { + int length = 0; + itzam_btree_cursor_read(&gCursorArray[handlerDB].m_cursor ,(void *)&search); + length = strlen(search.m_key); + if(length < bufSize) + { + memcpy(bufKeyName_out, search.m_key, length); + rval = 0; + } + else + { + printf("persistence_db_cursor_get_key ==> buffer to small » keySize: %d | bufSize: %d \n", length, bufSize); + } + } + else + { + printf("persistence_db_cursor_get_key ==> invalid handle: %d \n", handlerDB); + } + } + else + { + printf("persistence_db_cursor_get_key ==> handle bigger than max » handleDB: %d | max: : %d \n", handlerDB, maxPersHandle); + } + + return rval; +} + + + +int persistence_db_cursor_get_data(int handlerDB, char * bufData_out, int bufSize) +{ + int rval = -1; + KeyValuePair_s search; + + if(handlerDB < maxPersHandle) + { + if(gCursorArray[handlerDB].m_empty != 1) + { + int length = 0; + itzam_btree_cursor_read(&gCursorArray[handlerDB].m_cursor ,(void *)&search); + + length = strlen(search.m_data); + if(length < bufSize) + { + memcpy(bufData_out, search.m_data, length); + rval = 0; + } + else + { + printf("persistence_db_cursor_get_data ==> buffer to small » keySize: %d | bufSize: %d \n", length, bufSize); + } + } + else + { + printf("persistence_db_cursor_get_data ==> invalid handle: %d \n", handlerDB); + } + } + else + { + printf("persistence_db_cursor_get_data ==> handle bigger than max » handleDB: %d | max: : %d \n", handlerDB, maxPersHandle); + } + + return rval; +} + + + +int persistence_db_cursor_get_data_size(int handlerDB) +{ + int size = -1; + KeyValuePair_s search; + + if(handlerDB < maxPersHandle) + { + if(gCursorArray[handlerDB].m_empty != 1) + { + itzam_btree_cursor_read(&gCursorArray[handlerDB].m_cursor ,(void *)&search); + size = strlen(search.m_data); + } + else + { + printf("persistence_db_cursor_get_data ==> invalid handle: %d \n", handlerDB); + } + } + else + { + printf("persistence_db_cursor_get_data ==> handle bigger than max » handleDB: %d | max: : %d \n", handlerDB, maxPersHandle); + } + + return size; +} + + + +int persistence_db_cursor_destroy(int handlerDB) +{ + int rval = -1; + itzam_state state; + + state = itzam_btree_cursor_free(&gCursorArray[handlerDB].m_cursor); + if (state == ITZAM_OKAY) + { + rval = 0; + gCursorArray[handlerDB].m_empty = 1; + close_cursor_handle(handlerDB); + } + + return rval; +} + + + + //----------------------------------------------------------------------------- // code to print database content (for debugging) //----------------------------------------------------------------------------- diff --git a/src/persistence_client_library_data_access.h b/src/persistence_client_library_data_access.h index ab89ddd..ca547de 100644 --- a/src/persistence_client_library_data_access.h +++ b/src/persistence_client_library_data_access.h @@ -34,10 +34,12 @@ * @see */ +#define PERSIST_DATA_ACCESS_INTERFACE_VERSION (0x01000000U) + + #include "persistence_client_library.h" -#ifdef USE_GVDB -#include "gvdb-builder.h" -#endif + + /** * @brief write data to a key @@ -105,4 +107,68 @@ int persistence_delete_data(char* dbPath, char* dbKey, PersistenceStorage_e stor */ void database_close(PersistenceStorage_e storage); + + +/** + * @brief create a cursor to a DB ; if success, the cursor points to (-1) + * to access the first entry in DB, call persistence_db_cursor_next + * + * @param dbPath[in] absolute path to the database + * @param storage[in] the storage identifier (local, shared or custom) + * + * @return handler to the DB (to be used in successive calls) or error code (< 0) + */ +int persistence_db_cursor_create(char* dbPath, PersistenceStorage_e storage); + +/** + * @brief move cursor to the next position + * + * @param handlerDB[in] handler to DB (obtained with persistence_db_cursor_create()) + * + * @return 0 for success, negative value in case of error (check against EPERS_LAST_ENTRY_IN_DB) + */ +int persistence_db_cursor_next(int handlerDB) ; + +/** + * @brief get the name of the key pointed by the cursor associated with the database + * + * @param handlerDB[in] handler to DB (obtained with persistence_db_cursor_create()) + * @param bufKeyName_out[out] buffer where to pass the name of the key + * @param bufSize[out] size of bufKeyName_out + * + * @return read size (if >= 0), error other way + */ +int persistence_db_cursor_get_key(int handlerDB, char * bufKeyName_out, int bufSize) ; + +/** + * @brief get the data of the key pointed by the cursor associated with the database + * + * @param handlerDB[in] handler to DB (obtained with persistence_db_cursor_create()) + * @param bufKeyData_out[out] buffer where to pass the data of the key + * @param bufSize[out] size of bufKeyData_out + * + * @return read size (if >= 0), error other way + */ +int persistence_db_cursor_get_data(int handlerDB, char * bufData_out, int bufSize) ; + +/** + * @brief get the data size of the key pointed by the cursor associated with the database + * + * @param handlerDB[in] handler to DB (obtained with persistence_db_cursor_create()) + * + * @return positive value for data size, negative value for error + */ +int persistence_db_cursor_get_data_size(int handlerDB) ; + + +/** + * @brief remove the cursor + * + * @param handlerDB[in] handler to DB (obtained with persistence_db_cursor_create()) + * + * @return 0 for success, negative value in case of error + */ +int persistence_db_cursor_destroy(int handlerDB) ; + + #endif /* PERSISTENCY_CLIENT_LIBRARY_DATA_ACCESS_H */ diff --git a/src/persistence_client_library_dbus_service.c b/src/persistence_client_library_dbus_service.c index 263d2b3..0311f1c 100644 --- a/src/persistence_client_library_dbus_service.c +++ b/src/persistence_client_library_dbus_service.c @@ -142,6 +142,9 @@ int setup_dbus_mainloop(void) const char *pAddress = getenv("PERS_CLIENT_DBUS_ADDRESS"); dbus_error_init(&err); + // enable locking of data structures in the D-Bus library for multi threading. + dbus_threads_init_default(); + // Connect to the bus and check for errors if(pAddress != NULL) { diff --git a/src/persistence_client_library_handle.c b/src/persistence_client_library_handle.c index 6597a10..8b2564e 100644 --- a/src/persistence_client_library_handle.c +++ b/src/persistence_client_library_handle.c @@ -37,18 +37,19 @@ #include <stdlib.h> /// handle index -static int gHandleIdx = 0; +static int gHandleIdx = 1; static int gInitialized = 0; -/// persistence handle array -PersistenceHandle_s gHandleArray[maxPersHandle]; /// open file descriptor handle array int gOpenFdArray[maxPersHandle]; + +/// persistence handle array +PersistenceHandle_s gHandleArray[maxPersHandle]; /// free handle array int gFreeHandleArray[maxPersHandle]; -int gFreeHandleIdxHead = -1; +int gFreeHandleIdxHead = 0; pthread_mutex_t gMtx; @@ -66,15 +67,15 @@ int get_persistence_handle_idx() if(pthread_mutex_lock(&gMtx) == 0) { - if(gFreeHandleIdxHead != -1) // check if we have a free spot in the array before the current max + if(gFreeHandleIdxHead > 0) // check if we have a free spot in the array before the current max { - handle = gFreeHandleArray[gFreeHandleIdxHead--]; + handle = gFreeHandleArray[--gFreeHandleIdxHead]; } else { if(gHandleIdx < maxPersHandle-1) { - handle = gHandleIdx + 1; // no free spot before current max, increment handle index + handle = gHandleIdx++; // no free spot before current max, increment handle index } else { diff --git a/src/persistence_client_library_key.c b/src/persistence_client_library_key.c index 617048b..39b8d15 100644 --- a/src/persistence_client_library_key.c +++ b/src/persistence_client_library_key.c @@ -62,7 +62,8 @@ int key_handle_open(unsigned char ldbid, char* resource_id, unsigned char user_n // get database context: database path and database key storePolicy = get_db_context(ldbid, resource_id, user_no, seat_no, resIsNoFile, dbKey, dbPath); - if(storePolicy < PersistenceStoragePolicy_LastEntry) // check if store policy is valid + if( storePolicy >= PersistenceStorage_local + && storePolicy < PersistenceStoragePolicy_LastEntry) // check if store policy is valid { if(PersistenceStorage_custom == storePolicy) { @@ -99,7 +100,7 @@ int key_handle_open(unsigned char ldbid, char* resource_id, unsigned char user_n } else { - handle = EPERS_BADPOL; + handle = storePolicy; } diff --git a/test/data/Data.tar.gz b/test/data/Data.tar.gz Binary files differindex 0257b67..beaf27a 100644 --- a/test/data/Data.tar.gz +++ b/test/data/Data.tar.gz diff --git a/test/persistence_client_library_test.c b/test/persistence_client_library_test.c index 13232b5..2d4784a 100644 --- a/test/persistence_client_library_test.c +++ b/test/persistence_client_library_test.c @@ -44,6 +44,10 @@ #include "../include/persistence_client_library_file.h" #include "../include/persistence_client_library_error_def.h" +// internal header, should normally not included in any application +// only for testing the cursor functionality +#include "../src/persistence_client_library_data_access.h" + #define BUF_SIZE 64 #define NUM_OF_FILES 3 @@ -60,37 +64,37 @@ START_TEST (test_GetData) unsigned char buffer[READ_SIZE]; memset(buffer, 0, READ_SIZE); - ret = key_read_data(0xFF, "/language/country_code", 0, 0, buffer, READ_SIZE); // "/Data/mnt-c/Appl-1/cached.gvdb" => "/Node/pos/last position" + ret = key_read_data(0xFF, "language/country_code", 0, 0, buffer, READ_SIZE); // "/Data/mnt-c/Appl-1/cached.gvdb" => "/Node/pos/last position" fail_unless(strncmp((char*)buffer, "Custom plugin -> plugin_get_data_handle", strlen((char*)buffer)) == 0, "Buffer not correctly read"); fail_unless(ret = strlen("Custom plugin -> plugin_get_data_handle")); memset(buffer, 0, READ_SIZE); - ret = key_read_data(0xFF, "/pos/last_position", 0, 0, buffer, READ_SIZE); // "/Data/mnt-c/Appl-1/cached.gvdb" => "/Node/pos/last position" + ret = key_read_data(0xFF, "pos/last_position", 0, 0, buffer, READ_SIZE); // "/Data/mnt-c/Appl-1/cached.gvdb" => "/Node/pos/last position" fail_unless(strncmp((char*)buffer, "+48° 10' 38.95\", +8° 44' 39.06\"", strlen((char*)buffer)) == 0, "Buffer not correctly read"); fail_unless(ret = strlen("+48° 10' 38.95\", +8° 44' 39.06\"")); memset(buffer, 0, READ_SIZE); - ret = key_read_data(0, "/language/current_language", 3, 0, buffer, READ_SIZE); // "/Data/mnt-wt/Shared/Public/wt.dconf" => "/User/3/language/current_language" + ret = key_read_data(0, "language/current_language", 3, 0, buffer, READ_SIZE); // "/Data/mnt-wt/Shared/Public/wt.dconf" => "/User/3/language/current_language" fail_unless(strncmp((char*)buffer, "Kisuaheli", strlen((char*)buffer)) == 0, "Buffer not correctly read"); memset(buffer, 0, READ_SIZE); - ret = key_read_data(0xFF, "/status/open_document", 3, 2, buffer, READ_SIZE); // "/Data/mnt-c/Appl-1/cached.gvdb" => "/User/3/Seat/2/status/open_document" + ret = key_read_data(0xFF, "status/open_document", 3, 2, buffer, READ_SIZE); // "/Data/mnt-c/Appl-1/cached.gvdb" => "/User/3/Seat/2/status/open_document" fail_unless(strncmp((char*)buffer, "/var/opt/user_manual_climateControl.pdf", strlen((char*)buffer)) == 0, "Buffer not correctly read"); memset(buffer, 0, READ_SIZE); - ret = key_read_data(0x20, "/address/home_address", 4, 0, buffer, READ_SIZE); // "/Data/mnt-c/Shared/Group/20/cached.dconf" => "/User/4/address/home_address" + ret = key_read_data(0x20, "address/home_address", 4, 0, buffer, READ_SIZE); // "/Data/mnt-c/Shared/Group/20/cached.dconf" => "/User/4/address/home_address" fail_unless(strncmp((char*)buffer, "55327 Heimatstadt, Wohnstrasse 31", strlen((char*)buffer)) == 0, "Buffer not correctly read"); memset(buffer, 0, READ_SIZE); - ret = key_read_data(0xFF, "/pos/last satellites", 0, 0, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-1/wt.gvdb" => "/Node/pos/last satellites" + ret = key_read_data(0xFF, "pos/last_satellites", 0, 0, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-1/wt.gvdb" => "/Node/pos/last satellites" fail_unless(strncmp((char*)buffer, "17", strlen((char*)buffer)) == 0, "Buffer not correctly read"); memset(buffer, 0, READ_SIZE); - ret = key_read_data(0x84, "/links/last link", 2, 0, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-2/wt.gvdb" => "/84/User/2/links/last link" + ret = key_read_data(0x84, "links/last_link", 2, 0, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-2/wt.gvdb" => "/84/User/2/links/last link" fail_unless(strncmp((char*)buffer, "/last_exit/brooklyn", strlen((char*)buffer)) == 0, "Buffer not correctly read"); memset(buffer, 0, READ_SIZE); - ret = key_read_data(0x84, "/links/last link", 2, 1, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-2/wt.gvdb" => "/84/User/2/links/last link" + ret = key_read_data(0x84, "links/last_link", 2, 1, buffer, READ_SIZE); // "/Data/mnt-wt/Appl-2/wt.gvdb" => "/84/User/2/links/last link" fail_unless(strncmp((char*)buffer, "/last_exit/queens", strlen((char*)buffer)) == 0, "Buffer not correctly read"); } END_TEST @@ -112,7 +116,7 @@ START_TEST (test_GetDataHandle) snprintf(sysTimeBuffer, 128, "TimeAndData: \"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900), locTime->tm_hour, locTime->tm_min, locTime->tm_sec); // open handle --------------------------------------------------- - handle = key_handle_open(0xFF, "/posHandle/last position", 0, 0); + handle = key_handle_open(0xFF, "posHandle/last_position", 0, 0); fail_unless(handle >= 0, "Failed to open handle ==> /posHandle/last position"); ret = key_handle_read_data(handle, buffer, READ_SIZE); @@ -123,7 +127,7 @@ START_TEST (test_GetDataHandle) // open handle --------------------------------------------------- - handle2 = key_handle_open(0xFF, "/statusHandle/open_document", 3, 2); + handle2 = key_handle_open(0xFF, "statusHandle/open_document", 3, 2); fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document"); size = key_handle_write_data(handle2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer)); @@ -134,7 +138,7 @@ START_TEST (test_GetDataHandle) // open handle --------------------------------------------------- memset(buffer, 0, READ_SIZE); - handle4 = key_handle_open(0xFF, "/language/country_code", 0, 0); + handle4 = key_handle_open(0xFF, "language/country_code", 0, 0); fail_unless(handle4 >= 0, "Failed to open handle /language/country_code"); ret = key_handle_read_data(handle4, buffer, READ_SIZE); @@ -147,7 +151,7 @@ START_TEST (test_GetDataHandle) // open handle --------------------------------------------------- - handle3 = key_handle_open(0xFF, "/statusHandle/open_document", 3, 2); + handle3 = key_handle_open(0xFF, "statusHandle/open_document", 3, 2); fail_unless(handle3 >= 0, "Failed to open handle /statusHandle/open_document"); ret = key_handle_read_data(handle3, buffer, READ_SIZE); @@ -186,30 +190,30 @@ START_TEST(test_SetData) // write data snprintf(sysTimeBuffer, 128, "\"%s %d.%d.%d - %d:%.2d:%.2d Uhr\"", dayOfWeek[locTime->tm_wday], locTime->tm_mday, locTime->tm_mon, (locTime->tm_year+1900), locTime->tm_hour, locTime->tm_min, locTime->tm_sec); - ret = key_write_data(0xFF, "/69", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer)); + ret = key_write_data(0xFF, "69", 1, 2, (unsigned char*)sysTimeBuffer, strlen(sysTimeBuffer)); fail_unless(ret == strlen(sysTimeBuffer), "Wrong write size"); snprintf(write1, 128, "%s %s", "/70", sysTimeBuffer); - ret = key_write_data(0xFF, "/70", 1, 2, (unsigned char*)write1, strlen(write1)); + ret = key_write_data(0xFF, "70", 1, 2, (unsigned char*)write1, strlen(write1)); fail_unless(ret == strlen(write1), "Wrong write size"); snprintf(write2, 128, "%s %s", "/key_70", sysTimeBuffer); - ret = key_write_data(0xFF, "/key_70", 1, 2, (unsigned char*)write2, strlen(write2)); + ret = key_write_data(0xFF, "key_70", 1, 2, (unsigned char*)write2, strlen(write2)); fail_unless(ret == strlen(write2), "Wrong write size"); // read data again and and verify datat has been written correctly memset(buffer, 0, READ_SIZE); - ret = key_read_data(0xFF, "/69", 1, 2, buffer, READ_SIZE); + ret = key_read_data(0xFF, "69", 1, 2, buffer, READ_SIZE); fail_unless(strncmp((char*)buffer, sysTimeBuffer, strlen(sysTimeBuffer)) == 0, "Buffer not correctly read"); fail_unless(ret == strlen(sysTimeBuffer), "Wrong read size"); memset(buffer, 0, READ_SIZE); - ret = key_read_data(0xFF, "/70", 1, 2, buffer, READ_SIZE); + ret = key_read_data(0xFF, "70", 1, 2, buffer, READ_SIZE); fail_unless(strncmp((char*)buffer, write1, strlen(write1)) == 0, "Buffer not correctly read"); fail_unless(ret == strlen(write1), "Wrong read size"); memset(buffer, 0, READ_SIZE); - ret = key_read_data(0xFF, "/key_70", 1, 2, buffer, READ_SIZE); + ret = key_read_data(0xFF, "key_70", 1, 2, buffer, READ_SIZE); fail_unless(strncmp((char*)buffer, write2, strlen(write2)) == 0, "Buffer not correctly read"); fail_unless(ret == strlen(write2), "Wrong read size"); @@ -222,10 +226,10 @@ START_TEST(test_GetDataSize) { int size = 0; - size = key_get_size(0xFF, "/status/open_document", 3, 2); + size = key_get_size(0xFF, "status/open_document", 3, 2); fail_unless(size == strlen("/var/opt/user_manual_climateControl.pdf"), "Invalid size"); - size = key_get_size(0x84, "/links/last link", 2, 1); + size = key_get_size(0x84, "links/last_link", 2, 1); fail_unless(size == strlen("/last_exit/queens"), "Invalid size"); } END_TEST @@ -238,16 +242,16 @@ START_TEST(test_DeleteData) unsigned char buffer[READ_SIZE]; // delete key - rval = key_delete(0xFF, "/key_70", 1, 2); + rval = key_delete(0xFF, "key_70", 1, 2); fail_unless(rval == 0, "Failed to delete key"); // reading from key must fail now - rval = key_read_data(0xFF, "/key_70", 1, 2, buffer, READ_SIZE); + rval = key_read_data(0xFF, "key_70", 1, 2, buffer, READ_SIZE); fail_unless(rval == EPERS_NOKEY, "Read form key key_70 works, but should fail"); - rval = key_delete(0xFF, "/70", 1, 2); + rval = key_delete(0xFF, "70", 1, 2); fail_unless(rval == 0, "Failed to delete key"); - rval = key_read_data(0xFF, "/70", 1, 2, buffer, READ_SIZE); + rval = key_read_data(0xFF, "70", 1, 2, buffer, READ_SIZE); fail_unless(rval == EPERS_NOKEY, "Read form key 70 works, but should fail"); } END_TEST @@ -286,7 +290,7 @@ START_TEST(test_DataFile) close(fd); // open ---------------------------------------------------------- - fd = file_open(0xFF, "/media/mediaDB.db", 1, 1); + fd = file_open(0xFF, "media/mediaDB.db", 1, 1); fail_unless(fd != -1, "Could not open file ==> /media/mediaDB.db"); size = file_get_size(fd); @@ -301,7 +305,7 @@ START_TEST(test_DataFile) // open ---------------------------------------------------------- - fd = file_open(0xFF, "/media/mediaDBWrite.db", 1, 1); + fd = file_open(0xFF, "media/mediaDBWrite.db", 1, 1); fail_unless(fd != -1, "Could not open file ==> /media/mediaDBWrite.db"); size = file_write_data(fd, writeBuffer, strlen(writeBuffer)); @@ -312,15 +316,15 @@ START_TEST(test_DataFile) // remove ---------------------------------------------------------- - ret = file_remove(0xFF, "/media/mediaDBWrite.db", 1, 1); + ret = file_remove(0xFF, "media/mediaDBWrite.db", 1, 1); fail_unless(ret == 0, "File can't be removed ==> /media/mediaDBWrite.db"); - fd = file_open(0xFF, "/media/mediaDBWrite.db", 1, 1); + fd = file_open(0xFF, "media/mediaDBWrite.db", 1, 1); fail_unless(fd == -1, "File can be opend, but should not ==> /media/mediaDBWrite.db"); // map file ------------------------------------------------------ - fd = file_open(0xFF, "/media/mediaDB.db", 1, 1); + fd = file_open(0xFF, "media/mediaDB.db", 1, 1); size = file_get_size(fd); file_map_data(fileMap, size, 0, fd); @@ -346,11 +350,11 @@ START_TEST(test_DataHandle) int ret = 0; // test file handles - handle1 = file_open(0xFF, "/media/mediaDB.db", 1, 1); + handle1 = file_open(0xFF, "media/mediaDB.db", 1, 1); fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB.db"); ret = file_close(handle1); - fail_unless(handle1 != -1, "Could not open file ==> /media/mediaDB.db"); + fail_unless(handle1 != -1, "Could not closefile ==> /media/mediaDB.db"); ret = file_close(1024); fail_unless(ret == -1, "Could close file, but should not!!"); @@ -360,7 +364,7 @@ START_TEST(test_DataHandle) // test key handles - handle2 = key_handle_open(0xFF, "/statusHandle/open_document", 3, 2); + handle2 = key_handle_open(0xFF, "statusHandle/open_document", 3, 2); fail_unless(handle2 >= 0, "Failed to open handle /statusHandle/open_document"); ret = key_handle_close(handle2); @@ -373,6 +377,136 @@ END_TEST +START_TEST(test_DataHandleOpen) +{ + int hd1 = -2, hd2 = -2, hd3 = -2, hd4 = -2, hd5 = -2, hd6 = -2, hd7 = -2, hd8 = -2, hd9 = -2, ret = 0; + + // open handles ---------------------------------------------------- + hd1 = key_handle_open(0xFF, "posHandle/last_position1", 0, 0); + fail_unless(hd1 == 1, "Failed to open handle ==> /posHandle/last_position1"); + + hd2 = key_handle_open(0xFF, "posHandle/last_position2", 0, 0); + fail_unless(hd2 == 2, "Failed to open handle ==> /posHandle/last_position2"); + + hd3 = key_handle_open(0xFF, "posHandle/last_position3", 0, 0); + fail_unless(hd3 == 3, "Failed to open handle ==> /posHandle/last_position3"); + + // close handles --------------------------------------------------- + ret = key_handle_close(hd1); + fail_unless(ret != -1, "Failed to close handle!!"); + + ret = key_handle_close(hd2); + fail_unless(ret != -1, "Failed to close handle!!"); + + ret = key_handle_close(hd3); + fail_unless(ret != -1, "Failed to close handle!!"); + + // open handles ---------------------------------------------------- + hd4 = key_handle_open(0xFF, "posHandle/last_position4", 0, 0); + fail_unless(hd4 == 3, "Failed to open handle ==> /posHandle/last_position4"); + + hd5 = key_handle_open(0xFF, "posHandle/last_position5", 0, 0); + fail_unless(hd5 == 2, "Failed to open handle ==> /posHandle/last_position5"); + + hd6 = key_handle_open(0xFF, "posHandle/last_position6", 0, 0); + fail_unless(hd6 == 1, "Failed to open handle ==> /posHandle/last_position6"); + + hd7 = key_handle_open(0xFF, "posHandle/last_position7", 0, 0); + fail_unless(hd7 == 4, "Failed to open handle ==> /posHandle/last_position7"); + + hd8 = key_handle_open(0xFF, "posHandle/last_position8", 0, 0); + fail_unless(hd8 == 5, "Failed to open handle ==> /posHandle/last_position8"); + + hd9 = key_handle_open(0xFF, "posHandle/last_position9", 0, 0); + fail_unless(hd9 == 6, "Failed to open handle ==> /posHandle/last_position9"); + + // close handles --------------------------------------------------- + ret = key_handle_close(hd4); + fail_unless(ret != -1, "Failed to close handle!!"); + + ret = key_handle_close(hd5); + fail_unless(ret != -1, "Failed to close handle!!"); + + ret = key_handle_close(hd6); + fail_unless(ret != -1, "Failed to close handle!!"); + + ret = key_handle_close(hd7); + fail_unless(ret != -1, "Failed to close handle!!"); + + ret = key_handle_close(hd8); + fail_unless(ret != -1, "Failed to close handle!!"); + + ret = key_handle_close(hd9); + fail_unless(ret != -1, "Failed to close handle!!"); +} +END_TEST + + + +START_TEST(test_Cursor) +{ + int handle = -1, rval = 0, size = 0, handle1 = 0; + char bufferKey[READ_SIZE]; + char bufferData[READ_SIZE]; + memset(bufferKey, 0, READ_SIZE); + memset(bufferData, 0, READ_SIZE); + + // create cursor + handle = persistence_db_cursor_create("/Data/mnt-c/lt-persistence_client_library_test/cached.itz", PersistenceStorage_local); + fail_unless(handle != -1, "Failed to create cursor!!"); + do + { + memset(bufferKey, 0, READ_SIZE); + memset(bufferData, 0, READ_SIZE); + // get key + rval = persistence_db_cursor_get_key(handle, bufferKey, 128); + fail_unless(rval != -1, "Cursor failed to get key!!"); + // get data + rval = persistence_db_cursor_get_data(handle, bufferData, 128); + fail_unless(rval != -1, "Cursor failed to get data!!"); + // get size + size = persistence_db_cursor_get_data_size(handle); + fail_unless(size != -1, "Cursor failed to get size!!"); + + //printf("Key: %s | Data: %s » Size: %d \n", bufferKey, bufferData, size); + } + while(persistence_db_cursor_next(handle) == 0); // next cursor + + // create cursor + handle1 = persistence_db_cursor_create("/Data/mnt-c/lt-persistence_client_library_test/wt.itz", PersistenceStorage_local); + printf("Handle1: %d \n", handle1); + fail_unless(handle1 != -1, "Failed to create cursor!!"); + do + { + memset(bufferKey, 0, READ_SIZE); + memset(bufferData, 0, READ_SIZE); + // get key + rval = persistence_db_cursor_get_key(handle1, bufferKey, 128); + fail_unless(rval != -1, "Cursor failed to get key!!"); + // get data + rval = persistence_db_cursor_get_data(handle1, bufferData, 128); + fail_unless(rval != -1, "Cursor failed to get data!!"); + // get size + size = persistence_db_cursor_get_data_size(handle1); + fail_unless(size != -1, "Cursor failed to get size!!"); + + //printf("Key: %s | Data: %s » Size: %d \n", bufferKey, bufferData, size); + } + while(persistence_db_cursor_next(handle1) == 0); // next cursor + + + + // destory cursor + rval = persistence_db_cursor_destroy(handle); + fail_unless(rval != -1, "Failed to destroy cursor!!"); + + rval = persistence_db_cursor_destroy(handle1); + fail_unless(rval != -1, "Failed to destroy cursor!!"); +} +END_TEST + + + static Suite * persistencyClientLib_suite() { Suite * s = suite_create("Persistency client library"); @@ -395,9 +529,14 @@ static Suite * persistencyClientLib_suite() TCase * tc_persDataHandle = tcase_create("DataHandle"); tcase_add_test(tc_persDataHandle, test_DataHandle); + TCase * tc_persDataHandleOpen = tcase_create("DataHandleOpen"); + tcase_add_test(tc_persDataHandleOpen, test_DataHandleOpen); + TCase * tc_persDataFile = tcase_create("DataFile"); tcase_add_test(tc_persDataFile, test_DataFile); + TCase * tc_Cursor = tcase_create("Cursor"); + tcase_add_test(tc_Cursor, test_Cursor); suite_add_tcase(s, tc_persGetData); suite_add_tcase(s, tc_persSetData); @@ -405,7 +544,9 @@ static Suite * persistencyClientLib_suite() suite_add_tcase(s, tc_persDeleteData); suite_add_tcase(s, tc_persGetDataHandle); suite_add_tcase(s, tc_persDataHandle); + suite_add_tcase(s, tc_persDataHandleOpen); suite_add_tcase(s, tc_persDataFile); + suite_add_tcase(s, tc_Cursor); return s; } |