Optimization for custom plugins; corrected return values
authorIngo Huerner <ingo.huerner@xse.de>
Tue, 27 Aug 2013 12:20:24 +0000 (14:20 +0200)
committerIngo Huerner <ingo.huerner@xse.de>
Tue, 27 Aug 2013 12:20:24 +0000 (14:20 +0200)
include/persistence_client_library_error_def.h
include/persistence_client_library_key.h
src/persistence_client_library_db_access.c
src/persistence_client_library_handle.c
src/persistence_client_library_handle.h
src/persistence_client_library_key.c

index 6897667..87042ae 100644 (file)
@@ -93,6 +93,8 @@ extern "C" {
 #define EPERS_NOTIFY_SIG         (-31)
 /// client library has not been initialized
 #define EPERS_NOT_INITIALIZED  (-32)
+// max buffer size
+#define EPERS_MAX_BUFF_SIZE      (-33)
 
 #ifdef __cplusplus
 }
index bbd1c62..3a1c6b0 100644 (file)
@@ -198,7 +198,7 @@ int pclKeyHandleRegisterNotifyOnChange(int key_handle, pclChangeNotifyCallback_t
  *                    use environment variable PERS_MAX_KEY_VAL_DATA_SIZE to modify default size in bytes
  *
  * @return positive value: the bytes written; On error a negative value will be returned with the following error codes:
- * ::EPERS_LOCKFS
+ * ::EPERS_LOCKFS ::EPERS_MAX_BUFF_SIZE
  */
 int pclKeyHandleWriteData(int key_handle, unsigned char* buffer, int buffer_size);
 
index aab7a1f..4226076 100644 (file)
@@ -302,8 +302,6 @@ int pers_db_read_key(char* dbPath, char* key, PersistenceInfo_s* info, unsigned
             read_size = EPERS_NOPRCTABLE;
          }
       }
-
-
    }
    else if(PersistenceStorage_custom == info->configKey.storage)   // custom storage implementation via custom library
    {
index a6a952d..2550b41 100644 (file)
@@ -19,6 +19,7 @@
 
 
 #include "persistence_client_library_handle.h"
+#include "persistence_client_library_custom_loader.h"
 
 #include <pthread.h>
 #include <stdlib.h>
@@ -48,7 +49,7 @@ pthread_mutex_t gMtx;
 
 
 /// get persistence handle
-int get_persistence_handle_idx()
+int get_persistence_handle_idx(char* dbPath, char* key, PersistenceInfo_s* info)
 {
    int handle = 0;
 
@@ -60,38 +61,77 @@ int get_persistence_handle_idx()
 
    if(pthread_mutex_lock(&gMtx) == 0)
    {
-      if(gFreeHandleIdxHead > 0)   // check if we have a free spot in the array before the current max
+      if(   PersistenceStorage_shared == info->configKey.storage
+         || PersistenceStorage_local == info->configKey.storage)
       {
-         handle = gFreeHandleArray[--gFreeHandleIdxHead];
+         if(gFreeHandleIdxHead > 0)   // check if we have a free spot in the array before the current max
+         {
+            handle = gFreeHandleArray[--gFreeHandleIdxHead];
+         }
+         else
+         {
+            if(gHandleIdx < MaxPersHandle-1)
+            {
+               handle = gHandleIdx++;  // no free spot before current max, increment handle index
+            }
+            else
+            {
+               handle = -1;
+               DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_persistence_handle_idx => Reached maximum of open handles: "), DLT_INT(MaxPersHandle));
+            }
+         }
       }
-      else
+      else if(PersistenceStorage_custom ==  info->configKey.storage)
       {
-         if(gHandleIdx < MaxPersHandle-1)
+         int idx =  custom_client_name_to_id(dbPath, 1);
+         char workaroundPath[128];  // workaround, because /sys/ can not be accessed on host!!!!
+         snprintf(workaroundPath, 128, "%s%s", "/Data", dbPath  );
+
+         if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_open != NULL) )
          {
-            handle = gHandleIdx++;  // no free spot before current max, increment handle index
+            int flag = 0, mode = 0;
+            handle = gPersCustomFuncs[idx].custom_plugin_handle_open(workaroundPath, flag, mode);
          }
          else
          {
-            handle = -1;
-            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("get_persistence_handle_idx => Reached maximum of open handles: "), DLT_INT(MaxPersHandle));
+            handle = EPERS_NOPLUGINFUNCT;
          }
       }
       pthread_mutex_unlock(&gMtx);
    }
