[tracker/binary-log-2: 39/48] Reimplemented backup code. Restore is still TODO.



commit e4fb1bf8ce84698f7fd4268c26eea3009c569d94
Author: Philip Van Hoof <philip codeminded be>
Date:   Mon Jan 4 11:19:30 2010 +0100

    Reimplemented backup code. Restore is still TODO.

 data/dbus/tracker-backup.xml              |    2 -
 src/libtracker-data/tracker-data-backup.c |  110 ++++++++++++++++++++++++++---
 src/libtracker-data/tracker-data-backup.h |    4 +-
 src/libtracker-db/tracker-db-manager.c    |   14 +---
 src/tracker-store/tracker-backup.c        |   72 ++++---------------
 src/tracker-store/tracker-backup.h        |    2 -
 6 files changed, 117 insertions(+), 87 deletions(-)
---
diff --git a/data/dbus/tracker-backup.xml b/data/dbus/tracker-backup.xml
index 44f93f0..d079eee 100644
--- a/data/dbus/tracker-backup.xml
+++ b/data/dbus/tracker-backup.xml
@@ -5,11 +5,9 @@
     <method name="Save">
       <annotation name="org.freedesktop.DBus.GLib.Async" value="true"/>
       <arg type="s" name="destination-uri" direction="in" />
-      <arg type="s" name="journal-uri" direction="in" />
     </method>
     <method name="Restore">
       <annotation name="org.freedesktop.DBus.GLib.Async" value="true"/>
-      <arg type="s" name="backup-uri" direction="in" />
       <arg type="s" name="journal-uri" direction="in" />
     </method>
   </interface>
diff --git a/src/libtracker-data/tracker-data-backup.c b/src/libtracker-data/tracker-data-backup.c
index ee1c4fe..c20e422 100644
--- a/src/libtracker-data/tracker-data-backup.c
+++ b/src/libtracker-data/tracker-data-backup.c
@@ -27,31 +27,121 @@
 
 #include "tracker-data-backup.h"
 
-GQuark
-tracker_data_backup_error_quark (void)
+typedef struct {
+	GFile *destination, *journal;
+	TrackerDataBackupFinished callback;
+	gpointer user_data;
+	GDestroyNotify destroy;
+	GError *error;
+} BackupSaveInfo;
+
+static void
+free_backup_save_info (BackupSaveInfo *info)
+{
+	if (info->destination) {
+		g_object_unref (info->destination);
+	}
+
+	if (info->journal) {
+		g_object_unref (info->journal);
+	}
+
+	if (info->destroy) {
+		info->destroy (info->user_data);
+	}
+
+	g_clear_error (&info->error);
+
+	g_free (info);
+}
+
+static void
+on_journal_copied (GObject *source_object,
+                   GAsyncResult *res,
+                   gpointer user_data)
 {
-	return g_quark_from_static_string ("tracker-data-backup-error-quark");
+	BackupSaveInfo *info = user_data;
+	GError *error = NULL;
+
+	g_file_copy_finish (info->journal, res, &error);
+
+	if (info->callback) {
+		info->callback (error, info->user_data);
+	}
+
+	free_backup_save_info (info);
+
+	g_clear_error (&error);
 }
 
 void
 tracker_data_backup_save (GFile *destination,
-                          GFile *journal,
                           TrackerDataBackupFinished callback,
                           gpointer user_data,
                           GDestroyNotify destroy)
 {
-	// TODO: Unimplemented
-	g_critical ("tracker_data_backup_save unimplemented");
+	BackupSaveInfo *info;
+
+	info = g_new0 (BackupSaveInfo, 1);
+	info->destination = g_object_ref (destination);
+	info->journal = g_file_new_for_path (tracker_db_journal_filename ());
+	info->callback = callback;
+	info->user_data = user_data;
+	info->destroy = destroy;
+
+	g_file_copy_async (info->journal, info->destination,
+	                   G_FILE_COPY_OVERWRITE,
+	                   G_PRIORITY_HIGH,
+	                   NULL, NULL, NULL,
+	                   on_journal_copied,
+	                   info);
+}
+
+static gboolean
+on_restore_done (gpointer user_data)
+{
+	BackupSaveInfo *info = user_data;
+
+	if (info->callback) {
+		info->callback (info->error, info->user_data);
+	}
+
+	free_backup_save_info (info);
+
+	return FALSE;
 }
 
 void
