/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/_chk_map_py.py

  • Committer: Martin
  • Date: 2017-06-11 14:36:07 UTC
  • mto: This revision was merged to the branch mainline in revision 6688.
  • Revision ID: gzlist@googlemail.com-20170611143607-iuusrtbtuvgzrcho
Make test__rio pass on Python 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Python implementation of _search_key functions, etc."""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
import zlib
20
22
import struct
21
23
 
22
 
from bzrlib.static_tuple import StaticTuple
 
24
from .sixish import bytesintern
 
25
from .static_tuple import StaticTuple
23
26
 
24
27
_LeafNode = None
25
28
_InternalNode = None
42
45
 
43
46
def _search_key_16(key):
44
47
    """Map the key tuple into a search key string which has 16-way fan out."""
45
 
    return '\x00'.join(['%08X' % _crc32(bit) for bit in key])
 
48
    return b'\x00'.join([b'%08X' % _crc32(bit) for bit in key])
46
49
 
47
50
 
48
51
def _search_key_255(key):
51
54
    We use 255-way because '\n' is used as a delimiter, and causes problems
52
55
    while parsing.
53
56
    """
54
 
    bytes = '\x00'.join([struct.pack('>L', _crc32(bit)) for bit in key])
55
 
    return bytes.replace('\n', '_')
56
 
 
57
 
 
58
 
def _deserialise_leaf_node(bytes, key, search_key_func=None):
 
57
    data = b'\x00'.join([struct.pack('>L', _crc32(bit)) for bit in key])
 
58
    return data.replace(b'\n', b'_')
 
59
 
 
60
 
 
61
def _deserialise_leaf_node(data, key, search_key_func=None):
59
62
    """Deserialise bytes, with key key, into a LeafNode.
60
63
 
61
64
    :param bytes: The bytes of the node.
63
66
    """
64
67
    global _unknown, _LeafNode, _InternalNode
65
68
    if _LeafNode is None:
66
 
        from bzrlib import chk_map
 
69
        from breezy import chk_map
67
70
        _unknown = chk_map._unknown
68
71
        _LeafNode = chk_map.LeafNode
69
72
        _InternalNode = chk_map.InternalNode
70
73
    result = _LeafNode(search_key_func=search_key_func)
71
74
    # Splitlines can split on '\r' so don't use it, split('\n') adds an
72
75
    # extra '' if the bytes ends in a final newline.
73
 
    lines = bytes.split('\n')
 
76
    lines = data.split(b'\n')
74
77
    trailing = lines.pop()
75
 
    if trailing != '':
 
78
    if trailing != b'':
76
79
        raise AssertionError('We did not have a final newline for %s'
77
80
                             % (key,))
78
81
    items = {}
79
 
    if lines[0] != 'chkleaf:':
 
82
    if lines[0] != b'chkleaf:':
80
83
        raise ValueError("not a serialised leaf node: %r" % bytes)
81
84
    maximum_size = int(lines[1])
82
85
    width = int(lines[2])
85
88
    pos = 5
86
89
    while pos < len(lines):
87
90
        line = prefix + lines[pos]
88
 
        elements = line.split('\x00')
 
91
        elements = line.split(b'\x00')
89
92
        pos += 1
90
93
        if len(elements) != width + 1:
91
94
            raise AssertionError(
94
97
        num_value_lines = int(elements[-1])
95
98
        value_lines = lines[pos:pos+num_value_lines]
96
99
        pos += num_value_lines
97
 
        value = '\n'.join(value_lines)
 
100
        value = b'\n'.join(value_lines)
98
101
        items[StaticTuple.from_sequence(elements[:-1])] = value
99
102
    if len(items) != length:
100
103
        raise AssertionError("item count (%d) mismatch for key %s,"
113
116
    else:
114
117
        result._search_prefix = _unknown
115
118
        result._common_serialised_prefix = prefix
116
 
    if len(bytes) != result._current_size():
 
119
    if len(data) != result._current_size():
117
120
        raise AssertionError('_current_size computed incorrectly')
118
121
    return result
119
122
 
120
123
 
121
 
def _deserialise_internal_node(bytes, key, search_key_func=None):
 
124
def _deserialise_internal_node(data, key, search_key_func=None):
122
125
    global _unknown, _LeafNode, _InternalNode
123
126
    if _InternalNode is None:
124
 
        from bzrlib import chk_map
 
127
        from breezy import chk_map
125
128
        _unknown = chk_map._unknown
126
129
        _LeafNode = chk_map.LeafNode
127
130
        _InternalNode = chk_map.InternalNode
129
132
    # Splitlines can split on '\r' so don't use it, remove the extra ''
130
133
    # from the result of split('\n') because we should have a trailing
131
134
    # newline
132
 
    lines = bytes.split('\n')
133
 
    if lines[-1] != '':
 
135
    lines = data.split(b'\n')
 
136
    if lines[-1] != b'':
134
137
        raise ValueError("last line must be ''")
135
138
    lines.pop(-1)
136
139
    items = {}
137
 
    if lines[0] != 'chknode:':
 
140
    if lines[0] != b'chknode:':
138
141
        raise ValueError("not a serialised internal node: %r" % bytes)
139
142
    maximum_size = int(lines[1])
140
143
    width = int(lines[2])
142
145
    common_prefix = lines[4]
143
146
    for line in lines[5:]:
144
147
        line = common_prefix + line
145
 
        prefix, flat_key = line.rsplit('\x00', 1)
 
148
        prefix, flat_key = line.rsplit(b'\x00', 1)
146
149
        items[prefix] = StaticTuple(flat_key,)
147
150
    if len(items) == 0:
148
151
        raise AssertionError("We didn't find any item for %s" % key)
157
160
    result._node_width = len(prefix)
158
161
    result._search_prefix = common_prefix
159
162
    return result
 
163
 
 
164
 
 
165
def _bytes_to_text_key(data):
 
166
    """Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
 
167
    sections = data.split(b'\n')
 
168
    kind, file_id = sections[0].split(b': ')
 
169
    return (bytesintern(file_id), bytesintern(sections[3]))
 
170