Improve performance for resolving appid 41/60441/6 accepted/tizen/common/20160229.161108 accepted/tizen/common/20160301.120608 accepted/tizen/ivi/20160229.100325 accepted/tizen/mobile/20160229.100233 accepted/tizen/tv/20160229.100250 accepted/tizen/wearable/20160229.100305 submit/tizen/20160229.023838 submit/tizen_common/20160229.190608
authorJunghoon Park <jh9216.park@samsung.com>
Fri, 26 Feb 2016 07:48:58 +0000 (16:48 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Mon, 29 Feb 2016 01:06:57 +0000 (10:06 +0900)
- Optimize query statements (240ms  ==> 100ms)

Change-Id: I3986d3badc7e585e6763097159c2adc712012250
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
include/aul_svc_db.h
src/service.c
src/service_db.c

index 90e4383..ca38f02 100755 (executable)
@@ -41,12 +41,12 @@ int _svc_db_delete_with_pkgname(const char *pkg_name, uid_t uid);
 char* _svc_db_get_app(const char *op, const char *mime_type, const char *uri, uid_t uid);
 int _svc_db_is_defapp(const char *pkg_name, uid_t uid);
 int _svc_db_adjust_list_with_submode(int mainapp_mode, char *win_id, GSList **pkg_list, uid_t uid);
-int _svc_db_get_list_with_condition(char *op, char *uri, char *mime, GSList **pkg_list, uid_t uid);
-int _svc_db_get_list_with_collation(char *op, char *uri, char *mime, GSList **pkg_list, uid_t uid);
 int _svc_db_get_list_with_all_defapps(GSList **pkg_list, uid_t uid);
 int _svc_db_delete_all(uid_t uid);
 
-
+char *_svc_db_query_builder_add(char *old_query, char *op, char *uri, char *mime, bool collate);
+char *_svc_db_query_builder_build(char *old_query, bool collate);
+int _svc_db_exec_query(const char *query, GSList **pkg_list, uid_t uid);
 
 #ifdef __cplusplus
 }
index f0c850f..6d87940 100755 (executable)
@@ -466,56 +466,47 @@ static char* __get_alias_appid(char *appid)
        return alias_id;
 }
 
-static int __get_list_with_condition_mime_extened(char *op, char *uri,
-                                       char *mime, char *m_type, char *s_type,
-                                       GSList **pkg_list, uid_t uid)
+static char* __make_query(char *query, char *op, char *uri,
+                       char *mime, char *m_type, char *s_type)
 {
-       char *tmp;
+       char tmp[MAX_MIME_STR_SIZE] = { 0, };
 
-       tmp = malloc(MAX_MIME_STR_SIZE);
-       if (tmp == NULL) {
-               _E("out of memory");
-               return -1;
-       }
-
-       _svc_db_get_list_with_condition(op, uri, mime, pkg_list, uid);
+       query = _svc_db_query_builder_add(query, op, uri, mime, false);
        if ((strncmp(mime, "NULL", 4) != 0) && (strncmp(s_type, "%", 1) != 0)) {
                snprintf(tmp, MAX_MIME_STR_SIZE - 1, "%s/*", m_type);
-               _svc_db_get_list_with_condition(op, uri, tmp, pkg_list, uid);
+               query = _svc_db_query_builder_add(query, op, uri, tmp, false);
        }
+
        if ((strncmp(mime, "NULL", 4) != 0) && (strncmp(m_type, "%", 1) != 0)) {
                snprintf(tmp, MAX_MIME_STR_SIZE - 1, "*/*");
-               _svc_db_get_list_with_condition(op, uri, tmp, pkg_list, uid);
+               query = _svc_db_query_builder_add(query, op, uri, tmp, false);
        }
 
-       free(tmp);
-
-       return 0;
+       return query;
 }
 
 static int __get_list_with_condition_mime_extened_with_collation(char *op,
                                char *uri, char *mime, char *m_type,
                                char *s_type, GSList **pkg_list, uid_t uid)
 {
-       char *tmp;
+       char tmp[MAX_MIME_STR_SIZE];
+       char *query = NULL;
 
-       tmp = malloc(MAX_MIME_STR_SIZE);
-       if (tmp == NULL) {
-               _E("out of memory");
-               return -1;
-       }
+       query = _svc_db_query_builder_add(query, op, uri, mime, true);
 
-       _svc_db_get_list_with_collation(op, uri, mime, pkg_list, uid);
        if ((strncmp(mime, "NULL", 4) != 0) && (strncmp(s_type, "%", 1) != 0)) {
                snprintf(tmp, MAX_MIME_STR_SIZE - 1, "%s/*", m_type);
-               _svc_db_get_list_with_collation(op, uri, tmp, pkg_list, uid);
+               query = _svc_db_query_builder_add(query, op, uri, tmp, true);
        }
        if ((strncmp(mime, "NULL", 4) != 0) && (strncmp(m_type, "%", 1) != 0)) {
                snprintf(tmp, MAX_MIME_STR_SIZE - 1, "*/*");
-               _svc_db_get_list_with_collation(op, uri, tmp, pkg_list, uid);
+               query = _svc_db_query_builder_add(query, op, uri, tmp, true);
        }
 
-       free(tmp);
+       query = _svc_db_query_builder_build(query, true);
+       _svc_db_exec_query(query, pkg_list, uid);
+       if (query)
+               free(query);
 
        return 0;
 }
