bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
4763.2.4
by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry. |
1 |
# Copyright (C) 2009, 2010 Canonical Ltd
|
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
2 |
#
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
16 |
||
|
6379.6.7
by Jelmer Vernooij
Move importing from future until after doc string, otherwise the doc string will disappear. |
17 |
"""Python implementation of _search_key functions, etc."""
|
18 |
||
|
6379.6.1
by Jelmer Vernooij
Import absolute_import in a few places. |
19 |
from __future__ import absolute_import |
20 |
||
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
21 |
import zlib |
22 |
import struct |
|
23 |
||
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
24 |
from .sixish import bytesintern |
|
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
25 |
from .static_tuple import StaticTuple |
|
4679.9.4
by John Arbash Meinel
A bit broken, but getting there. |
26 |
|
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
27 |
_LeafNode = None |
28 |
_InternalNode = None |
|
29 |
_unknown = None |
|
30 |
||
31 |
def _crc32(bit): |
|
32 |
# Depending on python version and platform, zlib.crc32 will return either a
|
|
33 |
# signed (<= 2.5 >= 3.0) or an unsigned (2.5, 2.6).
|
|
34 |
# http://docs.python.org/library/zlib.html recommends using a mask to force
|
|
35 |
# an unsigned value to ensure the same numeric value (unsigned) is obtained
|
|
36 |
# across all python versions and platforms.
|
|
37 |
# Note: However, on 32-bit platforms this causes an upcast to PyLong, which
|
|
38 |
# are generally slower than PyInts. However, if performance becomes
|
|
39 |
# critical, we should probably write the whole thing as an extension
|
|
40 |
# anyway.
|
|
41 |
# Though we really don't need that 32nd bit of accuracy. (even 2**24
|
|
42 |
# is probably enough node fan out for realistic trees.)
|
|
43 |
return zlib.crc32(bit)&0xFFFFFFFF |
|
44 |
||
45 |
||
46 |
def _search_key_16(key): |
|
47 |
"""Map the key tuple into a search key string which has 16-way fan out.""" |
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
48 |
return b'\x00'.join([b'%08X' % _crc32(bit) for bit in key]) |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
49 |
|
50 |
||
51 |
def _search_key_255(key): |
|
52 |
"""Map the key tuple into a search key string which has 255-way fan out. |
|
53 |
||
54 |
We use 255-way because '\n' is used as a delimiter, and causes problems
|
|
55 |
while parsing.
|
|
56 |
"""
|
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
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): |
|
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
62 |
"""Deserialise bytes, with key key, into a LeafNode. |
63 |
||
64 |
:param bytes: The bytes of the node.
|
|
65 |
:param key: The key that the serialised node has.
|
|
66 |
"""
|
|
67 |
global _unknown, _LeafNode, _InternalNode |
|
68 |
if _LeafNode is None: |
|
|
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
69 |
from breezy import chk_map |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
70 |
_unknown = chk_map._unknown |
71 |
_LeafNode = chk_map.LeafNode |
|
72 |
_InternalNode = chk_map.InternalNode |
|
73 |
result = _LeafNode(search_key_func=search_key_func) |
|
74 |
# Splitlines can split on '\r' so don't use it, split('\n') adds an
|
|
75 |
# extra '' if the bytes ends in a final newline.
|
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
76 |
lines = data.split(b'\n') |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
77 |
trailing = lines.pop() |
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
78 |
if trailing != b'': |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
79 |
raise AssertionError('We did not have a final newline for %s' |
80 |
% (key,)) |
|
81 |
items = {} |
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
82 |
if lines[0] != b'chkleaf:': |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
83 |
raise ValueError("not a serialised leaf node: %r" % bytes) |
84 |
maximum_size = int(lines[1]) |
|
85 |
width = int(lines[2]) |
|
86 |
length = int(lines[3]) |
|
87 |
prefix = lines[4] |
|
88 |
pos = 5 |
|
89 |
while pos < len(lines): |
|
90 |
line = prefix + lines[pos] |
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
91 |
elements = line.split(b'\x00') |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
92 |
pos += 1 |
93 |
if len(elements) != width + 1: |
|
94 |
raise AssertionError( |
|
95 |
'Incorrect number of elements (%d vs %d) for: %r' |
|
96 |
% (len(elements), width + 1, line)) |
|
97 |
num_value_lines = int(elements[-1]) |
|
98 |
value_lines = lines[pos:pos+num_value_lines] |
|
99 |
pos += num_value_lines |
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
100 |
value = b'\n'.join(value_lines) |
|
4679.9.16
by John Arbash Meinel
More cleanups and clarifications. |
101 |
items[StaticTuple.from_sequence(elements[:-1])] = value |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
102 |
if len(items) != length: |
103 |
raise AssertionError("item count (%d) mismatch for key %s," |
|
104 |
" bytes %r" % (length, key, bytes)) |
|
105 |
result._items = items |
|
106 |
result._len = length |
|
107 |
result._maximum_size = maximum_size |
|
108 |
result._key = key |
|
109 |
result._key_width = width |
|
110 |
result._raw_size = (sum(map(len, lines[5:])) # the length of the suffix |
|
111 |
+ (length)*(len(prefix)) |
|
112 |
+ (len(lines)-5)) |
|
113 |
if not items: |
|
114 |
result._search_prefix = None |
|
115 |
result._common_serialised_prefix = None |
|
116 |
else: |
|
117 |
result._search_prefix = _unknown |
|
118 |
result._common_serialised_prefix = prefix |
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
119 |
if len(data) != result._current_size(): |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
120 |
raise AssertionError('_current_size computed incorrectly') |
121 |
return result |
|
122 |
||
123 |
||
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
124 |
def _deserialise_internal_node(data, key, search_key_func=None): |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
125 |
global _unknown, _LeafNode, _InternalNode |
126 |
if _InternalNode is None: |
|
|
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
127 |
from breezy import chk_map |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
128 |
_unknown = chk_map._unknown |
129 |
_LeafNode = chk_map.LeafNode |
|
130 |
_InternalNode = chk_map.InternalNode |
|
131 |
result = _InternalNode(search_key_func=search_key_func) |
|
132 |
# Splitlines can split on '\r' so don't use it, remove the extra ''
|
|
133 |
# from the result of split('\n') because we should have a trailing
|
|
134 |
# newline
|
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
135 |
lines = data.split(b'\n') |
136 |
if lines[-1] != b'': |
|
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
137 |
raise ValueError("last line must be ''") |
138 |
lines.pop(-1) |
|
139 |
items = {} |
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
140 |
if lines[0] != b'chknode:': |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
141 |
raise ValueError("not a serialised internal node: %r" % bytes) |
142 |
maximum_size = int(lines[1]) |
|
143 |
width = int(lines[2]) |
|
144 |
length = int(lines[3]) |
|
145 |
common_prefix = lines[4] |
|
146 |
for line in lines[5:]: |
|
147 |
line = common_prefix + line |
|
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
148 |
prefix, flat_key = line.rsplit(b'\x00', 1) |
|
4679.9.4
by John Arbash Meinel
A bit broken, but getting there. |
149 |
items[prefix] = StaticTuple(flat_key,) |
|
4241.6.1
by Ian Clatworthy
chk_map code from brisbane-core |
150 |
if len(items) == 0: |
151 |
raise AssertionError("We didn't find any item for %s" % key) |
|
152 |
result._items = items |
|
153 |
result._len = length |
|
154 |
result._maximum_size = maximum_size |
|
155 |
result._key = key |
|
156 |
result._key_width = width |
|
157 |
# XXX: InternalNodes don't really care about their size, and this will
|
|
158 |
# change if we add prefix compression
|
|
159 |
result._raw_size = None # len(bytes) |
|
160 |
result._node_width = len(prefix) |
|
161 |
result._search_prefix = common_prefix |
|
162 |
return result |
|
|
5218.2.1
by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value. |
163 |
|
164 |
||
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
165 |
def _bytes_to_text_key(data): |
|
5218.2.1
by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value. |
166 |
"""Take a CHKInventory value string and return a (file_id, rev_id) tuple""" |
|
6684.1.3
by Martin
Changes across many modules working towards Python 3 support |
167 |
sections = data.split(b'\n') |
168 |
kind, file_id = sections[0].split(b': ') |
|
169 |
return (bytesintern(file_id), bytesintern(sections[3])) |
|
|
5218.2.1
by John Arbash Meinel
Implement a compiled extension for parsing the text key out of a CHKInventory value. |
170 |