[tracker/binary-log-2: 12/48] Improve the journal API



commit 5f33bd08afb853a6e2a4224744a1a3503cc9ef3b
Author: Martyn Russell <martyn lanedo com>
Date:   Mon Jan 4 15:11:11 2010 +0000

    Improve the journal API
    
    Added return values in some cases, and make it clearer that there is
    an API to iterate the journal and an API to write to it.
    
    Also made the filename none optional, it is not important yet.

 src/libtracker-data/tracker-data-manager.c |    5 +-
 src/libtracker-db/tracker-db-journal.c     |  153 +++++++++++++++-------------
 src/libtracker-db/tracker-db-journal.h     |   26 ++++--
 tests/libtracker-db/tracker-db-journal.c   |    4 +-
 4 files changed, 103 insertions(+), 85 deletions(-)
---
diff --git a/src/libtracker-data/tracker-data-manager.c b/src/libtracker-data/tracker-data-manager.c
index 1995c98..7cead83 100644
--- a/src/libtracker-data/tracker-data-manager.c
+++ b/src/libtracker-data/tracker-data-manager.c
@@ -830,7 +830,7 @@ tracker_data_manager_init (TrackerDBManagerFlags  flags,
 
 	iface = tracker_db_manager_get_db_interface ();
 
-	tracker_db_journal_open ();
+	tracker_db_journal_init ();
 
 	if (is_first_time_index) {
 		TrackerClass **classes;
@@ -961,8 +961,7 @@ tracker_data_manager_shutdown (void)
 {
 	g_return_if_fail (initialized == TRUE);
 
-	tracker_db_journal_close ();
-
+	tracker_db_journal_shutdown ();
 	tracker_db_manager_shutdown ();
 
 	initialized = FALSE;
diff --git a/src/libtracker-db/tracker-db-journal.c b/src/libtracker-db/tracker-db-journal.c
index bf3e09f..c7c666d 100644
--- a/src/libtracker-db/tracker-db-journal.c
+++ b/src/libtracker-db/tracker-db-journal.c
@@ -125,37 +125,12 @@ cur_setstr (gchar       *dest,
 	memset (dest + (*pos)++, 0 & 0xff, 1);
 }
 
-gsize
-tracker_db_journal_get_size (void)
-{
-	return current_size;
-}
-
-const gchar *
-tracker_db_journal_filename (void)
-{
-	if (!filename) {
-		filename = g_build_filename (g_get_user_data_dir (),
-		                             "tracker",
-		                             "data",
-		                             JOURNAL_FILENAME,
-		                             NULL);
-	}
-
-	return filename;
-}
-
-void
-tracker_db_journal_open (const gchar *filen)
+gboolean
+tracker_db_journal_init (void)
 {
 	struct stat st;
 
-	if (!filen) {
-		tracker_db_journal_filename ();
-	} else {
-		filename = g_strdup (filen);
-	}
-
+	filename = tracker_db_journal_filename ();
 	journal = fopen (filename, "a");
 
 	if (stat (filename, &st) == 0) {
@@ -185,6 +160,42 @@ tracker_db_journal_open (const gchar *filen)
 
 		cur_block_kill ();
 	}
+
+	return TRUE;
+}
+
+gboolean
+tracker_db_journal_shutdown (void)
+{
+	g_assert (journal);
+
+	fclose (journal);
+	journal = NULL;
+
+	g_free (filename);
+	filename = NULL;
+
+	return TRUE;
+}
+
+gsize
+tracker_db_journal_get_size (void)
+{
+	return current_size;
+}
+
+const gchar *
+tracker_db_journal_filename (void)
+{
+	if (!filename) {
+		filename = g_build_filename (g_get_user_data_dir (),
+		                             "tracker",
+		                             "data",
+		                             JOURNAL_FILENAME,
+		                             NULL);
+	}
+
+	return (const gchar*) filename;
 }
 
 void
@@ -224,7 +235,6 @@ tracker_db_journal_append_delete_statement (guint32      s_code,
 	cur_block_len += size;
 }
 
-
 void
 tracker_db_journal_append_delete_statement_code (guint32 s_code,
                                                  guint32 p_code,
@@ -346,29 +356,19 @@ tracker_db_journal_fsync (void)
 	fsync (fileno (journal));
 }
 
-void
-tracker_db_journal_close (void)
-{
-	g_assert (journal);
-
-	fclose (journal);
-	journal = NULL;
-
-	g_free (filename);
-	filename = NULL;
-}
+/*
+ * Reader API
+ */
 
-void
-tracker_db_journal_reader_init (const gchar *filen)
+gboolean
+tracker_db_journal_entry_init (void)
 {
-	if (!filen) {
-		tracker_db_journal_filename ();
-	} else {
-		filename = g_strdup (filen);
-	}
+	const gchar *journal_filename;
+
+	journal_filename = tracker_db_journal_filename ();
 
 	/* TODO error handling */
-	journal_reader.file = g_mapped_file_new (filename, FALSE, NULL);
+	journal_reader.file = g_mapped_file_new (journal_filename, FALSE, NULL);
 	journal_reader.current = g_mapped_file_get_contents (journal_reader.file);
 	journal_reader.end = journal_reader.current + g_mapped_file_get_length (journal_reader.file);
 
@@ -376,15 +376,22 @@ tracker_db_journal_reader_init (const gchar *filen)
 	g_assert (journal_reader.end - journal_reader.current >= 8);
 	g_assert (memcmp (journal_reader.current, "trlog\001", 8) == 0);
 	journal_reader.current += 8;
+
+	return TRUE;
+}
+
+TrackerDBJournalEntryType
+tracker_db_journal_entry_get_type (void)
+{
+	return journal_reader.type;
 }
 
 gboolean
-tracker_db_journal_next (void)
+tracker_db_journal_entry_next (void)
 {
 	if (journal_reader.type == TRACKER_DB_JOURNAL_START ||
 	    journal_reader.type == TRACKER_DB_JOURNAL_END_TRANSACTION) {
 		/* expect new transaction or end of file */
-
 		guint32 entry_size;
 		guint32 crc32;
 
@@ -532,44 +539,46 @@ tracker_db_journal_next (void)
 	return FALSE;
 }
 
-TrackerDBJournalEntryType
-tracker_db_journal_get_type (void)
-{
-	return journal_reader.type;
-}
-
-void
-tracker_db_journal_get_resource (guint32      *code,
-                                 const gchar **uri)
+gboolean
+tracker_db_journal_entry_get_resource (guint32      *code,
+                                       const gchar **uri)
 {
-	g_return_if_fail (journal_reader.type == TRACKER_DB_JOURNAL_RESOURCE);
+	g_return_val_if_fail (journal_reader.type == TRACKER_DB_JOURNAL_RESOURCE, FALSE);
 
 	*code = journal_reader.s_code;
 	*uri = journal_reader.uri;
+
+	return TRUE;
 }
 
-void
-tracker_db_journal_get_statement (guint32      *s_code,
-                                  guint32      *p_code,
-                                  const gchar **object)
+gboolean
+tracker_db_journal_entry_get_statement (guint32      *s_code,
+                                        guint32      *p_code,
+                                        const gchar **object)
 {
-	g_return_if_fail (journal_reader.type == TRACKER_DB_JOURNAL_INSERT_STATEMENT ||
-	                  journal_reader.type == TRACKER_DB_JOURNAL_DELETE_STATEMENT);
+	g_return_val_if_fail (journal_reader.type == TRACKER_DB_JOURNAL_INSERT_STATEMENT ||
+	                      journal_reader.type == TRACKER_DB_JOURNAL_DELETE_STATEMENT,
+	                      FALSE);
 
 	*s_code = journal_reader.s_code;
 	*p_code = journal_reader.p_code;
 	*object = journal_reader.object;
+
+	return TRUE;
 }
 
-void
-tracker_db_journal_get_statement_code (guint32    *s_code,
-                                       guint32    *p_code,
-                                       guint32    *o_code)
+gboolean
+tracker_db_journal_entry_get_statement_code (guint32 *s_code,
+                                             guint32 *p_code,
+                                             guint32 *o_code)
 {
-	g_return_if_fail (journal_reader.type == TRACKER_DB_JOURNAL_INSERT_STATEMENT_CODE ||
-	                  journal_reader.type == TRACKER_DB_JOURNAL_DELETE_STATEMENT_CODE);
+	g_return_val_if_fail (journal_reader.type == TRACKER_DB_JOURNAL_INSERT_STATEMENT_CODE ||
+	                      journal_reader.type == TRACKER_DB_JOURNAL_DELETE_STATEMENT_CODE,
+	                      FALSE);
 
 	*s_code = journal_reader.s_code;
 	*p_code = journal_reader.p_code;
 	*o_code = journal_reader.o_code;
+
+	return TRUE;
 }
diff --git a/src/libtracker-db/tracker-db-journal.h b/src/libtracker-db/tracker-db-journal.h
index 2bd946e..2bf68da 100644
--- a/src/libtracker-db/tracker-db-journal.h
+++ b/src/libtracker-db/tracker-db-journal.h
@@ -38,8 +38,14 @@ typedef enum {
 	TRACKER_DB_JOURNAL_DELETE_STATEMENT_CODE
 } TrackerDBJournalEntryType;
 
+/*
+ * Writer API
+ */
+gboolean     tracker_db_journal_init                         (void);
+gboolean     tracker_db_journal_shutdown                     (void);
+
 const gchar* tracker_db_journal_filename                     (void);
-void         tracker_db_journal_open                         (const gchar *filen);
+gsize        tracker_db_journal_get_size                     (void);
 
 void         tracker_db_journal_start_transaction            (void);
 void         tracker_db_journal_append_delete_statement      (guint32      s_code,
@@ -62,18 +68,22 @@ void         tracker_db_journal_commit_transaction           (void);
 
 void         tracker_db_journal_close                        (void);
 void         tracker_db_journal_fsync                        (void);
-gsize        tracker_db_journal_get_size                     (void);
 
-void         tracker_db_journal_reader_init                  (const gchar  *filen);
-gboolean     tracker_db_journal_next                         (void);
+/*
+ * Reader API
+ */
+
+gboolean     tracker_db_journal_entry_init                   (void);
 TrackerDBJournalEntryType
-             tracker_db_journal_get_type                     (void);
-void         tracker_db_journal_get_resource                 (guint32      *code,
+             tracker_db_journal_entry_get_type               (void);
+
+gboolean     tracker_db_journal_entry_next                   (void);
+gboolean     tracker_db_journal_entry_get_resource           (guint32      *code,
                                                               const gchar **uri);
-void         tracker_db_journal_get_statement                (guint32      *s_code,
+gboolean     tracker_db_journal_entry_get_statement          (guint32      *s_code,
                                                               guint32      *p_code,
                                                               const gchar **object);
-void         tracker_db_journal_get_statement_code           (guint32      *s_code,
+gboolean     tracker_db_journal_entry_get_statement_code     (guint32      *s_code,
                                                               guint32      *p_code,
                                                               guint32      *o_code);
 
diff --git a/tests/libtracker-db/tracker-db-journal.c b/tests/libtracker-db/tracker-db-journal.c
index 258584f..1a47baf 100644
--- a/tests/libtracker-db/tracker-db-journal.c
+++ b/tests/libtracker-db/tracker-db-journal.c
@@ -24,7 +24,7 @@
 static void
 test_all (void)
 {
-	tracker_db_journal_open ("/tmp/test");
+	tracker_db_journal_init ();
 
 	tracker_db_journal_start_transaction ();
 	tracker_db_journal_append_resource (10, "http://resource";);
@@ -54,7 +54,7 @@ test_all (void)
 	tracker_db_journal_commit_transaction ();
 
 	tracker_db_journal_fsync ();
-	tracker_db_journal_close ();
+	tracker_db_journal_shutdown ();
 }
 
 int



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