make simple book-delete logic 74/83674/9
authorJeesun Kim <iamjs.kim@samsung.com>
Fri, 12 Aug 2016 09:27:48 +0000 (18:27 +0900)
committerJeesun Kim <iamjs.kim@samsung.com>
Mon, 22 Aug 2016 02:18:14 +0000 (11:18 +0900)
Change-Id: I0ccfe6479b78bedc00e4bba0b5760f707d9d66ea

server/cal_server.c
server/cal_server_calendar_delete.c [deleted file]
server/cal_server_cleanup.c [new file with mode: 0644]
server/cal_server_cleanup.h [moved from server/cal_server_calendar_delete.h with 91% similarity]
server/db/cal_db_plugin_book.c

index 4b06bc9..2d56ded 100644 (file)
@@ -41,7 +41,7 @@
 #include "cal_time.h"
 #include "cal_server_alarm.h"
 #include "cal_server_contacts.h"
-#include "cal_server_calendar_delete.h"
+#include "cal_server_cleanup.h"
 #include "cal_server_schema.h"
 #include "cal_server_update.h"
 #include "cal_server_service.h"
@@ -81,7 +81,6 @@ static int _cal_server_init(void)
 
        cal_db_initialize_view_table();
 
-       cal_server_calendar_delete_start();
        return CALENDAR_ERROR_NONE;
 }
 
@@ -157,6 +156,7 @@ int main(int argc, char *argv[])
        cal_server_update();
 
        _cal_server_init();
+       cal_server_cleanup_start();
        cal_server_alarm_init();
        cal_server_account_init();
        cal_server_contacts_init();
@@ -170,6 +170,7 @@ int main(int argc, char *argv[])
        cal_server_contacts_deinit();
        cal_server_account_deinit();
        cal_server_alarm_deinit();
+       cal_server_cleanup_end();
 
        _cal_server_deinit();
        cal_server_dbus_deinit(id);