@@ -721,6 +712,7 @@ API int aul_svc_run_service_for_uid(bundle *b, int request_code,
        GSList *pkg_list = NULL;
        GSList *iter = NULL;
        char *list_item;
+       char *query = NULL;
 
        if (b == NULL) {
                _E("bundle for aul_svc_set_appid is NULL");
@@ -772,22 +764,28 @@ API int aul_svc_run_service_for_uid(bundle *b, int request_code,
                                info.mime, info.m_type, info.s_type, &pkg_list, uid);
                pkg_count = g_slist_length(pkg_list);
                if (pkg_count > 0) {
-
                        if (info.uri_r_info) {
-                               __get_list_with_condition_mime_extened(info.op, info.uri_r_info,
-                                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+                               query = __make_query(query, info.op, info.uri_r_info,
+                                       info.mime, info.m_type, info.s_type);
                        }
 
-                       __get_list_with_condition_mime_extened(info.op, info.scheme,
-                               info.mime, info.m_type, info.s_type, &pkg_list, uid);
+                       query = __make_query(query, info.op, info.scheme,
+                               info.mime, info.m_type, info.s_type);
 
-                       __get_list_with_condition_mime_extened(info.op, "*",
-                               info.mime, info.m_type, info.s_type, &pkg_list, uid);
+                       query = __make_query(query, info.op, "*",
+                               info.mime, info.m_type, info.s_type);
 
                        if (info.scheme && (strcmp(info.scheme, "file") == 0)
                                && info.mime && (strcmp(info.mime, "NULL") != 0)) {
-                               __get_list_with_condition_mime_extened(info.op, "NULL",
-                                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+                               query = __make_query(query, info.op, "NULL",
+                                       info.mime, info.m_type, info.s_type);
+                       }
+
+                       query = _svc_db_query_builder_build(query, false);
+                       _svc_db_exec_query(query, &pkg_list, uid);
+                       if (query) {
+                               free(query);
+                               query = NULL;
                        }
 
                        if (info.category)
@@ -830,20 +828,27 @@ API int aul_svc_run_service_for_uid(bundle *b, int request_code,
                pkgname = _svc_db_get_app(info.op, info.origin_mime, info.uri_r_info, uid);
 
                if (pkgname == NULL) {
-                       __get_list_with_condition_mime_extened(info.op, info.uri_r_info,
-                                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+                       query = __make_query(query, info.op, info.uri_r_info,
+                                       info.mime, info.m_type, info.s_type);
                        pkg_count = g_slist_length(pkg_list);
                        if (pkg_count > 0) {
-                               __get_list_with_condition_mime_extened(info.op, info.scheme,
-                                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+                               query = __make_query(query, info.op, info.scheme,
+                                       info.mime, info.m_type, info.s_type);
 
-                               __get_list_with_condition_mime_extened(info.op, "*",
-                                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+                               query = __make_query(query, info.op, "*",
+                                       info.mime, info.m_type, info.s_type);
 
                                if (info.scheme && (strcmp(info.scheme, "file") == 0)
                                        && info.mime && (strcmp(info.mime, "NULL") != 0)) {
-                                       __get_list_with_condition_mime_extened(info.op, "NULL",
-                                               info.mime, info.m_type, info.s_type, &pkg_list, uid);
+                                       query = __make_query(query, info.op, "NULL",
+                                               info.mime, info.m_type, info.s_type);
+                               }
+
+                               query = _svc_db_query_builder_build(query, false);
+                               _svc_db_exec_query(query, &pkg_list, uid);
+                               if (query) {
+                                       free(query);
+                                       query = NULL;
                                }
 
                                if (info.category)
@@ -867,6 +872,13 @@ API int aul_svc_run_service_for_uid(bundle *b, int request_code,
                                                        cbfunc, data, uid);
                                        goto end;
                                }
+                       } else {
+                               query = _svc_db_query_builder_build(query, false);
+                               _svc_db_exec_query(query, &pkg_list, uid);
+                               if (query) {
+                                       free(query);
+                                       query = NULL;
+                               }
                        }
                        for (iter = pkg_list; iter != NULL; iter = g_slist_next(iter)) {
                                list_item = (char *)iter->data;
@@ -886,16 +898,24 @@ API int aul_svc_run_service_for_uid(bundle *b, int request_code,
        pkgname = _svc_db_get_app(info.op, info.origin_mime, info.scheme, uid);
 
        if (pkgname == NULL) {
-               __get_list_with_condition_mime_extened(info.op, info.scheme,
-                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+               query = __make_query(query, info.op, info.scheme,
+                       info.mime, info.m_type, info.s_type);
 
-               __get_list_with_condition_mime_extened(info.op, "*",
-                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+               query = __make_query(query, info.op, "*",
+                       info.mime, info.m_type, info.s_type);
 
                if (info.scheme && (strcmp(info.scheme, "file") == 0)
                        && info.mime && (strcmp(info.mime, "NULL") != 0)) {
-                       __get_list_with_condition_mime_extened(info.op, "NULL",
-                               info.mime, info.m_type, info.s_type, &pkg_list, uid);
+                       query = __make_query(query, info.op, "NULL",
+                               info.mime, info.m_type, info.s_type);
+               }
+
+               query = _svc_db_query_builder_build(query, false);
+               _svc_db_exec_query(query, &pkg_list, uid);
+
+               if (query) {
+                       free(query);
+                       query = NULL;
                }
 
                if (info.category)
@@ -954,6 +974,7 @@ API int aul_svc_get_list_for_uid(bundle *b, aul_svc_info_iter_fn iter_fn,
 
        GSList *pkg_list = NULL;
        GSList *iter = NULL;
+       char *query = NULL;
 
        if (b == NULL) {
                _E("bundle for aul_svc_run_service is NULL");
@@ -981,20 +1002,27 @@ API int aul_svc_get_list_for_uid(bundle *b, aul_svc_info_iter_fn iter_fn,
                        info.mime, info.m_type, info.s_type, &pkg_list, uid);
 
        if (info.uri_r_info) {
-               __get_list_with_condition_mime_extened(info.op, info.uri_r_info,
-                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+               query = __make_query(query, info.op, info.uri_r_info,
+                       info.mime, info.m_type, info.s_type);
        }
 
-       __get_list_with_condition_mime_extened(info.op, info.scheme,
-               info.mime, info.m_type, info.s_type, &pkg_list, uid);
+       query = __make_query(query, info.op, info.scheme,
+               info.mime, info.m_type, info.s_type);
 
-       __get_list_with_condition_mime_extened(info.op, "*",
-               info.mime, info.m_type, info.s_type, &pkg_list, uid);
+       query = __make_query(query, info.op, "*",
+               info.mime, info.m_type, info.s_type);
 
        if (info.scheme && (strcmp(info.scheme, "file") == 0)
                && info.mime && (strcmp(info.mime, "NULL") != 0)) {
-               __get_list_with_condition_mime_extened(info.op, "NULL",
-                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+               query = __make_query(query, info.op, "NULL",
+                       info.mime, info.m_type, info.s_type);
+       }
+
+       query = _svc_db_query_builder_build(query, false);
+       _svc_db_exec_query(query, &pkg_list, uid);
+       if (query) {
+               free(query);
+               query = NULL;
        }
 
        if (info.category)
index 3209b04..2b58015 100755 (executable)
@@ -634,8 +634,7 @@ int _svc_db_adjust_list_with_submode(int mainapp_mode, char *win_id, GSList **pk
        return 0;
 }
 
-int _svc_db_get_list_with_condition(char *op, char *uri, char *mime,
-                                       GSList **pkg_list, uid_t uid)
+int _svc_db_get_list_with_all_defapps(GSList **pkg_list, uid_t uid)
 {
        char query[QUERY_MAX_LEN];
        sqlite3_stmt* stmt;
@@ -645,18 +644,15 @@ int _svc_db_get_list_with_condition(char *op, char *uri, char *mime,
        char *pkgname = NULL;
        int found;
 
-       if (__init_app_info_db(uid) < 0)
-               return 0;
+       if (__init(uid, true) < 0)
+               return -1;
 
-       snprintf(query, QUERY_MAX_LEN,
-                       "select ac.app_id from package_app_app_control as ac, package_app_info ai where ac.app_id = ai.app_id and ac.app_control like '%%%s|%s|%s%%' and ai.component_type='uiapp'",
-                       op, uri, mime);
-       SECURE_LOGD("query : %s\n", query);
+       snprintf(query, QUERY_MAX_LEN, "select pkg_name from appsvc");
 
-       ret = sqlite3_prepare(app_info_db, query, strlen(query), &stmt, NULL);
+       ret = sqlite3_prepare(svc_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));
+                               ret, sqlite3_extended_errcode(svc_db));
                return -1;
        }
 
@@ -673,7 +669,7 @@ int _svc_db_get_list_with_condition(char *op, char *uri, char *mime,
                if (found == 0) {
                        pkgname = strdup(str);
                        *pkg_list = g_slist_append(*pkg_list, (void *)pkgname);
-                       _D("%s is added", pkgname);
+                       _D("[%s] is def app", pkgname);
                }
        }
 
@@ -682,57 +678,61 @@ int _svc_db_get_list_with_condition(char *op, char *uri, char *mime,
        return 0;
 }
 
-int _svc_db_get_list_with_collation(char *op, char *uri, char *mime,
-                                       GSList **pkg_list, uid_t uid)
+char *_svc_db_query_builder_add(char *old_query, char *op, char *uri, char *mime, bool collate)
 {
        char query[QUERY_MAX_LEN];
-       sqlite3_stmt* stmt;
-       int ret;
-       GSList *iter = NULL;
-       char *str = NULL;
-       char *pkgname = NULL;
-       int found;
 
-       if (__init_app_info_db(uid) < 0)
-               return 0;
+       if (collate) {
+               if (old_query) {
+                       snprintf(query, QUERY_MAX_LEN,
+                               "%s, '%s|%s|%s' collate appsvc_collation ",
+                               old_query, op, uri, mime);
+                       free(old_query);
+               } else {
+                       snprintf(query, QUERY_MAX_LEN,
+                               "'%s|%s|%s' collate appsvc_collation ",
+                               op, uri, mime);
+               }
 
-       snprintf(query, QUERY_MAX_LEN,
-                       "select ac.app_id from package_app_app_control as ac, package_app_info ai where ac.app_id = ai.app_id and ac.app_control='%s|%s|%s' collate appsvc_collation and ai.component_type='uiapp'",
+       } else {
+               if (old_query) {
+                       snprintf(query, QUERY_MAX_LEN,
+                               "%s OR ac.app_control like '%%%s|%s|%s%%' ",
+                               old_query, op, uri, mime);
+                       free(old_query);
+               } else {
+                       snprintf(query, QUERY_MAX_LEN,
+                       "ac.app_control like '%%%s|%s|%s%%' ",
                        op, uri, mime);
-       SECURE_LOGD("query : %s\n", query);
-
-       ret = sqlite3_prepare(app_info_db, query, strlen(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) {
-               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);
                }
        }
 
-       ret = sqlite3_finalize(stmt);
-
-       return 0;
+       return strdup(query);
 }
 
-int _svc_db_get_list_with_all_defapps(GSList **pkg_list, uid_t uid)
+char *_svc_db_query_builder_build(char *old_query, bool collate)
 {
        char query[QUERY_MAX_LEN];
+
+       if (old_query == NULL)
+               return NULL;
+
+       if (collate) {
+               snprintf(query, QUERY_MAX_LEN,
+                       "select ac.app_id from package_app_app_control as ac, package_app_info ai where ac.app_id = ai.app_id and ai.component_type='uiapp' and ac.app_control in(%s)",
+                       old_query);
+       } else {
+               snprintf(query, QUERY_MAX_LEN,
+                       "select ac.app_id from package_app_app_control as ac, package_app_info ai where ac.app_id = ai.app_id and ai.component_type='uiapp' and (%s)",
+                       old_query);
+       }
+       free(old_query);
+
+       return strdup(query);
+}
+
+int _svc_db_exec_query(const char *query, GSList **pkg_list, uid_t uid)
+{
        sqlite3_stmt* stmt;
        int ret;
        GSList *iter = NULL;
@@ -740,15 +740,15 @@ int _svc_db_get_list_with_all_defapps(GSList **pkg_list, uid_t uid)
        char *pkgname = NULL;
        int found;
 
-       if (__init(uid, true) < 0)
-               return -1;
+       if (__init_app_info_db(uid) < 0)
+               return 0;
 
-       snprintf(query, QUERY_MAX_LEN, "select pkg_name from appsvc");
+       SECURE_LOGD("query : %s\n", query);
 
-       ret = sqlite3_prepare(svc_db, query, sizeof(query), &stmt, NULL);
+       ret = sqlite3_prepare(app_info_db, query, strlen(query), &stmt, NULL);
        if (ret != SQLITE_OK) {
                _E("prepare error, ret = %d, extended = %d\n",
-                               ret, sqlite3_extended_errcode(svc_db));
+                               ret, sqlite3_extended_errcode(app_info_db));
                return -1;
        }
 
@@ -765,7 +765,7 @@ int _svc_db_get_list_with_all_defapps(GSList **pkg_list, uid_t uid)
                if (found == 0) {
                        pkgname = strdup(str);
                        *pkg_list = g_slist_append(*pkg_list, (void *)pkgname);
-                       _D("[%s] is def app", pkgname);
+                       _D("%s is added", pkgname);
                }
        }
 
@@ -774,3 +774,4 @@ int _svc_db_get_list_with_all_defapps(GSList **pkg_list, uid_t uid)
        return 0;
 }
 
+