[tracker/binary-log-2: 16/47] Refactored journal writer to use a struct instead of global variales



commit 3567658197f9d366d250c03e0076363570f1cdc6
Author: Philip Van Hoof <philip codeminded be>
Date:   Tue Jan 5 13:28:02 2010 +0100

    Refactored journal writer to use a struct instead of global variales
    
    Also fixed the tracker-control tool, it wasn't initializing the journal,
    meaning that the journal's filename was NULL.

 src/libtracker-db/tracker-db-journal.c |  239 ++++++++++++++++---------------
 src/libtracker-db/tracker-db-manager.c |    6 +-
 src/tracker-control/tracker-control.c  |    4 +
 3 files changed, 131 insertions(+), 118 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-journal.c b/src/libtracker-db/tracker-db-journal.c
index b2c403e..d0f57cc 100644
--- a/src/libtracker-db/tracker-db-journal.c
+++ b/src/libtracker-db/tracker-db-journal.c
@@ -57,15 +57,16 @@ static struct {
 	const gchar *object;
 } reader;
 
-static gchar *journal_filename = NULL;
-static int journal = 0;
-
-static gsize cur_size = 0;
-static guint cur_block_len = 0;
-static guint cur_block_alloc = 0;
-static gchar *cur_block = NULL;
-static guint cur_entry_amount = 0;
-static guint cur_pos = 0;
+static struct {
+	gchar *journal_filename;
+	int journal;
+	gsize cur_size;
+	guint cur_block_len;
+	guint cur_block_alloc;
+	gchar *cur_block;
+	guint cur_entry_amount;
+	guint cur_pos;
+} writer;
 
 static guint32
 read_uint32 (const gchar *data)
