[tracker/binary-log] Create TrackerDBJournalEntry for iterating through it



commit 6d19f7a7ab43c85d52eb274fcb2d6443ebe131d8
Author: Martyn Russell <martyn lanedo com>
Date:   Mon Jan 4 15:37:06 2010 +0000

    Create TrackerDBJournalEntry for iterating through it

 src/libtracker-db/tracker-db-journal.c |  230 ++++++++++++++++++--------------
 src/libtracker-db/tracker-db-journal.h |   30 +++--
 2 files changed, 147 insertions(+), 113 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-journal.c b/src/libtracker-db/tracker-db-journal.c
index c7c666d..48c2639 100644
--- a/src/libtracker-db/tracker-db-journal.c
+++ b/src/libtracker-db/tracker-db-journal.c
@@ -30,6 +30,8 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <glib/gstdio.h>
+
 #include <libtracker-common/tracker-crc32.h>
 
 #include "tracker-db-journal.h"
@@ -37,7 +39,7 @@
 #define JOURNAL_FILENAME  "tracker-store.journal"
 #define MIN_BLOCK_SIZE    1024
 
-static struct {
+struct TrackerDBJournalEntry {
 	GMappedFile *file;
 	const gchar *current;
 	const gchar *end;
@@ -50,11 +52,14 @@ static struct {
 	guint32 p_code;
 	guint32 o_code;
 	const gchar *object;
-} journal_reader;
+};
 
+/*
+ * Writer variables
+ */
 static gchar *filename = NULL;
 static FILE *journal = NULL;
-static gsize current_size = 0;
+static gsize cur_size = 0;
 static guint cur_block_len = 0;
 static guint cur_block_alloc = 0;
 static gchar *cur_block = NULL;
@@ -104,8 +109,8 @@ cur_block_kill (void)
 }
 
 static void
