Rev 3659: Start working on a compiled function for transforming in http://bzr.arbash-meinel.com/branches/bzr/1.7-dev/btree

John Arbash Meinel john at arbash-meinel.com
Thu Aug 21 21:22:09 BST 2008


At http://bzr.arbash-meinel.com/branches/bzr/1.7-dev/btree

------------------------------------------------------------
revno: 3659
revision-id: john at arbash-meinel.com-20080821202206-nvbybzwmfvagg4jm
parent: john at arbash-meinel.com-20080821195703-ze7jwvq1r2rbgmr3
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: btree
timestamp: Thu 2008-08-21 15:22:06 -0500
message:
  Start working on a compiled function for transforming
  a node => serialized key and line.
modified:
  bzrlib/_parse_btree_c.pyx      _parse_btree_c.pyx-20080703034413-3q25bklkenti3p8p-2
  bzrlib/_parse_btree_py.py      _parse_btree_py.py-20080703034413-3q25bklkenti3p8p-3
  bzrlib/tests/test_btree_index.py test_index.py-20080624222253-p0x5f92uyh5hw734-13
-------------- next part --------------
=== modified file 'bzrlib/_parse_btree_c.pyx'
--- a/bzrlib/_parse_btree_c.pyx	2008-08-20 16:49:05 +0000
+++ b/bzrlib/_parse_btree_c.pyx	2008-08-21 20:22:06 +0000
@@ -238,3 +238,28 @@
 def _parse_leaf_lines(bytes, key_length, ref_list_length):
     parser = BTreeLeafParser(bytes, key_length, ref_list_length)
     return parser.parse()
+
+
+def _flatten_node(node, reference_lists):
+    """Convert a node into the serialized form.
+
+    :param node: A tuple representing a node:
+        (index, key_tuple, value, references)
+    :param reference_lists: Does this index have reference lists?
+    :return: (string_key, flattened)
+        string_key  The serialized key for referencing this node
+        flattened   A string with the serialized form for the contents
+    """
+    # TODO: instead of using string joins, precompute the final string length,
+    #       and then malloc a single string and copy everything in.
+    flattened_references = []
+    if reference_lists:
+        for ref_list in node[3]:
+            ref_keys = []
+            for reference in ref_list:
+                ref_keys.append('\x00'.join(reference))
+            flattened_references.append('\r'.join(ref_keys))
+    string_key = '\x00'.join(node[1])
+    line = ("%s\x00%s\x00%s\n" % (string_key,
+        '\t'.join(flattened_references), node[2]))
+    return string_key, line

=== modified file 'bzrlib/_parse_btree_py.py'
--- a/bzrlib/_parse_btree_py.py	2008-08-19 23:12:01 +0000
+++ b/bzrlib/_parse_btree_py.py	2008-08-21 20:22:06 +0000
@@ -40,3 +40,27 @@
             node_value = (value, ())
         nodes.append((key, node_value))
     return nodes
+
+
+def _flatten_node(node, reference_lists):
+    """Convert a node into the serialized form.
+
+    :param node: A tuple representing a node (key_tuple, value, references)
+    :param reference_lists: Does this index have reference lists?
+    :return: (string_key, flattened)
+        string_key  The serialized key for referencing this node
+        flattened   A string with the serialized form for the contents
+    """
+    if reference_lists:
+        # TODO: Consider turning this back into the 'unoptimized' nested loop
+        #       form. It is probably more obvious for most people, and this is
+        #       just a reference implementation.
+        flattened_references = ['\r'.join(['\x00'.join(reference)
+                                           for reference in ref_list])
+                                for ref_list in node[3]]
+    else:
+        flattened_references = []
+    string_key = '\x00'.join(node[1])
+    line = ("%s\x00%s\x00%s\n" % (string_key,
+        '\t'.join(flattened_references), node[2]))
+    return string_key, line

=== modified file 'bzrlib/tests/test_btree_index.py'
--- a/bzrlib/tests/test_btree_index.py	2008-08-21 19:53:53 +0000
+++ b/bzrlib/tests/test_btree_index.py	2008-08-21 20:22:06 +0000
@@ -913,6 +913,16 @@
             ('11', '44'): ('value:4', ((), (('11', 'ref00'),)))
             }, node.keys)
 
+    def assertFlattened(self, expected, key, value, refs, reference_lists):
+        flat_key, flat_line = self.parse_btree._flatten_node(
+            (None, key, value, refs), reference_lists)
+        self.assertEqual('\x00'.join(key), flat_key)
+        self.assertEqual(expected, flat_line)
+
+    def test_flatten_node_to_line_no_references(self):
+        self.assertFlattened('key\x00\x00value\n',
+                             ('key',), 'value', [], False)
+
 
 class TestCompiledBtree(tests.TestCase):
 



More information about the bazaar-commits mailing list