[tracker/binary-log-2: 3/45] libtracker-db: Implement writer for new binary journal



commit 3e64d2cb369bc165050ee709c2da7a0fb608b2ec
Author: Philip Van Hoof <philip codeminded be>
Date:   Tue Dec 29 13:47:10 2009 +0100

    libtracker-db: Implement writer for new binary journal

 src/libtracker-common/Makefile.am      |    4 +-
 src/libtracker-common/tracker-crc32.c  |   95 +++++++++
 src/libtracker-common/tracker-crc32.h  |   49 +++++
 src/libtracker-db/tracker-db-journal.c |  339 ++++++++++++++++++++++++++------
 src/libtracker-db/tracker-db-journal.h |   42 +++--
 src/tracker-store/tracker-backup.c     |    4 -
 src/tracker-store/tracker-main.c       |    5 +-
 src/tracker-store/tracker-resources.c  |    4 -
 src/tracker-store/tracker-store.c      |   78 +-------
 src/tracker-store/tracker-store.h      |    4 +-
 10 files changed, 455 insertions(+), 169 deletions(-)
---
diff --git a/src/libtracker-common/Makefile.am b/src/libtracker-common/Makefile.am
index 1bc3019..e85ec86 100644
--- a/src/libtracker-common/Makefile.am
+++ b/src/libtracker-common/Makefile.am
@@ -73,6 +73,7 @@ libtracker_common_la_SOURCES =	 			\
 	tracker-statement-list.c			\
 	tracker-type-utils.c				\
 	tracker-utils.c					\
+	tracker-crc32.c					\
 	libtracker-common.vala.stamp			\
 	$(libtracker_common_la_VALASOURCES:.vala=.c)
 
@@ -97,7 +98,8 @@ libtracker_commoninclude_HEADERS =			\
 	tracker-sparql-builder.h			\
 	tracker-statement-list.h			\
 	tracker-type-utils.h				\
-	tracker-utils.h
+	tracker-utils.h					\
+	tracker-crc32.h
 
 libtracker-common.vala.stamp: $(libtracker_common_la_VALASOURCES) posix.vapi
 	$(AM_V_GEN)$(VALAC) $(GCOV_VALAFLAGS) -C $(VALAFLAGS) --pkg gio-2.0 -H tracker-sparql-builder.h $^