-cur_setnum (gchar   *dest, 
-            guint   *pos, 
+cur_setnum (gchar   *dest,
+            guint   *pos,
             guint32  val)
 {
 	memset (dest + (*pos)++, val >> 24 & 0xff, 1);
@@ -115,9 +120,9 @@ cur_setnum (gchar   *dest,
 }
 
 static void
-cur_setstr (gchar       *dest, 
-            guint       *pos, 
-            const gchar *str, 
+cur_setstr (gchar       *dest,
+            guint       *pos,
+            const gchar *str,
             gsize        len)
 {
 	memcpy (dest + *pos, str, len);
@@ -131,13 +136,13 @@ tracker_db_journal_init (void)
 	struct stat st;
 
 	filename = tracker_db_journal_filename ();
-	journal = fopen (filename, "a");
+	journal = g_fopen (filename, "a");
 
-	if (stat (filename, &st) == 0) {
-		current_size = (gsize) st.st_size;
+	if (g_stat (filename, &st) == 0) {
+		cur_size = (gsize) st.st_size;
 	}
 
-	if (current_size == 0) {
+	if (cur_size == 0) {
 		g_assert (cur_block_len == 0);
 		g_assert (cur_block_alloc == 0);
 		g_assert (cur_block == NULL);
@@ -156,7 +161,7 @@ tracker_db_journal_init (void)
 
 		write (fileno (journal), cur_block, 8);
 
-		current_size += 8;
+		cur_size += 8;
 
 		cur_block_kill ();
 	}
@@ -181,7 +186,7 @@ tracker_db_journal_shutdown (void)
 gsize
 tracker_db_journal_get_size (void)
 {
-	return current_size;
+	return cur_size;
 }
 
 const gchar *
@@ -205,8 +210,8 @@ tracker_db_journal_start_transaction (void)
 
 	cur_block_maybe_expand (size);
 
-	/* Leave space for size, amount and crc 
-	 * Check and keep in sync the offset variable at 
+	/* Leave space for size, amount and crc
+	 * Check and keep in sync the offset variable at
 	 * tracker_db_journal_commit_transaction too */
 
 	memset (cur_block, 0, size);
@@ -255,8 +260,8 @@ tracker_db_journal_append_delete_statement_code (guint32 s_code,
 }
 
 void
-tracker_db_journal_append_insert_statement (guint32      s_code, 
-                                            guint32      p_code, 
+tracker_db_journal_append_insert_statement (guint32      s_code,
+                                            guint32      p_code,
                                             const gchar *object)
 {
 	gint o_len = strlen (object);
@@ -343,7 +348,7 @@ tracker_db_journal_commit_transaction (void)
 
 	write (fileno (journal), cur_block, cur_block_len);
 
-	current_size += cur_block_len;
+	cur_size += cur_block_len;
 
 	cur_block_kill ();
 }
@@ -360,179 +365,196 @@ tracker_db_journal_fsync (void)
  * Reader API
  */
 
-gboolean
-tracker_db_journal_entry_init (void)
+TrackerDBJournalEntry *
+tracker_db_journal_entry_new (void)
 {
+	TrackerDBJournalEntry *entry;
 	const gchar *journal_filename;
 
 	journal_filename = tracker_db_journal_filename ();
 
+	entry = g_slice_new0 (TrackerDBJournalEntry);
+
 	/* TODO error handling */
-	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);
+	entry->file = g_mapped_file_new (journal_filename, FALSE, NULL);
+	entry->current = g_mapped_file_get_contents (entry->file);
+	entry->end = entry->current + g_mapped_file_get_length (entry->file);
 
 	/* verify journal file header */
-	g_assert (journal_reader.end - journal_reader.current >= 8);
-	g_assert (memcmp (journal_reader.current, "trlog\001", 8) == 0);
-	journal_reader.current += 8;
+	g_assert (entry->end - entry->current >= 8);
+	g_assert (memcmp (entry->current, "trlog\001", 8) == 0);
+	entry->current += 8;
+
+	return entry;
+}
+
+gboolean
+tracker_db_journal_entry_free (TrackerDBJournalEntry *entry)
+{
+	g_return_val_if_fail (entry != NULL, FALSE);
+
+
 
 	return TRUE;
 }
 
 TrackerDBJournalEntryType
-tracker_db_journal_entry_get_type (void)
+tracker_db_journal_entry_get_type (TrackerDBJournalEntry *entry)
 {
-	return journal_reader.type;
+	g_return_val_if_fail (entry != NULL, FALSE);
+
+	return entry->type;
 }
 
 gboolean
-tracker_db_journal_entry_next (void)
+tracker_db_journal_entry_next (TrackerDBJournalEntry *entry)
 {
-	if (journal_reader.type == TRACKER_DB_JOURNAL_START ||
-	    journal_reader.type == TRACKER_DB_JOURNAL_END_TRANSACTION) {
+	g_return_val_if_fail (entry != NULL, FALSE);
+
+	if (entry->type == TRACKER_DB_JOURNAL_START ||
+	    entry->type == TRACKER_DB_JOURNAL_END_TRANSACTION) {
 		/* expect new transaction or end of file */
 		guint32 entry_size;
 		guint32 crc32;
 
-		if (journal_reader.current >= journal_reader.end) {
+		if (entry->current >= entry->end) {
 			/* end of journal reached */
 			return FALSE;
 		}
 
-		if (journal_reader.end - journal_reader.current < sizeof (guint32)) {
+		if (entry->end - entry->current < sizeof (guint32)) {
 			/* damaged journal entry */
 			return FALSE;
 		}
 
-		journal_reader.entry_begin = journal_reader.current;
-		entry_size = read_uint32 (journal_reader.current);
-		journal_reader.entry_end = journal_reader.entry_begin + entry_size;
-		if (journal_reader.end < journal_reader.entry_end) {
+		entry->entry_begin = entry->current;
+		entry_size = read_uint32 (entry->current);
+		entry->entry_end = entry->entry_begin + entry_size;
+		if (entry->end < entry->entry_end) {
 			/* damaged journal entry */
 			return FALSE;
 		}
-		journal_reader.current += 4;
+		entry->current += 4;
 
 		/* compare with entry_size at end */
-		if (entry_size != read_uint32 (journal_reader.entry_end - 4)) {
+		if (entry_size != read_uint32 (entry->entry_end - 4)) {
 			/* damaged journal entry */
 			return FALSE;
 		}
 
-		journal_reader.amount_of_triples = read_uint32 (journal_reader.current);
-		journal_reader.current += 4;
+		entry->amount_of_triples = read_uint32 (entry->current);
+		entry->current += 4;
 
-		crc32 = read_uint32 (journal_reader.current);
-		journal_reader.current += 4;
+		crc32 = read_uint32 (entry->current);
+		entry->current += 4;
 
 		/* verify checksum */
-		if (crc32 != tracker_crc32 (journal_reader.entry_begin, entry_size)) {
+		if (crc32 != tracker_crc32 (entry->entry_begin, entry_size)) {
 			/* damaged journal entry */
 			return FALSE;
 		}
 
-		journal_reader.type = TRACKER_DB_JOURNAL_START_TRANSACTION;
+		entry->type = TRACKER_DB_JOURNAL_START_TRANSACTION;
 		return TRUE;
-	} else if (journal_reader.amount_of_triples == 0) {
+	} else if (entry->amount_of_triples == 0) {
 		/* end of transaction */
 
-		if (journal_reader.current + 4 != journal_reader.entry_end) {
+		if (entry->current + 4 != entry->entry_end) {
 			/* damaged journal entry */
 			return FALSE;
 		}
 
-		journal_reader.type = TRACKER_DB_JOURNAL_END_TRANSACTION;
+		entry->type = TRACKER_DB_JOURNAL_END_TRANSACTION;
 		return TRUE;
 	} else {
 		guint32 data_format;
 		gsize str_length;
 
-		if (journal_reader.end - journal_reader.current < sizeof (guint32)) {
+		if (entry->end - entry->current < sizeof (guint32)) {
 			/* damaged journal entry */
 			return FALSE;
 		}
 
-		data_format = read_uint32 (journal_reader.current);
-		journal_reader.current += 4;
+		data_format = read_uint32 (entry->current);
+		entry->current += 4;
 
 		if (data_format == 1) {
-			journal_reader.type = TRACKER_DB_JOURNAL_RESOURCE;
+			entry->type = TRACKER_DB_JOURNAL_RESOURCE;
 
-			if (journal_reader.end - journal_reader.current < sizeof (guint32) + 1) {
+			if (entry->end - entry->current < sizeof (guint32) + 1) {
 				/* damaged journal entry */
 				return FALSE;
 			}
 
-			journal_reader.s_code = read_uint32 (journal_reader.current);
-			journal_reader.current += 4;
+			entry->s_code = read_uint32 (entry->current);
+			entry->current += 4;
 
-			str_length = strnlen (journal_reader.current, journal_reader.end - journal_reader.current);
-			if (str_length == journal_reader.end - journal_reader.current) {
+			str_length = strnlen (entry->current, entry->end - entry->current);
+			if (str_length == entry->end - entry->current) {
 				/* damaged journal entry (no terminating '\0' character) */
 				return FALSE;
 			}
-			if (!g_utf8_validate (journal_reader.current, -1, NULL)) {
+			if (!g_utf8_validate (entry->current, -1, NULL)) {
 				/* damaged journal entry (invalid UTF-8) */
 				return FALSE;
 			}
-			journal_reader.uri = journal_reader.current;
-			journal_reader.current += str_length + 1;
+			entry->uri = entry->current;
+			entry->current += str_length + 1;
 		} else {
 			if (data_format & 4) {
 				if (data_format & 2) {
-					journal_reader.type = TRACKER_DB_JOURNAL_DELETE_STATEMENT_CODE;
+					entry->type = TRACKER_DB_JOURNAL_DELETE_STATEMENT_CODE;
 				} else {
-					journal_reader.type = TRACKER_DB_JOURNAL_DELETE_STATEMENT;
+					entry->type = TRACKER_DB_JOURNAL_DELETE_STATEMENT;
 				}
 			} else {
 				if (data_format & 2) {
-					journal_reader.type = TRACKER_DB_JOURNAL_INSERT_STATEMENT_CODE;
+					entry->type = TRACKER_DB_JOURNAL_INSERT_STATEMENT_CODE;
 				} else {
-					journal_reader.type = TRACKER_DB_JOURNAL_INSERT_STATEMENT;
+					entry->type = TRACKER_DB_JOURNAL_INSERT_STATEMENT;
 				}
 			}
 
-			if (journal_reader.end - journal_reader.current < 2 * sizeof (guint32)) {
+			if (entry->end - entry->current < 2 * sizeof (guint32)) {
 				/* damaged journal entry */
 				return FALSE;
 			}
 
-			journal_reader.s_code = read_uint32 (journal_reader.current);
-			journal_reader.current += 4;
+			entry->s_code = read_uint32 (entry->current);
+			entry->current += 4;
 
-			journal_reader.p_code = read_uint32 (journal_reader.current);
-			journal_reader.current += 4;
+			entry->p_code = read_uint32 (entry->current);
+			entry->current += 4;
 
 			if (data_format & 2) {
-				if (journal_reader.end - journal_reader.current < sizeof (guint32)) {
+				if (entry->end - entry->current < sizeof (guint32)) {
 					/* damaged journal entry */
 					return FALSE;
 				}
 
-				journal_reader.o_code = read_uint32 (journal_reader.current);
-				journal_reader.current += 4;
+				entry->o_code = read_uint32 (entry->current);
+				entry->current += 4;
 			} else {
-				if (journal_reader.end - journal_reader.current < 1) {
+				if (entry->end - entry->current < 1) {
 					/* damaged journal entry */
 					return FALSE;
 				}
 
-				str_length = strnlen (journal_reader.current, journal_reader.end - journal_reader.current);
-				if (str_length == journal_reader.end - journal_reader.current) {
+				str_length = strnlen (entry->current, entry->end - entry->current);
+				if (str_length == entry->end - entry->current) {
 					/* damaged journal entry (no terminating '\0' character) */
 					return FALSE;
 				}
-				if (!g_utf8_validate (journal_reader.current, -1, NULL)) {
+				if (!g_utf8_validate (entry->current, -1, NULL)) {
 					/* damaged journal entry (invalid UTF-8) */
 					return FALSE;
 				}
-				journal_reader.object = journal_reader.current;
-				journal_reader.current += str_length + 1;
+				entry->object = entry->current;
+				entry->current += str_length + 1;
 			}
 		}
 
-		journal_reader.amount_of_triples--;
+		entry->amount_of_triples--;
 		return TRUE;
 	}
 
@@ -540,45 +562,51 @@ tracker_db_journal_entry_next (void)
 }
 
 gboolean
-tracker_db_journal_entry_get_resource (guint32      *code,
-                                       const gchar **uri)
+tracker_db_journal_entry_get_resource (TrackerDBJournalEntry  *entry,
+                                       guint32                *code,
+                                       const gchar           **uri)
 {
-	g_return_val_if_fail (journal_reader.type == TRACKER_DB_JOURNAL_RESOURCE, FALSE);
+	g_return_val_if_fail (entry != NULL, FALSE);
+	g_return_val_if_fail (entry->type == TRACKER_DB_JOURNAL_RESOURCE, FALSE);
 
-	*code = journal_reader.s_code;
-	*uri = journal_reader.uri;
+	*code = entry->s_code;
+	*uri = entry->uri;
 
 	return TRUE;
 }
 
 gboolean
-tracker_db_journal_entry_get_statement (guint32      *s_code,
-                                        guint32      *p_code,
-                                        const gchar **object)
+tracker_db_journal_entry_get_statement (TrackerDBJournalEntry  *entry,
+                                        guint32                *s_code,
+                                        guint32                *p_code,
+                                        const gchar           **object)
 {
-	g_return_val_if_fail (journal_reader.type == TRACKER_DB_JOURNAL_INSERT_STATEMENT ||
-	                      journal_reader.type == TRACKER_DB_JOURNAL_DELETE_STATEMENT,
+	g_return_val_if_fail (entry != NULL, FALSE);
+	g_return_val_if_fail (entry->type == TRACKER_DB_JOURNAL_INSERT_STATEMENT ||
+	                      entry->type == TRACKER_DB_JOURNAL_DELETE_STATEMENT,
 	                      FALSE);
 
-	*s_code = journal_reader.s_code;
-	*p_code = journal_reader.p_code;
-	*object = journal_reader.object;
+	*s_code = entry->s_code;
+	*p_code = entry->p_code;
+	*object = entry->object;
 
 	return TRUE;
 }
 
 gboolean
-tracker_db_journal_entry_get_statement_code (guint32 *s_code,
-                                             guint32 *p_code,
-                                             guint32 *o_code)
+tracker_db_journal_entry_get_statement_code (TrackerDBJournalEntry *entry,
+                                             guint32               *s_code,
+                                             guint32               *p_code,
+                                             guint32               *o_code)
 {
-	g_return_val_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 (entry != NULL, FALSE);
+	g_return_val_if_fail (entry->type == TRACKER_DB_JOURNAL_INSERT_STATEMENT_CODE ||
+	                      entry->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;
+	*s_code = entry->s_code;
+	*p_code = entry->p_code;
+	*o_code = entry->o_code;
 
 	return TRUE;
 }
diff --git a/src/libtracker-db/tracker-db-journal.h b/src/libtracker-db/tracker-db-journal.h
index 2bf68da..13bb1a6 100644
--- a/src/libtracker-db/tracker-db-journal.h
+++ b/src/libtracker-db/tracker-db-journal.h
@@ -27,6 +27,8 @@
 
 G_BEGIN_DECLS
 
+typedef struct TrackerDBJournalEntry TrackerDBJournalEntry;
+
 typedef enum {
 	TRACKER_DB_JOURNAL_START,
 	TRACKER_DB_JOURNAL_START_TRANSACTION,
@@ -72,20 +74,24 @@ void         tracker_db_journal_fsync                        (void);
 /*
  * Reader API
  */
-
-gboolean     tracker_db_journal_entry_init                   (void);
+TrackerDBJournalEntry *
+             tracker_db_journal_entry_new                    (void);
+gboolean     tracker_db_journal_entry_free                   (TrackerDBJournalEntry  *entry);
 TrackerDBJournalEntryType
-             tracker_db_journal_entry_get_type               (void);
+             tracker_db_journal_entry_get_type               (TrackerDBJournalEntry  *entry);
 
-gboolean     tracker_db_journal_entry_next                   (void);
-gboolean     tracker_db_journal_entry_get_resource           (guint32      *code,
-                                                              const gchar **uri);
-gboolean     tracker_db_journal_entry_get_statement          (guint32      *s_code,
-                                                              guint32      *p_code,
-                                                              const gchar **object);
-gboolean     tracker_db_journal_entry_get_statement_code     (guint32      *s_code,
-                                                              guint32      *p_code,
-                                                              guint32      *o_code);
+gboolean     tracker_db_journal_entry_next                   (TrackerDBJournalEntry  *entry);
+gboolean     tracker_db_journal_entry_get_resource           (TrackerDBJournalEntry  *entry,
+                                                              guint32                *code,
+                                                              const gchar           **uri);
+gboolean     tracker_db_journal_entry_get_statement          (TrackerDBJournalEntry  *entry,
+                                                              guint32                *s_code,
+                                                              guint32                *p_code,
+                                                              const gchar           **object);
+gboolean     tracker_db_journal_entry_get_statement_code     (TrackerDBJournalEntry  *entry,
+                                                              guint32                *s_code,
+                                                              guint32                *p_code,
+                                                              guint32                *o_code);
 
 G_END_DECLS
 



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