-tracker_data_backup_restore (GFile *backup,
-                             GFile *journal,
+tracker_data_backup_restore (GFile *journal,
                              TrackerDataBackupFinished callback,
                              gpointer user_data,
                              GDestroyNotify destroy)
 {
-	// TODO: Unimplemented
-	g_critical ("tracker_data_backup_restore");
+	BackupSaveInfo *info;
+
+	tracker_db_manager_disconnect ();
+	tracker_db_journal_close ();
+
+	info = g_new0 (BackupSaveInfo, 1);
+	info->destination = g_file_new_for_path (tracker_db_journal_filename ());
+	info->journal = g_object_ref (journal);
+	info->callback = callback;
+	info->user_data = user_data;
+	info->destroy = destroy;
+
+	/* TODO, this depends on Jürg's replay APIs */
+
+	g_file_copy (info->journal, info->destination,
+	             G_FILE_COPY_OVERWRITE,
+	             NULL, NULL, NULL,
+	             &info->error);
+
+	tracker_db_journal_open (NULL);
+	/* tracker_db_journal_replay (); */
+	tracker_db_manager_reconnect ();
+
+	tracker_db_backup_sync_fts ();
+
+	g_idle_add (on_restore_done, info);
 }
 
diff --git a/src/libtracker-data/tracker-data-backup.h b/src/libtracker-data/tracker-data-backup.h
index 5941dd0..c5abf06 100644
--- a/src/libtracker-data/tracker-data-backup.h
+++ b/src/libtracker-data/tracker-data-backup.h
@@ -34,12 +34,10 @@ typedef void (*TrackerDataBackupFinished) (GError *error, gpointer user_data);
 
 GQuark tracker_data_backup_error_quark (void);
 void   tracker_data_backup_save        (GFile                     *destination,
-                                        GFile                     *journal,
                                         TrackerDataBackupFinished  callback,
                                         gpointer                   user_data,
                                         GDestroyNotify             destroy);
-void   tracker_data_backup_restore     (GFile                     *backup,
-                                        GFile                     *journal,
+void   tracker_data_backup_restore     (GFile                     *journal,
                                         TrackerDataBackupFinished  callback,
                                         gpointer                   user_data,
                                         GDestroyNotify             destroy);
diff --git a/src/libtracker-db/tracker-db-manager.c b/src/libtracker-db/tracker-db-manager.c
index b1607d7..913db90 100644
--- a/src/libtracker-db/tracker-db-manager.c
+++ b/src/libtracker-db/tracker-db-manager.c
@@ -847,7 +847,7 @@ db_interface_create (TrackerDB db)
 }
 
 static void
-db_manager_remove_all (gboolean rm_backup_and_log, gboolean not_meta)
+db_manager_remove_all (gboolean rm_backup_and_log)
 {
 	guint i;
 
@@ -858,10 +858,6 @@ db_manager_remove_all (gboolean rm_backup_and_log, gboolean not_meta)
 	 */
 	for (i = 1; i < G_N_ELEMENTS (dbs); i++) {
 
-		if (not_meta && i == TRACKER_DB_METADATA) {
-			continue;
-		}
-
 		g_message ("  Removing database:'%s'",
 		           dbs[i].abs_filename);
 		g_unlink (dbs[i].abs_filename);
@@ -1067,7 +1063,7 @@ tracker_db_manager_init (TrackerDBManagerFlags  flags,
 	gchar              *filename;
 	const gchar        *dir;
 	const gchar        *env_path;
-	gboolean            need_reindex, did_copy = FALSE;
+	gboolean            need_reindex;
 	guint               i;
 	gchar              *in_use_filename;
 	int                 in_use_file;
@@ -1316,10 +1312,6 @@ tracker_db_manager_init (TrackerDBManagerFlags  flags,
 		                                                        TRACKER_DB_CONTENTS);
 	}
 
-	if (did_copy) {
-		tracker_db_backup_sync_fts ();
-	}
-
 	return TRUE;
 }
 
@@ -1435,7 +1427,7 @@ tracker_db_manager_remove_all (gboolean rm_backup_and_log)
 {
 	g_return_if_fail (initialized != FALSE);
 
-	db_manager_remove_all (rm_backup_and_log, FALSE);
+	db_manager_remove_all (rm_backup_and_log);
 }
 
 void
diff --git a/src/tracker-store/tracker-backup.c b/src/tracker-store/tracker-backup.c
index b677e3f..e575427 100644
--- a/src/tracker-store/tracker-backup.c
+++ b/src/tracker-store/tracker-backup.c
@@ -34,8 +34,6 @@
 typedef struct {
 	DBusGMethodInvocation *context;
 	guint request_id;
-	gboolean play_journal;
-	GFile *destination, *journal;
 } TrackerDBusMethodInfo;
 
 G_DEFINE_TYPE (TrackerBackup, tracker_backup, G_TYPE_OBJECT)
@@ -57,22 +55,6 @@ tracker_backup_new (void)
 }
 
 static void
-destroy_method_info (gpointer user_data)
-{
-	TrackerDBusMethodInfo *info = user_data;
-
-	if (info->destination) {
-		g_object_unref (info->destination);
-	}
-
-	if (info->journal) {
-		g_object_unref (info->journal);
-	}
-
-	g_free (info);
-}
-
-static void
 backup_callback (GError *error, gpointer user_data)
 {
 	TrackerDBusMethodInfo *info = user_data;
@@ -90,29 +72,15 @@ backup_callback (GError *error, gpointer user_data)
 	tracker_dbus_request_success (info->request_id);
 }
 
-static void
-on_batch_commit (gpointer user_data)
-{
-	TrackerDBusMethodInfo *info = user_data;
-
-	/* At this point no transactions are left open, we can now start the
-	 * sqlite3_backup API, which will run itself as a GSource within the
-	 * mainloop after it got initialized (which will reopen the mainloop) */
-
-	tracker_data_backup_save (info->destination, info->journal,
-	                          backup_callback,
-	                          info, destroy_method_info);
-}
-
 void
 tracker_backup_save (TrackerBackup          *object,
                      const gchar            *destination_uri,
-                     const gchar            *journal_uri,
                      DBusGMethodInvocation  *context,
                      GError                **error)
 {
 	guint request_id;
 	TrackerDBusMethodInfo *info;
+	GFile *destination;
 
 	request_id = tracker_dbus_get_next_request_id ();
 
@@ -124,56 +92,42 @@ tracker_backup_save (TrackerBackup          *object,
 
 	info->request_id = request_id;
 	info->context = context;
-	info->play_journal = FALSE;
-	info->destination = g_file_new_for_uri (destination_uri);
-	info->journal = g_file_new_for_uri (journal_uri);
+	destination = g_file_new_for_uri (destination_uri);
 
-	/* The sqlite3_backup API apparently doesn't much like open transactions,
-	 * this queue_commit will first call the currently open transaction
-	 * of the open batch (if any), and then in the callback we'll idd
-	 * continue with making the backup itself (using sqlite3_backup's API) */
+	tracker_data_backup_save (destination,
+	                          backup_callback,
+	                          info, 
+	                          (GDestroyNotify) g_free);
 
-	tracker_store_queue_commit (on_batch_commit, NULL, info, NULL);
+	g_object_unref (destination);
 }
 
 void
 tracker_backup_restore (TrackerBackup          *object,
-                        const gchar            *backup_uri,
                         const gchar            *journal_uri,
                         DBusGMethodInvocation  *context,
                         GError                **error)
 {
 	guint request_id;
 	TrackerDBusMethodInfo *info;
-	GFile *destination, *journal;
+	GFile *journal;
 
 	request_id = tracker_dbus_get_next_request_id ();
 
 	tracker_dbus_request_new (request_id,
 	                          "D-Bus request to restore backup from '%s'",
-	                          backup_uri);
-
-	destination = g_file_new_for_uri (backup_uri);
-	journal = g_file_new_for_uri (journal_uri);
+	                          journal_uri);
 
 	info = g_new0 (TrackerDBusMethodInfo, 1);
-
 	info->request_id = request_id;
 	info->context = context;
-	info->play_journal = TRUE;
-
-	/* This call is mostly synchronous, because we want to block the
-	 * mainloop during a restore procedure (you're switching the active
-	 * database here, let's not allow queries during this time)
-	 *
-	 * No need for commits or anything. Whatever is in the current db will
-	 * be eliminated in favor of the data in `backup_uri` and `journal_uri`. */
+	journal = g_file_new_for_uri (journal_uri);
 
-	tracker_data_backup_restore (destination, journal,
+	tracker_data_backup_restore (journal,
 	                             backup_callback,
-	                             info, destroy_method_info);
+	                             info, 
+	                             (GDestroyNotify) g_free);
 
-	g_object_unref (destination);
 	g_object_unref (journal);
 }
 
diff --git a/src/tracker-store/tracker-backup.h b/src/tracker-store/tracker-backup.h
index 2eebc2f..d59c07f 100644
--- a/src/tracker-store/tracker-backup.h
+++ b/src/tracker-store/tracker-backup.h
@@ -52,11 +52,9 @@ GType          tracker_backup_get_type (void) G_GNUC_CONST;
 TrackerBackup *tracker_backup_new      (void);
 void           tracker_backup_save     (TrackerBackup          *object,
                                         const gchar            *destination_uri,
-                                        const gchar            *journal_uri,
                                         DBusGMethodInvocation  *context,
                                         GError                **error);
 void           tracker_backup_restore  (TrackerBackup          *object,
-                                        const gchar            *backup_uri,
                                         const gchar            *journal_uri,
                                         DBusGMethodInvocation  *context,
                                         GError                **error);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]