@@ -88,26 +89,26 @@ nearest_pow (gint num)
 static void
 cur_block_maybe_expand (guint len)
 {
-	guint want_alloc = cur_block_len + len;
+	guint want_alloc = writer.cur_block_len + len;
 
-	if (want_alloc > cur_block_alloc) {
+	if (want_alloc > writer.cur_block_alloc) {
 		want_alloc = nearest_pow (want_alloc);
 		want_alloc = MAX (want_alloc, MIN_BLOCK_SIZE);
-		cur_block = g_realloc (cur_block, want_alloc);
-		cur_block_alloc = want_alloc;
+		writer.cur_block = g_realloc (writer.cur_block, want_alloc);
+		writer.cur_block_alloc = want_alloc;
 	}
 }
 
 static void
 cur_block_kill (void)
 {
-	cur_block_len = 0;
-	cur_pos = 0;
-	cur_entry_amount = 0;
-	cur_block_alloc = 0;
+	writer.cur_block_len = 0;
+	writer.cur_pos = 0;
+	writer.cur_entry_amount = 0;
+	writer.cur_block_alloc = 0;
 
-	g_free (cur_block);
-	cur_block = NULL;
+	g_free (writer.cur_block);
+	writer.cur_block = NULL;
 }
 
 static void
@@ -139,9 +140,9 @@ write_all_data (int    fd,
 {
 	gssize written;
 	gboolean result;
-	
+
 	result = FALSE;
-	
+
 	while (len > 0) {
 		written = write (fd, data, len);
 		
@@ -157,11 +158,11 @@ write_all_data (int    fd,
 		len -= written;
 		data += written;
 	}
-	
+
 	result = TRUE; /* Succeeded! */
-	
+
 out:
-	
+
 	return result;
 }
 
@@ -178,60 +179,66 @@ tracker_db_journal_init (const gchar *filename)
 	int flags;
 	int mode;
 
-	g_return_val_if_fail (journal == 0, FALSE);
+	g_return_val_if_fail (writer.journal == 0, FALSE);
+
+	writer.cur_block_len = 0;
+	writer.cur_pos = 0;
+	writer.cur_entry_amount = 0;
+	writer.cur_block_alloc = 0;
+	writer.cur_block = NULL;
 
 	/* Used mostly for testing */
 	if (G_UNLIKELY (filename)) {
-		journal_filename = g_strdup (filename);
+		writer.journal_filename = g_strdup (filename);
 	} else {
-		journal_filename = g_build_filename (g_get_user_data_dir (),
-		                                     "tracker",
-		                                     "data",
-		                                     JOURNAL_FILENAME,
-		                                     NULL);
+		writer.journal_filename = g_build_filename (g_get_user_data_dir (),
+		                                            "tracker",
+		                                            "data",
+		                                            JOURNAL_FILENAME,
+		                                            NULL);
 	}
 
 	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
 	flags = O_WRONLY | O_APPEND | O_CREAT;
-	journal = g_open (journal_filename, flags, mode);
+	writer.journal = g_open (writer.journal_filename, flags, mode);
 
-	if (journal == -1) {
+	if (writer.journal == -1) {
 		g_critical ("Could not open journal for writing, %s", 
 		            g_strerror (errno));
 
-		g_free (journal_filename);
-		journal_filename = NULL;
+		g_free (writer.journal_filename);
+		writer.journal_filename = NULL;
 		return FALSE;
 	}
 
-	if (g_stat (journal_filename, &st) == 0) {
-		cur_size = (gsize) st.st_size;
+	if (g_stat (writer.journal_filename, &st) == 0) {
+		writer.cur_size = (gsize) st.st_size;
 	}
 
-	if (cur_size == 0) {
-		g_assert (cur_block_len == 0);
-		g_assert (cur_block_alloc == 0);
-		g_assert (cur_block == NULL);
-		g_assert (cur_block == NULL);
+	if (writer.cur_size == 0) {
+		g_assert (writer.cur_block_len == 0);
+		g_assert (writer.cur_block_alloc == 0);
+		g_assert (writer.cur_block == NULL);
+		g_assert (writer.cur_block == NULL);
 
 		cur_block_maybe_expand (8);
 
-		cur_block[0] = 't';
-		cur_block[1] = 'r';
-		cur_block[2] = 'l';
-		cur_block[3] = 'o';
-		cur_block[4] = 'g';
-		cur_block[5] = '\0';
-		cur_block[6] = '0';
-		cur_block[7] = '1';
-
-		if (!write_all_data (journal, cur_block, 8)) {
-			g_free (journal_filename);
-			journal_filename = NULL;
+		writer.cur_block[0] = 't';
+		writer.cur_block[1] = 'r';
+		writer.cur_block[2] = 'l';
+		writer.cur_block[3] = 'o';
+		writer.cur_block[4] = 'g';
+		writer.cur_block[5] = '\0';
+		writer.cur_block[6] = '0';
+		writer.cur_block[7] = '1';
+
+		if (!write_all_data (writer.journal, writer.cur_block, 8)) {
+			g_free (writer.journal_filename);
+			writer.journal_filename = NULL;
 			return FALSE;
 		}
 
-		cur_size += 8;
+		writer.cur_size += 8;
 		cur_block_kill ();
 	}
 
@@ -241,18 +248,19 @@ tracker_db_journal_init (const gchar *filename)
 gboolean
 tracker_db_journal_shutdown (void)
 {
-	g_return_val_if_fail (journal > 0, FALSE);
+	if (writer.journal == 0)
+		return TRUE;
 
-	if (close (journal) != 0) {
+	if (close (writer.journal) != 0) {
 		g_warning ("Could not close journal, %s", 
 		           g_strerror (errno));
 		return FALSE;
 	}
-	
-	journal = 0;
 
-	g_free (journal_filename);
-	journal_filename = NULL;
+	writer.journal = 0;
+
+	g_free (writer.journal_filename);
+	writer.journal_filename = NULL;
 
 	return TRUE;
 }
@@ -260,17 +268,17 @@ tracker_db_journal_shutdown (void)
 gsize
 tracker_db_journal_get_size (void)
 {
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
-	return cur_size;
+	return writer.cur_size;
 }
 
 const gchar *
 tracker_db_journal_get_filename (void)
 {
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
-	return (const gchar*) journal_filename;
+	return (const gchar*) writer.journal_filename;
 }
 
 gboolean
@@ -278,7 +286,7 @@ tracker_db_journal_start_transaction (void)
 {
 	guint size;
 
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	size = sizeof (guint32) * 3;
 	cur_block_maybe_expand (size);
@@ -287,10 +295,10 @@ tracker_db_journal_start_transaction (void)
 	 * Check and keep in sync the offset variable at
 	 * tracker_db_journal_commit_transaction too */
 
-	memset (cur_block, 0, size);
+	memset (writer.cur_block, 0, size);
 
-	cur_pos = cur_block_len = size;
-	cur_entry_amount = 0;
+	writer.cur_pos = writer.cur_block_len = size;
+	writer.cur_entry_amount = 0;
 
 	return TRUE;
 }
@@ -304,7 +312,7 @@ tracker_db_journal_append_delete_statement (guint32      s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	o_len = strlen (object);
 	data_format = 0x04;
@@ -312,13 +320,13 @@ tracker_db_journal_append_delete_statement (guint32      s_code,
 
 	cur_block_maybe_expand (size);
 
-	cur_setnum (cur_block, &cur_pos, data_format);
-	cur_setnum (cur_block, &cur_pos, s_code);
-	cur_setnum (cur_block, &cur_pos, p_code);
-	cur_setstr (cur_block, &cur_pos, object, o_len);
+	cur_setnum (writer.cur_block, &writer.cur_pos, data_format);
+	cur_setnum (writer.cur_block, &writer.cur_pos, s_code);
+	cur_setnum (writer.cur_block, &writer.cur_pos, p_code);
+	cur_setstr (writer.cur_block, &writer.cur_pos, object, o_len);
 
-	cur_entry_amount++;
-	cur_block_len += size;
+	writer.cur_entry_amount++;
+	writer.cur_block_len += size;
 
 	return TRUE;
 }
@@ -331,20 +339,20 @@ tracker_db_journal_append_delete_statement_code (guint32 s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	data_format = 0x06;
 	size = sizeof (guint32) * 4;
 
 	cur_block_maybe_expand (size);
 
-	cur_setnum (cur_block, &cur_pos, data_format);
-	cur_setnum (cur_block, &cur_pos, s_code);
-	cur_setnum (cur_block, &cur_pos, p_code);
-	cur_setnum (cur_block, &cur_pos, o_code);
+	cur_setnum (writer.cur_block, &writer.cur_pos, data_format);
+	cur_setnum (writer.cur_block, &writer.cur_pos, s_code);
+	cur_setnum (writer.cur_block, &writer.cur_pos, p_code);
+	cur_setnum (writer.cur_block, &writer.cur_pos, o_code);
 
-	cur_entry_amount++;
-	cur_block_len += size;
+	writer.cur_entry_amount++;
+	writer.cur_block_len += size;
 
 	return TRUE;
 }
@@ -358,7 +366,7 @@ tracker_db_journal_append_insert_statement (guint32      s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	o_len = strlen (object);
 	data_format = 0x00;
@@ -366,13 +374,13 @@ tracker_db_journal_append_insert_statement (guint32      s_code,
 
 	cur_block_maybe_expand (size);
 
-	cur_setnum (cur_block, &cur_pos, data_format);
-	cur_setnum (cur_block, &cur_pos, s_code);
-	cur_setnum (cur_block, &cur_pos, p_code);
-	cur_setstr (cur_block, &cur_pos, object, o_len);
+	cur_setnum (writer.cur_block, &writer.cur_pos, data_format);
+	cur_setnum (writer.cur_block, &writer.cur_pos, s_code);
+	cur_setnum (writer.cur_block, &writer.cur_pos, p_code);
+	cur_setstr (writer.cur_block, &writer.cur_pos, object, o_len);
 
-	cur_entry_amount++;
-	cur_block_len += size;
+	writer.cur_entry_amount++;
+	writer.cur_block_len += size;
 
 	return TRUE;
 }
@@ -385,20 +393,20 @@ tracker_db_journal_append_insert_statement_code (guint32 s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	data_format = 0x02;
 	size = sizeof (guint32) * 4;
 
 	cur_block_maybe_expand (size);
 
-	cur_setnum (cur_block, &cur_pos, data_format);
-	cur_setnum (cur_block, &cur_pos, s_code);
-	cur_setnum (cur_block, &cur_pos, p_code);
-	cur_setnum (cur_block, &cur_pos, o_code);
+	cur_setnum (writer.cur_block, &writer.cur_pos, data_format);
+	cur_setnum (writer.cur_block, &writer.cur_pos, s_code);
+	cur_setnum (writer.cur_block, &writer.cur_pos, p_code);
+	cur_setnum (writer.cur_block, &writer.cur_pos, o_code);
 
-	cur_entry_amount++;
-	cur_block_len += size;
+	writer.cur_entry_amount++;
+	writer.cur_block_len += size;
 
 	return TRUE;
 }
@@ -411,7 +419,7 @@ tracker_db_journal_append_resource (guint32      s_code,
 	gchar data_format;
 	gint size;
 
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	o_len = strlen (uri);
 	data_format = 0x01;
@@ -419,12 +427,12 @@ tracker_db_journal_append_resource (guint32      s_code,
 
 	cur_block_maybe_expand (size);
 
-	cur_setnum (cur_block, &cur_pos, data_format);
-	cur_setnum (cur_block, &cur_pos, s_code);
-	cur_setstr (cur_block, &cur_pos, uri, o_len);
+	cur_setnum (writer.cur_block, &writer.cur_pos, data_format);
+	cur_setnum (writer.cur_block, &writer.cur_pos, s_code);
+	cur_setstr (writer.cur_block, &writer.cur_pos, uri, o_len);
 
-	cur_entry_amount++;
-	cur_block_len += size;
+	writer.cur_entry_amount++;
+	writer.cur_block_len += size;
 
 	return TRUE;
 }
@@ -432,7 +440,7 @@ tracker_db_journal_append_resource (guint32      s_code,
 gboolean
 tracker_db_journal_rollback_transaction (void)
 {
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	cur_block_kill ();
 
@@ -447,7 +455,7 @@ tracker_db_journal_commit_transaction (void)
 	guint size;
 	guint offset;
 
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
 	begin_pos = 0;
 	size = sizeof (guint32);
@@ -455,24 +463,24 @@ tracker_db_journal_commit_transaction (void)
 
 	cur_block_maybe_expand (size);
 
-	cur_setnum (cur_block, &begin_pos, cur_block_len);
-	cur_setnum (cur_block, &begin_pos, cur_entry_amount);
+	cur_setnum (writer.cur_block, &begin_pos, writer.cur_block_len);
+	cur_setnum (writer.cur_block, &begin_pos, writer.cur_entry_amount);
 
-	cur_setnum (cur_block, &cur_pos, cur_block_len);
+	cur_setnum (writer.cur_block, &writer.cur_pos, writer.cur_block_len);
 
-	cur_block_len += size;
+	writer.cur_block_len += size;
 
 	/* CRC is calculated from entries until appended amount int */
 
-	crc = tracker_crc32 (cur_block + offset, cur_block_len - offset);
-	cur_setnum (cur_block, &begin_pos, crc);
+	crc = tracker_crc32 (writer.cur_block + offset, writer.cur_block_len - offset);
+	cur_setnum (writer.cur_block, &begin_pos, crc);
 
-	if (!write_all_data (journal, cur_block, cur_block_len)) {
+	if (!write_all_data (writer.journal, writer.cur_block, writer.cur_block_len)) {
 		g_critical ("Could not write to journal, %s", g_strerror (errno));
 		return FALSE;
 	}
 
-	cur_size += cur_block_len;
+	writer.cur_size += writer.cur_block_len;
 	cur_block_kill ();
 
 	return TRUE;
@@ -481,9 +489,9 @@ tracker_db_journal_commit_transaction (void)
 gboolean
 tracker_db_journal_fsync (void)
 {
-	g_return_val_if_fail (journal > 0, FALSE);
+	g_return_val_if_fail (writer.journal > 0, FALSE);
 
-	return fsync (journal) == 0;
+	return fsync (writer.journal) == 0;
 }
 
 /*
@@ -510,7 +518,6 @@ tracker_db_journal_reader_init (const gchar *filename)
 
 	reader.type = TRACKER_DB_JOURNAL_START;
 	reader.filename = filename_used;
-
 	reader.file = g_mapped_file_new (reader.filename, FALSE, &error);
 
 	if (error) {
diff --git a/src/libtracker-db/tracker-db-manager.c b/src/libtracker-db/tracker-db-manager.c
index 8099a28..72bceeb 100644
--- a/src/libtracker-db/tracker-db-manager.c
+++ b/src/libtracker-db/tracker-db-manager.c
@@ -863,14 +863,16 @@ db_manager_remove_all (gboolean rm_backup_and_log, gboolean not_meta)
 
 	if (rm_backup_and_log) {
 		GFile *file;
-		const gchar *cpath;
+		gchar *cpath;
 
-		cpath = tracker_db_journal_get_filename ();
+		cpath = g_strdup (tracker_db_journal_get_filename ());
+		tracker_db_journal_shutdown ();
 		g_message ("  Removing journal:'%s'",
 		           cpath);
 		file = g_file_new_for_path (cpath);
 		g_file_delete (file, NULL, NULL);
 		g_object_unref (file);
+		g_free (cpath);
 	}
 }
 
diff --git a/src/tracker-control/tracker-control.c b/src/tracker-control/tracker-control.c
index dec66ea..337fbed 100644
--- a/src/tracker-control/tracker-control.c
+++ b/src/tracker-control/tracker-control.c
@@ -347,6 +347,9 @@ main (int argc, char **argv)
 
 		g_log_set_default_handler (log_handler, NULL);
 
+		/* This call is needed to set the journal's filename */
+		tracker_db_journal_init (NULL);
+
 		/* Clean up */
 		if (!tracker_db_manager_init (TRACKER_DB_MANAGER_REMOVE_ALL, NULL, FALSE, NULL)) {
 			return EXIT_FAILURE;
@@ -354,6 +357,7 @@ main (int argc, char **argv)
 
 		tracker_db_manager_remove_all (hard_reset);
 		tracker_db_manager_shutdown ();
+		tracker_db_journal_shutdown ();
 
 		/* Unset log handler */
 		g_log_remove_handler (NULL, log_handler_id);



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