diff --git a/src/libtracker-common/tracker-crc32.c b/src/libtracker-common/tracker-crc32.c
new file mode 100644
index 0000000..82bd3d8
--- /dev/null
+++ b/src/libtracker-common/tracker-crc32.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright © 2002, 2003 Sun Microsystems, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of Sun Microsystems, Inc. nor the names of
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any kind.
+ *
+ * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
+ * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
+ * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
+ * SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR
+ * LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE,
+ * MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES.
+ * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
+ * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
+ * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE
+ * THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE
+ * SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ */
+
+/* $Id$ */
+/* @(#)crc32.c 1.5 03/01/08 SMI */
+
+/*
+ *
+ * @file crc32.c
+ * @brief CRC-32 calculation function
+ * @author Alexander Gelfenbain
+ *
+ */
+
+#include <libtracker-common/tracker-crc32.h>
+
+static const guint32 crcTable[256] = {
+  0x00000000UL, 0x77073096UL, 0xEE0E612CUL, 0x990951BAUL, 0x076DC419UL, 0x706AF48FUL, 0xE963A535UL, 0x9E6495A3UL,
+  0x0EDB8832UL, 0x79DCB8A4UL, 0xE0D5E91EUL, 0x97D2D988UL, 0x09B64C2BUL, 0x7EB17CBDUL, 0xE7B82D07UL, 0x90BF1D91UL,
+  0x1DB71064UL, 0x6AB020F2UL, 0xF3B97148UL, 0x84BE41DEUL, 0x1ADAD47DUL, 0x6DDDE4EBUL, 0xF4D4B551UL, 0x83D385C7UL,
+  0x136C9856UL, 0x646BA8C0UL, 0xFD62F97AUL, 0x8A65C9ECUL, 0x14015C4FUL, 0x63066CD9UL, 0xFA0F3D63UL, 0x8D080DF5UL,
+  0x3B6E20C8UL, 0x4C69105EUL, 0xD56041E4UL, 0xA2677172UL, 0x3C03E4D1UL, 0x4B04D447UL, 0xD20D85FDUL, 0xA50AB56BUL,
+  0x35B5A8FAUL, 0x42B2986CUL, 0xDBBBC9D6UL, 0xACBCF940UL, 0x32D86CE3UL, 0x45DF5C75UL, 0xDCD60DCFUL, 0xABD13D59UL,
+  0x26D930ACUL, 0x51DE003AUL, 0xC8D75180UL, 0xBFD06116UL, 0x21B4F4B5UL, 0x56B3C423UL, 0xCFBA9599UL, 0xB8BDA50FUL,
+  0x2802B89EUL, 0x5F058808UL, 0xC60CD9B2UL, 0xB10BE924UL, 0x2F6F7C87UL, 0x58684C11UL, 0xC1611DABUL, 0xB6662D3DUL,
+  0x76DC4190UL, 0x01DB7106UL, 0x98D220BCUL, 0xEFD5102AUL, 0x71B18589UL, 0x06B6B51FUL, 0x9FBFE4A5UL, 0xE8B8D433UL,
+  0x7807C9A2UL, 0x0F00F934UL, 0x9609A88EUL, 0xE10E9818UL, 0x7F6A0DBBUL, 0x086D3D2DUL, 0x91646C97UL, 0xE6635C01UL,
+  0x6B6B51F4UL, 0x1C6C6162UL, 0x856530D8UL, 0xF262004EUL, 0x6C0695EDUL, 0x1B01A57BUL, 0x8208F4C1UL, 0xF50FC457UL,
+  0x65B0D9C6UL, 0x12B7E950UL, 0x8BBEB8EAUL, 0xFCB9887CUL, 0x62DD1DDFUL, 0x15DA2D49UL, 0x8CD37CF3UL, 0xFBD44C65UL,
+  0x4DB26158UL, 0x3AB551CEUL, 0xA3BC0074UL, 0xD4BB30E2UL, 0x4ADFA541UL, 0x3DD895D7UL, 0xA4D1C46DUL, 0xD3D6F4FBUL,
+  0x4369E96AUL, 0x346ED9FCUL, 0xAD678846UL, 0xDA60B8D0UL, 0x44042D73UL, 0x33031DE5UL, 0xAA0A4C5FUL, 0xDD0D7CC9UL,
+  0x5005713CUL, 0x270241AAUL, 0xBE0B1010UL, 0xC90C2086UL, 0x5768B525UL, 0x206F85B3UL, 0xB966D409UL, 0xCE61E49FUL,
+  0x5EDEF90EUL, 0x29D9C998UL, 0xB0D09822UL, 0xC7D7A8B4UL, 0x59B33D17UL, 0x2EB40D81UL, 0xB7BD5C3BUL, 0xC0BA6CADUL,
+  0xEDB88320UL, 0x9ABFB3B6UL, 0x03B6E20CUL, 0x74B1D29AUL, 0xEAD54739UL, 0x9DD277AFUL, 0x04DB2615UL, 0x73DC1683UL,
+  0xE3630B12UL, 0x94643B84UL, 0x0D6D6A3EUL, 0x7A6A5AA8UL, 0xE40ECF0BUL, 0x9309FF9DUL, 0x0A00AE27UL, 0x7D079EB1UL,
+  0xF00F9344UL, 0x8708A3D2UL, 0x1E01F268UL, 0x6906C2FEUL, 0xF762575DUL, 0x806567CBUL, 0x196C3671UL, 0x6E6B06E7UL,
+  0xFED41B76UL, 0x89D32BE0UL, 0x10DA7A5AUL, 0x67DD4ACCUL, 0xF9B9DF6FUL, 0x8EBEEFF9UL, 0x17B7BE43UL, 0x60B08ED5UL,
+  0xD6D6A3E8UL, 0xA1D1937EUL, 0x38D8C2C4UL, 0x4FDFF252UL, 0xD1BB67F1UL, 0xA6BC5767UL, 0x3FB506DDUL, 0x48B2364BUL,
+  0xD80D2BDAUL, 0xAF0A1B4CUL, 0x36034AF6UL, 0x41047A60UL, 0xDF60EFC3UL, 0xA867DF55UL, 0x316E8EEFUL, 0x4669BE79UL,
+  0xCB61B38CUL, 0xBC66831AUL, 0x256FD2A0UL, 0x5268E236UL, 0xCC0C7795UL, 0xBB0B4703UL, 0x220216B9UL, 0x5505262FUL,
+  0xC5BA3BBEUL, 0xB2BD0B28UL, 0x2BB45A92UL, 0x5CB36A04UL, 0xC2D7FFA7UL, 0xB5D0CF31UL, 0x2CD99E8BUL, 0x5BDEAE1DUL,
+  0x9B64C2B0UL, 0xEC63F226UL, 0x756AA39CUL, 0x026D930AUL, 0x9C0906A9UL, 0xEB0E363FUL, 0x72076785UL, 0x05005713UL,
+  0x95BF4A82UL, 0xE2B87A14UL, 0x7BB12BAEUL, 0x0CB61B38UL, 0x92D28E9BUL, 0xE5D5BE0DUL, 0x7CDCEFB7UL, 0x0BDBDF21UL,
+  0x86D3D2D4UL, 0xF1D4E242UL, 0x68DDB3F8UL, 0x1FDA836EUL, 0x81BE16CDUL, 0xF6B9265BUL, 0x6FB077E1UL, 0x18B74777UL,
+  0x88085AE6UL, 0xFF0F6A70UL, 0x66063BCAUL, 0x11010B5CUL, 0x8F659EFFUL, 0xF862AE69UL, 0x616BFFD3UL, 0x166CCF45UL,
+  0xA00AE278UL, 0xD70DD2EEUL, 0x4E048354UL, 0x3903B3C2UL, 0xA7672661UL, 0xD06016F7UL, 0x4969474DUL, 0x3E6E77DBUL,
+  0xAED16A4AUL, 0xD9D65ADCUL, 0x40DF0B66UL, 0x37D83BF0UL, 0xA9BCAE53UL, 0xDEBB9EC5UL, 0x47B2CF7FUL, 0x30B5FFE9UL,
+  0xBDBDF21CUL, 0xCABAC28AUL, 0x53B39330UL, 0x24B4A3A6UL, 0xBAD03605UL, 0xCDD70693UL, 0x54DE5729UL, 0x23D967BFUL,
+  0xB3667A2EUL, 0xC4614AB8UL, 0x5D681B02UL, 0x2A6F2B94UL, 0xB40BBE37UL, 0xC30C8EA1UL, 0x5A05DF1BUL, 0x2D02EF8DUL
+};
+
+guint32
+tracker_crc32 (gconstpointer ptr, gsize len)
+{
+  guint32 crc = 0xFFFFFFFF;
+  const guint8 *bp = (const guint8 *) ptr;
+  size_t i;
+
+  for (i=0; i<len; i++)
+    crc = crcTable[(crc ^ bp[i]) & 0xFF] ^ (crc >> 8);
+
+  return crc ^ 0xFFFFFFFF;
+}
diff --git a/src/libtracker-common/tracker-crc32.h b/src/libtracker-common/tracker-crc32.h
new file mode 100644
index 0000000..973942e
--- /dev/null
+++ b/src/libtracker-common/tracker-crc32.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2002, 2003 Sun Microsystems, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of Sun Microsystems, Inc. nor the names of
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * This software is provided "AS IS," without a warranty of any kind.
+ *
+ * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
+ * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
+ * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
+ * SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR
+ * LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE,
+ * MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES.
+ * IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
+ * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
+ * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE
+ * THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE
+ * SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+ *
+ */
+
+/* $Id$ */
+/* @(#)crc32.h 1.6 03/01/08 SMI */
+
+/*
+ *
+ * @file crc32.h
+ * @brief CRC-32 calculation function
+ * @author Alexander Gelfenbain
+ *
+ */
+
+#include <glib.h>
+
+guint32 tracker_crc32 (gconstpointer ptr, gsize len);
diff --git a/src/libtracker-db/tracker-db-journal.c b/src/libtracker-db/tracker-db-journal.c
index bec8d8c..ae1d3c3 100644
--- a/src/libtracker-db/tracker-db-journal.c
+++ b/src/libtracker-db/tracker-db-journal.c
@@ -18,6 +18,7 @@
  *
  * Author: Philip Van Hoof <philip codeminded be>
  */