-
    return handle;
 }
 
 
 /// close persistence handle
-void set_persistence_handle_close_idx(int handle)
+int set_persistence_handle_close_idx(int handle, char* dbPath, char* key, PersistenceInfo_s* info)
 {
+   int rval = 0;
+
    if(pthread_mutex_lock(&gMtx) == 0)
    {
-      if(gFreeHandleIdxHead < MaxPersHandle)
+      if(   PersistenceStorage_shared == info->configKey.storage
+         || PersistenceStorage_local == info->configKey.storage)
       {
-         gFreeHandleArray[gFreeHandleIdxHead++] = handle;
+         if(gFreeHandleIdxHead < MaxPersHandle)
+         {
+            gFreeHandleArray[gFreeHandleIdxHead++] = handle;
+         }
       }
+      else if(PersistenceStorage_custom == gKeyHandleArray[handle].info.configKey.storage )
+      {
+         int idx =  custom_client_name_to_id(gKeyHandleArray[handle].dbPath, 1);
+
+         if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_close != NULL) )
+         {
+            rval = gPersCustomFuncs[idx].custom_plugin_handle_close(handle);
+         }
+         else
+         {
+            rval = EPERS_NOPLUGINFUNCT;
+         }
+      }
+
       pthread_mutex_unlock(&gMtx);
    }
 }
index 6d56c46..e1c23a6 100644 (file)
@@ -63,15 +63,17 @@ extern int gOpenFdArray[MaxPersHandle];
  *
  * @return a new handle or 0 if an error occured
  */
-int get_persistence_handle_idx();
+int get_persistence_handle_idx(char* dbPath, char* key, PersistenceInfo_s* info);
 
 
 /**
  * @brief close persistence handle
  *
  * @param the handle to close
+ *
+ * @return error code
  */
-void set_persistence_handle_close_idx(int handle);
+int set_persistence_handle_close_idx(int handle, char* dbPath, char* key, PersistenceInfo_s* info);
 
 
 
index 3b481b8..8afc6c5 100644 (file)
@@ -26,7 +26,7 @@
 #include "persistence_client_library_handle.h"
 #include "persistence_client_library_pas_interface.h"
 #include "persistence_client_library_prct_access.h"
-#include "persistence_client_library_custom_loader.h"
+
 
 
 // ----------------------------------------------------------------------------
