~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/index.py

  • Committer: Robert Collins
  • Date: 2007-07-12 16:49:54 UTC
  • mto: (2592.5.3 pack-repository)
  • mto: This revision was merged to the branch mainline in revision 2624.
  • Revision ID: robertc@robertcollins.net-20070712164954-gtnnmqd5oyfob20t
Handle basic node adds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Indexing facilities."""
18
18
 
19
19
from cStringIO import StringIO
 
20
import re
20
21
 
21
22
from bzrlib import errors
22
23
 
24
25
_SIGNATURE = "Bazaar Graph Index 1\n"
25
26
 
26
27
 
 
28
_whitespace_re = re.compile('[\t\n\x0b\x0c\r ]')
 
29
_newline_null_re = re.compile('[\n\0]')
 
30
 
 
31
 
27
32
class GraphIndexBuilder(object):
28
33
    """A builder that can build a GraphIndex."""
29
34
 
34
39
            entry.
35
40
        """
36
41
        self.reference_lists = reference_lists
 
42
        self._nodes = []
 
43
 
 
44
    def add_node(self, key, references, value):
 
45
        """Add a node to the index.
 
46
 
 
47
        :param key: The key. keys must be whitespace free utf8.
 
48
        :param references: An iterable of iterables of keys. Each is a
 
49
            reference to another key.
 
50
        :param value: The value to associate with the key. It may be any
 
51
            bytes as long as it does not contain \0 or \n.
 
52
        """
 
53
        if _whitespace_re.search(key) is not None:
 
54
            raise errors.BadIndexKey(key)
 
55
        if _newline_null_re.search(value) is not None:
 
56
            raise errors.BadIndexValue(value)
 
57
        self._nodes.append((key, references, value))
37
58
 
38
59
    def finish(self):
39
60
        lines = [_SIGNATURE]
40
61
        lines.append(_OPTION_NODE_REFS + str(self.reference_lists) + '\n')
 
62
        for key, references, value in self._nodes:
 
63
            flattened_references = ''
 
64
            lines.append("%s\0%s\0%s\n" % (key, flattened_references, value))
41
65
        lines.append('\n')
42
66
        return StringIO(''.join(lines))
43
67
 
48
72
    The index maps keys to a list of key reference lists, and a value.
49
73
    Each node has the same number of key reference lists. Each key reference
50
74
    list can be empty or an arbitrary length. The value is an opaque NULL
51
 
    terminated string.
 
75
    terminated string without any newlines.
52
76
    """
53
77
 
54
78
    def __init__(self, transport, name):