[tracker/wip/carlosg/domain-ontologies: 39/39] libtracker-data: Make journal writers non-singletons



commit a2c76859d665daf2143ec00351cc57cdc07b84a9
Author: Carlos Garnacho <carlosg gnome org>
Date:   Tue Jun 6 12:26:22 2017 +0200

    libtracker-data: Make journal writers non-singletons
    
    We keep two separate writers, one for the ontology and another one
    for regular data. The ontology one is just used during database
    initialization and is not expected to survive past
    tracker_data_manager_init(). The second writer will remain in place
    in order to dump user updates.

 src/libtracker-data/tracker-data-backup.c  |    5 +-
 src/libtracker-data/tracker-data-manager.c |  112 +++++---
 src/libtracker-data/tracker-data-manager.h |    3 +
 src/libtracker-data/tracker-data-update.c  |   93 ++++---
 src/libtracker-data/tracker-db-journal.c   |  427 +++++++++++-----------------
 src/libtracker-data/tracker-db-journal.h   |   96 ++++---
 src/tracker/tracker-reset.c                |   11 +-
 tests/libtracker-data/tracker-db-journal.c |   73 +++---
 8 files changed, 383 insertions(+), 437 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-backup.c b/src/libtracker-data/tracker-data-backup.c
index cdcf045..5e4c3df 100644
--- a/src/libtracker-data/tracker-data-backup.c
+++ b/src/libtracker-data/tracker-data-backup.c
@@ -573,6 +573,7 @@ tracker_data_backup_restore (GFile                *journal,
 
        if (g_file_query_exists (info->journal, NULL)) {
                TrackerDBManagerFlags flags;
+               TrackerDBJournal *journal_writer;
                guint select_cache_size, update_cache_size;
                gboolean is_first;
 #ifndef DISABLE_JOURNAL
@@ -665,7 +666,7 @@ tracker_data_backup_restore (GFile                *journal,
                tracker_db_manager_set_need_mtime_check (TRUE);
 
 #ifndef DISABLE_JOURNAL
-               tracker_db_journal_init (data_location, FALSE, &n_error);
+               journal_writer = tracker_db_journal_new (data_location, FALSE, &n_error);
 
                if (n_error) {
                        if (!info->error) {
@@ -684,7 +685,7 @@ tracker_data_backup_restore (GFile                *journal,
                        remove_temp (cache_location, data_location);
                }
 
-               tracker_db_journal_shutdown (&n_error);
+               tracker_db_journal_free (journal_writer, &n_error);
 
                if (n_error) {
                        g_warning ("Ignored error while shuting down journal during backup: %s",
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index 6f9bc45..83d59a4 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -73,6 +73,8 @@ static gboolean  initialized;
 static gboolean  reloading = FALSE;
 #ifndef DISABLE_JOURNAL
 static gboolean  in_journal_replay;
+static TrackerDBJournal *journal_writer = NULL;
+static TrackerDBJournal *ontology_writer = NULL;
 #endif
 
 typedef struct {
@@ -2485,7 +2487,7 @@ insert_uri_in_resource_table (TrackerDBInterface  *iface,
 
 #ifndef DISABLE_JOURNAL
        if (!in_journal_replay) {
-               tracker_db_journal_append_resource (id, uri);
+               tracker_db_journal_append_resource (ontology_writer, id, uri);
        }
 #endif /* DISABLE_JOURNAL */
 
@@ -3946,11 +3948,7 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                }
 
 #ifndef DISABLE_JOURNAL
-               /* If we are not replaying, truncate journal as it does not even
-                * contain a single valid transaction, or is explicitly ignored
-                * (journal_check == FALSE, only for test cases)
-                */
-               tracker_db_journal_init (data_location, !in_journal_replay, &internal_error);
+               ontology_writer = tracker_db_journal_ontology_new (data_location, &internal_error);
 
                if (internal_error) {
                        g_propagate_error (error, internal_error);
@@ -3995,7 +3993,8 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                        g_propagate_error (error, internal_error);
 
 #ifndef DISABLE_JOURNAL
-                       tracker_db_journal_shutdown (NULL);
+                       tracker_db_journal_free (ontology_writer, NULL);
+                       ontology_writer = NULL;
 #endif /* DISABLE_JOURNAL */
                        tracker_db_manager_shutdown ();
                        tracker_ontologies_shutdown ();
@@ -4016,7 +4015,8 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                        g_propagate_error (error, internal_error);
 
 #ifndef DISABLE_JOURNAL
-                       tracker_db_journal_shutdown (NULL);
+                       tracker_db_journal_free (ontology_writer, NULL);
+                       ontology_writer = NULL;
 #endif /* DISABLE_JOURNAL */
                        tracker_db_manager_shutdown ();
                        tracker_ontologies_shutdown ();
@@ -4043,7 +4043,8 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                                if (internal_error) {
                                        g_propagate_error (error, internal_error);
 
-                                       tracker_db_journal_shutdown (NULL);
+                                       tracker_db_journal_free (ontology_writer, NULL);
+                                       ontology_writer = NULL;
                                        tracker_db_manager_shutdown ();
                                        tracker_ontologies_shutdown ();
                                        if (!reloading) {
@@ -4063,13 +4064,15 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                }
 
                tracker_data_commit_transaction (&internal_error);
+
                if (internal_error) {
                        g_propagate_error (error, internal_error);
-#ifndef DISABLE_JOURNAL
-                       tracker_db_journal_shutdown (NULL);
-#endif /* DISABLE_JOURNAL */
                        tracker_db_manager_shutdown ();
                        tracker_ontologies_shutdown ();
+#ifndef DISABLE_JOURNAL
+                       tracker_db_journal_free (ontology_writer, NULL);
+                       ontology_writer = NULL;
+#endif /* DISABLE_JOURNAL */
                        if (!reloading) {
                                tracker_locale_shutdown ();
                        }
@@ -4087,17 +4090,14 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                check_ontology = FALSE;
 
 #ifndef DISABLE_JOURNAL
-               /* If we are replaying, close it here again after the ontology
-                * has been written.
-                */
-               if (!read_only && in_journal_replay)
-                       tracker_db_journal_shutdown (NULL);
+               tracker_db_journal_free (ontology_writer, NULL);
+               ontology_writer = NULL;
 #endif /* DISABLE_JOURNAL */
        } else {
                if (!read_only) {
 
 #ifndef DISABLE_JOURNAL
-                       tracker_db_journal_init (data_location, FALSE, &internal_error);
+                       ontology_writer = tracker_db_journal_ontology_new (data_location, &internal_error);
 
                        if (internal_error) {
                                g_propagate_error (error, internal_error);
@@ -4260,7 +4260,8 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                                                        g_propagate_error (error, internal_error);
 
 #ifndef DISABLE_JOURNAL
-                                                       tracker_db_journal_shutdown (NULL);
+                                                       tracker_db_journal_free (ontology_writer, NULL);
+                                                       ontology_writer = NULL;
 #endif /* DISABLE_JOURNAL */
                                                        tracker_db_manager_shutdown ();
                                                        tracker_ontologies_shutdown ();
@@ -4353,7 +4354,8 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                                                g_propagate_error (error, internal_error);
 
 #ifndef DISABLE_JOURNAL
-                                               tracker_db_journal_shutdown (NULL);
+                                               tracker_db_journal_free (ontology_writer, NULL);
+                                               ontology_writer = NULL;
 #endif /* DISABLE_JOURNAL */
                                                tracker_db_manager_shutdown ();
                                                tracker_ontologies_shutdown ();
@@ -4538,7 +4540,8 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                                g_propagate_error (error, ontology_error);
 
 #ifndef DISABLE_JOURNAL
-                               tracker_db_journal_shutdown (NULL);
+                               tracker_db_journal_free (ontology_writer, NULL);
+                               ontology_writer = NULL;
 #endif /* DISABLE_JOURNAL */
                                tracker_db_manager_shutdown ();
                                tracker_ontologies_shutdown ();
@@ -4574,7 +4577,8 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                                g_propagate_error (error, internal_error);
 
 #ifndef DISABLE_JOURNAL
-                               tracker_db_journal_shutdown (NULL);
+                               tracker_db_journal_free (ontology_writer, NULL);
+                               ontology_writer = NULL;
 #endif /* DISABLE_JOURNAL */
                                tracker_db_manager_shutdown ();
                                tracker_ontologies_shutdown ();
@@ -4585,6 +4589,11 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
 
                                return FALSE;
                        }
+
+#ifndef DISABLE_JOURNAL
+                       tracker_db_journal_free (ontology_writer, NULL);
+                       ontology_writer = NULL;
+#endif /* DISABLE_JOURNAL */
                }
 
                g_hash_table_unref (ontos_table);
@@ -4612,7 +4621,6 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                                tracker_db_manager_shutdown ();
                                /* Call may fail without notice, we're in error handling already.
                                 * When fails it means that close() of journal file failed. */
-                               tracker_db_journal_shutdown (&n_error);
                                if (n_error) {
                                        g_warning ("Error closing journal: %s",
                                                   n_error->message ? n_error->message : "No error given");
@@ -4623,7 +4631,6 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                        g_hash_table_unref (uri_id_map);
                        g_propagate_error (error, internal_error);
 
-                       tracker_db_journal_shutdown (NULL);
                        tracker_db_manager_shutdown ();
                        tracker_ontologies_shutdown ();
                        if (!reloading) {
@@ -4635,26 +4642,23 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                }
 
                in_journal_replay = FALSE;
+               g_hash_table_unref (uri_id_map);
+       }
 
-               /* open journal for writing */
-               tracker_db_journal_init (data_location, FALSE, &internal_error);
+       /* open journal for writing */
+       journal_writer = tracker_db_journal_new (data_location, FALSE, &internal_error);
 
-               if (internal_error) {
-                       g_hash_table_unref (uri_id_map);
-                       g_propagate_error (error, internal_error);
-
-                       tracker_db_journal_shutdown (NULL);
-                       tracker_db_manager_shutdown ();
-                       tracker_ontologies_shutdown ();
-                       if (!reloading) {
-                               tracker_locale_shutdown ();
-                       }
-                       tracker_data_update_shutdown ();
+       if (internal_error) {
+               g_propagate_error (error, internal_error);
 
-                       return FALSE;
+               tracker_db_manager_shutdown ();
+               tracker_ontologies_shutdown ();
+               if (!reloading) {
+                       tracker_locale_shutdown ();
                }
+               tracker_data_update_shutdown ();
 
-               g_hash_table_unref (uri_id_map);
+               return FALSE;
        }
 #endif /* DISABLE_JOURNAL */
 
@@ -4677,7 +4681,7 @@ tracker_data_manager_init (TrackerDBManagerFlags   flags,
                        g_propagate_error (error, internal_error);
 
 #ifndef DISABLE_JOURNAL
-                       tracker_db_journal_shutdown (NULL);
+                       tracker_db_journal_free (journal_writer, NULL);
 #endif /* DISABLE_JOURNAL */
                        tracker_db_manager_shutdown ();
                        tracker_ontologies_shutdown ();
@@ -4725,13 +4729,15 @@ tracker_data_manager_shutdown (void)
 
 #ifndef DISABLE_JOURNAL
        /* Make sure we shutdown all other modules we depend on */
-       tracker_db_journal_shutdown (&error);
+       if (journal_writer) {
+               tracker_db_journal_free (journal_writer, &error);
 
-       if (error) {
-               /* TODO: propagate error */
-               g_warning ("While shutting down journal %s",
-                          error->message ? error->message : "No error given");
-               g_error_free (error);
+               if (error) {
+                       /* TODO: propagate error */
+                       g_warning ("While shutting down journal %s",
+                                  error->message ? error->message : "No error given");
+                       g_error_free (error);
+               }
        }
 #endif /* DISABLE_JOURNAL */
 
@@ -4751,3 +4757,17 @@ tracker_data_manager_shutdown (void)
 
        initialized = FALSE;
 }
+
+#ifndef DISABLE_JOURNAL
+TrackerDBJournal *
+tracker_data_manager_get_journal_writer (void)
+{
+       return journal_writer;
+}
+
+TrackerDBJournal *
+tracker_data_manager_get_ontology_writer (void)
+{
+       return ontology_writer;
+}
+#endif
diff --git a/src/libtracker-data/tracker-data-manager.h b/src/libtracker-data/tracker-data-manager.h
index da7abfc..520df12 100644
--- a/src/libtracker-data/tracker-data-manager.h
+++ b/src/libtracker-data/tracker-data-manager.h
@@ -31,6 +31,7 @@
 #include <libtracker-data/tracker-data-update.h>
 #include <libtracker-data/tracker-db-interface.h>
 #include <libtracker-data/tracker-db-manager.h>
+#include <libtracker-data/tracker-db-journal.h>
 
 G_BEGIN_DECLS
 
@@ -69,6 +70,8 @@ gboolean tracker_data_manager_reload                 (TrackerBusyCallback     bu
 
 GFile * tracker_data_manager_get_cache_location();
 GFile * tracker_data_manager_get_data_location ();
+TrackerDBJournal * tracker_data_manager_get_journal_writer (void);
+TrackerDBJournal * tracker_data_manager_get_ontology_writer (void);
 
 gboolean tracker_data_manager_init_fts               (TrackerDBInterface     *interface,
                                                      gboolean                create);
diff --git a/src/libtracker-data/tracker-data-update.c b/src/libtracker-data/tracker-data-update.c
index beb3018..725ae84 100644
--- a/src/libtracker-data/tracker-data-update.c
+++ b/src/libtracker-data/tracker-data-update.c
@@ -152,6 +152,8 @@ static GPtrArray *rollback_callbacks = NULL;
 static gint max_service_id = 0;
 static gint max_ontology_id = 0;
 
+static TrackerDBJournal *journal_writer = NULL;
+
 static gint         ensure_resource_id         (const gchar      *uri,
                                                 gboolean         *create);
 static void         cache_insert_value         (const gchar      *table_name,
@@ -657,7 +659,7 @@ ensure_resource_id (const gchar *uri,
 
 #ifndef DISABLE_JOURNAL
                if (!in_journal_replay) {
-                       tracker_db_journal_append_resource (id, uri);
+                       tracker_db_journal_append_resource (journal_writer, id, uri);
                }
 #endif /* DISABLE_JOURNAL */
 
@@ -1785,7 +1787,8 @@ delete_first_object (TrackerProperty  *field,
 
 #ifndef DISABLE_JOURNAL
                if (!in_journal_replay && change && !tracker_property_get_transient (field)) {
-                       tracker_db_journal_append_delete_statement_id (graph_id,
+                       tracker_db_journal_append_delete_statement_id (journal_writer,
+                                                                      graph_id,
                                                                       resource_buffer->id,
                                                                       pred_id,
                                                                       object_id);
@@ -1817,12 +1820,14 @@ delete_first_object (TrackerProperty  *field,
 
                                damaged = tracker_ontologies_get_property_by_uri (TRACKER_PREFIX_TRACKER 
"damaged");
 
-                               tracker_db_journal_append_insert_statement (graph_id,
+                               tracker_db_journal_append_insert_statement (journal_writer,
+                                                                           graph_id,
                                                                            resource_buffer->id,
                                                                            tracker_property_get_id (damaged),
                                                                            "true");
                        } else {
-                               tracker_db_journal_append_delete_statement (graph_id,
+                               tracker_db_journal_append_delete_statement (journal_writer,
+                                                                           graph_id,
                                                                            resource_buffer->id,
                                                                            pred_id,
                                                                            object_str);
@@ -2378,6 +2383,7 @@ tracker_data_delete_statement (const gchar  *graph,
 #ifndef DISABLE_JOURNAL
                        if (!in_journal_replay) {
                                tracker_db_journal_append_delete_statement_id (
+                                      journal_writer,
                                       (graph != NULL ? query_resource_id (graph) : 0),
                                       resource_buffer->id,
                                       tracker_data_query_resource_id (predicate),
@@ -2411,7 +2417,8 @@ tracker_data_delete_statement (const gchar  *graph,
                                        tried = TRUE;
 
 #ifndef DISABLE_JOURNAL
-                                       tracker_db_journal_append_delete_statement_id (graph_id,
+                                       tracker_db_journal_append_delete_statement_id (journal_writer,
+                                                                                      graph_id,
                                                                                       resource_buffer->id,
                                                                                       pred_id,
                                                                                       object_id);
@@ -2430,12 +2437,14 @@ tracker_data_delete_statement (const gchar  *graph,
 
                                                damaged = tracker_ontologies_get_property_by_uri 
(TRACKER_PREFIX_TRACKER "damaged");
 
-                                               tracker_db_journal_append_insert_statement (graph_id,
+                                               tracker_db_journal_append_insert_statement (journal_writer,
+                                                                                           graph_id,
                                                                                            
resource_buffer->id,
                                                                                            
tracker_property_get_id (damaged),
                                                                                            "true");
                                        } else {
-                                               tracker_db_journal_append_delete_statement (graph_id,
+                                               tracker_db_journal_append_delete_statement (journal_writer,
+                                                                                           graph_id,
                                                                                            
resource_buffer->id,
                                                                                            pred_id,
                                                                                            object);
@@ -2764,6 +2773,7 @@ tracker_data_insert_statement_with_uri (const gchar            *graph,
 #ifndef DISABLE_JOURNAL
        if (!in_journal_replay && change && !tracker_property_get_transient (property)) {
                tracker_db_journal_append_insert_statement_id (
+                       journal_writer,
                        (graph != NULL ? query_resource_id (graph) : 0),
                        resource_buffer->id,
                        final_prop_id,
@@ -2860,12 +2870,14 @@ tracker_data_insert_statement_with_string (const gchar            *graph,
                        TrackerProperty *damaged;
 
                        damaged = tracker_ontologies_get_property_by_uri (TRACKER_PREFIX_TRACKER "damaged");
-                       tracker_db_journal_append_insert_statement (graph_id,
+                       tracker_db_journal_append_insert_statement (journal_writer,
+                                                                   graph_id,
                                                                    resource_buffer->id,
                                                                    tracker_property_get_id (damaged),
                                                                    "true");
                } else {
-                       tracker_db_journal_append_insert_statement (graph_id,
+                       tracker_db_journal_append_insert_statement (journal_writer,
+                                                                   graph_id,
                                                                    resource_buffer->id,
                                                                    pred_id,
                                                                    object);
@@ -3069,6 +3081,7 @@ tracker_data_update_statement_with_uri (const gchar            *graph,
 #ifndef DISABLE_JOURNAL
        if (!in_journal_replay && change && !tracker_property_get_transient (property)) {
                tracker_db_journal_append_update_statement_id (
+                       journal_writer,
                        (graph != NULL ? query_resource_id (graph) : 0),
                        resource_buffer->id,
                        final_prop_id,
@@ -3205,12 +3218,14 @@ tracker_data_update_statement_with_string (const gchar            *graph,
                        TrackerProperty *damaged;
 
                        damaged = tracker_ontologies_get_property_by_uri (TRACKER_PREFIX_TRACKER "damaged");
-                       tracker_db_journal_append_update_statement (graph_id,
+                       tracker_db_journal_append_update_statement (journal_writer,
+                                                                   graph_id,
                                                                    resource_buffer->id,
                                                                    tracker_property_get_id (damaged),
                                                                    "true");
                } else {
-                       tracker_db_journal_append_update_statement (graph_id,
+                       tracker_db_journal_append_update_statement (journal_writer,
+                                                                   graph_id,
                                                                    resource_buffer->id,
                                                                    pred_id,
                                                                    object);
@@ -3311,20 +3326,13 @@ tracker_data_begin_transaction (GError **error)
 
 #ifndef DISABLE_JOURNAL
        if (!in_journal_replay) {
-               if (in_ontology_transaction) {
-                       GError *n_error = NULL;
-                       tracker_db_journal_start_ontology_transaction (resource_time, &n_error);
-
-                       if (n_error) {
-                               /* No need for rollback here */
-                               tracker_db_interface_end_db_transaction (iface, NULL);
-                               g_propagate_error (error, n_error);
-                               return;
-                       }
+               g_assert (journal_writer == NULL);
+               /* Pick the right journal writer for this transaction */
+               journal_writer = in_ontology_transaction ?
+                       tracker_data_manager_get_ontology_writer () :
+                       tracker_data_manager_get_journal_writer ();
 
-               } else {
-                       tracker_db_journal_start_transaction (resource_time);
-               }
+               tracker_db_journal_start_transaction (journal_writer, resource_time);
        }
 #endif /* DISABLE_JOURNAL */
 
@@ -3374,15 +3382,18 @@ tracker_data_commit_transaction (GError **error)
 
 #ifndef DISABLE_JOURNAL
        if (!in_journal_replay) {
+               g_assert (journal_writer != NULL);
                if (has_persistent || in_ontology_transaction) {
-                       tracker_db_journal_commit_db_transaction (&actual_error);
+                       tracker_db_journal_commit_db_transaction (journal_writer, &actual_error);
                } else {
                        /* If we only had transient properties, then we must not write
                         * anything to the journal. So we roll it back, but only the
                         * journal's part. */
-                       tracker_db_journal_rollback_transaction (&actual_error);
+                       tracker_db_journal_rollback_transaction (journal_writer);
                }
 
+               journal_writer = NULL;
+
                if (actual_error) {
                        /* Can't write in journal anymore; quite a serious problem */
                        g_propagate_error (error, actual_error);
@@ -3462,18 +3473,11 @@ tracker_data_rollback_transaction (void)
        if (!in_journal_replay) {
 
 #ifndef DISABLE_JOURNAL
-               tracker_db_journal_rollback_transaction (&ignorable);
-
-               if (ignorable) {
-                       /* Not sure if this is also ignorable: it's the close() of the
-                        * journal file failing (in case of TRANSACTION_FORMAT_ONTOLOGY) */
-                       g_warning ("Error ignored while rolling back transaction in journal: %s",
-                                  ignorable->message ? ignorable->message : "No error given");
-                       g_clear_error (&ignorable);
-               }
+               g_assert (journal_writer != NULL);
+               tracker_db_journal_rollback_transaction (journal_writer);
+               journal_writer = NULL;
 #endif /* DISABLE_JOURNAL */
 
-
                if (rollback_callbacks) {
                        guint n;
                        for (n = 0; n < rollback_callbacks->len; n++) {
@@ -3548,7 +3552,15 @@ void
 tracker_data_sync (void)
 {
 #ifndef DISABLE_JOURNAL
-       tracker_db_journal_fsync ();
+       TrackerDBJournal *writer;
+
+       writer = tracker_data_manager_get_journal_writer ();
+       if (writer)
+               tracker_db_journal_fsync (writer);
+
+       writer = tracker_data_manager_get_ontology_writer ();
+       if (writer)
+               tracker_db_journal_fsync (writer);
 #endif
 }
 
@@ -3834,6 +3846,7 @@ tracker_data_replay_journal (TrackerBusyCallback   busy_callback,
                GError *n_error = NULL;
                gsize size;
                GFile *cache_location, *data_location;
+               TrackerDBJournal *writer;
 
                size = tracker_db_journal_reader_get_size_of_correct (reader);
                tracker_db_journal_reader_free (reader);
@@ -3841,7 +3854,7 @@ tracker_data_replay_journal (TrackerBusyCallback   busy_callback,
                cache_location = tracker_data_manager_get_cache_location();
                data_location = tracker_data_manager_get_data_location();
 
-               tracker_db_journal_init (data_location, FALSE, &n_error);
+               writer = tracker_db_journal_new (data_location, FALSE, &n_error);
                g_object_unref (cache_location);
                g_object_unref (data_location);
 
@@ -3851,8 +3864,8 @@ tracker_data_replay_journal (TrackerBusyCallback   busy_callback,
                        g_propagate_error (error, n_error);
                        return;
                }
-               tracker_db_journal_truncate (size);
-               tracker_db_journal_shutdown (&n_error);
+               tracker_db_journal_truncate (writer, size);
+               tracker_db_journal_free (writer, &n_error);
 
                if (n_error) {
                        g_clear_error (&journal_error);
diff --git a/src/libtracker-data/tracker-db-journal.c b/src/libtracker-data/tracker-db-journal.c
index 8bd3f2b..44a310c 100644
--- a/src/libtracker-data/tracker-db-journal.c
+++ b/src/libtracker-data/tracker-db-journal.c
@@ -94,7 +94,7 @@ struct _TrackerDBJournalReader {
        guint total_chunks;
 };
 
-typedef struct {
+struct _TrackerDBJournal {
        gchar *journal_filename;
        GFile *journal_location;
        int journal;
@@ -104,7 +104,10 @@ typedef struct {
        gchar *cur_block;
        guint cur_entry_amount;
        guint cur_pos;
-} JournalWriter;
+
+       TransactionFormat transaction_format;
+       gboolean in_transaction;
+};
 
 static struct {
        gsize chunk_size;
@@ -113,15 +116,8 @@ static struct {
        gboolean rotate_progress_flag;
 } rotating_settings = {0};
 
-static JournalWriter writer = {0};
-static JournalWriter ontology_writer = {0};
-
-static TransactionFormat current_transaction_format;
-
-static gboolean tracker_db_journal_rotate (GError **error);
-
-static gboolean db_journal_ontology_init (GFile   *journal_location,
-                                          GError **error);
+static gboolean tracker_db_journal_rotate (TrackerDBJournal  *jwriter,
+                                           GError           **error);
 
 #ifndef HAVE_STRNLEN
 
@@ -360,7 +356,8 @@ nearest_pow (gint num)
 }
 
 static void
-cur_block_maybe_expand (JournalWriter *jwriter, guint len)
+cur_block_maybe_expand (TrackerDBJournal *jwriter,
+                        guint             len)
 {
        guint want_alloc = jwriter->cur_block_len + len;
 
@@ -373,7 +370,7 @@ cur_block_maybe_expand (JournalWriter *jwriter, guint len)
 }
 
 static void
-cur_block_kill (JournalWriter *jwriter)
+cur_block_kill (TrackerDBJournal *jwriter)
 {
        jwriter->cur_block_len = 0;
        jwriter->cur_pos = 0;
@@ -453,9 +450,9 @@ tracker_db_journal_error_quark (void)
 }
 
 static gboolean
-db_journal_init_file (JournalWriter  *jwriter,
-                      gboolean        truncate,
-                      GError        **error)
+db_journal_init_file (TrackerDBJournal  *jwriter,
+                      gboolean           truncate,
+                      GError           **error)
 {
        struct stat st;
        int flags;
@@ -525,12 +522,12 @@ db_journal_init_file (JournalWriter  *jwriter,
 }
 
 static gboolean
-db_journal_writer_init (JournalWriter  *jwriter,
-                        gboolean        truncate,
-                        gboolean        global_writer,
-                        const gchar    *filename,
-                        GFile          *journal_location,
-                        GError        **error)
+db_journal_writer_init (TrackerDBJournal  *jwriter,
+                        gboolean           truncate,
+                        gboolean           global_writer,
+                        const gchar       *filename,
+                        GFile             *journal_location,
+                        GError           **error)
 {
        gchar *directory;
        gint mode;
@@ -568,17 +565,19 @@ db_journal_writer_init (JournalWriter  *jwriter,
        return ret;
 }
 
-gboolean
-tracker_db_journal_init (GFile     *data_location,
-                         gboolean   truncate,
-                         GError   **error)
+TrackerDBJournal *
+tracker_db_journal_new (GFile        *data_location,
+                        gboolean      truncate,
+                        GError      **error)
 {
+       TrackerDBJournal *writer;
        gboolean ret;
        gchar *filename;
        GError *n_error = NULL;
        GFile *child;
 
-       g_return_val_if_fail (writer.journal == 0, FALSE);
+       writer = g_new0 (TrackerDBJournal, 1);
+       writer->transaction_format = TRANSACTION_FORMAT_DATA;
 
        child = g_file_get_child (data_location, TRACKER_DB_JOURNAL_FILENAME);
        filename = g_file_get_path (child);
@@ -586,52 +585,50 @@ tracker_db_journal_init (GFile     *data_location,
 
        g_assert (filename != NULL);
 
-       ret = db_journal_writer_init (&writer, truncate, TRUE, filename, data_location, &n_error);
+       ret = db_journal_writer_init (writer, truncate, TRUE, filename, data_location, &n_error);
        g_free (filename);
 
        if (!ret) {
                g_propagate_error (error, n_error);
-               return ret;
-       }
-
-       ret = db_journal_ontology_init (data_location, &n_error);
-
-       if (!ret) {
-               g_propagate_error (error, n_error);
-               return ret;
+               g_clear_pointer (&writer, g_free);
        }
 
-       return ret;
+       return writer;
 }
 
-static gboolean
-db_journal_ontology_init (GFile   *journal_location,
-                          GError **error)
+TrackerDBJournal *
+tracker_db_journal_ontology_new (GFile     *data_location,
+                                 GError   **error)
 {
+       TrackerDBJournal *writer;
        gboolean ret;
        gchar *filename;
        GError *n_error = NULL;
        GFile *child;
 
-       g_return_val_if_fail (ontology_writer.journal == 0, FALSE);
+       writer = g_new0 (TrackerDBJournal, 1);
+       writer->transaction_format = TRANSACTION_FORMAT_ONTOLOGY;
 
-       child = g_file_get_child (journal_location, TRACKER_DB_JOURNAL_ONTOLOGY_FILENAME);
+       child = g_file_get_child (data_location, TRACKER_DB_JOURNAL_ONTOLOGY_FILENAME);
        filename = g_file_get_path (child);
        g_object_unref (child);
 
-       ret = db_journal_writer_init (&ontology_writer, FALSE, FALSE, filename, journal_location, &n_error);
+       g_assert (filename != NULL);
+
+       ret = db_journal_writer_init (writer, FALSE, FALSE, filename, data_location, &n_error);
        g_free (filename);
 
-       if (n_error) {
+       if (!ret) {
                g_propagate_error (error, n_error);
+               g_clear_pointer (&writer, g_free);
        }
 
-       return ret;
+       return writer;
 }
 
 static gboolean
-db_journal_writer_shutdown (JournalWriter  *jwriter,
-                            GError        **error)
+db_journal_writer_clear (TrackerDBJournal  *jwriter,
+                         GError           **error)
 {
        g_clear_pointer (&jwriter->journal_filename, g_free);
        g_clear_object (&jwriter->journal_location);
@@ -654,45 +651,40 @@ db_journal_writer_shutdown (JournalWriter  *jwriter,
 }
 
 gboolean
-tracker_db_journal_shutdown (GError **error)
+tracker_db_journal_free (TrackerDBJournal  *writer,
+                         GError           **error)
 {
        GError *n_error = NULL;
-       gboolean ret;
 
-       ret = db_journal_writer_shutdown (&writer, &n_error);
-       if (!ret) {
-               g_propagate_error (error, n_error);
-               return ret;
-       }
+       db_journal_writer_clear (writer, &n_error);
+       g_free (writer);
 
-       ret = db_journal_writer_shutdown (&ontology_writer, &n_error);
-       if (!ret) {
+       if (n_error) {
                g_propagate_error (error, n_error);
-               return ret;
+               return FALSE;
        }
 
-       return ret;
+       return TRUE;
 }
 
 gsize
-tracker_db_journal_get_size (void)
+tracker_db_journal_get_size (TrackerDBJournal *writer)
 {
-       g_return_val_if_fail (writer.journal > 0, FALSE);
+       g_return_val_if_fail (writer->journal > 0, FALSE);
 
-       return writer.cur_size;
+       return writer->cur_size;
 }
 
-static gboolean
-db_journal_writer_start_transaction (JournalWriter    *jwriter,
-                                     time_t            time,
-                                     TransactionFormat kind)
+gboolean
+tracker_db_journal_start_transaction (TrackerDBJournal *jwriter,
+                                      time_t            time)
 {
        guint size;
 
        g_return_val_if_fail (jwriter->journal > 0, FALSE);
-       g_return_val_if_fail (current_transaction_format == TRANSACTION_FORMAT_NONE, FALSE);
+       g_return_val_if_fail (jwriter->in_transaction == FALSE, FALSE);
 
-       current_transaction_format = kind;
+       jwriter->in_transaction = TRUE;
 
        size = sizeof (guint32) * 3;
        cur_block_maybe_expand (jwriter, size);
@@ -713,33 +705,18 @@ db_journal_writer_start_transaction (JournalWriter    *jwriter,
 
        /* Add format */
        cur_block_maybe_expand (jwriter, sizeof (gint32));
-       cur_setnum (jwriter->cur_block, &(jwriter->cur_pos), kind);
+       cur_setnum (jwriter->cur_block, &(jwriter->cur_pos), jwriter->transaction_format);
        jwriter->cur_block_len += sizeof (gint32);
 
        return TRUE;
 }
 
 gboolean
-tracker_db_journal_start_transaction (time_t time)
-{
-       return db_journal_writer_start_transaction (&writer, time,
-                                                   TRANSACTION_FORMAT_DATA);
-}
-
-gboolean
-tracker_db_journal_start_ontology_transaction (time_t   time,
-                                               GError **error)
-{
-       return db_journal_writer_start_transaction (&ontology_writer, time,
-                                                   TRANSACTION_FORMAT_ONTOLOGY);
-}
-
-static gboolean
-db_journal_writer_append_delete_statement (JournalWriter *jwriter,
-                                           gint           g_id,
-                                           gint           s_id,
-                                           gint           p_id,
-                                           const gchar   *object)
+tracker_db_journal_append_delete_statement (TrackerDBJournal *jwriter,
+                                            gint              g_id,
+                                            gint              s_id,
+                                            gint              p_id,
+                                            const gchar      *object)
 {
        gint o_len;
        DataFormat df;
@@ -750,6 +727,11 @@ db_journal_writer_append_delete_statement (JournalWriter *jwriter,
        g_return_val_if_fail (s_id > 0, FALSE);
        g_return_val_if_fail (p_id > 0, FALSE);
        g_return_val_if_fail (object != NULL, FALSE);
+       g_return_val_if_fail (jwriter->in_transaction == TRUE, FALSE);
+
+       if (jwriter->transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
+               return TRUE;
+       }
 
        o_len = strlen (object);
        if (g_id == 0) {
@@ -777,25 +759,11 @@ db_journal_writer_append_delete_statement (JournalWriter *jwriter,
 }
 
 gboolean
-tracker_db_journal_append_delete_statement (gint         g_id,
-                                            gint         s_id,
-                                            gint         p_id,
-                                            const gchar *object)
-{
-       if (current_transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
-               return TRUE;
-       }
-
-       return db_journal_writer_append_delete_statement (&writer,
-                                                         g_id, s_id, p_id, object);
-}
-
-static gboolean
-db_journal_writer_append_delete_statement_id  (JournalWriter *jwriter,
-                                               gint           g_id,
-                                               gint           s_id,
-                                               gint           p_id,
-                                               gint           o_id)
+tracker_db_journal_append_delete_statement_id  (TrackerDBJournal *jwriter,
+                                                gint              g_id,
+                                                gint              s_id,
+                                                gint              p_id,
+                                                gint              o_id)
 {
        DataFormat df;
        gint size;
@@ -805,6 +773,11 @@ db_journal_writer_append_delete_statement_id  (JournalWriter *jwriter,
        g_return_val_if_fail (s_id > 0, FALSE);
        g_return_val_if_fail (p_id > 0, FALSE);
        g_return_val_if_fail (o_id > 0, FALSE);
+       g_return_val_if_fail (jwriter->in_transaction == TRUE, FALSE);
+
+       if (jwriter->transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
+               return TRUE;
+       }
 
        if (g_id == 0) {
                df = DATA_FORMAT_OPERATION_DELETE | DATA_FORMAT_OBJECT_ID;
@@ -831,25 +804,11 @@ db_journal_writer_append_delete_statement_id  (JournalWriter *jwriter,
 }
 
 gboolean
-tracker_db_journal_append_delete_statement_id (gint g_id,
-                                               gint s_id,
-                                               gint p_id,
-                                               gint o_id)
-{
-       if (current_transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
-               return TRUE;
-       }
-
-       return db_journal_writer_append_delete_statement_id (&writer,
-                                                            g_id, s_id, p_id, o_id);
-}
-
-static gboolean
-db_journal_writer_append_insert_statement (JournalWriter *jwriter,
-                                           gint           g_id,
-                                           gint           s_id,
-                                           gint           p_id,
-                                           const gchar   *object)
+tracker_db_journal_append_insert_statement (TrackerDBJournal *jwriter,
+                                            gint              g_id,
+                                            gint              s_id,
+                                            gint              p_id,
+                                            const gchar      *object)
 {
        gint o_len;
        DataFormat df;
@@ -860,6 +819,11 @@ db_journal_writer_append_insert_statement (JournalWriter *jwriter,
        g_return_val_if_fail (s_id > 0, FALSE);
        g_return_val_if_fail (p_id > 0, FALSE);
        g_return_val_if_fail (object != NULL, FALSE);
+       g_return_val_if_fail (jwriter->in_transaction == TRUE, FALSE);
+
+       if (jwriter->transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
+               return TRUE;
+       }
 
        o_len = strlen (object);
        if (g_id == 0) {
@@ -887,25 +851,11 @@ db_journal_writer_append_insert_statement (JournalWriter *jwriter,
 }
 
 gboolean
-tracker_db_journal_append_insert_statement (gint         g_id,
-                                            gint         s_id,
-                                            gint         p_id,
-                                            const gchar *object)
-{
-       if (current_transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
-               return TRUE;
-       }
-
-       return db_journal_writer_append_insert_statement (&writer,
-                                                         g_id, s_id, p_id, object);
-}
-
-static gboolean
-db_journal_writer_append_insert_statement_id (JournalWriter *jwriter,
-                                              gint           g_id,
-                                              gint           s_id,
-                                              gint           p_id,
-                                              gint           o_id)
+tracker_db_journal_append_insert_statement_id (TrackerDBJournal *jwriter,
+                                               gint              g_id,
+                                               gint              s_id,
+                                               gint              p_id,
+                                               gint              o_id)
 {
        DataFormat df;
        gint size;
@@ -915,6 +865,11 @@ db_journal_writer_append_insert_statement_id (JournalWriter *jwriter,
        g_return_val_if_fail (s_id > 0, FALSE);
        g_return_val_if_fail (p_id > 0, FALSE);
        g_return_val_if_fail (o_id > 0, FALSE);
+       g_return_val_if_fail (jwriter->in_transaction == TRUE, FALSE);
+
+       if (jwriter->transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
+               return TRUE;
+       }
 
        if (g_id == 0) {
                df = DATA_FORMAT_OBJECT_ID;
@@ -941,25 +896,11 @@ db_journal_writer_append_insert_statement_id (JournalWriter *jwriter,
 }
 
 gboolean
-tracker_db_journal_append_insert_statement_id (gint g_id,
-                                               gint s_id,
-                                               gint p_id,
-                                               gint o_id)
-{
-       if (current_transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
-               return TRUE;
-       }
-
-       return db_journal_writer_append_insert_statement_id (&writer,
-                                                            g_id, s_id, p_id, o_id);
-}
-
-static gboolean
-db_journal_writer_append_update_statement (JournalWriter *jwriter,
-                                           gint           g_id,
-                                           gint           s_id,
-                                           gint           p_id,
-                                           const gchar   *object)
+tracker_db_journal_append_update_statement (TrackerDBJournal *jwriter,
+                                            gint              g_id,
+                                            gint              s_id,
+                                            gint              p_id,
+                                            const gchar      *object)
 {
        gint o_len;
        DataFormat df;
@@ -970,6 +911,11 @@ db_journal_writer_append_update_statement (JournalWriter *jwriter,
        g_return_val_if_fail (s_id > 0, FALSE);
        g_return_val_if_fail (p_id > 0, FALSE);
        g_return_val_if_fail (object != NULL, FALSE);
+       g_return_val_if_fail (jwriter->in_transaction == TRUE, FALSE);
+
+       if (jwriter->transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
+               return TRUE;
+       }
 
        o_len = strlen (object);
        if (g_id == 0) {
@@ -997,25 +943,11 @@ db_journal_writer_append_update_statement (JournalWriter *jwriter,
 }
 
 gboolean
-tracker_db_journal_append_update_statement (gint         g_id,
-                                            gint         s_id,
-                                            gint         p_id,
-                                            const gchar *object)
-{
-       if (current_transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
-               return TRUE;
-       }
-
-       return db_journal_writer_append_update_statement (&writer,
-                                                         g_id, s_id, p_id, object);
-}
-
-static gboolean
-db_journal_writer_append_update_statement_id (JournalWriter *jwriter,
-                                              gint           g_id,
-                                              gint           s_id,
-                                              gint           p_id,
-                                              gint           o_id)
+tracker_db_journal_append_update_statement_id (TrackerDBJournal *jwriter,
+                                               gint              g_id,
+                                               gint              s_id,
+                                               gint              p_id,
+                                               gint              o_id)
 {
        DataFormat df;
        gint size;
@@ -1025,6 +957,11 @@ db_journal_writer_append_update_statement_id (JournalWriter *jwriter,
        g_return_val_if_fail (s_id > 0, FALSE);
        g_return_val_if_fail (p_id > 0, FALSE);
        g_return_val_if_fail (o_id > 0, FALSE);
+       g_return_val_if_fail (jwriter->in_transaction == TRUE, FALSE);
+
+       if (jwriter->transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
+               return TRUE;
+       }
 
        if (g_id == 0) {
                df = DATA_FORMAT_OPERATION_UPDATE | DATA_FORMAT_OBJECT_ID;
@@ -1051,23 +988,9 @@ db_journal_writer_append_update_statement_id (JournalWriter *jwriter,
 }
 
 gboolean
-tracker_db_journal_append_update_statement_id (gint g_id,
-                                               gint s_id,
-                                               gint p_id,
-                                               gint o_id)
-{
-       if (current_transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
-               return TRUE;
-       }
-
-       return db_journal_writer_append_update_statement_id (&writer,
-                                                            g_id, s_id, p_id, o_id);
-}
-
-static gboolean
-db_journal_writer_append_resource (JournalWriter *jwriter,
-                                   gint           s_id,
-                                   const gchar   *uri)
+tracker_db_journal_append_resource (TrackerDBJournal *jwriter,
+                                    gint              s_id,
+                                    const gchar      *uri)
 {
        gint o_len;
        DataFormat df;
@@ -1092,56 +1015,29 @@ db_journal_writer_append_resource (JournalWriter *jwriter,
 }
 
 gboolean
-tracker_db_journal_append_resource (gint         s_id,
-                                    const gchar *uri)
-{
-       gboolean ret;
-
-       g_return_val_if_fail (current_transaction_format != TRANSACTION_FORMAT_NONE, FALSE);
-
-       if (current_transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
-               ret = db_journal_writer_append_resource (&ontology_writer, s_id, uri);
-       } else {
-               ret = db_journal_writer_append_resource (&writer, s_id, uri);
-       }
-
-       return ret;
-}
-
-gboolean
-tracker_db_journal_rollback_transaction (GError **error)
+tracker_db_journal_rollback_transaction (TrackerDBJournal *writer)
 {
-       GError *n_error = NULL;
+       g_return_val_if_fail (writer->journal > 0, FALSE);
+       g_return_val_if_fail (writer->in_transaction == TRUE, FALSE);
 
-       g_return_val_if_fail (writer.journal > 0, FALSE);
-       g_return_val_if_fail (current_transaction_format != TRANSACTION_FORMAT_NONE, FALSE);
-
-       cur_block_kill (&writer);
-
-       if (current_transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
-               cur_block_kill (&ontology_writer);
-       }
-
-       if (n_error) {
-               g_propagate_error (error, n_error);
-       }
-
-       current_transaction_format = TRANSACTION_FORMAT_NONE;
+       cur_block_kill (writer);
+       writer->in_transaction = FALSE;
 
        return TRUE;
 }
 
 gboolean
-tracker_db_journal_truncate (gsize new_size)
+tracker_db_journal_truncate (TrackerDBJournal *writer,
+                             gsize             new_size)
 {
-       g_return_val_if_fail (writer.journal > 0, FALSE);
+       g_return_val_if_fail (writer->journal > 0, FALSE);
 
-       return (ftruncate (writer.journal, new_size) != -1);
+       return (ftruncate (writer->journal, new_size) != -1);
 }
 
 static gboolean
-db_journal_writer_commit_db_transaction (JournalWriter  *jwriter,
-                                         GError        **error)
+db_journal_writer_commit_db_transaction (TrackerDBJournal  *jwriter,
+                                         GError           **error)
 {
        guint32 crc;
        guint begin_pos;
@@ -1188,22 +1084,19 @@ db_journal_writer_commit_db_transaction (JournalWriter  *jwriter,
 }
 
 gboolean
-tracker_db_journal_commit_db_transaction (GError **error)
+tracker_db_journal_commit_db_transaction (TrackerDBJournal  *writer,
+                                          GError           **error)
 {
        gboolean ret;
        GError *n_error = NULL;
 
-       g_return_val_if_fail (current_transaction_format != TRANSACTION_FORMAT_NONE, FALSE);
+       g_return_val_if_fail (writer->in_transaction == TRUE, FALSE);
 
-       if (current_transaction_format == TRANSACTION_FORMAT_ONTOLOGY) {
-               ret = db_journal_writer_commit_db_transaction (&ontology_writer, &n_error);
-       } else {
-               ret = db_journal_writer_commit_db_transaction (&writer, &n_error);
+       ret = db_journal_writer_commit_db_transaction (writer, &n_error);
 
-               if (ret) {
-                       if (rotating_settings.do_rotating && (writer.cur_size > 
rotating_settings.chunk_size)) {
-                               ret = tracker_db_journal_rotate (&n_error);
-                       }
+       if (ret && writer->transaction_format == TRANSACTION_FORMAT_DATA) {
+               if (rotating_settings.do_rotating && (writer->cur_size > rotating_settings.chunk_size)) {
+                       ret = tracker_db_journal_rotate (writer, &n_error);
                }
        }
 
@@ -1211,17 +1104,17 @@ tracker_db_journal_commit_db_transaction (GError **error)
                g_propagate_error (error, n_error);
        }
 
-       current_transaction_format = TRANSACTION_FORMAT_NONE;
+       writer->in_transaction = FALSE;
 
        return ret;
 }
 
 gboolean
-tracker_db_journal_fsync (void)
+tracker_db_journal_fsync (TrackerDBJournal *writer)
 {
-       g_return_val_if_fail (writer.journal > 0, FALSE);
+       g_return_val_if_fail (writer->journal > 0, FALSE);
 
-       return fsync (writer.journal) == 0;
+       return fsync (writer->journal) == 0;
 }
 
 /*
@@ -1401,7 +1294,8 @@ tracker_db_journal_reader_new (GFile   *data_location,
        reader = g_new0 (TrackerDBJournalReader, 1);
 
        if (!db_journal_reader_init (reader, TRUE, filename, data_location, &n_error)) {
-               g_propagate_error (error, n_error);
+               if (n_error)
+                       g_propagate_error (error, n_error);
                g_clear_pointer (&reader, g_free);
        }
 
@@ -2077,7 +1971,8 @@ on_chunk_copied_delete (GObject      *source_object,
 }
 
 static gboolean
-tracker_db_journal_rotate (GError **error)
+tracker_db_journal_rotate (TrackerDBJournal  *writer,
+                           GError           **error)
 {
        GFile *source, *destination;
        GFile *dest_dir;
@@ -2099,7 +1994,7 @@ tracker_db_journal_rotate (GError **error)
                GDir *journal_dir;
                const gchar *f_name;
 
-               directory = g_path_get_dirname (writer.journal_filename);
+               directory = g_path_get_dirname (writer->journal_filename);
                journal_dir = g_dir_open (directory, 0, NULL);
 
                f_name = g_dir_read_name (journal_dir);
@@ -2127,9 +2022,9 @@ tracker_db_journal_rotate (GError **error)
                g_free (directory);
        }
 
-       tracker_db_journal_fsync ();
+       tracker_db_journal_fsync (writer);
 
-       if (close (writer.journal) != 0) {
+       if (close (writer->journal) != 0) {
                g_set_error (error, TRACKER_DB_JOURNAL_ERROR,
                             TRACKER_DB_JOURNAL_ERROR_COULD_NOT_CLOSE,
                             "Could not close journal, %s",
@@ -2137,13 +2032,13 @@ tracker_db_journal_rotate (GError **error)
                return FALSE;
        }
 
-       fullpath = g_strdup_printf ("%s.%d", writer.journal_filename, ++max);
+       fullpath = g_strdup_printf ("%s.%d", writer->journal_filename, ++max);
 
-       if (g_rename (writer.journal_filename, fullpath) < 0) {
+       if (g_rename (writer->journal_filename, fullpath) < 0) {
                g_set_error (error, TRACKER_DB_JOURNAL_ERROR,
                             TRACKER_DB_JOURNAL_ERROR_COULD_NOT_WRITE,
                             "Could not rotate journal file %s: %s",
-                            writer.journal_filename,
+                            writer->journal_filename,
                             g_strerror (errno));
                return FALSE;
        }
@@ -2179,19 +2074,19 @@ tracker_db_journal_rotate (GError **error)
 
        g_free (fullpath);
 
-       ret = db_journal_init_file (&writer, TRUE, &n_error);
+       ret = db_journal_init_file (writer, TRUE, &n_error);
 
        if (n_error) {
                g_propagate_error (error, n_error);
-               g_free (writer.journal_filename);
-               writer.journal_filename = NULL;
+               g_free (writer->journal_filename);
+               writer->journal_filename = NULL;
        }
 
        return ret;
 }
 
 void
-tracker_db_journal_remove (void)
+tracker_db_journal_remove (TrackerDBJournal *writer)
 {
        gchar *path;
        gchar *directory;
@@ -2202,7 +2097,7 @@ tracker_db_journal_remove (void)
        /* We duplicate the path here because later we shutdown the
         * journal which frees this data. We want to survive that.
         */
-       path = g_strdup (writer.journal_filename);
+       path = g_strdup (writer->journal_filename);
        if (!path) {
                return;
        }
@@ -2210,7 +2105,7 @@ tracker_db_journal_remove (void)
        g_info ("  Removing journal:'%s'", path);
 
        directory = g_path_get_dirname (path);
-       tracker_db_journal_shutdown (&error);
+       tracker_db_journal_free (writer, &error);
 
        if (error) {
                /* TODO: propagate error */
diff --git a/src/libtracker-data/tracker-db-journal.h b/src/libtracker-data/tracker-db-journal.h
index 62d360c..9fd64f3 100644
--- a/src/libtracker-data/tracker-db-journal.h
+++ b/src/libtracker-data/tracker-db-journal.h
@@ -54,6 +54,7 @@ typedef enum {
        TRACKER_DB_JOURNAL_UPDATE_STATEMENT_ID,
 } TrackerDBJournalEntryType;
 
+typedef struct _TrackerDBJournal TrackerDBJournal;
 typedef struct _TrackerDBJournalReader TrackerDBJournalReader;
 
 GQuark       tracker_db_journal_error_quark                  (void);
@@ -61,12 +62,17 @@ GQuark       tracker_db_journal_error_quark                  (void);
 /*
  * Writer API
  */
-gboolean     tracker_db_journal_init                         (GFile        *data_location,
+TrackerDBJournal *
+             tracker_db_journal_new                          (GFile        *data_location,
                                                               gboolean      truncate,
                                                               GError      **error);
-gboolean     tracker_db_journal_shutdown                     (GError      **error);
+TrackerDBJournal *
+             tracker_db_journal_ontology_new                 (GFile        *data_location,
+                                                              GError      **error);
+gboolean     tracker_db_journal_free                         (TrackerDBJournal  *writer,
+                                                              GError           **error);
 
-gsize        tracker_db_journal_get_size                     (void);
+gsize        tracker_db_journal_get_size                     (TrackerDBJournal  *writer);
 
 void         tracker_db_journal_set_rotating                 (gboolean     do_rotating,
                                                               gsize        chunk_size,
@@ -76,42 +82,52 @@ void         tracker_db_journal_get_rotating                 (gboolean    *do_ro
                                                               gsize       *chunk_size,
                                                               gchar      **rotate_to);
 
-gboolean     tracker_db_journal_start_transaction            (time_t       time);
-gboolean     tracker_db_journal_start_ontology_transaction   (time_t       time,
-                                                              GError     **error);
-
-gboolean     tracker_db_journal_append_delete_statement      (gint         g_id,
-                                                              gint         s_id,
-                                                              gint         p_id,
-                                                              const gchar *object);
-gboolean     tracker_db_journal_append_delete_statement_id   (gint         g_id,
-                                                              gint         s_id,
-                                                              gint         p_id,
-                                                              gint         o_id);
-gboolean     tracker_db_journal_append_insert_statement      (gint         g_id,
-                                                              gint         s_id,
-                                                              gint         p_id,
-                                                              const gchar *object);
-gboolean     tracker_db_journal_append_insert_statement_id   (gint         g_id,
-                                                              gint         s_id,
-                                                              gint         p_id,
-                                                              gint         o_id);
-gboolean     tracker_db_journal_append_update_statement      (gint         g_id,
-                                                              gint         s_id,
-                                                              gint         p_id,
-                                                              const gchar *object);
-gboolean     tracker_db_journal_append_update_statement_id   (gint         g_id,
-                                                              gint         s_id,
-                                                              gint         p_id,
-                                                              gint         o_id);
-gboolean     tracker_db_journal_append_resource              (gint         s_id,
-                                                              const gchar *uri);
-
-gboolean     tracker_db_journal_rollback_transaction         (GError **error);
-gboolean     tracker_db_journal_commit_db_transaction        (GError **error);
-
-gboolean     tracker_db_journal_fsync                        (void);
-gboolean     tracker_db_journal_truncate                     (gsize new_size);
+gboolean     tracker_db_journal_start_transaction            (TrackerDBJournal   *writer,
+                                                              time_t              time);
+
+gboolean     tracker_db_journal_append_delete_statement      (TrackerDBJournal   *writer,
+                                                              gint                g_id,
+                                                              gint                s_id,
+                                                              gint                p_id,
+                                                              const gchar        *object);
+gboolean     tracker_db_journal_append_delete_statement_id   (TrackerDBJournal   *writer,
+                                                              gint                g_id,
+                                                              gint                s_id,
+                                                              gint                p_id,
+                                                              gint                o_id);
+gboolean     tracker_db_journal_append_insert_statement      (TrackerDBJournal   *writer,
+                                                              gint                g_id,
+                                                              gint                s_id,
+                                                              gint                p_id,
+                                                              const gchar        *object);
+gboolean     tracker_db_journal_append_insert_statement_id   (TrackerDBJournal   *writer,
+                                                              gint                g_id,
+                                                              gint                s_id,
+                                                              gint                p_id,
+                                                              gint                o_id);
+gboolean     tracker_db_journal_append_update_statement      (TrackerDBJournal   *writer,
+                                                              gint                g_id,
+                                                              gint                s_id,
+                                                              gint                p_id,
+                                                              const gchar        *object);
+gboolean     tracker_db_journal_append_update_statement_id   (TrackerDBJournal   *writer,
+                                                              gint                g_id,
+                                                              gint                s_id,
+                                                              gint                p_id,
+                                                              gint                o_id);
+gboolean     tracker_db_journal_append_resource              (TrackerDBJournal   *writer,
+                                                              gint                s_id,
+                                                              const gchar        *uri);
+
+gboolean     tracker_db_journal_rollback_transaction         (TrackerDBJournal   *writer);
+gboolean     tracker_db_journal_commit_db_transaction        (TrackerDBJournal   *writer,
+                                                              GError            **error);
+
+gboolean     tracker_db_journal_fsync                        (TrackerDBJournal   *writer);
+gboolean     tracker_db_journal_truncate                     (TrackerDBJournal   *writer,
+                                                              gsize               new_size);
+
+void         tracker_db_journal_remove                       (TrackerDBJournal *writer);
 
 /*
  * Reader API
@@ -148,8 +164,6 @@ gdouble      tracker_db_journal_reader_get_progress          (TrackerDBJournalRe
 gboolean     tracker_db_journal_reader_verify_last           (GFile                   *data_location,
                                                               GError                 **error);
 
-void         tracker_db_journal_remove                       (void);
-
 G_END_DECLS
 
 #endif /* __LIBTRACKER_DB_JOURNAL_H__ */
diff --git a/src/tracker/tracker-reset.c b/src/tracker/tracker-reset.c
index 75630e3..dce61f0 100644
--- a/src/tracker/tracker-reset.c
+++ b/src/tracker/tracker-reset.c
@@ -266,6 +266,7 @@ reset_run (void)
                TrackerDBConfig *db_config;
                gsize chunk_size;
                gint chunk_size_mb;
+               TrackerDBJournal *journal_writer;
 #endif /* DISABLE_JOURNAL */
 
                dir = g_build_filename (g_get_user_cache_dir (), "tracker", NULL);
@@ -318,17 +319,15 @@ reset_run (void)
 
                        return EXIT_FAILURE;
                }
+
+               tracker_db_manager_remove_all ();
 #ifndef DISABLE_JOURNAL
-               tracker_db_journal_init (data_location, FALSE, NULL);
-               tracker_db_journal_remove ();
+               journal_writer = tracker_db_journal_new (data_location, FALSE, NULL);
+               tracker_db_journal_remove (journal_writer);
 #endif /* DISABLE_JOURNAL */
 
                tracker_db_manager_remove_version_file ();
-               tracker_db_manager_remove_all ();
                tracker_db_manager_shutdown ();
-#ifndef DISABLE_JOURNAL
-               tracker_db_journal_shutdown (NULL);
-#endif /* DISABLE_JOURNAL */
 
                /* Unset log handler */
                g_log_remove_handler (NULL, log_handler_id);
diff --git a/tests/libtracker-data/tracker-db-journal.c b/tests/libtracker-data/tracker-db-journal.c
index a21c1ba..e9da1a4 100644
--- a/tests/libtracker-data/tracker-db-journal.c
+++ b/tests/libtracker-data/tracker-db-journal.c
@@ -32,6 +32,7 @@ test_init_and_shutdown (void)
        gboolean result;
        gchar *path;
        GFile *data_location, *child;
+       TrackerDBJournal *writer;
 
        path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-db", NULL);
        data_location = g_file_new_for_path (path);
@@ -44,20 +45,20 @@ test_init_and_shutdown (void)
 
        /* check double init/shutdown */
        tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
-       result = tracker_db_journal_init (data_location, FALSE, &error);
+       writer = tracker_db_journal_new (data_location, FALSE, &error);
        g_assert_no_error (error);
-       g_assert (result == TRUE);
+       g_assert_nonnull (writer);
 
-       result = tracker_db_journal_shutdown (&error);
+       result = tracker_db_journal_free (writer, &error);
        g_assert_no_error (error);
        g_assert (result == TRUE);
 
        tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
-       result = tracker_db_journal_init (data_location, FALSE, &error);
+       writer = tracker_db_journal_new (data_location, FALSE, &error);
        g_assert_no_error (error);
-       g_assert (result == TRUE);
+       g_assert_nonnull (writer);
 
-       result = tracker_db_journal_shutdown (&error);
+       result = tracker_db_journal_free (writer, &error);
        g_assert_no_error (error);
        g_assert (result == TRUE);
 
@@ -73,6 +74,7 @@ test_write_functions (void)
        gboolean result;
        GError *error = NULL;
        GFile *data_location, *child;
+       TrackerDBJournal *writer;
 
        path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-db", NULL);
        data_location = g_file_new_for_path (path);
@@ -84,80 +86,79 @@ test_write_functions (void)
        g_object_unref (child);
 
        tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
-       tracker_db_journal_init (data_location, FALSE, &error);
+       writer = tracker_db_journal_new (data_location, FALSE, &error);
        g_object_unref (data_location);
        g_assert_no_error (error);
 
        /* Size is 8 due to header */
-       actual_size = tracker_db_journal_get_size ();
+       actual_size = tracker_db_journal_get_size (writer);
        g_assert_cmpint (actual_size, ==, 8);
 
        /* Check with rollback, nothing is added */
-       initial_size = tracker_db_journal_get_size ();
-       result = tracker_db_journal_start_transaction (time (NULL));
+       initial_size = tracker_db_journal_get_size (writer);
+       result = tracker_db_journal_start_transaction (writer, time (NULL));
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (10, "http://resource";);
+       result = tracker_db_journal_append_resource (writer, 10, "http://resource";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (11, "http://predicate";);
+       result = tracker_db_journal_append_resource (writer, 11, "http://predicate";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_delete_statement (0, 10, 11, "test");
+       result = tracker_db_journal_append_delete_statement (writer, 0, 10, 11, "test");
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_rollback_transaction (&error);
-       g_assert_no_error (error);
+       result = tracker_db_journal_rollback_transaction (writer);
        g_assert_cmpint (result, ==, TRUE);
-       actual_size = tracker_db_journal_get_size ();
+       actual_size = tracker_db_journal_get_size (writer);
        g_assert_cmpint (initial_size, ==, actual_size);
 
        /* Check with commit, somethign is added */
-       result = tracker_db_journal_start_transaction (time (NULL));
+       result = tracker_db_journal_start_transaction (writer, time (NULL));
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (12, "http://resource";);
+       result = tracker_db_journal_append_resource (writer, 12, "http://resource";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (13, "http://predicate";);
+       result = tracker_db_journal_append_resource (writer, 13, "http://predicate";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (14, "http://resource";);
+       result = tracker_db_journal_append_resource (writer, 14, "http://resource";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_delete_statement_id (0, 12, 13, 14);
+       result = tracker_db_journal_append_delete_statement_id (writer, 0, 12, 13, 14);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_commit_db_transaction (&error);
+       result = tracker_db_journal_commit_db_transaction (writer, &error);
        g_assert_no_error (error);
        g_assert_cmpint (result, ==, TRUE);
-       actual_size = tracker_db_journal_get_size ();
+       actual_size = tracker_db_journal_get_size (writer);
        g_assert_cmpint (initial_size, !=, actual_size);
 
        /* Test insert statement */
-       result = tracker_db_journal_start_transaction (time (NULL));
+       result = tracker_db_journal_start_transaction (writer, time (NULL));
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (15, "http://resource";);
+       result = tracker_db_journal_append_resource (writer, 15, "http://resource";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (16, "http://predicate";);
+       result = tracker_db_journal_append_resource (writer, 16, "http://predicate";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_insert_statement (0, 15, 16, "test");
+       result = tracker_db_journal_append_insert_statement (writer, 0, 15, 16, "test");
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_commit_db_transaction (&error);
+       result = tracker_db_journal_commit_db_transaction (writer, &error);
        g_assert_no_error (error);
        g_assert_cmpint (result, ==, TRUE);
 
        /* Test insert id */
-       result = tracker_db_journal_start_transaction (time (NULL));
+       result = tracker_db_journal_start_transaction (writer, time (NULL));
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (17, "http://resource";);
+       result = tracker_db_journal_append_resource (writer, 17, "http://resource";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (18, "http://predicate";);
+       result = tracker_db_journal_append_resource (writer, 18, "http://predicate";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_resource (19, "http://resource";);
+       result = tracker_db_journal_append_resource (writer, 19, "http://resource";);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_append_insert_statement_id (0, 17, 18, 19);
+       result = tracker_db_journal_append_insert_statement_id (writer, 0, 17, 18, 19);
        g_assert_cmpint (result, ==, TRUE);
-       result = tracker_db_journal_commit_db_transaction (&error);
+       result = tracker_db_journal_commit_db_transaction (writer, &error);
        g_assert_no_error (error);
        g_assert_cmpint (result, ==, TRUE);
 
        /* Test fsync */
-       result = tracker_db_journal_fsync ();
+       result = tracker_db_journal_fsync (writer);
        g_assert_cmpint (result, ==, TRUE);
 
-       tracker_db_journal_shutdown (&error);
+       tracker_db_journal_free (writer, &error);
        g_assert_no_error (error);
 
        g_free (path);


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