metadata table cached in memory
authorVictor Kirhenshtein <victor@netxms.org>
Mon, 13 Jun 2016 11:08:24 +0000 (14:08 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Mon, 13 Jun 2016 11:08:24 +0000 (14:08 +0300)
src/server/core/config.cpp
src/server/core/main.cpp
src/server/include/nms_core.h

index ddae94c..e8c2625 100644 (file)
@@ -161,36 +161,78 @@ stop_search:
    return bSuccess;
 }
 
+/**
+ * Metadata cache
+ */
+static StringMap s_metadataCache;
+static RWLOCK s_metadataCacheLock = RWLockCreate();
+
+/**
+ * Pre-load metadata
+ */
+void MetaDataPreLoad()
+{
+   DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
+   DB_RESULT hResult = DBSelect(hdb, _T("SELECT var_name,var_value FROM metadata"));
+   if (hResult != NULL)
+   {
+      RWLockWriteLock(s_metadataCacheLock, INFINITE);
+      s_metadataCache.clear();
+      int count = DBGetNumRows(hResult);
+      for(int i = 0; i < count; i++)
+      {
+         s_metadataCache.setPreallocated(DBGetField(hResult, i, 0, NULL, 0), DBGetField(hResult, i, 1, NULL, 0));
+      }
+      RWLockUnlock(s_metadataCacheLock);
+      DBFreeResult(hResult);
+   }
+   DBConnectionPoolReleaseConnection(hdb);
+}
+
 /**
  * Read string value from metadata table
  */
-bool NXCORE_EXPORTABLE MetaDataReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault)
+bool NXCORE_EXPORTABLE MetaDataReadStr(const TCHAR *name, TCHAR *buffer, int bufSize, const TCHAR *defaultValue)
 {
-   DB_RESULT hResult;
    bool bSuccess = false;
 
-   nx_strncpy(szBuffer, szDefault, iBufSize);
-   if (_tcslen(szVar) > 127)
+   nx_strncpy(buffer, defaultValue, bufSize);
+   if (_tcslen(name) > 127)
       return false;
 
-   DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
-       DB_STATEMENT hStmt = DBPrepare(hdb, _T("SELECT var_value FROM metadata WHERE var_name=?"));
-       if (hStmt != NULL)
-       {
-               DBBind(hStmt, 1, DB_SQLTYPE_VARCHAR, szVar, DB_BIND_STATIC);
-               hResult = DBSelectPrepared(hStmt);
-               if (hResult != NULL)
-               {
-                       if (DBGetNumRows(hResult) > 0)
-                       {
-                               DBGetField(hResult, 0, 0, szBuffer, iBufSize);
-                               bSuccess = true;
-                       }
-                       DBFreeResult(hResult);
-               }
-               DBFreeStatement(hStmt);
-       }
-       DBConnectionPoolReleaseConnection(hdb);
+   RWLockReadLock(s_metadataCacheLock, INFINITE);
+   const TCHAR *value = s_metadataCache.get(name);
+   if (value != NULL)
+   {
+      nx_strncpy(buffer, value, bufSize);
+      bSuccess = true;
+   }
+   RWLockUnlock(s_metadataCacheLock);
+
+   if (!bSuccess)
+   {
+      DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
+      DB_STATEMENT hStmt = DBPrepare(hdb, _T("SELECT var_value FROM metadata WHERE var_name=?"));
+      if (hStmt != NULL)
+      {
+         DBBind(hStmt, 1, DB_SQLTYPE_VARCHAR, name, DB_BIND_STATIC);
+         DB_RESULT hResult = DBSelectPrepared(hStmt);
+         if (hResult != NULL)
+         {
+            if (DBGetNumRows(hResult) > 0)
+            {
+               DBGetField(hResult, 0, 0, buffer, bufSize);
+               RWLockWriteLock(s_metadataCacheLock, INFINITE);
+               s_metadataCache.setPreallocated(_tcsdup(name), DBGetField(hResult, 0, 0, NULL, 0));
+               RWLockUnlock(s_metadataCacheLock);
+               bSuccess = true;
+            }
+            DBFreeResult(hResult);
+         }
+         DBFreeStatement(hStmt);
+      }
+      DBConnectionPoolReleaseConnection(hdb);
+   }
    return bSuccess;
 }
 
@@ -220,6 +262,10 @@ bool NXCORE_EXPORTABLE MetaDataWriteStr(const TCHAR *varName, const TCHAR *value
    if (_tcslen(varName) > 63)
       return false;
 
+   RWLockWriteLock(s_metadataCacheLock, INFINITE);
+   s_metadataCache.set(varName, value);
+   RWLockUnlock(s_metadataCacheLock);
+
    DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
 
    // Check for variable existence
index b239240..207983a 100644 (file)
@@ -646,6 +646,8 @@ BOOL NXCORE_EXPORTABLE Initialize()
    if (lrt != 0)
       DBSetLongRunningThreshold(lrt);
 
+   MetaDataPreLoad();
+
        // Read server ID
        MetaDataReadStr(_T("ServerID"), szInfo, 256, _T(""));
        StrStrip(szInfo);
index 9dd1ae9..b0c98bc 100644 (file)
@@ -910,6 +910,7 @@ TCHAR NXCORE_EXPORTABLE *ConfigReadCLOB(const TCHAR *var, const TCHAR *defValue)
 bool NXCORE_EXPORTABLE ConfigWriteCLOB(const TCHAR *var, const TCHAR *value, bool bCreate);
 bool NXCORE_EXPORTABLE ConfigDelete(const TCHAR *name);
 
+void MetaDataPreLoad();
 bool NXCORE_EXPORTABLE MetaDataReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault);
 INT32 NXCORE_EXPORTABLE MetaDataReadInt(const TCHAR *var, UINT32 defaultValue);
 bool NXCORE_EXPORTABLE MetaDataWriteStr(const TCHAR *varName, const TCHAR *value);