@@ -56,43 +56,21 @@ int pclKeyHandleOpen(unsigned int ldbid, const char* resource_id, unsigned int u
       if(   (handle >= 0)
          && (dbContext.configKey.type == PersistenceResourceType_key) )          // check if type matches
       {
-         if(dbContext.configKey.storage < PersistenceStorage_LastEntry)    // check if store policy is valid
-         {
-            if(PersistenceStorage_custom ==  dbContext.configKey.storage)
-            {
-               int idx =  custom_client_name_to_id(dbPath, 1);
-               char workaroundPath[128];  // workaround, because /sys/ can not be accessed on host!!!!
-               snprintf(workaroundPath, 128, "%s%s", "/Data", dbPath  );
+         handle = get_persistence_handle_idx(dbPath, dbKey, &dbContext);
 
-               if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_open != NULL) )
-               {
-                  int flag = 0, mode = 0;
-                  handle = gPersCustomFuncs[idx].custom_plugin_handle_open(workaroundPath, flag, mode);
-               }
-               else
-               {
-                  handle = EPERS_NOPLUGINFUNCT;
-               }
-            }
-            else
-            {
-               handle = get_persistence_handle_idx();
-            }
-
-            if((handle < MaxPersHandle) && (0 <= handle))
-            {
-               // remember data in handle array
-               strncpy(gKeyHandleArray[handle].dbPath, dbPath, DbPathMaxLen);
-               strncpy(gKeyHandleArray[handle].dbKey,  dbKey,  DbKeyMaxLen);
-               strncpy(gKeyHandleArray[handle].resourceID,  resource_id,  DbResIDMaxLen);
-               gKeyHandleArray[handle].dbPath[DbPathMaxLen-1] = '\0'; // Ensures 0-Termination
-               gKeyHandleArray[handle].dbKey[ DbPathMaxLen-1] = '\0'; // Ensures 0-Termination
-               gKeyHandleArray[handle].info = dbContext;
-            }
-            else
-            {
-               DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyHandleOpen: error - handleId out of bounds:"), DLT_INT(handle));
-            }
+         if((handle < MaxPersHandle) && (0 <= handle))
+         {
+            // remember data in handle array
+            strncpy(gKeyHandleArray[handle].dbPath, dbPath, DbPathMaxLen);
+            strncpy(gKeyHandleArray[handle].dbKey,  dbKey,  DbKeyMaxLen);
+            strncpy(gKeyHandleArray[handle].resourceID,  resource_id,  DbResIDMaxLen);
+            gKeyHandleArray[handle].dbPath[DbPathMaxLen-1] = '\0'; // Ensures 0-Termination
+            gKeyHandleArray[handle].dbKey[ DbPathMaxLen-1] = '\0'; // Ensures 0-Termination
+            gKeyHandleArray[handle].info = dbContext;
+         }
+         else
+         {
+            DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyHandleOpen: error - handleId out of bounds:"), DLT_INT(handle));
          }
       }
       else
@@ -118,23 +96,9 @@ int pclKeyHandleClose(int key_handle)
    {
       if(key_handle < MaxPersHandle)
       {
-         if(PersistenceStorage_custom == gKeyHandleArray[key_handle].info.configKey.storage )
-         {
-            int idx =  custom_client_name_to_id(gKeyHandleArray[key_handle].dbPath, 1);
-
-            if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_close != NULL) )
-            {
-               rval = gPersCustomFuncs[idx].custom_plugin_handle_close(key_handle);
-            }
-            else
-            {
-               rval = EPERS_NOPLUGINFUNCT;
-            }
-         }
-         else
-         {
-            set_persistence_handle_close_idx(key_handle);
-         }
+         rval= set_persistence_handle_close_idx(key_handle, gKeyHandleArray[key_handle].dbPath,
+                                                            gKeyHandleArray[key_handle].dbKey,
+                                                            &gKeyHandleArray[key_handle].info);
 
          // invalidate entries
          memset(gKeyHandleArray[key_handle].dbPath, 0, DbPathMaxLen);
@@ -167,24 +131,12 @@ int pclKeyHandleGetSize(int key_handle)
    {
       if(key_handle < MaxPersHandle)
       {
-         if(PersistenceStorage_custom ==  gKeyHandleArray[key_handle].info.configKey.storage)
-         {
-            int idx =  custom_client_name_to_id(gKeyHandleArray[key_handle].dbPath, 1);
-
-            if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_get_size != NULL) )
-            {
-               size = gPersCustomFuncs[idx].custom_plugin_get_size(gKeyHandleArray[key_handle].dbPath);
-            }
-            else
-            {
-               size = EPERS_NOPLUGINFUNCT;
-            }
-         }
-         else
-         {
-            size = pers_db_get_key_size(gKeyHandleArray[key_handle].dbPath, gKeyHandleArray[key_handle].dbKey,
-                                             &gKeyHandleArray[key_handle].info);
-         }
+         size = pers_db_get_key_size(gKeyHandleArray[key_handle].dbPath, gKeyHandleArray[key_handle].dbKey,
+                                      &gKeyHandleArray[key_handle].info);
+      }
+      else
+      {
+         size = EPERS_MAXHANDLE;
       }
    }
 