+
 #include "config.h"
 
 #define _GNU_SOURCE
@@ -29,14 +30,21 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <libtracker-common/tracker-crc32.h>
+
 #include "tracker-db-journal.h"
 
 static gchar *filename = NULL;
 static FILE *journal = NULL;
-static GMappedFile *mapped = NULL;
 static gsize current_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;
 
-#define TRACKER_DB_JOURNAL_LOG_FILENAME                 "log.sparql.txt"
+#define TRACKER_DB_JOURNAL_LOG_FILENAME  "tracker-store.journal"
+#define MIN_BLOCK_SIZE                   1024
 
 static void
 get_filename (void)
@@ -63,107 +71,312 @@ tracker_db_journal_filename (void)
 	return (const gchar *) filename;
 }
 
+static void
+kill_cur_block (void)
+{
+	cur_block_len = 0;
+	cur_pos = 0;
+	cur_entry_amount = 0;
+	cur_block_alloc = 0;
+	g_free (cur_block);
+	cur_block = NULL;
+}
+
+static gint
+nearest_pow (gint num)
+{
+	gint n = 1;
+	while (n < num)
+		n <<= 1;
+	return n;
+}
+
+static void
+cur_block_maybe_expand (guint len)
+{
+	guint want_alloc = cur_block_len + len;
+
+	if (want_alloc > 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;
+	}
+}
+
 void
