Add hash table 41/86441/1
authorpr.jung <pr.jung@samsung.com>
Thu, 1 Sep 2016 07:19:38 +0000 (16:19 +0900)
committerpr.jung <pr.jung@samsung.com>
Thu, 1 Sep 2016 07:19:38 +0000 (16:19 +0900)
- Find hash table before DB

Change-Id: Icad177d108e3b990d33e81df8f8dce3cdda4c26d
Signed-off-by: pr.jung <pr.jung@samsung.com>
CMakeLists.txt
src/system_info.c

index 1dac3e9..cd9df44 100644 (file)
@@ -9,7 +9,7 @@ PROJECT(${fw_name})
 SET(INC_DIR include)
 INCLUDE_DIRECTORIES(${INC_DIR})
 
-SET(requires "dlog capi-base-common")
+SET(requires "glib-2.0 dlog capi-base-common")
 SET(pc_requires "capi-base-common")
 
 INCLUDE(FindPkgConfig)
index e2176c9..1b541c2 100644 (file)
@@ -23,6 +23,7 @@
 #include <gdbm.h>
 #include <fcntl.h>
 #include <sys/stat.h>
+#include <glib.h>
 
 #include <system_info.h>
 #include <system_info_private.h>
@@ -37,6 +38,8 @@
 
 #define GDBM_CACHE_SIZE 10 /* GDBM default == 100 */
 
+GHashTable *hashtable = NULL;
+
 enum tag_type {
        TAG_TYPE_PLATFORM,
        TAG_TYPE_CUSTOM,
@@ -62,6 +65,11 @@ API int system_info_get_value_string(system_info_key_e key, char **value)
        return SYSTEM_INFO_ERROR_NOT_SUPPORTED;
 }
 
+static void destroy_key_value(gpointer data)
+{
+       free(data);
+}
+
 static int db_get_value(enum tag_type tag, const char *key,
                const char *type, char *value, size_t len)
 {
@@ -72,6 +80,7 @@ static int db_get_value(enum tag_type tag, const char *key,
        int ret;
        char *tag_s;
        int cache_size = GDBM_CACHE_SIZE;
+       char *temp;
 
        if (!key || !type || !value)
                return SYSTEM_INFO_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
@@ -87,6 +96,23 @@ static int db_get_value(enum tag_type tag, const char *key,
                return -EINVAL;
        }
 
+       if (strstr(key, KEY_PREFIX) == key)
+               snprintf(key_internal, sizeof(key_internal),
+                               "%s:%s:%s", key, type, tag_s);
+       else
+               snprintf(key_internal, sizeof(key_internal),
+                               "%s%s:%s:%s", KEY_PREFIX, key, type, tag_s);
+
+       if (!hashtable) {
+               hashtable = g_hash_table_new_full(g_str_hash, g_str_equal, destroy_key_value, destroy_key_value);
+       } else {
+               temp = (char *)g_hash_table_lookup(hashtable, key_internal);
+               if (temp) {
+                       snprintf(value, len, "%s", temp);
+                       return SYSTEM_INFO_ERROR_NONE;
+               }
+       }
+
        db = gdbm_open(SYSTEM_INFO_DB_PATH, 0, GDBM_READER, S_IRUSR | S_IRGRP | S_IROTH, NULL);
        if (!db) {
                _E("Failed to open db (%d, %s)", gdbm_errno, gdbm_strerror(gdbm_errno)); //LCOV_EXCL_LINE
@@ -97,13 +123,6 @@ static int db_get_value(enum tag_type tag, const char *key,
        if (ret < 0)
                _E("Failed to set cache size to (%d) (ret:%d)", cache_size, gdbm_errno); //LCOV_EXCL_LINE
 
-       if (strstr(key, KEY_PREFIX) == key)
-               snprintf(key_internal, sizeof(key_internal),
-                               "%s:%s:%s", key, type, tag_s);
-       else
-               snprintf(key_internal, sizeof(key_internal),
-                               "%s%s:%s:%s", KEY_PREFIX, key, type, tag_s);
-
        d_key.dptr = key_internal;
        d_key.dsize = strlen(key_internal) + 1;
 
@@ -117,6 +136,7 @@ static int db_get_value(enum tag_type tag, const char *key,
        snprintf(value, len, "%s", d_data.dptr);
        ret = SYSTEM_INFO_ERROR_NONE;
 
+       g_hash_table_insert(hashtable, strdup(key_internal), strdup(value));
 out:
        if (db)
                gdbm_close(db);
@@ -143,6 +163,7 @@ static int system_info_get_type(enum tag_type tag, const char *key,
        int ret, i;
        char *tag_s;
        int cache_size = GDBM_CACHE_SIZE;
+       char *temp;
 
        if (!key || !type)
                return SYSTEM_INFO_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
@@ -158,6 +179,25 @@ static int system_info_get_type(enum tag_type tag, const char *key,
                return -EINVAL;
        }
 
+       if (!hashtable) {
+               hashtable = g_hash_table_new_full(g_str_hash, g_str_equal, destroy_key_value, destroy_key_value);
+       } else {
+               for (i = 0 ; i < ARRAY_SIZE(info_type); i++) {
+                       if (strstr(key, KEY_PREFIX) == key)
+                               snprintf(key_internal, sizeof(key_internal),
+                                               "%s:%s:%s", key, info_type[i].type_str, tag_s);
+                       else
+                               snprintf(key_internal, sizeof(key_internal),
+                                               "%s%s:%s:%s", KEY_PREFIX, key, info_type[i].type_str, tag_s);
+                       temp = g_hash_table_lookup(hashtable, key_internal);
+                       if (!temp)
+                               continue;
+
+                       *type = info_type[i].type_e;
+                       return SYSTEM_INFO_ERROR_NONE;
+               }
+       }
+
        db = gdbm_open(SYSTEM_INFO_DB_PATH, 0, GDBM_READER, S_IRUSR | S_IRGRP | S_IROTH, NULL);
        if (!db) {
                _E("Failed to open db (%d, %s)", gdbm_errno, gdbm_strerror(gdbm_errno)); //LCOV_EXCL_LINE
@@ -183,6 +223,8 @@ static int system_info_get_type(enum tag_type tag, const char *key,
                if (d_data.dptr) {
                        *type = info_type[i].type_e;
                        ret = SYSTEM_INFO_ERROR_NONE;
+
+                       g_hash_table_insert(hashtable, strdup(key_internal), strdup(d_data.dptr));
                        goto out;
                }
        }