Improve performance for resolving appid 21/60621/4 accepted/tizen/ivi/20160302.224502 accepted/tizen/mobile/20160302.224414 accepted/tizen/tv/20160302.224431 accepted/tizen/wearable/20160302.224447 submit/tizen/20160302.082049
authorJunghoon Park <jh9216.park@samsung.com>
Mon, 29 Feb 2016 10:30:53 +0000 (19:30 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Wed, 2 Mar 2016 01:12:07 +0000 (10:12 +0900)
- merge query statements (100ms ==> 70ms)

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

index ca38f02..d8e9cd8 100755 (executable)
@@ -44,8 +44,10 @@ int _svc_db_adjust_list_with_submode(int mainapp_mode, char *win_id, GSList **pk
 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_in(const char *field, char *args);
+char *_svc_db_query_builder_or(char *q1, char *q2);
 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);
+char *_svc_db_query_builder_build(char *old_query);
 int _svc_db_exec_query(const char *query, GSList **pkg_list, uid_t uid);
 
 #ifdef __cplusplus
index 6d87940..e8b4d49 100755 (executable)
@@ -485,9 +485,7 @@ static char* __make_query(char *query, char *op, char *uri,
        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)
+static char* __make_query_with_collation(char *op, char *uri, char *mime, char *m_type, char *s_type)
 {
        char tmp[MAX_MIME_STR_SIZE];
        char *query = NULL;
@@ -503,12 +501,9 @@ static int __get_list_with_condition_mime_extened_with_collation(char *op,
                query = _svc_db_query_builder_add(query, op, uri, tmp, true);
        }
 
-       query = _svc_db_query_builder_build(query, true);
-       _svc_db_exec_query(query, pkg_list, uid);
-       if (query)
-               free(query);
+       query = _svc_db_query_builder_in("ac.app_control collate appsvc_collation ", query);
 
-       return 0;
+       return query;
 }
 
 
@@ -713,6 +708,7 @@ API int aul_svc_run_service_for_uid(bundle *b, int request_code,
        GSList *iter = NULL;
        char *list_item;
        char *query = NULL;
+       char *query2 = NULL;
 
        if (b == NULL) {
                _E("bundle for aul_svc_set_appid is NULL");
@@ -759,145 +755,81 @@ API int aul_svc_run_service_for_uid(bundle *b, int request_code,
 
        /*uri*/
        pkgname = _svc_db_get_app(info.op, info.origin_mime, info.uri, uid);
-       if (pkgname == NULL) {
-               __get_list_with_condition_mime_extened_with_collation(info.op, info.uri,
-                               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) {
-                               query = __make_query(query, info.op, info.uri_r_info,
-                                       info.mime, info.m_type, info.s_type);
-                       }
+       if (pkgname != NULL) {
+               ret = __run_svc_with_pkgname(pkgname, b, request_code,
+                       cbfunc, data, uid);
+               free(pkgname);
+               goto end;
+       }
 
-                       query = __make_query(query, info.op, info.scheme,
-                               info.mime, info.m_type, info.s_type);
+       query2 = __make_query_with_collation(info.op, info.uri,
+                       info.mime, info.m_type, info.s_type);
+       if (info.uri_r_info) {
+               query = __make_query(query, info.op, info.uri_r_info,
+                       info.mime, info.m_type, info.s_type);
+       }
 
-                       query = __make_query(query, info.op, "*",
-                               info.mime, info.m_type, info.s_type);
+       query = __make_query(query, info.op, info.scheme,
+               info.mime, info.m_type, info.s_type);
 
-                       if (info.scheme && (strcmp(info.scheme, "file") == 0)
-                               && info.mime && (strcmp(info.mime, "NULL") != 0)) {
-                               query = __make_query(query, info.op, "NULL",
-                                       info.mime, info.m_type, info.s_type);
-                       }
+       query = __make_query(query, info.op, "*",
+               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.scheme && (strcmp(info.scheme, "file") == 0)
+               && info.mime && (strcmp(info.mime, "NULL") != 0)) {
+               query = __make_query(query, info.op, "NULL",
+                       info.mime, info.m_type, info.s_type);
+       }
+
+       query = _svc_db_query_builder_or(query2, query);
+       query = _svc_db_query_builder_build(query);
+       _svc_db_exec_query(query, &pkg_list, uid);
+       if (query) {
+               free(query);
+               query = NULL;
+       }
 
-                       if (info.category)
-                               __get_list_with_category(info.category, &pkg_list, uid);
+       if (info.category)
+               __get_list_with_category(info.category, &pkg_list, uid);
 
-                       __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
+       __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
 
-                       pkg_count = g_slist_length(pkg_list);
-                       _D("pkg_count : %d", pkg_count);
+       pkg_count = g_slist_length(pkg_list);
+       _D("pkg_count : %d", pkg_count);
 
-                       if (pkg_count == 1) {
-                               pkgname = (char *)pkg_list->data;
-                               if (pkgname != NULL) {
-                                       ret = __run_svc_with_pkgname(pkgname, b, request_code,
-                                                       cbfunc, data, uid);
-                                       goto end;
-                               }
-                       } else {
-                               bundle_add(b, AUL_SVC_K_URI_R_INFO, info.uri);
-                               ret = __run_svc_with_pkgname(APP_SELECTOR, b, request_code,
-                                               cbfunc, data, uid);
-                               goto end;
-                       }
-                       for (iter = pkg_list; iter != NULL; iter = g_slist_next(iter)) {
-                               list_item = (char *)iter->data;
-                               g_free(list_item);
-                       }
-                       g_slist_free(pkg_list);
-                       pkg_list = NULL;
+       if (pkg_count == 1) {
+               pkgname = (char *)pkg_list->data;
+               if (pkgname != NULL) {
+                       ret = __run_svc_with_pkgname(pkgname, b, request_code,
+                                       cbfunc, data, uid);
+                       goto end;
                }
        } else {
-               ret = __run_svc_with_pkgname(pkgname, b, request_code,
+               bundle_add(b, AUL_SVC_K_URI_R_INFO, info.uri);
+               ret = __run_svc_with_pkgname(APP_SELECTOR, b, request_code,
                                cbfunc, data, uid);
-               free(pkgname);
                goto end;
        }
+       for (iter = pkg_list; iter != NULL; iter = g_slist_next(iter)) {
+               list_item = (char *)iter->data;
+               g_free(list_item);
+       }
+       g_slist_free(pkg_list);
+       pkg_list = NULL;
 
        /*scheme & host*/
        if (info.uri_r_info) {
                pkgname = _svc_db_get_app(info.op, info.origin_mime, info.uri_r_info, uid);
 
-               if (pkgname == NULL) {
-                       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) {
-                               query = __make_query(query, info.op, info.scheme,
-                                       info.mime, info.m_type, info.s_type);
-
-                               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)) {
-                                       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)
-                                       __get_list_with_category(info.category, &pkg_list, uid);
-
-                               __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
-
-                               pkg_count = g_slist_length(pkg_list);
-                               _D("pkg_count : %d", pkg_count);
-
-                               if (pkg_count == 1) {
-                                       pkgname = (char *)pkg_list->data;
-                                       if (pkgname != NULL) {
-                                               ret = __run_svc_with_pkgname(pkgname, b, request_code,
-                                                               cbfunc, data, uid);
-                                               goto end;
-                                       }
-                               } else {
-                                       bundle_add(b, AUL_SVC_K_URI_R_INFO, info.uri_r_info);
-                                       ret = __run_svc_with_pkgname(APP_SELECTOR, b, 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;
-                               g_free(list_item);
-                       }
-                       g_slist_free(pkg_list);
-                       pkg_list = NULL;
-               }  else {
+               if (pkgname != NULL) {
                        ret = __run_svc_with_pkgname(pkgname, b, request_code,
                                        cbfunc, data, uid);
                        free(pkgname);
                        goto end;
                }
-       }
-
-       /*scheme*/
-       pkgname = _svc_db_get_app(info.op, info.origin_mime, info.scheme, uid);
 
-       if (pkgname == NULL) {
+               query2 = __make_query(query, info.op, info.uri_r_info,
+                       info.mime, info.m_type, info.s_type);
                query = __make_query(query, info.op, info.scheme,
                        info.mime, info.m_type, info.s_type);
 
@@ -910,9 +842,9 @@ API int aul_svc_run_service_for_uid(bundle *b, int request_code,
                                info.mime, info.m_type, info.s_type);
                }
 
-               query = _svc_db_query_builder_build(query, false);
+               query = _svc_db_query_builder_or(query2, query);
+               query = _svc_db_query_builder_build(query);
                _svc_db_exec_query(query, &pkg_list, uid);
-
                if (query) {
                        free(query);
                        query = NULL;
@@ -931,27 +863,83 @@ API int aul_svc_run_service_for_uid(bundle *b, int request_code,
                        if (pkgname != NULL) {
                                ret = __run_svc_with_pkgname(pkgname, b, request_code,
                                                cbfunc, data, uid);
+                               goto end;
                        }
-               } else if (pkg_count < 1) {
-                       __free_resolve_info_data(&info);
-                       return AUL_SVC_RET_ENOMATCH;
                } else {
-                       bundle_add(b, AUL_SVC_K_URI_R_INFO, info.scheme);
+                       bundle_add(b, AUL_SVC_K_URI_R_INFO, info.uri_r_info);
                        ret = __run_svc_with_pkgname(APP_SELECTOR, b, request_code,
                                        cbfunc, data, uid);
+                       goto end;
                }
 
                for (iter = pkg_list; iter != NULL; iter = g_slist_next(iter)) {
                        list_item = (char *)iter->data;
                        g_free(list_item);
                }
+
                g_slist_free(pkg_list);
-       } else {
+               pkg_list = NULL;
+       }
+
+       /*scheme*/
+       pkgname = _svc_db_get_app(info.op, info.origin_mime, info.scheme, uid);
+
+       if (pkgname != NULL) {
                ret = __run_svc_with_pkgname(pkgname, b, request_code,
-                               cbfunc, data, uid);
+                       cbfunc, data, uid);
                free(pkgname);
+               goto end;
        }
 
+       query = __make_query(query, info.op, info.scheme,
+               info.mime, info.m_type, info.s_type);
+
+       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)) {
+               query = __make_query(query, info.op, "NULL",
+                       info.mime, info.m_type, info.s_type);
+       }
+
+       query = _svc_db_query_builder_build(query);
+       _svc_db_exec_query(query, &pkg_list, uid);
+
+       if (query) {
+               free(query);
+               query = NULL;
+       }
+
+       if (info.category)
+               __get_list_with_category(info.category, &pkg_list, uid);
+
+       __get_list_with_submode(info.op, info.win_id, &pkg_list, uid);
+
+       pkg_count = g_slist_length(pkg_list);
+       _D("pkg_count : %d", pkg_count);
+
+       if (pkg_count == 1) {
+               pkgname = (char *)pkg_list->data;
+               if (pkgname != NULL) {
+                       ret = __run_svc_with_pkgname(pkgname, b, request_code,
+                                       cbfunc, data, uid);
+               }
+       } else if (pkg_count < 1) {
+               __free_resolve_info_data(&info);
+               return AUL_SVC_RET_ENOMATCH;
+       } else {
+               bundle_add(b, AUL_SVC_K_URI_R_INFO, info.scheme);
+               ret = __run_svc_with_pkgname(APP_SELECTOR, b, request_code,
+                               cbfunc, data, uid);
+       }
+
+       for (iter = pkg_list; iter != NULL; iter = g_slist_next(iter)) {
+               list_item = (char *)iter->data;
+               g_free(list_item);
+       }
+       g_slist_free(pkg_list);
+
 end:
        __free_resolve_info_data(&info);
 
@@ -975,6 +963,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;
+       char *query2 = NULL;
 
        if (b == NULL) {
                _E("bundle for aul_svc_run_service is NULL");
@@ -998,8 +987,8 @@ API int aul_svc_get_list_for_uid(bundle *b, aul_svc_info_iter_fn iter_fn,
        _D("operation - %s / shceme - %s / mime - %s\n", info.op, info.scheme,
           info.mime);
 
-       __get_list_with_condition_mime_extened_with_collation(info.op, info.uri,
-                       info.mime, info.m_type, info.s_type, &pkg_list, uid);
+       query2 = __make_query_with_collation(info.op, info.uri,
+                       info.mime, info.m_type, info.s_type);
 
        if (info.uri_r_info) {
                query = __make_query(query, info.op, info.uri_r_info,
@@ -1018,7 +1007,8 @@ 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);
        }
 
-       query = _svc_db_query_builder_build(query, false);
+       query = _svc_db_query_builder_or(query2, query);
+       query = _svc_db_query_builder_build(query);
        _svc_db_exec_query(query, &pkg_list, uid);
        if (query) {
                free(query);
index 2b58015..4e6edea 100755 (executable)
@@ -685,12 +685,12 @@ char *_svc_db_query_builder_add(char *old_query, char *op, char *uri, char *mime
        if (collate) {
                if (old_query) {
                        snprintf(query, QUERY_MAX_LEN,
-                               "%s, '%s|%s|%s' collate appsvc_collation ",
+                               "%s, '%s|%s|%s'",
                                old_query, op, uri, mime);
                        free(old_query);
                } else {
                        snprintf(query, QUERY_MAX_LEN,
-                               "'%s|%s|%s' collate appsvc_collation ",
+                               "'%s|%s|%s'",
                                op, uri, mime);
                }
 
@@ -710,22 +710,38 @@ char *_svc_db_query_builder_add(char *old_query, char *op, char *uri, char *mime
        return strdup(query);
 }
 
-char *_svc_db_query_builder_build(char *old_query, bool collate)
+char *_svc_db_query_builder_or(char *q1, char *q2)
+{
+       char query[QUERY_MAX_LEN];
+
+       snprintf(query, QUERY_MAX_LEN, "(%s) or (%s)", q1, q2);
+       free(q1);
+       free(q2);
+
+       return strdup(query);
+}
+
+char *_svc_db_query_builder_in(const char *field, char *args)
+{
+       char query[QUERY_MAX_LEN];
+
+       snprintf(query, QUERY_MAX_LEN, "%s in(%s)", field, args);
+       free(args);
+
+       return strdup(query);
+}
+
+char *_svc_db_query_builder_build(char *old_query)
 {
        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);
-       }
+       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);