Modify query list in media-server 69/149969/9
authorMinje Ahn <minje.ahn@samsung.com>
Thu, 14 Sep 2017 00:44:53 +0000 (09:44 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Thu, 14 Sep 2017 07:16:01 +0000 (16:16 +0900)
Change-Id: Ibadd92eefe3deee2fea9324d3293e724c2c9496c
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
lib/media-util-db.c
src/server/media-server-socket.c

index cf66b88..d5e3c80 100755 (executable)
 #include "media-util.h"
 
 #define BUFSIZE 4096
-static __thread char **sql_list = NULL;
-static __thread int g_list_idx = 0;
+#define BATCH_START "BEGIN"
+#define BATCH_END "COMMIT"
+
+static __thread GArray *sql_list = NULL;
 
 static int __media_db_busy_handler(void *pData, int count);
 static int __media_db_connect_db_with_handle(sqlite3 **db_handle, uid_t uid, bool need_write);
@@ -45,13 +47,17 @@ static int __media_db_request_recovery(uid_t uid);
 
 static void __media_db_destroy_sql_list()
 {
-       int i = 0;
+       if (sql_list != NULL) {
+               while (sql_list->len != 0) {
+                       char *sql = g_array_index(sql_list, char *, 0);
+                       g_array_remove_index(sql_list, 0);
 
-       for (i = 0; i < g_list_idx; i++)
-               MS_SAFE_FREE(sql_list[i]);
+                       MS_SAFE_FREE(sql);
+               }
 
-       MS_SAFE_FREE(sql_list);
-       g_list_idx = 0;
+               g_array_unref(sql_list);
+               sql_list = NULL;
+       }
 }
 
 static int __media_db_busy_handler(void *pData, int count)
@@ -518,17 +524,25 @@ int media_db_update_db(MediaDBHandle *handle, const char *query_str)
 int media_db_update_db_batch_start(const char *query_str)
 {
        int ret = MS_MEDIA_ERR_NONE;
+       char *query = NULL;
 
        MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query");
 
-       if (g_list_idx != 0) {
-               MSAPI_DBG_ERR("Current idx is not 0");
+       if (sql_list != NULL) {
+               MSAPI_DBG_ERR("sql_list is already allocated");
                ret = MS_MEDIA_ERR_DB_SERVER_BUSY_FAIL;
        } else {
-               sql_list = (char**)malloc(sizeof(char*));
+               sql_list = g_array_new(FALSE, FALSE, sizeof(char*));
                MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
-               sql_list[g_list_idx++] = strdup(query_str);
-               MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
+
+               query = g_strdup(query_str);
+               if (MS_STRING_VALID(query)) {
+                       g_array_append_val(sql_list, query);
+               } else {
+                       MSAPI_DBG_ERR("query is null");
+                       __media_db_destroy_sql_list();
+                       ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
        }
 
        return ret;
@@ -537,14 +551,19 @@ int media_db_update_db_batch_start(const char *query_str)
 int media_db_update_db_batch(const char *query_str)
 {
        int ret = MS_MEDIA_ERR_NONE;
+       char *query = NULL;
 
        MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query");
 
-       sql_list = (char**)realloc(sql_list, (g_list_idx + 1) * sizeof(char*));
-       MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
-
-       sql_list[g_list_idx++] = strdup(query_str);
-       MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
+       if (sql_list) {
+               query = g_strdup(query_str);
+               if (MS_STRING_VALID(query)) {
+                       g_array_append_val(sql_list, query);
+               } else {
+                       MSAPI_DBG_ERR("query is null");
+                       ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+       }
 
        return ret;
 }
@@ -553,6 +572,7 @@ int media_db_update_db_batch_end(MediaDBHandle *handle, const char *query_str)
 {
        sqlite3 * db_handle = (sqlite3 *)handle;
        int ret = MS_MEDIA_ERR_NONE;
+       char *query = NULL;
 
        if (db_handle == NULL || (!MS_STRING_VALID(query_str))) {
                __media_db_destroy_sql_list();
@@ -560,37 +580,36 @@ int media_db_update_db_batch_end(MediaDBHandle *handle, const char *query_str)
                return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
-       sql_list = (char**)realloc(sql_list, (g_list_idx + 1) * sizeof(char*));
-       if (sql_list == NULL) {
-               __media_db_destroy_sql_list();
-               MSAPI_DBG_ERR("Out of memory");
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
-
-       sql_list[g_list_idx++] = strdup(query_str);
-       if (sql_list[g_list_idx - 1] == NULL) {
-               __media_db_destroy_sql_list();
-               MSAPI_DBG_ERR("Out of memory");
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
+       if (sql_list) {
+               query = g_strdup(query_str);
+               if (MS_STRING_VALID(query)) {
+                       g_array_append_val(sql_list, query);
+               } else {
+                       __media_db_destroy_sql_list();
+                       MSAPI_DBG_ERR("query is null");
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
 
-       int i = 0;
-       char *current_sql = NULL;
-       for (i = 0; i < g_list_idx; i++) {
-               current_sql = sql_list[i];
-               ret = _media_db_update_directly(db_handle, current_sql);
-               if (ret < 0) {
-                       if (i == 0) {
-                               /* This is fail of "BEGIN" */
-                               MSAPI_DBG_ERR("Query failed : %s", current_sql);
-                               break;
-                       } else if (i == g_list_idx - 1) {
-                               /* This is fail of "COMMIT" */
-                               MSAPI_DBG_ERR("Query failed : %s", current_sql);
-                               break;
-                       } else {
-                               MSAPI_DBG_ERR("Query failed : %s, but keep going to run remaining queries", current_sql);
+               while (sql_list->len != 0) {
+                       char *current_sql = NULL;
+
+                       current_sql = g_array_index(sql_list, char *, 0);
+                       g_array_remove_index(sql_list, 0);
+
+                       if (MS_STRING_VALID(current_sql)) {
+                               ret = _media_db_update_directly(db_handle, current_sql);
+                               if (ret < 0) {
+                                       if (strncmp(current_sql, BATCH_START, strlen(BATCH_START)) == 0 || strncmp(current_sql, BATCH_END, strlen(BATCH_END)) == 0) {
+                                               MSAPI_DBG_ERR("Query failed : %s", current_sql);
+                                               MS_SAFE_FREE(current_sql);
+                                               break;
+                                       } else {
+                                               MSAPI_DBG_ERR("Query failed : %s, but keep going to run remaining queries", current_sql);
+                                       }
+                               }
                        }
+
+                       MS_SAFE_FREE(current_sql);
                }
        }
 
index 8b51857..19ffd6a 100755 (executable)
@@ -712,9 +712,8 @@ void _ms_process_tcp_message(gpointer data, gpointer user_data)
                                break;
                        }
 
-                       if (ret < MS_MEDIA_ERR_NONE && recv_msg.msg_type == MS_MSG_DB_UPDATE_BATCH_START) {
+                       if (ret != MS_MEDIA_ERR_NONE && recv_msg.msg_type == MS_MSG_DB_UPDATE_BATCH_START) {
                                MS_DBG_ERR("Batch job start is failed!client sockfd [%d]", client_sock);
-                               media_db_request_update_db_batch_clear();
                                break;
                        }
 
@@ -734,8 +733,8 @@ void _ms_process_tcp_message(gpointer data, gpointer user_data)
        /* Disconnect DB*/
        media_db_disconnect(db_handle);
 
-       if (g_atomic_int_dec_and_test(&cur_running_task) == FALSE)
-               MS_DBG_ERR("g_atomic_int_dec_and_test failed");
+       if (g_atomic_int_dec_and_test(&cur_running_task) == TRUE)
+               MS_DBG_INFO("There is no running task");
 
        return;
 
@@ -753,8 +752,8 @@ ERROR:
        /* Disconnect DB*/
        media_db_disconnect(db_handle);
 
-       if (g_atomic_int_dec_and_test(&cur_running_task) == FALSE)
-               MS_DBG_ERR("g_atomic_int_dec_and_test failed");
+       if (g_atomic_int_dec_and_test(&cur_running_task) == TRUE)
+               MS_DBG_INFO("There is no running task");
 
        return;
 }