diff --git a/server/cal_server_calendar_delete.c b/server/cal_server_calendar_delete.c
deleted file mode 100644 (file)
index 4bfc930..0000000
+++ /dev/null
@@ -1,341 +0,0 @@
-/*
- * Calendar Service
- *
- * Copyright (c) 2012 - 2015 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 <stdlib.h>
-#include <glib.h>
-#include <unistd.h>
-
-#include "calendar.h"
-#include "cal_typedef.h"
-#include "cal_internal.h"
-#include "cal_server_calendar_delete.h"
-#include "cal_server_service.h"
-#include "cal_db.h"
-#include "cal_db_util.h"
-
-#define CAL_SERVER_CALENDAR_DELETE_COUNT 50
-#define CAL_SERVER_CALENDAR_DELETE_STEP_TIME 1
-#define CAL_SERVER_CALENDAR_DELETE_THREAD_NAME "cal_server_calendar_delete"
-
-typedef enum {
-       STEP_1, /* create calendar_id_list */
-       STEP_2, /* delete schedule_table <-- CAL_SERVER_CALENDAR_DELETE_COUNT */
-       STEP_3, /* delete calendar_table*/
-} __calendar_delete_step_e;
-
-typedef struct {
-       GList *calendar_id_list;
-       int current_calendar_id;
-       __calendar_delete_step_e step;
-} __calendar_delete_data_s;
-
-GThread *_cal_server_calendar_delete_thread = NULL;
-GCond _cal_server_calendar_delete_cond;
-GMutex _cal_server_calendar_delete_mutex;
-
-static bool _cal_server_calendar_delete_step(int ret, __calendar_delete_data_s* data);
-static int _cal_server_calendar_delete_step1(__calendar_delete_data_s* data);
-static int _cal_server_calendar_delete_step2(__calendar_delete_data_s* data);
-static int _cal_server_calendar_delete_step3(__calendar_delete_data_s* data);
-static bool  _cal_server_calendar_run(__calendar_delete_data_s* data);
-static gpointer  _cal_server_calendar_main(gpointer user_data);
-
-static bool _cal_server_calendar_delete_step(int ret, __calendar_delete_data_s* data)
-{
-       if (CALENDAR_ERROR_NONE != ret && CALENDAR_ERROR_NO_DATA != ret) {
-               /* LCOV_EXCL_START */
-               ERR("_cal_server_calendar_delete_step Fail(%d)", ret);
-               if (data->calendar_id_list)
-                       g_list_free(data->calendar_id_list);
-
-               CAL_FREE(data);
-               return false;
-               /* LCOV_EXCL_STOP */
-       }
-       switch (data->step) {
-       case STEP_1:
-               if (ret == CALENDAR_ERROR_NO_DATA) {
-                       if (data->calendar_id_list)
-                               g_list_free(data->calendar_id_list);
-                       CAL_FREE(data);
-                       return false;
-               }
-               data->step = STEP_2;
-               break;
-       case STEP_2:
-               if (ret == CALENDAR_ERROR_NO_DATA)
-                       data->step = STEP_3;
-
-               break;
-       case STEP_3:
-               data->step = STEP_1;
-               break;
-       default:
-               data->step = STEP_1;
-               break;
-       }
-
-       return true;
-}
-
-static int _cal_server_calendar_delete_step1(__calendar_delete_data_s* data)
-{
-       int ret = 0;
-       char query[CAL_DB_SQL_MIN_LEN] = {0,};
-       sqlite3_stmt *stmt = NULL;
-       int count = 0;
-
-       CAL_FN_CALL();
-
-       if (NULL == data->calendar_id_list) {
-               /* get event_list */
-               snprintf(query, sizeof(query), "SELECT id FROM %s WHERE deleted = 1", CAL_TABLE_CALENDAR);
-               ret = cal_db_util_query_prepare(query, &stmt);
-               if (CALENDAR_ERROR_NONE != ret) {
-                       /* LCOV_EXCL_START */
-                       ERR("cal_db_util_query_prepare() Fail(%d)", ret);
-                       SECURE("query[%s]", query);
-                       return ret;
-                       /* LCOV_EXCL_STOP */
-               }
-               while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
-                       int id = 0;
-                       id = sqlite3_column_int(stmt, 0);
-                       data->calendar_id_list = g_list_append(data->calendar_id_list, GINT_TO_POINTER(id));
-               }
-               sqlite3_finalize(stmt);
-       }
-
-       count = g_list_length(data->calendar_id_list);
-       if (count <= 0)
-               return CALENDAR_ERROR_NO_DATA;
-
-       GList *cursor = g_list_first(data->calendar_id_list);
-       if (cursor) {
-               data->current_calendar_id = GPOINTER_TO_INT(cursor->data);
-               data->calendar_id_list = g_list_remove(data->calendar_id_list, GINT_TO_POINTER(data->current_calendar_id));
-
-               return CALENDAR_ERROR_NONE;
-       } else {
-               return CALENDAR_ERROR_NO_DATA;
-       }
-}
-
-static int _cal_server_calendar_delete_step2(__calendar_delete_data_s* data)
-{
-       char query[CAL_DB_SQL_MIN_LEN] = {0};
-       int ret = 0;
-       GList *list = NULL;
-       sqlite3_stmt *stmt = NULL;
-       int count = 0;
-
-       CAL_FN_CALL();
-
-       ret = cal_db_util_begin_trans();
-       if (CALENDAR_ERROR_NONE != ret) {
-               /* LCOV_EXCL_START */
-               ERR("cal_db_util_begin_trans() failed");
-               return CALENDAR_ERROR_DB_FAILED;
-               /* LCOV_EXCL_STOP */
-       }
-
-       /* get event_list */
-       snprintf(query, sizeof(query), "SELECT id FROM %s WHERE calendar_id = %d LIMIT %d",
-                       CAL_TABLE_SCHEDULE, data->current_calendar_id, CAL_SERVER_CALENDAR_DELETE_COUNT);
-
-       ret = cal_db_util_query_prepare(query, &stmt);
-       if (CALENDAR_ERROR_NONE != ret) {
-               /* LCOV_EXCL_START */
-               ERR("cal_db_util_query_prepare() Fail(%d)", ret);
-               SECURE("query[%s]", query);
-               cal_db_util_end_trans(false);
-               return ret;
-               /* LCOV_EXCL_STOP */
-       }
-
-       while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
-               int id = 0;
-               id = sqlite3_column_int(stmt, 0);
-               list = g_list_append(list, GINT_TO_POINTER(id));
-       }
-       sqlite3_finalize(stmt);
-       stmt = NULL;
-
-       count = g_list_length(list);
-       if (count <= 0) {
-               cal_db_util_end_trans(false);
-               return CALENDAR_ERROR_NO_DATA;
-       }
-
-       GList *cursor = g_list_first(list);
-       while (cursor) {
-               int id = GPOINTER_TO_INT(cursor->data);
-               /* delete event table */
-               snprintf(query, sizeof(query), "DELETE FROM %s WHERE id=%d", CAL_TABLE_SCHEDULE, id);
-               ret = cal_db_util_query_exec(query);
-               if (CALENDAR_ERROR_NONE != ret) {
-                       /* LCOV_EXCL_START */
-                       ERR("cal_db_util_query_exec() Fail(%d)", ret);
-                       SECURE("[%s]", query);
-                       cal_db_util_end_trans(false);
-                       g_list_free(list);
-                       return ret;
-                       /* LCOV_EXCL_STOP */
-               }
-               cursor = g_list_next(cursor);
-       }
-
-       g_list_free(list);
-
-       cal_db_util_end_trans(true);
-
-       return CALENDAR_ERROR_NONE;
-}
-
-static int _cal_server_calendar_delete_step3(__calendar_delete_data_s* data)
-{
-       int ret = CALENDAR_ERROR_NONE;
-       char query[CAL_DB_SQL_MIN_LEN] = {0};
-
-       ret = cal_db_util_begin_trans();
-       if (CALENDAR_ERROR_NONE != ret) {
-               /* LCOV_EXCL_START */
-               ERR("cal_db_util_begin_trans() failed");
-               return CALENDAR_ERROR_DB_FAILED;
-               /* LCOV_EXCL_STOP */
-       }
-
-       CAL_FN_CALL();
-
-       /* delete event table */
-       snprintf(query, sizeof(query), "DELETE FROM %s WHERE id=%d", CAL_TABLE_CALENDAR, data->current_calendar_id);
-       ret = cal_db_util_query_exec(query);
-       if (CALENDAR_ERROR_NONE != ret) {
-               /* LCOV_EXCL_START */
-               ERR("cal_db_util_query_exec() Fail(%d)", ret);
-               SECURE("[%s]", query);
-               cal_db_util_end_trans(false);
-               return ret;
-               /* LCOV_EXCL_STOP */
-       }
-       cal_db_util_end_trans(true);
-
-       return CALENDAR_ERROR_NONE;
-}
-
-static bool  _cal_server_calendar_run(__calendar_delete_data_s* data)
-{
-       int ret = CALENDAR_ERROR_NONE;
-
-       CAL_FN_CALL();
-
-       if (data == NULL) {
-               /* LCOV_EXCL_START */
-               ERR("data is NULL");
-               return false;
-               /* LCOV_EXCL_STOP */
-       }
-
-       switch (data->step) {
-       case STEP_1:
-               ret = _cal_server_calendar_delete_step1(data);
-               break;
-       case STEP_2:
-               ret = _cal_server_calendar_delete_step2(data);
-               break;
-       case STEP_3:
-               ret = _cal_server_calendar_delete_step3(data);
-               break;
-       default:
-               /* LCOV_EXCL_START */
-               ERR("invalid step");
-               if (data->calendar_id_list)
-                       g_list_free(data->calendar_id_list);
-
-               CAL_FREE(data);
-               return false;
-               /* LCOV_EXCL_STOP */
-       }
-
-       return _cal_server_calendar_delete_step(ret, data);
-
-}
-
-static gpointer _cal_server_calendar_main(gpointer user_data)
-{
-       int ret = CALENDAR_ERROR_NONE;
-       CAL_FN_CALL();
-
-       while (1) {
-               __calendar_delete_data_s *callback_data = NULL;
-               callback_data = calloc(1, sizeof(__calendar_delete_data_s));
-               if (NULL == callback_data) {
-                       /* LCOV_EXCL_START */
-                       ERR("calloc() Fail");
-                       break;
-                       /* LCOV_EXCL_STOP */
-               }
-
-               callback_data->step = STEP_1;
-
-               /* delete */
-               ret = cal_connect();
-               if (CALENDAR_ERROR_NONE != ret) {
-                       /* LCOV_EXCL_START */
-                       ERR("cal_connect() Fail(%d)", ret);
-                       free(callback_data);
-                       break;
-                       /* LCOV_EXCL_STOP */
-               }
-
-               while (1) {
-                       sleep(CAL_SERVER_CALENDAR_DELETE_STEP_TIME); /* sleep 1 sec. */
-                       ret = _cal_server_calendar_run(callback_data);
-                       if (false == ret) {
-                               DBG("_cal_server_calendar_run() return false");
-                               callback_data = NULL;
-                               DBG("end");
-                               break;
-                       }
-               }
-               cal_disconnect();
-               CAL_FREE(callback_data);
-
-               g_mutex_lock(&_cal_server_calendar_delete_mutex);
-               DBG("wait");
-               g_cond_wait(&_cal_server_calendar_delete_cond, &_cal_server_calendar_delete_mutex);
-               g_mutex_unlock(&_cal_server_calendar_delete_mutex);
-       }
-
-       return NULL;
-}
-
-void cal_server_calendar_delete_start(void)
-{
-       CAL_FN_CALL();
-
-       if (NULL == _cal_server_calendar_delete_thread) {
-               g_mutex_init(&_cal_server_calendar_delete_mutex);
-               g_cond_init(&_cal_server_calendar_delete_cond);
-               _cal_server_calendar_delete_thread = g_thread_new(CAL_SERVER_CALENDAR_DELETE_THREAD_NAME,
-                               _cal_server_calendar_main, NULL);
-       }
-       /* don't use mutex. */
-       g_cond_signal(&_cal_server_calendar_delete_cond);
-}
diff --git a/server/cal_server_cleanup.c b/server/cal_server_cleanup.c
new file mode 100644 (file)
index 0000000..1cd5ecd
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ * Calendar Service
+ *
+ * Copyright (c) 2012 - 2015 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 <stdlib.h>
+#include <glib.h>
+#include <unistd.h>
+
+#include "calendar.h"
+#include "cal_typedef.h"
+#include "cal_internal.h"
+#include "cal_db.h"
+#include "cal_db_util.h"
+#include "cal_server_service.h"
+#include "cal_server_ondemand.h"
+#include "cal_server_cleanup.h"
+
+#define CAL_SERVER_CALENDAR_DELETE_COUNT 50
+#define CAL_SERVER_CALENDAR_DELETE_STEP_TIME 1
+#define CAL_SERVER_CALENDAR_DELETE_THREAD_NAME "cal_server_cleanup"
+
+GThread *_cal_server_cleanup_thread = NULL;
+GCond _cal_server_cleanup_cond;
+GMutex _cal_server_cleanup_mutex;
+static bool server_killed = false;
+
+static int cal_server_calendar_get_count(int *out_count)
+{
+       RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
+
+       int ret = 0;
+       char query[CAL_DB_SQL_MIN_LEN] = {0};
+       sqlite3_stmt *stmt = NULL;
+
+       snprintf(query, sizeof(query), "SELECT count(id) FROM "CAL_TABLE_CALENDAR" "
+                       "WHERE deleted = 1");
+       ret = cal_db_util_query_prepare(query, &stmt);
+       if (CALENDAR_ERROR_NONE != ret) {
+               /* LCOV_EXCL_START */
+               ERR("cal_db_util_query_prepare() Fail(%d)", ret);
+               SECURE("query[%s]", query);
+               return ret;
+               /* LCOV_EXCL_STOP */
+       }
+
+       ret = cal_db_util_stmt_step(stmt);
+       if (CAL_SQLITE_ROW != ret) {
+               /* LCOV_EXCL_START */
+               ERR("cal_db_util_stmt_step() Fail(%d)", ret);
+               sqlite3_finalize(stmt);
+               return ret;
+               /* LCOV_EXCL_STOP */
+       }
+
+       *out_count = sqlite3_column_int(stmt, 0);
+       sqlite3_finalize(stmt);
+
+       return CALENDAR_ERROR_NONE;
+}
+
+static void cal_server_calendar_clean_book(int in_count)
+{
+       int ret = 0;
+       char query[CAL_DB_SQL_MIN_LEN] = {0};
+
+       while (in_count > 0) {
+               sleep(CAL_SERVER_CALENDAR_DELETE_STEP_TIME); /* sleep 1 sec. */
+
+               ret = cal_db_util_begin_trans();
+               if (CALENDAR_ERROR_NONE != ret) {
+                       /* LCOV_EXCL_START */
+                       ERR("cal_db_util_begin_trans() Fail(%d)", ret);
+                       break;
+                       /* LCOV_EXCL_STOP */
+               }
+
+               /* SQLITE ENABLE UPDATE DELETE LIMIT is nat activated */
+               snprintf(query, sizeof(query), "DELETE FROM "CAL_TABLE_CALENDAR" "
+                               "WHERE id in (SELECT id FROM "CAL_TABLE_CALENDAR" WHERE "
+                               "deleted = 1 LIMIT %d)", CAL_SERVER_CALENDAR_DELETE_COUNT);
+               ret = cal_db_util_query_exec(query);
+               if (CALENDAR_ERROR_NONE != ret) {
+                       /* LCOV_EXCL_START */
+                       ERR("cal_db_util_query_exec() Fail(%d)", ret);
+                       SECURE("[%s]", query);
+                       cal_db_util_end_trans(false);
+                       break;
+                       /* LCOV_EXCL_STOP */
+               }
+               cal_db_util_end_trans(true);
+               in_count -= CAL_SERVER_CALENDAR_DELETE_COUNT;
+       }
+}
+
+static gpointer _cal_server_calendar_main(gpointer user_data)
+{
+       int ret = CALENDAR_ERROR_NONE;
+       CAL_FN_CALL();
+
+       while (1) {
+               /*
+                * While syncing with contacts,
+                * because calendar-service could be stopped by on-demand,
+                * holding on-demand is needed.
+                */
+               cal_server_ondemand_hold();
+
+               ret = cal_connect();
+               if (CALENDAR_ERROR_NONE != ret) {
+                       /* LCOV_EXCL_START */
+                       ERR("cal_connect() Fail(%d)", ret);
+                       cal_server_ondemand_release();
+                       cal_server_ondemand_start();
+                       break;
+                       /* LCOV_EXCL_STOP */
+               }
+
+               do {
+                       int deleted_count = 0;
+                       ret = cal_server_calendar_get_count(&deleted_count);
+                       if (CALENDAR_ERROR_NONE != ret)
+                               break;
+                       cal_server_calendar_clean_book(deleted_count);
+               } while (0);
+
+               cal_disconnect();
+
+               g_mutex_lock(&_cal_server_cleanup_mutex);
+               DBG("wait");
+               cal_server_ondemand_release();
+               cal_server_ondemand_start();
+               g_cond_wait(&_cal_server_cleanup_cond, &_cal_server_cleanup_mutex);
+               g_mutex_unlock(&_cal_server_cleanup_mutex);
+               if (server_killed)
+                       break;
+       }
+       g_thread_exit(NULL);
+
+       return NULL;
+}
+
+void cal_server_cleanup_start(void)
+{
+       CAL_FN_CALL();
+
+       if (server_killed)
+               return;
+
+       if (NULL == _cal_server_cleanup_thread) {
+               g_mutex_init(&_cal_server_cleanup_mutex);
+               g_cond_init(&_cal_server_cleanup_cond);
+               _cal_server_cleanup_thread = g_thread_new(CAL_SERVER_CALENDAR_DELETE_THREAD_NAME,
+                               _cal_server_calendar_main, NULL);
+       }
+       /* don't use mutex. */
+       g_cond_signal(&_cal_server_cleanup_cond);
+}
+
+void cal_server_cleanup_end(void)
+{
+       server_killed = true;
+       /* don't use mutex. */
+       g_cond_signal(&_cal_server_cleanup_cond);
+       g_thread_join(_cal_server_cleanup_thread);
+       g_thread_unref(_cal_server_cleanup_thread);
+}
similarity index 91%
rename from server/cal_server_calendar_delete.h
rename to server/cal_server_cleanup.h
index 84028c0..d0cc526 100644 (file)
@@ -19,6 +19,7 @@
 #ifndef __CAL_SERVER_CALENDAR_DELETE_H__
 #define __CAL_SERVER_CALENDAR_DELETE_H__
 
-void cal_server_calendar_delete_start(void);
+void cal_server_cleanup_start(void);
+void cal_server_cleanup_end(void);
 
 #endif /*__CAL_SERVER_CALENDAR_DELETE_H__*/
index 33627e5..b22eb22 100644 (file)
@@ -30,7 +30,7 @@
 #include "cal_utils.h"
 
 #ifdef CAL_IPC_SERVER
-#include "cal_server_calendar_delete.h"
+#include "cal_server_cleanup.h"
 #endif
 #include "cal_access_control.h"
 
@@ -338,7 +338,7 @@ static int _cal_db_book_delete_record(int id)
                snprintf(query, sizeof(query), "UPDATE %s SET deleted = 1 WHERE id = %d", CAL_TABLE_CALENDAR, id);
                ret = cal_db_util_query_exec(query);
                RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_util_query_exec() Fail(%d)", ret);
-               cal_server_calendar_delete_start();
+               cal_server_cleanup_start();
        } else {
                snprintf(query, sizeof(query), "DELETE FROM %s WHERE id = %d", CAL_TABLE_CALENDAR, id);
                ret = cal_db_util_query_exec(query);