[tracker/binary-log] Use GIO streams for db journal



commit a9b406eb46f46f2e737893331cf298688eac830d
Author: Martyn Russell <martyn lanedo com>
Date:   Tue Jan 5 10:45:05 2010 +0000

    Use GIO streams for db journal

 src/libtracker-db/tracker-db-journal.c |  116 +++++++++++++++++++++++++-------
 1 files changed, 91 insertions(+), 25 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-journal.c b/src/libtracker-db/tracker-db-journal.c
index e4ff221..58bcb9d 100644
--- a/src/libtracker-db/tracker-db-journal.c
+++ b/src/libtracker-db/tracker-db-journal.c
@@ -57,7 +57,8 @@ static struct {
 } reader;
 
 static gchar *journal_filename = NULL;
-static FILE *journal = NULL;
+static GFile *journal_file = NULL;
+static GOutputStream *journal_stream = NULL;
 
 static gsize cur_size = 0;
 static guint cur_block_len = 0;
@@ -131,6 +132,25 @@ cur_setstr (gchar       *dest,
 	memset (dest + (*pos)++, 0 & 0xff, 1);
 }
 
+static gboolean
+write_data (const gchar *buffer,
+            gsize        size)
+{
+        GError *error = NULL;
+	gboolean result;
+
+	result = g_output_stream_write_all (journal_stream, buffer, size, NULL, NULL, &error);
+
+        if (error) {
+                g_warning ("Could not append to journal %" G_GSSIZE_FORMAT " bytes, %s", 
+                           size,
+                           error->message);
+                g_clear_error (&error);
+        }
+
+        return TRUE;
+}
+
 GQuark
 tracker_db_journal_error_quark (void)
 {
@@ -141,8 +161,9 @@ gboolean
 tracker_db_journal_init (const gchar *filename)
 {
 	struct stat st;
+	GError *error = NULL;
 
-	g_return_val_if_fail (journal == NULL, FALSE);
+	g_return_val_if_fail (journal_stream == NULL, FALSE);
 
 	/* Used mostly for testing */
 	if (G_UNLIKELY (filename)) {
@@ -155,7 +176,27 @@ tracker_db_journal_init (const gchar *filename)
 		                                     NULL);
 	}
 
-	journal = g_fopen (journal_filename, "a");
+	journal_file = g_file_new_for_path (journal_filename);
+
+	journal_stream = G_OUTPUT_STREAM (g_file_append_to (journal_file, 
+	                                                    G_FILE_CREATE_NONE, 
+	                                                    NULL, 
+	                                                    &error));
+
+        if (!journal_stream || error) {
+                g_warning ("Could not open stream to append to journal, %s", 
+                           error ? error->message : "no error given");
+
+                if (journal_stream) {
+                        g_object_unref (journal_stream);
+                }
+
+                if (error) {
+                        g_error_free (error);
+                }
+
+                return FALSE;
+        }
 
 	if (g_stat (journal_filename, &st) == 0) {
 		cur_size = (gsize) st.st_size;
@@ -178,7 +219,9 @@ tracker_db_journal_init (const gchar *filename)
 		cur_block[6] = '0';
 		cur_block[7] = '1';
 
-		write (fileno (journal), cur_block, 8);
+		if (!write_data (cur_block, 8)) {
+			return FALSE;
+		}
 
 		cur_size += 8;
 
@@ -191,23 +234,36 @@ tracker_db_journal_init (const gchar *filename)
 gboolean
 tracker_db_journal_shutdown (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	GError *error = NULL;
 
-	if (journal) {
-		fclose (journal);
-		journal = NULL;
-	}
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
+
+        g_output_stream_close (journal_stream, NULL, &error);
+        g_object_unref (journal_stream);
+        journal_stream = NULL;
 
 	g_free (journal_filename);
 	journal_filename = NULL;
 
+	if (journal_file) {
+		g_object_unref (journal_file);
+		journal_file = NULL;
+	}
+
+        if (error) {
+                g_warning ("Could not close journal output stream, %s", 
+                           error->message);
+                g_error_free (error);
+                return FALSE;
+        }
+
 	return TRUE;
 }
 
 gsize
 tracker_db_journal_get_size (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	return cur_size;
 }
@@ -215,7 +271,7 @@ tracker_db_journal_get_size (void)
 const gchar *
 tracker_db_journal_get_filename (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	return (const gchar*) journal_filename;
 }
@@ -225,7 +281,7 @@ tracker_db_journal_start_transaction (void)
 {
 	guint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	size = sizeof (guint32) * 3;
 	cur_block_maybe_expand (size);
@@ -251,7 +307,7 @@ tracker_db_journal_append_delete_statement (guint32      s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	o_len = strlen (object);
 	data_format = 0x04;
@@ -278,7 +334,7 @@ tracker_db_journal_append_delete_statement_code (guint32 s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	data_format = 0x06;
 	size = sizeof (guint32) * 4;
@@ -305,7 +361,7 @@ tracker_db_journal_append_insert_statement (guint32      s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	o_len = strlen (object);
 	data_format = 0x00;
@@ -332,7 +388,7 @@ tracker_db_journal_append_insert_statement_code (guint32 s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	data_format = 0x02;
 	size = sizeof (guint32) * 4;
@@ -358,7 +414,7 @@ tracker_db_journal_append_resource (guint32      s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	o_len = strlen (uri);
 	data_format = 0x01;
@@ -379,7 +435,7 @@ tracker_db_journal_append_resource (guint32      s_code,
 gboolean
 tracker_db_journal_rollback_transaction (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	cur_block_kill ();
 
@@ -394,7 +450,7 @@ tracker_db_journal_commit_transaction (void)
 	guint size;
 	guint offset;
 
-	g_return_val_if_fail (journal != NULL, FALSE);
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
 
 	begin_pos = 0;
 	size = sizeof (guint32);
@@ -414,12 +470,10 @@ tracker_db_journal_commit_transaction (void)
 	crc = tracker_crc32 (cur_block + offset, cur_block_len - offset);
 	cur_setnum (cur_block, &begin_pos, crc);
 
-	/* FIXME: What if we don't write all of len, needs improving. */
-	if (write (fileno (journal), cur_block, cur_block_len) == -1) {
-		g_critical ("Could not write to journal, %s", g_strerror (errno));
+	if (!write_data (cur_block, cur_block_len)) {
 		return FALSE;
 	}
-
+	
 	cur_size += cur_block_len;
 	cur_block_kill ();
 
@@ -429,9 +483,21 @@ tracker_db_journal_commit_transaction (void)
 gboolean
 tracker_db_journal_fsync (void)
 {
-	g_return_val_if_fail (journal != NULL, FALSE);
+        GError *error = NULL;
+	gboolean result;
+
+	g_return_val_if_fail (journal_stream != NULL, FALSE);
+
+	result = g_output_stream_flush (journal_stream, NULL, &error);
+
+        if (error) {
+                g_warning ("Could not flush stream for journal, %s", 
+                           error->message);
+                g_error_free (error);
+                return FALSE;
+        }
 
-	return fsync (fileno (journal)) == 0;
+        return result;
 }
 
 /*



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