From bb4305148dba90ff1f0b7ea6e1542efa822a295f Mon Sep 17 00:00:00 2001 From: Hyunho Kang Date: Tue, 14 Jun 2016 14:32:24 +0900 Subject: [PATCH] Refactoring rua api - Make *_internal.h for amd APIs (DB direct write operation). Change-Id: Ife8983639fa275aa429cd47eed115b9b1a35f7a4 Signed-off-by: Hyunho Kang --- CMakeLists.txt | 5 +- include/perf-measure.h | 46 ------- include/rua.h | 27 +--- include/rua_internal.h | 75 +++++++++++ include/rua_stat.h | 9 -- include/rua_stat_internal.h | 65 ++++++++++ src/perf-measure.c | 57 --------- src/rua.c | 195 +--------------------------- src/rua_internal.c | 173 +++++++++++++++++++++++++ src/rua_stat.c | 303 +------------------------------------------- src/rua_stat_internal.c | 273 +++++++++++++++++++++++++++++++++++++++ test/rua-test.c | 3 +- 12 files changed, 608 insertions(+), 623 deletions(-) delete mode 100644 include/perf-measure.h create mode 100644 include/rua_internal.h create mode 100644 include/rua_stat_internal.h delete mode 100644 src/perf-measure.c create mode 100644 src/rua_internal.c create mode 100644 src/rua_stat_internal.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c8be007..c1a36dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,9 +7,10 @@ SET(INCLUDEDIR "\${prefix}/include/${PROJECT_NAME}") SET(VERSION 0.1.0) SET(SRCS - src/perf-measure.c src/rua.c + src/rua_internal.c src/rua_stat.c + src/rua_stat_internal.c ) INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include) @@ -40,7 +41,9 @@ CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${PROJECT_NAME}.pc @ONLY) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua.h DESTINATION include/${PROJECT_NAME}) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_internal.h DESTINATION include/${PROJECT_NAME}) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_stat.h DESTINATION include/${PROJECT_NAME}) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/rua_stat_internal.h DESTINATION include/${PROJECT_NAME}) ADD_SUBDIRECTORY(test) diff --git a/include/perf-measure.h b/include/perf-measure.h deleted file mode 100644 index fbb252d..0000000 --- a/include/perf-measure.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __PERF_MEASURE_H__ -#define __PERF_MEASURE_H__ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef PERF_MEASURE_ENABLE - -unsigned int _perf_measure_start(const char *tag, char *func_name, int line); -unsigned int _perf_measure_end(const char *tag, unsigned int ts_start, - char *func_name, int line); - -#define PERF_MEASURE_START(tag) _perf_measure_start(tag, __func__,__LINE__) -#define PERF_MEASURE_END(tag, ts_start) \ - _perf_measure_end(tag, ts_start,__func__, __LINE__) - -#else - -#define PERF_MEASURE_START(tag) (0) -#define PERF_MEASURE_END(tag, ts_start) (0) - -#endif - -#ifdef __cplusplus -} -#endif -#endif /* __PERF_MEASURE_H__ */ diff --git a/include/rua.h b/include/rua.h index 8c341c8..4007df2 100755 --- a/include/rua.h +++ b/include/rua.h @@ -56,30 +56,14 @@ extern "C" { * @brief RUA record info structure */ struct rua_rec { - int id; /**< primary key */ + int id; /**< primary key */ char *pkg_name; /**< package name */ char *app_path; /**< application path */ char *arg; /**< application launching argument */ - time_t launch_time; /**< application launching time */ + time_t launch_time; /**< application launching time */ }; /** - * @brief Delete history from DB - * @return 0 on success, otherwise a nagative error value - * @retval 0 on successful - * @retval -1 on failed - */ -API int rua_delete_history_from_db(bundle *b); - -/** - * @brief Clear history - * @return 0 on success, otherwise a nagative error value - * @retval 0 on successful - * @retval -1 on failed - */ -API int rua_clear_history(void); - -/** * @brief Delete history with pkg_name * @param[in] pkg_name package name to delete history * @return 0 on success, otherwise a nagative error value @@ -98,13 +82,12 @@ API int rua_delete_history_with_pkgname(char *pkg_name); API int rua_delete_history_with_apppath(char *app_path); /** - * @brief Add application to recently used application list - * @param[in] rec record to add history - * @return 0 on success, otherwise a nagative error value + * @brief Clear history + * @return 0 on success, otherwise a nagative error value * @retval 0 on successful * @retval -1 on failed */ -API int rua_add_history(struct rua_rec *rec); +API int rua_clear_history(void); /** * @brief Load recently used application history db. diff --git a/include/rua_internal.h b/include/rua_internal.h new file mode 100644 index 0000000..e6a6c6c --- /dev/null +++ b/include/rua_internal.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file rua.h + * @brief RUA INTERNAL API declaration header file. + * @author hyunho kang (hhstark.kang@samsung.com) + * @version 0.1 + * @history 0.1: RUA INTERNAL API Declarations, structure declaration + */ + +#ifndef __RUA_INTERNAL_H__ +#define __RUA_INTERNAL_H__ + +#include +#include +#include +#include + +#include "rua.h" + +#ifndef API +#define API __attribute__ ((visibility("default"))) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "RUA" +#define RUA_DB_NAME ".rua.db" +#define RUA_HISTORY "rua_history" +#define QUERY_MAXLEN 4096 +#define Q_LATEST \ + "select pkg_name from rua_history " \ + "order by launch_time desc limit 1 " + +/** + * @brief Delete history from DB + * @return 0 on success, otherwise a nagative error value + * @retval 0 on successful + * @retval -1 on failed + */ +API int rua_db_delete_history(bundle *b); + +/** + * @brief Add application to recently used application list + * @param[in] rec record to add history + * @return 0 on success, otherwise a nagative error value + * @retval 0 on successful + * @retval -1 on failed + */ +API int rua_db_add_history(struct rua_rec *rec); + +#ifdef __cplusplus +} +#endif +#endif /*__RUA_INTERNAL_H__*/ \ No newline at end of file diff --git a/include/rua_stat.h b/include/rua_stat.h index c91d633..795d13b 100755 --- a/include/rua_stat.h +++ b/include/rua_stat.h @@ -26,7 +26,6 @@ #define __RUA_STAT_H__ #include -#include #ifndef API #define API __attribute__ ((visibility("default"))) @@ -36,14 +35,6 @@ extern "C" { #endif -/** - * @brief Add application launch status. - * @param[in] caller, rua_stat_tag - * @return 0 on success, otherwise a nagative error value - * @retval 0 on successful - * @retval -1 on failed - */ -API int rua_stat_update(char *caller, char *rua_stat_tag); /** * @brief Get rua status tag list diff --git a/include/rua_stat_internal.h b/include/rua_stat_internal.h new file mode 100644 index 0000000..cba004f --- /dev/null +++ b/include/rua_stat_internal.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file rua_stat_internal.h + * @brief RUA STATUS INTERNAL API declaration header file. + * @author Hyunho Kang (hhstark.kang@samsung.com) + * @version 0.1 + * @history 0.1: RUA STAT INTERNAL API Declarations, structure declaration + */ + +#ifndef __RUA_STAT_INTERNAL_H__ +#define __RUA_STAT_INTERNAL_H__ + +#include +#include + +#ifndef API +#define API __attribute__ ((visibility("default"))) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "RUA" + +#define RUA_STAT_DB_NAME ".rua_stat.db" +#define QUERY_MAXLEN 4096 +#define WIN_SCORE 100 +#define LOSE_SCORE_RATE 0.7f + +int _rua_stat_init(sqlite3 *db, int flags); +int _rua_stat_fini(sqlite3 *db); + +/** + * @brief Add application launch status. + * @param[in] caller, rua_stat_tag + * @return 0 on success, otherwise a nagative error value + * @retval 0 on successful + * @retval -1 on failed + */ +API int rua_stat_db_update(char *caller, char *rua_stat_tag); + +#ifdef __cplusplus +} +#endif +#endif /*__RUA_STAT_INTERNAL_H__*/ diff --git a/src/perf-measure.c b/src/perf-measure.c deleted file mode 100644 index e1fa9e7..0000000 --- a/src/perf-measure.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include - -unsigned int _perf_measure_start(const char *tag, char *func_name, int line) -{ - struct timeval t; - unsigned int ts_start; - - if (!tag) - return 0; - - gettimeofday(&t, NULL); - ts_start = t.tv_sec * 1000000UL + t.tv_usec; - - printf("### START [%s:%d] timestamp(%u)\n", func_name, line, ts_start); - - return ts_start; -} - -unsigned int _perf_measure_end(const char *tag, unsigned int ts_start, - char *func_name, int line) -{ - struct timeval t; - unsigned int ts_end; - unsigned int elapsed_time = 0; - - gettimeofday(&t, NULL); - ts_end = t.tv_sec * 1000000UL + t.tv_usec; - - if (ts_start) - elapsed_time = ts_end - ts_start; - - printf("### END [%s:%d] timestamp(%u)\n", func_name, line, ts_end); - - if (elapsed_time) - printf("### ELAPSED [%s:%d] timestamp(%u)\n", func_name, line, - elapsed_time); - - return ts_end; -} - diff --git a/src/rua.c b/src/rua.c index 8ba783e..ed8f847 100644 --- a/src/rua.c +++ b/src/rua.c @@ -22,81 +22,9 @@ /* For multi-user support */ #include -#include - -#ifdef LOG_TAG -#undef LOG_TAG -#endif - -#define LOG_TAG "RUA" - +#include "rua_internal.h" #include "rua.h" #include "db-schema.h" -#include "perf-measure.h" - -#define RUA_DB_NAME ".rua.db" -#define RUA_HISTORY "rua_history" -#define QUERY_MAXLEN 4096 -#define Q_LATEST \ - "select pkg_name from rua_history " \ - "order by launch_time desc limit 1 " - -static int __exec(sqlite3 *db, char *query); -static int __create_table(sqlite3 *db); -static sqlite3 *__db_init(); - -int rua_delete_history_from_db(bundle *b) -{ - int r; - sqlite3 *db = NULL; - char query[QUERY_MAXLEN]; - - char *pkg_name = NULL; - char *app_path = NULL; - char *errmsg = NULL; - int result = 0; - - db = __db_init(); - if (db == NULL) { - LOGE("Error db null"); - return -1; - } - - if (b != NULL) { - bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name); - bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path); - } - - if (pkg_name != NULL) - snprintf(query, QUERY_MAXLEN, "delete from rua_history where pkg_name = '%s';", pkg_name); - else if (app_path != NULL) - snprintf(query, QUERY_MAXLEN, "delete from rua_history where app_path = '%s';", app_path); - else - snprintf(query, QUERY_MAXLEN, "delete from rua_history;"); - - LOGI("rua_delete_history_from_db : %s", query); - r = sqlite3_exec(db, query, NULL, NULL, &errmsg); - - if (r != SQLITE_OK) { - LOGE("fail to exec delete query %s : %s", query, errmsg); - sqlite3_free(errmsg); - result = -1; - } - - if (db != NULL) - db_util_close(db); - - return result; - -} - -int rua_clear_history(void) -{ - int r; - r = aul_delete_rua_history(NULL); - LOGI("rua_clear_history result : %d ", r); - return r; -} int rua_delete_history_with_pkgname(char *pkg_name) { @@ -131,67 +59,11 @@ int rua_delete_history_with_apppath(char *app_path) return r; } -int rua_add_history(struct rua_rec *rec) +int rua_clear_history(void) { int r; - int cnt = 0; - char query[QUERY_MAXLEN]; - sqlite3_stmt *stmt; - sqlite3 *db = NULL; - - db = __db_init(); - if (db == NULL) { - LOGE("Error db null"); - return -1; - } - - if (rec == NULL) { - LOGE("Error rec null"); - db_util_close(db); - return -1; - } - - snprintf(query, QUERY_MAXLEN, - "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY, - rec->pkg_name); - - r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); - if (r != SQLITE_OK) { - LOGE("Error sqlite3_prepare fail"); - db_util_close(db); - return -1; - } - - r = sqlite3_step(stmt); - if (r == SQLITE_ROW) - cnt = sqlite3_column_int(stmt, 0); - - sqlite3_finalize(stmt); - - if (cnt == 0) - /* insert */ - snprintf(query, QUERY_MAXLEN, - "insert into %s ( pkg_name, app_path, arg, launch_time ) " - " values ( \"%s\", \"%s\", \"%s\", %d ) ", - RUA_HISTORY, - rec->pkg_name ? rec->pkg_name : "", - rec->app_path ? rec->app_path : "", - rec->arg ? rec->arg : "", (int)time(NULL)); - else - /* update */ - snprintf(query, QUERY_MAXLEN, - "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';", - RUA_HISTORY, - rec->arg ? rec->arg : "", (int)time(NULL), rec->pkg_name); - - r = __exec(db, query); - if (r == -1) { - LOGE("[RUA ADD HISTORY ERROR] %s\n", query); - db_util_close(db); - return -1; - } - - db_util_close(db); + r = aul_delete_rua_history(NULL); + LOGI("rua_clear_history result : %d ", r); return r; } @@ -348,61 +220,4 @@ int rua_init(void) int rua_fini(void) { return 0; -} - -static int __exec(sqlite3 *db, char *query) -{ - int r; - char *errmsg = NULL; - - if (db == NULL) - return -1; - - r = sqlite3_exec(db, query, NULL, NULL, &errmsg); - - if (r != SQLITE_OK) { - sqlite3_free(errmsg); - return -1; - } - - return 0; -} - -static int __create_table(sqlite3 *db) -{ - int r; - - r = __exec(db, CREATE_RUA_HISTORY_TABLE); - if (r == -1) - return -1; - - return 0; -} - -static sqlite3 *__db_init() -{ - int r; - sqlite3 *db = NULL; - - char defname[FILENAME_MAX]; - const char *rua_db_path = tzplatform_getenv(TZ_USER_DB); - if (rua_db_path == NULL) { - LOGE("fail to get rua_db_path"); - return NULL; - } - snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME); - - r = db_util_open_with_options(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL); - if (r) { - db_util_close(db); - return NULL; - } - - r = __create_table(db); - if (r) { - db_util_close(db); - return NULL; - } - - return db; -} +} \ No newline at end of file diff --git a/src/rua_internal.c b/src/rua_internal.c new file mode 100644 index 0000000..4cc5f05 --- /dev/null +++ b/src/rua_internal.c @@ -0,0 +1,173 @@ + +#include +#include +#include + +#include "rua_internal.h" +#include "db-schema.h" + +static int __exec(sqlite3 *db, char *query) +{ + int r; + char *errmsg = NULL; + + if (db == NULL) + return -1; + + r = sqlite3_exec(db, query, NULL, NULL, &errmsg); + + if (r != SQLITE_OK) { + sqlite3_free(errmsg); + return -1; + } + + return 0; +} + +static int __create_table(sqlite3 *db) +{ + int r; + + r = __exec(db, CREATE_RUA_HISTORY_TABLE); + if (r == -1) + return -1; + + return 0; +} + +static sqlite3 *__db_init() +{ + int r; + sqlite3 *db = NULL; + + char defname[FILENAME_MAX]; + const char *rua_db_path = tzplatform_getenv(TZ_USER_DB); + if (rua_db_path == NULL) { + LOGE("fail to get rua_db_path"); + return NULL; + } + snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME); + + r = db_util_open_with_options(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL); + if (r) { + db_util_close(db); + return NULL; + } + + r = __create_table(db); + if (r) { + db_util_close(db); + return NULL; + } + + return db; +} + +int rua_db_delete_history(bundle *b) +{ + int r; + sqlite3 *db = NULL; + char query[QUERY_MAXLEN]; + + char *pkg_name = NULL; + char *app_path = NULL; + char *errmsg = NULL; + int result = 0; + + db = __db_init(); + if (db == NULL) { + LOGE("Error db null"); + return -1; + } + + if (b != NULL) { + bundle_get_str(b, AUL_K_RUA_PKGNAME, &pkg_name); + bundle_get_str(b, AUL_K_RUA_APPPATH, &app_path); + } + + if (pkg_name != NULL) + snprintf(query, QUERY_MAXLEN, "delete from rua_history where pkg_name = '%s';", pkg_name); + else if (app_path != NULL) + snprintf(query, QUERY_MAXLEN, "delete from rua_history where app_path = '%s';", app_path); + else + snprintf(query, QUERY_MAXLEN, "delete from rua_history;"); + + LOGI("rua_delete_history_from_db : %s", query); + r = sqlite3_exec(db, query, NULL, NULL, &errmsg); + + if (r != SQLITE_OK) { + LOGE("fail to exec delete query %s : %s", query, errmsg); + sqlite3_free(errmsg); + result = -1; + } + + if (db != NULL) + db_util_close(db); + + return result; + +} + +int rua_db_add_history(struct rua_rec *rec) +{ + int r; + int cnt = 0; + char query[QUERY_MAXLEN]; + sqlite3_stmt *stmt; + sqlite3 *db = NULL; + + db = __db_init(); + if (db == NULL) { + LOGE("Error db null"); + return -1; + } + + if (rec == NULL) { + LOGE("Error rec null"); + db_util_close(db); + return -1; + } + + snprintf(query, QUERY_MAXLEN, + "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY, + rec->pkg_name); + + r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); + if (r != SQLITE_OK) { + LOGE("Error sqlite3_prepare fail"); + db_util_close(db); + return -1; + } + + r = sqlite3_step(stmt); + if (r == SQLITE_ROW) + cnt = sqlite3_column_int(stmt, 0); + + sqlite3_finalize(stmt); + + if (cnt == 0) + /* insert */ + snprintf(query, QUERY_MAXLEN, + "insert into %s ( pkg_name, app_path, arg, launch_time ) " + " values ( \"%s\", \"%s\", \"%s\", %d ) ", + RUA_HISTORY, + rec->pkg_name ? rec->pkg_name : "", + rec->app_path ? rec->app_path : "", + rec->arg ? rec->arg : "", (int)time(NULL)); + else + /* update */ + snprintf(query, QUERY_MAXLEN, + "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';", + RUA_HISTORY, + rec->arg ? rec->arg : "", (int)time(NULL), rec->pkg_name); + + r = __exec(db, query); + if (r == -1) { + LOGE("[RUA ADD HISTORY ERROR] %s\n", query); + db_util_close(db); + return -1; + } + + db_util_close(db); + return r; +} diff --git a/src/rua_stat.c b/src/rua_stat.c index f00a78b..78f7da4 100755 --- a/src/rua_stat.c +++ b/src/rua_stat.c @@ -26,243 +26,8 @@ #include -/* For multi-user support */ -#include - +#include "rua_stat_internal.h" #include "rua_stat.h" -#include "db-schema.h" -#include "perf-measure.h" - -#include - -#ifdef LOG_TAG -#undef LOG_TAG -#endif - -#define LOG_TAG "RUA" - -#define RUA_STAT_DB_NAME ".rua_stat.db" -#define QUERY_MAXLEN 4096 -#define WIN_SCORE 100 -#define LOSE_SCORE_RATE 0.7f - -static sqlite3 *_db = NULL; -static int __exec(sqlite3 *db, char *query); -static sqlite3 *__db_init(char *root, int flags); - -int __rua_stat_init(int flags) -{ - char defname[FILENAME_MAX]; - const char *rua_stat_db_path = tzplatform_getenv(TZ_USER_DB); - - if (_db) - return 0; - - snprintf(defname, sizeof(defname), "%s/%s", rua_stat_db_path, RUA_STAT_DB_NAME); - _db = __db_init(defname, flags); - - if (_db == NULL) { - LOGW("__rua_stat_init error"); - return -1; - } - - return 0; -} - -int __rua_stat_fini(void) -{ - if (_db) { - db_util_close(_db); - _db = NULL; - } - - return 0; -} - -int __rua_stat_insert(char *caller, char *rua_stat_tag) -{ - int r; - char query[QUERY_MAXLEN]; - sqlite3_stmt *stmt = NULL; - - sqlite3_snprintf(QUERY_MAXLEN, query, - "INSERT INTO rua_panel_stat (caller_panel, rua_stat_tag, score) VALUES (?,?,?)"); - - r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL); - if (r != SQLITE_OK) { - LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db)); - goto out; - } - - r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); - if (r != SQLITE_OK) { - LOGE("caller bind error(%d) \n", r); - goto out; - } - - r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC); - if (r != SQLITE_OK) { - LOGE("rua_stat_tag bind error(%d) \n", r); - goto out; - } - - r = sqlite3_bind_int(stmt, 3, WIN_SCORE); - if (r != SQLITE_OK) { - LOGE("arg bind error(%d) \n", r); - goto out; - } - - r = sqlite3_step(stmt); - if (r != SQLITE_DONE) { - LOGE("step error(%d) \n", r); - goto out; - } - -out: - if (stmt) - sqlite3_finalize(stmt); - - return r; -} - -int __rua_stat_lose_score_update(char *caller, char *rua_stat_tag) -{ - int r; - char query[QUERY_MAXLEN]; - sqlite3_stmt *stmt = NULL; - - sqlite3_snprintf(QUERY_MAXLEN, query, - "UPDATE rua_panel_stat SET score = score * %f WHERE caller_panel = ? AND rua_stat_tag != ?", - LOSE_SCORE_RATE); - - LOGD("lose score update sql : %s", query); - r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL); - if (r != SQLITE_OK) { - LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db)); - goto out; - } - - r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); - if (r != SQLITE_OK) { - LOGE("caller bind error(%d) \n", r); - goto out; - } - - r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC); - if (r != SQLITE_OK) { - LOGE("rua_stat_tag bind error(%d) \n", r); - goto out; - } - - r = sqlite3_step(stmt); - if (r != SQLITE_DONE) { - LOGE("step error(%d) \n", r); - goto out; - } - -out: - if (stmt) - sqlite3_finalize(stmt); - - return r; -} - -int __rua_stat_win_score_update(char *caller, char *rua_stat_tag) -{ - int r; - char query[QUERY_MAXLEN]; - sqlite3_stmt *stmt = NULL; - - sqlite3_snprintf(QUERY_MAXLEN, query, - "UPDATE rua_panel_stat SET score = score + %d WHERE caller_panel = ? AND rua_stat_tag = ?", - WIN_SCORE); - - LOGD("win score update sql : %s", query); - - r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL); - if (r != SQLITE_OK) { - LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db)); - goto out; - } - - r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); - if (r != SQLITE_OK) { - LOGE("caller bind error(%d) \n", r); - goto out; - } - - r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC); - if (r != SQLITE_OK) { - LOGE("rua_stat_tag bind error(%d) \n", r); - goto out; - } - - r = sqlite3_step(stmt); - if (r != SQLITE_DONE) { - LOGE("step error(%d) \n", r); - goto out; - } - -out: - if (stmt) - sqlite3_finalize(stmt); - - return r; -} - -int rua_stat_update(char *caller, char *rua_stat_tag) -{ - int r; - int affected_rows = 0; - - LOGD("rua_stat_update start"); - - r = __rua_stat_init(SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE); - if (r == -1) { - LOGE("__rua_stat_init fail"); - return -1; - } - - if (_db == NULL) { - LOGE("rua_stat is not initialized"); - return -1; - } - - if (caller == NULL) { - LOGE("caller is null"); - return -1; - } - - if (rua_stat_tag == NULL) { - LOGE("rua_stat_tag is null"); - return -1; - } - - - r = __rua_stat_lose_score_update(caller, rua_stat_tag); - if (r != SQLITE_DONE) { - LOGE("__rua_stat_lose_score_insert fail."); - return -1; - } - - r = __rua_stat_win_score_update(caller, rua_stat_tag); - affected_rows = sqlite3_changes(_db); - if ((r != SQLITE_DONE) || (affected_rows == 0)) { - r = __rua_stat_insert(caller, rua_stat_tag); - - if (r != SQLITE_DONE) { - LOGE("__rua_stat_insert fail."); - return -1; - } - } - - __rua_stat_fini(); - LOGD("rua_stat_update done"); - - return r; - -} - int rua_stat_get_stat_tags(char *caller, int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data), @@ -272,8 +37,9 @@ int rua_stat_get_stat_tags(char *caller, sqlite3_stmt *stmt; char query[QUERY_MAXLEN]; const unsigned char *ct; + sqlite3 *db = NULL; - r = __rua_stat_init(SQLITE_OPEN_READONLY); + r = _rua_stat_init(db, SQLITE_OPEN_READONLY); if (r == -1) { LOGE("__rua_stat_init fail"); return -1; @@ -282,12 +48,9 @@ int rua_stat_get_stat_tags(char *caller, sqlite3_snprintf(QUERY_MAXLEN, query, "SELECT rua_stat_tag FROM rua_panel_stat WHERE caller_panel = ? ORDER BY score DESC"); - if (!_db) - return -1; - - r = sqlite3_prepare(_db, query, sizeof(query), &stmt, NULL); + r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); if (r != SQLITE_OK) { - LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(_db), sqlite3_errmsg(_db)); + LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db)); goto out; } @@ -309,61 +72,7 @@ out: if (stmt) sqlite3_finalize(stmt); - __rua_stat_fini(); + _rua_stat_fini(db); return r; } - -static int __exec(sqlite3 *db, char *query) -{ - int r; - char *errmsg = NULL; - - if (db == NULL) - return -1; - - r = sqlite3_exec(db, query, NULL, NULL, &errmsg); - if (r != SQLITE_OK) { - SECURE_LOGE("query(%s) exec error(%s)", query, errmsg); - sqlite3_free(errmsg); - return -1; - } - - return 0; -} - -static int __create_table(sqlite3 *db) -{ - int r; - - r = __exec(db, CREATE_RUA_STAT_TABLE); - if (r == -1) { - LOGE("create table error"); - return -1; - } - - return 0; -} - -static sqlite3 *__db_init(char *root, int flags) -{ - int r; - sqlite3 *db = NULL; - - r = db_util_open_with_options(root, &db, flags, NULL); - if (r) { - LOGE("db util open error(%d/%d/%d/%s)", r, - sqlite3_errcode(db), - sqlite3_extended_errcode(db), - sqlite3_errmsg(db)); - return NULL; - - } - r = __create_table(db); - if (r) { - db_util_close(db); - return NULL; - } - - return db; -} diff --git a/src/rua_stat_internal.c b/src/rua_stat_internal.c new file mode 100644 index 0000000..254e7d3 --- /dev/null +++ b/src/rua_stat_internal.c @@ -0,0 +1,273 @@ +#include +#include +#include + +#include +/* For multi-user support */ +#include + +#include "db-schema.h" +#include "rua_stat_internal.h" + +int __rua_stat_insert(sqlite3 *db, char *caller, char *rua_stat_tag) +{ + int r; + char query[QUERY_MAXLEN]; + sqlite3_stmt *stmt = NULL; + + sqlite3_snprintf(QUERY_MAXLEN, query, + "INSERT INTO rua_panel_stat (caller_panel, rua_stat_tag, score) VALUES (?,?,?)"); + + r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); + if (r != SQLITE_OK) { + LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db)); + goto out; + } + + r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); + if (r != SQLITE_OK) { + LOGE("caller bind error(%d) \n", r); + goto out; + } + + r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC); + if (r != SQLITE_OK) { + LOGE("rua_stat_tag bind error(%d) \n", r); + goto out; + } + + r = sqlite3_bind_int(stmt, 3, WIN_SCORE); + if (r != SQLITE_OK) { + LOGE("arg bind error(%d) \n", r); + goto out; + } + + r = sqlite3_step(stmt); + if (r != SQLITE_DONE) { + LOGE("step error(%d) \n", r); + goto out; + } + +out: + if (stmt) + sqlite3_finalize(stmt); + + return r; +} + +int __rua_stat_lose_score_update(sqlite3 *db, char *caller, char *rua_stat_tag) +{ + int r; + char query[QUERY_MAXLEN]; + sqlite3_stmt *stmt = NULL; + + sqlite3_snprintf(QUERY_MAXLEN, query, + "UPDATE rua_panel_stat SET score = score * %f WHERE caller_panel = ? AND rua_stat_tag != ?", + LOSE_SCORE_RATE); + + LOGD("lose score update sql : %s", query); + r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); + if (r != SQLITE_OK) { + LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db)); + goto out; + } + + r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); + if (r != SQLITE_OK) { + LOGE("caller bind error(%d) \n", r); + goto out; + } + + r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC); + if (r != SQLITE_OK) { + LOGE("rua_stat_tag bind error(%d) \n", r); + goto out; + } + + r = sqlite3_step(stmt); + if (r != SQLITE_DONE) { + LOGE("step error(%d) \n", r); + goto out; + } + +out: + if (stmt) + sqlite3_finalize(stmt); + + return r; +} + +int __rua_stat_win_score_update(sqlite3 *db, char *caller, char *rua_stat_tag) +{ + int r; + char query[QUERY_MAXLEN]; + sqlite3_stmt *stmt = NULL; + + sqlite3_snprintf(QUERY_MAXLEN, query, + "UPDATE rua_panel_stat SET score = score + %d WHERE caller_panel = ? AND rua_stat_tag = ?", + WIN_SCORE); + + LOGD("win score update sql : %s", query); + + r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); + if (r != SQLITE_OK) { + LOGE("sqlite3_prepare error(%d , %d, %s)", r, sqlite3_extended_errcode(db), sqlite3_errmsg(db)); + goto out; + } + + r = sqlite3_bind_text(stmt, 1, caller, strlen(caller), SQLITE_STATIC); + if (r != SQLITE_OK) { + LOGE("caller bind error(%d) \n", r); + goto out; + } + + r = sqlite3_bind_text(stmt, 2, rua_stat_tag, strlen(rua_stat_tag), SQLITE_STATIC); + if (r != SQLITE_OK) { + LOGE("rua_stat_tag bind error(%d) \n", r); + goto out; + } + + r = sqlite3_step(stmt); + if (r != SQLITE_DONE) { + LOGE("step error(%d) \n", r); + goto out; + } + +out: + if (stmt) + sqlite3_finalize(stmt); + + return r; +} + +static int __exec(sqlite3 *db, char *query) +{ + int r; + char *errmsg = NULL; + + if (db == NULL) + return -1; + + r = sqlite3_exec(db, query, NULL, NULL, &errmsg); + if (r != SQLITE_OK) { + SECURE_LOGE("query(%s) exec error(%s)", query, errmsg); + sqlite3_free(errmsg); + return -1; + } + + return 0; +} + +static int __create_table(sqlite3 *db) +{ + int r; + + r = __exec(db, CREATE_RUA_STAT_TABLE); + if (r == -1) { + LOGE("create table error"); + return -1; + } + + return 0; +} + +static sqlite3 *__db_init(sqlite3 *db, char *root, int flags) +{ + int r; + + r = db_util_open_with_options(root, &db, flags, NULL); + if (r) { + LOGE("db util open error(%d/%d/%d/%s)", r, + sqlite3_errcode(db), + sqlite3_extended_errcode(db), + sqlite3_errmsg(db)); + return NULL; + + } + r = __create_table(db); + if (r) { + db_util_close(db); + return NULL; + } + + return db; +} + +int _rua_stat_init(sqlite3 *db, int flags) +{ + char defname[FILENAME_MAX]; + const char *rua_stat_db_path = tzplatform_getenv(TZ_USER_DB); + + snprintf(defname, sizeof(defname), "%s/%s", rua_stat_db_path, RUA_STAT_DB_NAME); + __db_init(db, defname, flags); + + if (db == NULL) { + LOGW("__rua_stat_init error"); + return -1; + } + + return 0; +} + +int _rua_stat_fini(sqlite3 *db) +{ + if (db) { + db_util_close(db); + db = NULL; + } + return 0; +} + +int rua_stat_db_update(char *caller, char *rua_stat_tag) +{ + int r; + int affected_rows = 0; + sqlite3 *db = NULL; + + LOGD("rua_stat_update start"); + + r = _rua_stat_init(db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE); + if (r == -1) { + LOGE("__rua_stat_init fail"); + return -1; + } + + if (db == NULL) { + LOGE("rua_stat is not initialized"); + return -1; + } + + if (caller == NULL) { + LOGE("caller is null"); + return -1; + } + + if (rua_stat_tag == NULL) { + LOGE("rua_stat_tag is null"); + return -1; + } + + + r = __rua_stat_lose_score_update(db, caller, rua_stat_tag); + if (r != SQLITE_DONE) { + LOGE("__rua_stat_lose_score_insert fail."); + return -1; + } + + r = __rua_stat_win_score_update(db, caller, rua_stat_tag); + affected_rows = sqlite3_changes(db); + if ((r != SQLITE_DONE) || (affected_rows == 0)) { + r = __rua_stat_insert(db, caller, rua_stat_tag); + + if (r != SQLITE_DONE) { + LOGE("__rua_stat_insert fail."); + return -1; + } + } + + _rua_stat_fini(db); + LOGD("rua_stat_update done"); + + return r; +} + diff --git a/test/rua-test.c b/test/rua-test.c index b84f742..68dcbe7 100644 --- a/test/rua-test.c +++ b/test/rua-test.c @@ -24,6 +24,7 @@ #include #include "rua.h" +#include "rua_internal.h" static int __add_history(char *pkgname) { @@ -34,7 +35,7 @@ static int __add_history(char *pkgname) rec.pkg_name = pkgname; rec.app_path = (char *)tzplatform_mkpath(TZ_SYS_RW_APP, pkgname); - ret = rua_add_history(&rec); + ret = rua_db_add_history(&rec); return ret; } -- 2.7.4