[PATCH] sort lots and entries on save

Jim Radford radford at blackbean.org
Mon Sep 15 14:35:56 EDT 2008


The following patch sorts the lots and entries before saving them to
the gnucash XML file.

This is an attempt to make saves more idempotent thereby facilitating
the use of a revision control system on the gnucash XML files.

With this patch most of the needless and seemingly random churn is
gone and I can add and remove a transation and expect there to be no
changes to the gnucash file and I can add a transaction and expect
that addition to be the only change in the file.

I'm curious to see if this patch affects save performance noticeably
for anyone.  I don't have any large files to test with.

-Jim

diff -u gnucash-2.2.4/src/backend/file/gnc-account-xml-v2.c gnucash-2.2.4-sorted/src/backend/file/gnc-account-xml-v2.c
--- gnucash-2.2.4/src/backend/file/gnc-account-xml-v2.c	2008-03-02 05:24:27.000000000 -0800
+++ gnucash-2.2.4-sorted/src/backend/file/gnc-account-xml-v2.c	2008-04-23 15:06:13.000000000 -0700
@@ -139,6 +139,8 @@
     {
        xmlNodePtr toaddto = xmlNewChild(ret, NULL, BAD_CAST act_lots_string, NULL);
 
+       lots = g_list_sort(lots, qof_instance_guid_compare);
+
        for (n = lots; n; n=n->next)
        {
           GNCLot * lot = n->data;
--- gnucash-2.2.5/src/business/business-core/file/gnc-entry-xml-v2.c	2008-01-07 17:05:59.000000000 -0800
+++ gnucash-2.2.5/src/business/business-core/file/gnc-entry-xml-v2.c~	2008-07-01 17:41:19.000000000 -0700
@@ -814,9 +814,27 @@
 }
 
+static void
+do_append (QofInstance *qof_p, gpointer list_p)
+{
+  GList **list = list_p;
+  *list = g_list_append(*list, qof_p);
+}
+
+static void
+qof_object_foreach_sorted (QofIdTypeConst type_name, QofBook *book, QofInstanceForeachCB cb, gpointer user_data)
+{
+  GList *list = NULL, *n, *l;
+  qof_object_foreach(type_name, book, do_append, &list);
+  list = g_list_sort(list, qof_instance_guid_compare);
+  for (l = list; l; l = l->next)
+    cb(l->data, user_data);
+  g_list_free(list);
+}
+
 static void
 entry_write (FILE *out, QofBook *book)
 {
-  qof_object_foreach (_GNC_MOD_NAME, book, xml_add_entry, (gpointer) out);
+  qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_entry, (gpointer) out);
 }
 
 static void


More information about the gnucash-devel mailing list