17
17
"""Python implementation of _search_key functions, etc."""
19
from __future__ import absolute_import
22
from ..static_tuple import StaticTuple
24
from brzlib.static_tuple import StaticTuple
25
27
_InternalNode = None
30
31
# Depending on python version and platform, zlib.crc32 will return either a
31
32
# signed (<= 2.5 >= 3.0) or an unsigned (2.5, 2.6).
39
40
# Though we really don't need that 32nd bit of accuracy. (even 2**24
40
41
# is probably enough node fan out for realistic trees.)
41
return zlib.crc32(bit) & 0xFFFFFFFF
42
return zlib.crc32(bit)&0xFFFFFFFF
44
45
def _search_key_16(key):
45
46
"""Map the key tuple into a search key string which has 16-way fan out."""
46
return b'\x00'.join([b'%08X' % _crc32(bit) for bit in key])
47
return '\x00'.join(['%08X' % _crc32(bit) for bit in key])
49
50
def _search_key_255(key):
52
53
We use 255-way because '\n' is used as a delimiter, and causes problems
55
data = b'\x00'.join([struct.pack('>L', _crc32(bit)) for bit in key])
56
return data.replace(b'\n', b'_')
59
def _deserialise_leaf_node(data, key, search_key_func=None):
56
bytes = '\x00'.join([struct.pack('>L', _crc32(bit)) for bit in key])
57
return bytes.replace('\n', '_')
60
def _deserialise_leaf_node(bytes, key, search_key_func=None):
60
61
"""Deserialise bytes, with key key, into a LeafNode.
62
63
:param bytes: The bytes of the node.
65
66
global _unknown, _LeafNode, _InternalNode
66
67
if _LeafNode is None:
68
from brzlib import chk_map
68
69
_unknown = chk_map._unknown
69
70
_LeafNode = chk_map.LeafNode
70
71
_InternalNode = chk_map.InternalNode
71
72
result = _LeafNode(search_key_func=search_key_func)
72
73
# Splitlines can split on '\r' so don't use it, split('\n') adds an
73
74
# extra '' if the bytes ends in a final newline.
74
lines = data.split(b'\n')
75
lines = bytes.split('\n')
75
76
trailing = lines.pop()
77
78
raise AssertionError('We did not have a final newline for %s'
80
if lines[0] != b'chkleaf:':
81
if lines[0] != 'chkleaf:':
81
82
raise ValueError("not a serialised leaf node: %r" % bytes)
82
83
maximum_size = int(lines[1])
83
84
width = int(lines[2])
87
88
while pos < len(lines):
88
89
line = prefix + lines[pos]
89
elements = line.split(b'\x00')
90
elements = line.split('\x00')
91
92
if len(elements) != width + 1:
92
93
raise AssertionError(
93
94
'Incorrect number of elements (%d vs %d) for: %r'
94
95
% (len(elements), width + 1, line))
95
96
num_value_lines = int(elements[-1])
96
value_lines = lines[pos:pos + num_value_lines]
97
value_lines = lines[pos:pos+num_value_lines]
97
98
pos += num_value_lines
98
value = b'\n'.join(value_lines)
99
value = '\n'.join(value_lines)
99
100
items[StaticTuple.from_sequence(elements[:-1])] = value
100
101
if len(items) != length:
101
102
raise AssertionError("item count (%d) mismatch for key %s,"
102
" bytes %r" % (length, key, bytes))
103
" bytes %r" % (length, key, bytes))
103
104
result._items = items
104
105
result._len = length
105
106
result._maximum_size = maximum_size
106
107
result._key = key
107
108
result._key_width = width
108
result._raw_size = (sum(map(len, lines[5:])) # the length of the suffix
109
+ (length) * (len(prefix))
109
result._raw_size = (sum(map(len, lines[5:])) # the length of the suffix
110
+ (length)*(len(prefix))
112
113
result._search_prefix = None
113
114
result._common_serialised_prefix = None
115
116
result._search_prefix = _unknown
116
117
result._common_serialised_prefix = prefix
117
if len(data) != result._current_size():
118
if len(bytes) != result._current_size():
118
119
raise AssertionError('_current_size computed incorrectly')
122
def _deserialise_internal_node(data, key, search_key_func=None):
123
def _deserialise_internal_node(bytes, key, search_key_func=None):
123
124
global _unknown, _LeafNode, _InternalNode
124
125
if _InternalNode is None:
125
from . import chk_map
126
from brzlib import chk_map
126
127
_unknown = chk_map._unknown
127
128
_LeafNode = chk_map.LeafNode
128
129
_InternalNode = chk_map.InternalNode
130
131
# Splitlines can split on '\r' so don't use it, remove the extra ''
131
132
# from the result of split('\n') because we should have a trailing
133
lines = data.split(b'\n')
134
lines = bytes.split('\n')
135
136
raise ValueError("last line must be ''")
138
if lines[0] != b'chknode:':
139
if lines[0] != 'chknode:':
139
140
raise ValueError("not a serialised internal node: %r" % bytes)
140
141
maximum_size = int(lines[1])
141
142
width = int(lines[2])
143
144
common_prefix = lines[4]
144
145
for line in lines[5:]:
145
146
line = common_prefix + line
146
prefix, flat_key = line.rsplit(b'\x00', 1)
147
prefix, flat_key = line.rsplit('\x00', 1)
147
148
items[prefix] = StaticTuple(flat_key,)
148
149
if len(items) == 0:
149
150
raise AssertionError("We didn't find any item for %s" % key)
154
155
result._key_width = width
155
156
# XXX: InternalNodes don't really care about their size, and this will
156
157
# change if we add prefix compression
157
result._raw_size = None # len(bytes)
158
result._raw_size = None # len(bytes)
158
159
result._node_width = len(prefix)
159
160
result._search_prefix = common_prefix
163
def _bytes_to_text_key(data):
164
def _bytes_to_text_key(bytes):
164
165
"""Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
165
sections = data.split(b'\n')
166
kind, file_id = sections[0].split(b': ')
167
return (file_id, sections[3])
166
sections = bytes.split('\n')
167
kind, file_id = sections[0].split(': ')
168
return (intern(file_id), intern(sections[3]))