Improve the query performance 55/117555/1
authorJunghoon Park <jh9216.park@samsung.com>
Mon, 6 Mar 2017 02:14:21 +0000 (11:14 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Mon, 6 Mar 2017 11:21:25 +0000 (03:21 -0800)
- Because of multi-user concept, user db file is added
- Attaching db file caused bad performance

Change-Id: I76acda5c3eba13cbcdfc4c6a47d44c502f1bbf82
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
(cherry picked from commit 38d4f9780b3d254464f4bfe81208646c4d2bf3d2)

src/service_db.c

index 3d34ea7..4052119 100755 (executable)
 
 #define SVC_COLLATION "appsvc_collation"
 
-#define QUERY_ATTACH "attach database '%s' as Global"
-#define QUERY_CREATE_VIEW_1 "CREATE temp VIEW package_app_app_control as select * "\
-       "from (select  *,0 as for_all_users from  main.package_app_app_control union select *,1 as for_all_users from Global.package_app_app_control)"
-#define QUERY_CREATE_VIEW_2 "CREATE temp VIEW package_app_info as select * "\
-       "from (select  *,0 as for_all_users from  main.package_app_info union select *,1 as for_all_users from Global.package_app_info)"
 #define QUERY_CREATE_TABLE_APPSVC "create table if not exists appsvc " \
        "(operation text, " \
        "mime_type text, " \
@@ -62,37 +57,7 @@ struct allowed_info_s {
 
 static sqlite3 *svc_db;
 static sqlite3 *app_info_db;
-
-static int __attach_create_view_appinfo_db(sqlite3 *handle, uid_t uid)
-{
-       char *error_message = NULL;
-       char query_attach[QUERY_MAX_LEN] = {'\0'};
-       if (uid != GLOBAL_USER) {
-               snprintf(query_attach, QUERY_MAX_LEN - 1, QUERY_ATTACH, APP_INFO_DB_PATH);
-               if (SQLITE_OK !=
-                       sqlite3_exec(handle, query_attach,
-                                NULL, NULL, &error_message)) {
-                       _D("Don't execute query = %s error message = %s\n",
-                                  query_attach, error_message);
-                       sqlite3_free(error_message);
-               }
-               if (SQLITE_OK !=
-                       sqlite3_exec(handle, QUERY_CREATE_VIEW_1,
-                               NULL, NULL, &error_message)) {
-                       _D("Don't execute query = %s error message = %s\n",
-                               QUERY_CREATE_VIEW_1, error_message);
-                       sqlite3_free(error_message);
-               }
-               if (SQLITE_OK !=
-                       sqlite3_exec(handle, QUERY_CREATE_VIEW_2,
-                               NULL, NULL, &error_message)) {
-                       _D("Don't execute query = %s error message = %s\n",
-                               QUERY_CREATE_VIEW_2, error_message);
-                       sqlite3_free(error_message);
-               }
-       }
-       return SQLITE_OK;
-}
+static sqlite3 *global_app_info_db;
 
 static char *__get_svc_db(uid_t uid)
 {
@@ -216,7 +181,7 @@ static int __init_app_info_db(uid_t uid)
        int rc;
        char *db_path;
 
-       if (app_info_db) {
+       if (app_info_db && global_app_info_db) {
                _D("Already initialized\n");
                return 0;
        }
@@ -235,26 +200,44 @@ static int __init_app_info_db(uid_t uid)
                goto err;
        }
 
-       rc = __attach_create_view_appinfo_db(app_info_db, uid);
+       rc = sqlite3_exec(app_info_db, "PRAGMA journal_mode = PERSIST",
+                                       NULL, NULL, NULL);
        if (SQLITE_OK != rc) {
                _D("Fail to change journal mode\n");
                goto err;
        }
 
-       rc = sqlite3_exec(app_info_db, "PRAGMA journal_mode = PERSIST",
+       sqlite3_create_collation(app_info_db, SVC_COLLATION, SQLITE_UTF8,
+                                       NULL, __collate_appsvc);
+
+       rc = sqlite3_open_v2(APP_INFO_DB_PATH, &global_app_info_db, SQLITE_OPEN_READONLY, NULL);
+       if (rc) {
+               _E("Can't open database: %d, %s, extended: %d", rc, sqlite3_errmsg(global_app_info_db),
+                  sqlite3_extended_errcode(global_app_info_db));
+               goto err;
+       }
+
+       rc = sqlite3_exec(global_app_info_db, "PRAGMA journal_mode = PERSIST",
                                        NULL, NULL, NULL);
        if (SQLITE_OK != rc) {
                _D("Fail to change journal mode\n");
                goto err;
        }
 
-       sqlite3_create_collation(app_info_db, SVC_COLLATION, SQLITE_UTF8,
+       sqlite3_create_collation(global_app_info_db, SVC_COLLATION, SQLITE_UTF8,
                                        NULL, __collate_appsvc);
 
        return 0;
 err:
-       sqlite3_close(app_info_db);
-       app_info_db = NULL;
+       if (app_info_db) {
+               sqlite3_close(app_info_db);
+               app_info_db = NULL;
+       }
+
+       if (global_app_info_db) {
+               sqlite3_close(global_app_info_db);
+               global_app_info_db = NULL;
+       }
 
        return -1;
 }
@@ -569,7 +552,43 @@ int _svc_db_adjust_list_with_submode(int mainapp_mode, char *win_id, GSList **pk
                }
        }
 
-       ret = sqlite3_finalize(stmt);
+       sqlite3_finalize(stmt);
+       ret = sqlite3_prepare(global_app_info_db, query, sizeof(query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               _E("prepare error, ret = %d, extended = %d\n", ret,
+                       sqlite3_extended_errcode(app_info_db));
+               return -1;
+       }
+
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               subappid = (char *)sqlite3_column_text(stmt, 0);
+               submode_mainid = (char *)sqlite3_column_text(stmt, 1);
+               subapp = g_slist_find_custom(*pkg_list, subappid, __appid_compare);
+
+               if (subapp == NULL)
+                       continue;
+
+               /* find if the main app is in the pkg_list */
+               mainapp = g_slist_find_custom(*pkg_list, submode_mainid, __appid_compare);
+               if (mainapp == NULL)
+                       continue;
+
+               if (win_id && !mainapp_mode)
+                       /* subapp mode - remove mainapp from list */
+                       excluded_appid = (char *)mainapp->data;
+               else
+                       /* mainapp mode - remove subapp from list */
+                       excluded_appid = (char *)subapp->data;
+
+               if (excluded_appid) {
+                       _E("remove %s from app list with submode", excluded_appid);
+                       *pkg_list = g_slist_remove(*pkg_list, excluded_appid);
+                       free(excluded_appid);
+                       excluded_appid = NULL;
+               }
+       }
+
+       sqlite3_finalize(stmt);
        return 0;
 }
 
@@ -722,7 +741,32 @@ int _svc_db_exec_query(const char *query, GSList **pkg_list, uid_t uid)
                }
        }
 
-       ret = sqlite3_finalize(stmt);
+       sqlite3_finalize(stmt);
+       ret = sqlite3_prepare(global_app_info_db, query, strlen(query), &stmt, NULL);
+       if (ret != SQLITE_OK) {
+               _E("prepare error, ret = %d, extended = %d\n",
+                               ret, sqlite3_extended_errcode(global_app_info_db));
+               return -1;
+       }
+
+       while (sqlite3_step(stmt) == SQLITE_ROW) {
+               str = (char *)sqlite3_column_text(stmt, 0);
+               found = 0;
+               for (iter = *pkg_list; iter != NULL; iter = g_slist_next(iter)) {
+                       pkgname = (char *)iter->data;
+                       if (strncmp(str, pkgname, MAX_PACKAGE_STR_SIZE - 1) == 0) {
+                               found = 1;
+                               break;
+                       }
+               }
+               if (found == 0) {
+                       pkgname = strdup(str);
+                       *pkg_list = g_slist_append(*pkg_list, (void *)pkgname);
+                       _D("%s is added", pkgname);
+               }
+       }
+
+       sqlite3_finalize(stmt);
 
        return 0;
 }