-tracker_db_journal_open (void)
+tracker_db_journal_open (const gchar *filen)
 {
 	struct stat st;
 
-	get_filename ();
+	if (!filen) {
+		get_filename ();
+	} else {
+		filename = g_strdup (filen);
+	}
 
 	journal = fopen (filename, "a");
 
 	if (stat (filename, &st) == 0) {
 		current_size = (gsize) st.st_size;
 	}
+
+	if (current_size == 0) {
+		g_assert (cur_block_len == 0);
+		g_assert (cur_block_alloc == 0);
+		g_assert (cur_block == NULL);
+		g_assert (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';
+
+		write (fileno (journal), cur_block, 8);
+
+		current_size += 8;
+
+		kill_cur_block ();
+	}
 }
 
 void
-tracker_db_journal_log (const gchar *query)
+tracker_db_journal_start_transaction (void)
 {
-	if (journal) {
-		size_t len = strlen (query);
-		write (fileno (journal), query, len);
-		write (fileno (journal), "\n\0", 2);
-		current_size += (len + 2);
-	}
+	guint size = sizeof (guint32) * 3;
+
+	cur_block_maybe_expand (size);
+
+	/* 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);
+
+	cur_pos = cur_block_len = size;
+	cur_entry_amount = 0;
 }
 
-void 
-tracker_db_journal_fsync (void)
+static void
+cur_setnum (gchar   *dest,
+            guint   *pos,
+            guint32  val)
 {
-	if (journal) {
-		fsync (fileno (journal));
-	}
+	memset (dest + (*pos)++, val >> 24 & 0xff, 1);
+	memset (dest + (*pos)++, val >> 16 & 0xff, 1);
+	memset (dest + (*pos)++, val >>  8 & 0xff, 1);
+	memset (dest + (*pos)++, val >>  0 & 0xff, 1);
+}
+
+static void
+cur_setstr (gchar       *dest,
+            guint       *pos,
+            const gchar *str,
+            gsize        len)
+{
+	memcpy (dest + *pos, str, len);
+	(*pos) += len;
+	memset (dest + (*pos)++, 0 & 0xff, 1);
 }
 
 void
-tracker_db_journal_truncate (void)
+tracker_db_journal_append_delete_statement (guint32      s_code,
+                                            guint32      p_code,
+                                            const gchar *object)
 {
-	if (journal) {
-		ftruncate(fileno (journal), 0);
-		current_size = 0;
-		fsync (fileno (journal));
-	}
+	gint o_len = strlen (object);
+	gchar data_format = 0x04;
+	gint size = (sizeof (guint32) * 3) + o_len + 1;
+
+	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_entry_amount++;
+	cur_block_len += size;
 }
 
+
 void
-tracker_db_journal_close (void)
+tracker_db_journal_append_delete_statement_code (guint32 s_code,
+                                                 guint32 p_code,
+                                                 guint32 o_code)
 {
-	if (journal) {
-		fclose (journal);
-		journal = NULL;
-	}
+	gchar data_format = 0x06;
+	gint size = sizeof (guint32) * 4;
 
-	g_free (filename);
-	filename = NULL;
+	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_entry_amount++;
+	cur_block_len += size;
 }
 
-TrackerJournalContents*
-tracker_db_journal_get_contents (guint transaction_size)
+void
+tracker_db_journal_append_insert_statement (guint32      s_code, 
+                                            guint32      p_code, 
+                                            const gchar *object)
 {
-	GPtrArray *lines;
-	gsize max_pos, next_len;
-	gchar *cur;
+	gint o_len = strlen (object);
+	gchar data_format = 0x00;
+	gint size = (sizeof (guint32) * 3) + o_len + 1;
 
-	get_filename ();
+	cur_block_maybe_expand (size);
 
-	if (!mapped) {
-		GError *error = NULL;
+	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);
 
-		mapped = g_mapped_file_new (filename, FALSE, &error);
+	cur_entry_amount++;
+	cur_block_len += size;
+}
 
-		if (error) {
-			g_clear_error (&error);
-			mapped = NULL;
-			return NULL;
-		}
-	}
+void
+tracker_db_journal_append_insert_statement_code (guint32 s_code,
+                                                 guint32 p_code,
+                                                 guint32 o_code)
+{
+	gchar data_format = 0x02;
+	gint size = sizeof (guint32) * 4;
 
-	lines = g_ptr_array_sized_new (transaction_size > 0 ? transaction_size : 2000);
+	cur_block_maybe_expand (size);
 
-	cur = g_mapped_file_get_contents (mapped);
-	max_pos = (gsize) (cur + g_mapped_file_get_length (mapped));
+	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);
 
-	while (((gsize)cur) < max_pos) {
-		next_len = strnlen (cur, max_pos - ((gsize)cur)) + 1;
-		g_ptr_array_add (lines, cur);
-		cur += next_len;
-	}
+	cur_entry_amount++;
+	cur_block_len += size;
+}
+
+void
+tracker_db_journal_append_resource (guint32      s_code,
+                                    const gchar *uri)
+{
+	gint o_len = strlen (uri);
+	gchar data_format = 0x01;
+	gint size = (sizeof (guint32) * 2) + o_len + 1;
 
-	return (TrackerJournalContents *) lines;
+	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_entry_amount++;
+	cur_block_len += size;
+}
+
+void
+tracker_db_journal_rollback_transaction (void)
+{
+	kill_cur_block ();
+}
+
+void
+tracker_db_journal_commit_transaction (void)
+{
+	guint32 crc;
+	guint begin_pos = 0;
+	guint size = sizeof (guint32);
+	guint offset = sizeof(guint32) * 3;
+
+	g_assert (journal);
+
+	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 (cur_block, &cur_pos, cur_block_len);
+
+	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);
+
+	write (fileno (journal), cur_block, cur_block_len);
+
+	current_size += cur_block_len;
+
+	kill_cur_block ();
 }
 
 void 
-tracker_db_journal_free_contents (TrackerJournalContents *contents)
+tracker_db_journal_fsync (void)
 {
-	if (mapped) {
-#if GLIB_CHECK_VERSION(2,22,0)
-		g_mapped_file_unref (mapped);
-#else
-		g_mapped_file_free (mapped);
-#endif
-		mapped = NULL;
-	}
+	g_assert (journal);
 
-	g_ptr_array_free ((GPtrArray *)contents, TRUE);
+	fsync (fileno (journal));
+}
+
+void
+tracker_db_journal_close (void)
+{
+	g_assert (journal);
+
+	fclose (journal);
+	journal = NULL;
+
+	g_free (filename);
+	filename = NULL;
 }
+
+#ifdef TEST
+
+#include "libtracker-common/tracker-crc32.c"
+
+int main () {
+	guint i;
+
+	tracker_db_journal_open ("/tmp/test");
+
+	tracker_db_journal_start_transaction ();
+	tracker_db_journal_append_resource (10, "http://resource";);
+	tracker_db_journal_append_resource (11, "http://predicate";);
+	tracker_db_journal_append_delete_statement (10, 11, "test");
+	tracker_db_journal_commit_transaction ();
+
+	tracker_db_journal_start_transaction ();
+	tracker_db_journal_append_resource (12, "http://resource";);
+	tracker_db_journal_append_resource (13, "http://predicate";);
+	tracker_db_journal_append_resource (14, "http://resource";);
+	tracker_db_journal_append_delete_statement_code (12, 13, 14);
+	tracker_db_journal_commit_transaction ();
+
+
+	tracker_db_journal_start_transaction ();
+	tracker_db_journal_append_resource (15, "http://resource";);
+	tracker_db_journal_append_resource (16, "http://predicate";);
+	tracker_db_journal_append_insert_statement (15, 16, "test");
+	tracker_db_journal_commit_transaction ();
+
+	tracker_db_journal_start_transaction ();
+	tracker_db_journal_append_resource (17, "http://resource";);
+	tracker_db_journal_append_resource (18, "http://predicate";);
+	tracker_db_journal_append_resource (19, "http://resource";);
+	tracker_db_journal_append_insert_statement_code (17, 18, 19);
+	tracker_db_journal_commit_transaction ();
+
+	tracker_db_journal_fsync ();
+	tracker_db_journal_close ();
+	return 0;
+}
+#endif
diff --git a/src/libtracker-db/tracker-db-journal.h b/src/libtracker-db/tracker-db-journal.h
index 0400321..b28bd08 100644
--- a/src/libtracker-db/tracker-db-journal.h
+++ b/src/libtracker-db/tracker-db-journal.h
@@ -27,23 +27,31 @@
 
 G_BEGIN_DECLS
 
-#if !defined (__LIBTRACKER_DB_INSIDE__) && !defined (TRACKER_COMPILATION)
-#error "only <libtracker-db/tracker-db.h> must be included directly."
-#endif
-
-#define TRACKER_DB_JOURNAL_MAX_SIZE 52428800
-
-typedef GPtrArray TrackerJournalContents;
-
-const gchar*            tracker_db_journal_filename      (void);
-void                    tracker_db_journal_open          (void);
-void                    tracker_db_journal_log           (const gchar            *query);
-void                    tracker_db_journal_truncate      (void);
-void                    tracker_db_journal_close         (void);
-TrackerJournalContents* tracker_db_journal_get_contents  (guint                   transaction_size);
-void                    tracker_db_journal_free_contents (TrackerJournalContents *contents);
-void                    tracker_db_journal_fsync         (void);
-gsize                   tracker_db_journal_get_size      (void);
+const gchar* tracker_db_journal_filename                     (void);
+void         tracker_db_journal_open                         (const gchar *filen);
+
+void         tracker_db_journal_start_transaction            (void);
+void         tracker_db_journal_append_delete_statement      (guint32      s_code,
+                                                              guint32      p_code,
+                                                              const gchar *object);
+void         tracker_db_journal_append_delete_statement_code (guint32      s_code,
+                                                              guint32      p_code,
+                                                              guint32      o_code);
+void         tracker_db_journal_append_insert_statement      (guint32      s_code, 
+                                                              guint32      p_code, 
+                                                              const gchar *object);
+void         tracker_db_journal_append_insert_statement_code (guint32      s_code,
+                                                              guint32      p_code,
+                                                              guint32      o_code);
+void         tracker_db_journal_append_resource              (guint32      s_code,
+                                                              const gchar *uri);
+
+void         tracker_db_journal_rollback_transaction         (void);
+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);
 
 G_END_DECLS
 
diff --git a/src/tracker-store/tracker-backup.c b/src/tracker-store/tracker-backup.c
index b9d7f45..b677e3f 100644
--- a/src/tracker-store/tracker-backup.c
+++ b/src/tracker-store/tracker-backup.c
@@ -85,10 +85,6 @@ backup_callback (GError *error, gpointer user_data)
 		return;
 	}
 
-	if (info->play_journal) {
-		tracker_store_play_journal ();
-	}
-
 	dbus_g_method_return (info->context);
 
 	tracker_dbus_request_success (info->request_id);
diff --git a/src/tracker-store/tracker-main.c b/src/tracker-store/tracker-main.c
index 5271c4e..6af9255 100644
--- a/src/tracker-store/tracker-main.c
+++ b/src/tracker-store/tracker-main.c
@@ -435,7 +435,10 @@ main (gint argc, gchar *argv[])
 		return EXIT_FAILURE;
 	}
 
-	tracker_store_init (need_journal);
+	/* TODO binary-log: need_journal might contain whether the db was corrupt
+	 * or not Do something with that.*/
+
+	tracker_store_init ();
 
 	if (private->shutdown) {
 		goto shutdown;
diff --git a/src/tracker-store/tracker-resources.c b/src/tracker-store/tracker-resources.c
index 4a61559..4cd564c 100644
--- a/src/tracker-store/tracker-resources.c
+++ b/src/tracker-store/tracker-resources.c
@@ -418,10 +418,6 @@ on_statements_committed (gpointer user_data)
 
 	priv = TRACKER_RESOURCES_GET_PRIVATE (resources);
 
-	/* For more information about this call, look at the function end_batch
-	 * of tracker-store.c */
-	tracker_store_flush_journal ();
-
 	/* Class signals feature */
 	events = tracker_events_get_pending ();
 
diff --git a/src/tracker-store/tracker-store.c b/src/tracker-store/tracker-store.c
index 6d47de0..2af72d0 100644
--- a/src/tracker-store/tracker-store.c
+++ b/src/tracker-store/tracker-store.c
@@ -26,9 +26,7 @@
 
 #include <libtracker-common/tracker-dbus.h>
 #include <libtracker-db/tracker-db-dbus.h>
-#include <libtracker-db/tracker-db-journal.h>
 #include <libtracker-db/tracker-db-interface-sqlite.h>
-#include <libtracker-db/tracker-db-journal.h>
 
 #include <libtracker-data/tracker-data-update.h>
 #include <libtracker-data/tracker-data-query.h>
@@ -36,7 +34,7 @@
 
 #include "tracker-store.h"
 
-#define TRACKER_STORE_TRANSACTION_MAX                   4000            /* At commit is journal fsynced too */
+#define TRACKER_STORE_TRANSACTION_MAX                   4000
 
 typedef struct {
 	gboolean  have_handler, have_sync_handler;
@@ -157,21 +155,11 @@ end_batch (TrackerStorePrivate *private)
 		/* commit pending batch items */
 		tracker_data_commit_transaction ();
 
-		/* The on_statements_committed in tracker-resources.c performs
-		 * the flush on the journal, I can only register one callback
-		 * for this atm, so that's why it's called over there as a
-		 * tracker_store_flush_journal */
-
 		private->batch_mode = FALSE;
 		private->batch_count = 0;
 	}
 }
 
-static void
-log_to_journal (TrackerStorePrivate *private, const gchar *query)
-{
-	tracker_db_journal_log (query);
-}
 
 static gboolean
 queue_idle_handler (gpointer user_data)
@@ -189,10 +177,6 @@ queue_idle_handler (gpointer user_data)
 
 		tracker_data_update_sparql (task->data.update.query, &error);
 
-		if (private->start_log) {
-			log_to_journal (private, task->data.update.query);
-		}
-
 		if (!error) {
 			private->batch_count++;
 			if (private->batch_count >= TRACKER_STORE_TRANSACTION_MAX) {
@@ -282,54 +266,9 @@ queue_idle_destroy (gpointer user_data)
 	private->have_handler = FALSE;
 }
 
-void
-tracker_store_flush_journal (void)
-{
-	TrackerStorePrivate *private;
-
-	private = g_static_private_get (&private_key);
-	g_return_if_fail (private != NULL);
-
-	tracker_db_journal_fsync ();
-}
-
-static void
-internal_play_journal (TrackerStorePrivate *private)
-{
-	TrackerJournalContents *lines;
-
-	lines = tracker_db_journal_get_contents (TRACKER_STORE_TRANSACTION_MAX);
-
-	if (lines) {
-		guint i;
-
-		tracker_data_begin_transaction ();
-		private->start_log = FALSE;
-		for (i = 0; i < lines->len; i++) {
-			const gchar *line = g_ptr_array_index (lines, i);
-			tracker_store_sparql_update (line, NULL);
-		}
-		tracker_db_journal_truncate ();
-		private->start_log = TRUE;
-		tracker_data_commit_transaction ();
-
-		tracker_db_journal_free_contents (lines);
-	}
-}
-
-void
-tracker_store_play_journal (void)
-{
-	TrackerStorePrivate *private;
-
-	private = g_static_private_get (&private_key);
-	g_return_if_fail (private != NULL);
-
-	internal_play_journal (private);
-}
 
 void
-tracker_store_init (gboolean load_journal)
+tracker_store_init (void)
 {
 	TrackerStorePrivate *private;
 
@@ -341,11 +280,6 @@ tracker_store_init (gboolean load_journal)
 	                      private,
 	                      private_free);
 
-	if (load_journal) {
-		internal_play_journal (private);
-	}
-
-	tracker_db_journal_open ();
 }
 
 void
@@ -488,10 +422,6 @@ tracker_store_sparql_update (const gchar *sparql,
 	tracker_data_update_sparql (sparql, error);
 	tracker_data_commit_transaction ();
 
-	if (private->start_log) {
-		log_to_journal (private, sparql);
-	}
-
 }
 
 GPtrArray *
@@ -517,10 +447,6 @@ tracker_store_sparql_update_blank (const gchar *sparql,
 	blank_nodes = tracker_data_update_sparql_blank (sparql, error);
 	tracker_data_commit_transaction ();
 
-	if (private->start_log) {
-		log_to_journal (private, sparql);
-	}
-
 	return blank_nodes;
 }
 
diff --git a/src/tracker-store/tracker-store.h b/src/tracker-store/tracker-store.h
index 1ad95f2..e2eb10c 100644
--- a/src/tracker-store/tracker-store.h
+++ b/src/tracker-store/tracker-store.h
@@ -35,7 +35,7 @@ typedef void (* TrackerStoreCommitCallback)       (gpointer         user_data);
 typedef void (* TrackerStoreTurtleCallback)       (GError          *error,
                                                    gpointer         user_data);
 
-void         tracker_store_init                   (gboolean load_journal);
+void         tracker_store_init                   (void);
 void         tracker_store_shutdown               (void);
 void         tracker_store_queue_commit           (TrackerStoreCommitCallback callback,
                                                    const gchar   *client_id,
@@ -60,8 +60,6 @@ tracker_store_sparql_query           (const gchar   *sparql,
 
 guint        tracker_store_get_queue_size         (void);
 
-void         tracker_store_play_journal           (void);
-void         tracker_store_flush_journal          (void);
 void         tracker_store_unreg_batches          (const gchar   *client_id);
 
 G_END_DECLS



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