@@ -197,35 +149,23 @@ int pclKeyHandleReadData(int key_handle, unsigned char* buffer, int buffer_size)
 {
    int size = EPERS_NOT_INITIALIZED;
 
-   //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclKeyHandleReadData: "),
-   //             DLT_INT(gKeyHandleArray[key_handle].info.context.ldbid), DLT_STRING(gKeyHandleArray[key_handle].resourceID) );
+      //DLT_LOG(gDLTContext, DLT_LOG_INFO, DLT_STRING("pclKeyHandleReadData: "),
+      //             DLT_INT(gKeyHandleArray[key_handle].info.context.ldbid), DLT_STRING(gKeyHandleArray[key_handle].resourceID) );
 
-   if(gPclInitialized >= PCLinitialized)
-   {
-      if(key_handle < MaxPersHandle)
+      if(gPclInitialized >= PCLinitialized)
       {
-         if(PersistenceStorage_custom ==  gKeyHandleArray[key_handle].info.configKey.storage)
+         if(key_handle < MaxPersHandle)
          {
-            int idx =  custom_client_name_to_id(gKeyHandleArray[key_handle].dbPath, 1);
-
-            if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_get_data != NULL) )
-            {
-               size = gPersCustomFuncs[idx].custom_plugin_handle_get_data(key_handle, (char*)buffer, buffer_size-1);
-            }
-            else
-            {
-               size = EPERS_NOPLUGINFUNCT;
-            }
+            size = pers_db_read_key(gKeyHandleArray[key_handle].dbPath, gKeyHandleArray[key_handle].dbKey,
+                                    &gKeyHandleArray[key_handle].info, buffer, buffer_size);
          }
          else
          {
-            size = pers_db_read_key(gKeyHandleArray[key_handle].dbPath, gKeyHandleArray[key_handle].dbKey,
-                                        &gKeyHandleArray[key_handle].info, buffer, buffer_size);
+            size = EPERS_MAXHANDLE;
          }
       }
-   }
 
-   return size;
+      return size;
 }
 
 
@@ -246,6 +186,10 @@ int pclKeyHandleRegisterNotifyOnChange(int key_handle, pclChangeNotifyCallback_t
                                       gKeyHandleArray[key_handle].info.context.user_no,
                                       gKeyHandleArray[key_handle].info.context.seat_no, callback);
       }
+      else
+      {
+         rval = EPERS_MAXHANDLE;
+      }
    }
 
    return rval;
@@ -268,24 +212,9 @@ int pclKeyHandleWriteData(int key_handle, unsigned char* buffer, int buffer_size
          {
             if(key_handle < MaxPersHandle)
             {
-               if(PersistenceStorage_custom ==  gKeyHandleArray[key_handle].info.configKey.storage)
-               {
-                  int idx =  custom_client_name_to_id(gKeyHandleArray[key_handle].dbPath, 1);
-
-                  if( (idx < PersCustomLib_LastEntry) && (gPersCustomFuncs[idx].custom_plugin_handle_set_data != NULL) )
-                  {
-                     size = gPersCustomFuncs[idx].custom_plugin_handle_set_data(key_handle, (char*)buffer, buffer_size-1);
-                  }
-                  else
-                  {
-                     size = EPERS_NOPLUGINFUNCT;
-                  }
-               }
-               else
-               {
-                  size = pers_db_write_key(gKeyHandleArray[key_handle].dbPath, gKeyHandleArray[key_handle].dbKey,
-                                           &gKeyHandleArray[key_handle].info, buffer, buffer_size);
-               }
+
+               size = pers_db_write_key(gKeyHandleArray[key_handle].dbPath, gKeyHandleArray[key_handle].dbKey,
+                                        &gKeyHandleArray[key_handle].info, buffer, buffer_size);
             }
             else
             {
@@ -294,6 +223,7 @@ int pclKeyHandleWriteData(int key_handle, unsigned char* buffer, int buffer_size
          }
          else
          {
+            size = EPERS_MAX_BUFF_SIZE;
             DLT_LOG(gDLTContext, DLT_LOG_ERROR, DLT_STRING("pclKeyHandleWriteData: error - buffer_size to big, limit is [bytes]:"), DLT_INT(gMaxKeyValDataSize));
          }
       }