/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
1
# Copyright (C) 2009 Canonical Ltd
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
17
4241.6.4 by Robert Collins
Really build on old pyrex.
18
#python2.4 support
19
cdef extern from "python-compat.h":
4265.1.1 by John Arbash Meinel
Merge the a couple rev older brisbane-core into bzr.dev, most things are resolve in favor of bzr.dev
20
    ctypedef int Py_ssize_t # Required for older pyrex versions
4241.6.4 by Robert Collins
Really build on old pyrex.
21
4241.6.1 by Ian Clatworthy
chk_map code from brisbane-core
22
cdef extern from *:
23
    ctypedef unsigned int size_t
24
    int memcmp(void *, void*, size_t)
25
    void memcpy(void *, void*, size_t)
26
    void *memchr(void *s, int c, size_t len)
27
    long strtol(char *, char **, int)
28
    void sprintf(char *, char *, ...)
29
30
cdef extern from "Python.h":
31
    struct _PyObject:
32
        pass
33
    ctypedef _PyObject PyObject
34
    int PyTuple_CheckExact(object p)
35
    Py_ssize_t PyTuple_GET_SIZE(object t)
36
    int PyString_CheckExact(object)
37
    char *PyString_AS_STRING(object s)
38
    Py_ssize_t PyString_GET_SIZE(object)
39
40
    int PyDict_SetItem(object d, object k, object v) except -1
41
42
    object PyTuple_New(Py_ssize_t count)
43
    void PyTuple_SET_ITEM(object t, Py_ssize_t offset, object)
44
45
    void Py_INCREF(object)
46
47
    PyObject * PyTuple_GET_ITEM_ptr "PyTuple_GET_ITEM" (object t,
48
                                                        Py_ssize_t offset)
49
    int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *p)
50
    Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *s)
51
    char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
52
    object PyString_FromStringAndSize(char*, Py_ssize_t)
53
54
cdef extern from "zlib.h":
55
    ctypedef unsigned long uLong
56
    ctypedef unsigned int uInt
57
    ctypedef unsigned char Bytef
58
59
    uLong crc32(uLong crc, Bytef *buf, uInt len)
60
61
62
_LeafNode = None
63
_InternalNode = None
64
_unknown = None
65
66
# We shouldn't just copy this from _dirstate_helpers_c
67
cdef void* _my_memrchr(void *s, int c, size_t n):
68
    # memrchr seems to be a GNU extension, so we have to implement it ourselves
69
    cdef char *pos
70
    cdef char *start
71
72
    start = <char*>s
73
    pos = start + n - 1
74
    while pos >= start:
75
        if pos[0] == c:
76
            return <void*>pos
77
        pos = pos - 1
78
    return NULL
79
80
81
def _search_key_16(key):
82
    """See chk_map._search_key_16."""
83
    cdef Py_ssize_t num_bits
84
    cdef Py_ssize_t i, j
85
    cdef Py_ssize_t num_out_bytes
86
    cdef Bytef *c_bit
87
    cdef uLong c_len
88
    cdef uInt crc_val
89
    cdef Py_ssize_t out_off
90
    cdef char *c_out
91
    cdef PyObject *bit
92
93
    if not PyTuple_CheckExact(key):
94
        raise TypeError('key %r is not a tuple' % (key,))
95
    num_bits = PyTuple_GET_SIZE(key)
96
    # 4 bytes per crc32, and another 1 byte between bits
97
    num_out_bytes = (9 * num_bits) - 1
98
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
99
    c_out = PyString_AS_STRING(out)
100
    for i from 0 <= i < num_bits:
101
        if i > 0:
102
            c_out[0] = c'\x00'
103
            c_out = c_out + 1
104
        # We use the _ptr variant, because GET_ITEM returns a borrowed
105
        # reference, and Pyrex assumes that returned 'object' are a new
106
        # reference
107
        bit = PyTuple_GET_ITEM_ptr(key, i)
108
        if not PyString_CheckExact_ptr(bit):
109
            raise TypeError('Bit %d of %r is not a string' % (i, key))
110
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
111
        c_len = PyString_GET_SIZE_ptr(bit)
112
        crc_val = crc32(0, c_bit, c_len)
113
        # Hex(val) order
114
        sprintf(c_out, '%08X', crc_val)
115
        c_out = c_out + 8
116
    return out
117
118
119
def _search_key_255(key):
120
    """See chk_map._search_key_255."""
121
    cdef Py_ssize_t num_bits
122
    cdef Py_ssize_t i, j
123
    cdef Py_ssize_t num_out_bytes
124
    cdef Bytef *c_bit
125
    cdef uLong c_len
126
    cdef uInt crc_val
127
    cdef Py_ssize_t out_off
128
    cdef char *c_out
129
    cdef PyObject *bit
130
131
    if not PyTuple_CheckExact(key):
132
        raise TypeError('key %r is not a tuple' % (key,))
133
    num_bits = PyTuple_GET_SIZE(key)
134
    # 4 bytes per crc32, and another 1 byte between bits
135
    num_out_bytes = (5 * num_bits) - 1
136
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
137
    c_out = PyString_AS_STRING(out)
138
    for i from 0 <= i < num_bits:
139
        if i > 0:
140
            c_out[0] = c'\x00'
141
            c_out = c_out + 1
142
        bit = PyTuple_GET_ITEM_ptr(key, i)
143
        if not PyString_CheckExact_ptr(bit):
144
            raise TypeError('Bit %d of %r is not a string: %r' % (i, key,
145
            <object>bit))
146
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
147
        c_len = PyString_GET_SIZE_ptr(bit)
148
        crc_val = crc32(0, c_bit, c_len)
149
        # MSB order
150
        c_out[0] = (crc_val >> 24) & 0xFF
151
        c_out[1] = (crc_val >> 16) & 0xFF
152
        c_out[2] = (crc_val >> 8) & 0xFF
153
        c_out[3] = (crc_val >> 0) & 0xFF
154
        for j from 0 <= j < 4:
155
            if c_out[j] == c'\n':
156
                c_out[j] = c'_'
157
        c_out = c_out + 4
158
    return out
159
160
161
cdef int _get_int_from_line(char **cur, char *end, char *message) except -1:
162
    """Read a positive integer from the data stream.
163
164
    :param cur: The start of the data, this will be moved to after the
165
        trailing newline when done.
166
    :param end: Do not parse any data past this byte.
167
    :return: The integer stored in those bytes
168
    """
169
    cdef int value
170
    cdef char *next_line, *next
171
172
    next_line = <char *>memchr(cur[0], c'\n', end - cur[0])
173
    if next_line == NULL:
174
        raise ValueError("Missing %s line\n" % message)
175
176
    value = strtol(cur[0], &next, 10)
177
    if next != next_line:
178
        raise ValueError("%s line not a proper int\n" % message)
179
    cur[0] = next_line + 1
180
    return value
181
182
183
def _deserialise_leaf_node(bytes, key, search_key_func=None):
184
    """Deserialise bytes, with key key, into a LeafNode.
185
186
    :param bytes: The bytes of the node.
187
    :param key: The key that the serialised node has.
188
    """
189
    cdef char *c_bytes, *cur, *next, *end
190
    cdef char *next_line
191
    cdef Py_ssize_t c_bytes_len, prefix_length, items_length
192
    cdef int maximum_size, width, length, i, prefix_tail_len
193
    cdef int num_value_lines, num_prefix_bits
194
    cdef char *prefix, *value_start, *prefix_tail
195
    cdef char *next_null, *last_null, *line_start
196
    cdef char *c_entry, *entry_start
197
198
    if _LeafNode is None:
199
        from bzrlib import chk_map
200
        _LeafNode = chk_map.LeafNode
201
        _InternalNode = chk_map.InternalNode
202
        _unknown = chk_map._unknown
203
204
    result = _LeafNode(search_key_func=search_key_func)
205
    # Splitlines can split on '\r' so don't use it, split('\n') adds an
206
    # extra '' if the bytes ends in a final newline.
207
    if not PyString_CheckExact(bytes):
208
        raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
209
210
    c_bytes = PyString_AS_STRING(bytes)
211
    c_bytes_len = PyString_GET_SIZE(bytes)
212
213
    if c_bytes_len < 9 or memcmp(c_bytes, "chkleaf:\n", 9) != 0:
214
        raise ValueError("not a serialised leaf node: %r" % bytes)
215
    if c_bytes[c_bytes_len - 1] != c'\n':
216
        raise ValueError("bytes does not end in a newline")
217
218
    end = c_bytes + c_bytes_len
219
    cur = c_bytes + 9
220
    maximum_size = _get_int_from_line(&cur, end, "maximum_size")
221
    width = _get_int_from_line(&cur, end, "width")
222
    length = _get_int_from_line(&cur, end, "length")
223
224
    next_line = <char *>memchr(cur, c'\n', end - cur)
225
    if next_line == NULL:
226
        raise ValueError('Missing the prefix line\n')
227
    prefix = cur
228
    prefix_length = next_line - cur
229
    cur = next_line + 1
230
231
    prefix_bits = []
232
    prefix_tail = prefix
233
    num_prefix_bits = 0
234
    next_null = <char *>memchr(prefix, c'\0', prefix_length)
235
    while next_null != NULL:
236
        num_prefix_bits = num_prefix_bits + 1
237
        prefix_bits.append(
238
            PyString_FromStringAndSize(prefix_tail, next_null - prefix_tail))
239
        prefix_tail = next_null + 1
240
        next_null = <char *>memchr(prefix_tail, c'\0', next_line - prefix_tail)
241
    prefix_tail_len = next_line - prefix_tail
242
243
    if num_prefix_bits >= width:
244
        raise ValueError('Prefix has too many nulls versus width')
245
246
    items_length = end - cur
247
    items = {}
248
    while cur < end:
249
        line_start = cur
250
        next_line = <char *>memchr(cur, c'\n', end - cur)
251
        if next_line == NULL:
252
            raise ValueError('null line\n')
253
        last_null = <char *>_my_memrchr(cur, c'\0', next_line - cur)
254
        if last_null == NULL:
255
            raise ValueError('fail to find the num value lines null')
256
        next_null = last_null + 1 # move past NULL
257
        num_value_lines = _get_int_from_line(&next_null, next_line + 1,
258
                                             "num value lines")
259
        cur = next_line + 1
260
        value_start = cur
261
        # Walk num_value_lines forward
262
        for i from 0 <= i < num_value_lines:
263
            next_line = <char *>memchr(cur, c'\n', end - cur)
264
            if next_line == NULL:
265
                raise ValueError('missing trailing newline')
266
            cur = next_line + 1
267
        entry_bits = PyTuple_New(width)
268
        for i from 0 <= i < num_prefix_bits:
269
            entry = prefix_bits[i]
270
            # SET_ITEM 'steals' a reference
271
            Py_INCREF(entry)
272
            PyTuple_SET_ITEM(entry_bits, i, entry)
273
        value = PyString_FromStringAndSize(value_start, next_line - value_start)
274
        # The next entry bit needs the 'tail' from the prefix, and first part
275
        # of the line
276
        entry_start = line_start
277
        next_null = <char *>memchr(entry_start, c'\0',
278
                                   last_null - entry_start + 1)
279
        if next_null == NULL:
280
            raise ValueError('bad no null, bad')
281
        entry = PyString_FromStringAndSize(NULL,
282
                    prefix_tail_len + next_null - line_start)
283
        c_entry = PyString_AS_STRING(entry)
284
        if prefix_tail_len > 0:
285
            memcpy(c_entry, prefix_tail, prefix_tail_len)
286
        if next_null - line_start > 0:
287
            memcpy(c_entry + prefix_tail_len, line_start, next_null - line_start)
288
        Py_INCREF(entry)
289
        i = num_prefix_bits
290
        PyTuple_SET_ITEM(entry_bits, i, entry)
291
        while next_null != last_null: # We have remaining bits
292
            i = i + 1
293
            if i > width:
294
                raise ValueError("Too many bits for entry")
295
            entry_start = next_null + 1
296
            next_null = <char *>memchr(entry_start, c'\0',
297
                                       last_null - entry_start + 1)
298
            if next_null == NULL:
299
                raise ValueError('bad no null')
300
            entry = PyString_FromStringAndSize(entry_start,
301
                                               next_null - entry_start)
302
            Py_INCREF(entry)
303
            PyTuple_SET_ITEM(entry_bits, i, entry)
304
        if len(entry_bits) != width:
305
            raise AssertionError(
306
                'Incorrect number of elements (%d vs %d)'
307
                % (len(entry_bits)+1, width + 1))
308
        PyDict_SetItem(items, entry_bits, value)
309
    if len(items) != length:
310
        raise ValueError("item count (%d) mismatch for key %s,"
311
                         " bytes %r" % (length, entry_bits, bytes))
312
    result._items = items
313
    result._len = length
314
    result._maximum_size = maximum_size
315
    result._key = key
316
    result._key_width = width
317
    result._raw_size = items_length + length * prefix_length
318
    if length == 0:
319
        result._search_prefix = None
320
        result._common_serialised_prefix = None
321
    else:
322
        result._search_prefix = _unknown
323
        result._common_serialised_prefix = PyString_FromStringAndSize(prefix,
324
                                                prefix_length)
325
    if c_bytes_len != result._current_size():
326
        raise AssertionError('_current_size computed incorrectly %d != %d',
327
            c_bytes_len, result._current_size())
328
    return result
329
330
331
def _deserialise_internal_node(bytes, key, search_key_func=None):
332
    cdef char *c_bytes, *cur, *next, *end
333
    cdef char *next_line
334
    cdef Py_ssize_t c_bytes_len, prefix_length
335
    cdef int maximum_size, width, length, i, prefix_tail_len
336
    cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
337
338
    if _InternalNode is None:
339
        from bzrlib import chk_map
340
        _LeafNode = chk_map.LeafNode
341
        _InternalNode = chk_map.InternalNode
342
        _unknown = chk_map._unknown
343
    result = _InternalNode(search_key_func=search_key_func)
344
345
    if not PyString_CheckExact(bytes):
346
        raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
347
348
    c_bytes = PyString_AS_STRING(bytes)
349
    c_bytes_len = PyString_GET_SIZE(bytes)
350
351
    if c_bytes_len < 9 or memcmp(c_bytes, "chknode:\n", 9) != 0:
352
        raise ValueError("not a serialised internal node: %r" % bytes)
353
    if c_bytes[c_bytes_len - 1] != c'\n':
354
        raise ValueError("bytes does not end in a newline")
355
356
    items = {}
357
    cur = c_bytes + 9
358
    end = c_bytes + c_bytes_len
359
    maximum_size = _get_int_from_line(&cur, end, "maximum_size")
360
    width = _get_int_from_line(&cur, end, "width")
361
    length = _get_int_from_line(&cur, end, "length")
362
363
    next_line = <char *>memchr(cur, c'\n', end - cur)
364
    if next_line == NULL:
365
        raise ValueError('Missing the prefix line\n')
366
    prefix = cur
367
    prefix_length = next_line - cur
368
    cur = next_line + 1
369
370
    while cur < end:
371
        # Find the null separator
372
        next_line = <char *>memchr(cur, c'\n', end - cur)
373
        if next_line == NULL:
374
            raise ValueError('missing trailing newline')
375
        next_null = <char *>_my_memrchr(cur, c'\0', next_line - cur)
376
        if next_null == NULL:
377
            raise ValueError('bad no null')
378
        item_prefix = PyString_FromStringAndSize(NULL,
379
            prefix_length + next_null - cur)
380
        c_item_prefix = PyString_AS_STRING(item_prefix)
381
        if prefix_length:
382
            memcpy(c_item_prefix, prefix, prefix_length)
383
        memcpy(c_item_prefix + prefix_length, cur, next_null - cur)
384
        flat_key = PyString_FromStringAndSize(next_null + 1,
385
                                              next_line - next_null - 1)
386
        PyDict_SetItem(items, item_prefix, (flat_key,))
387
        cur = next_line + 1
388
    assert len(items) > 0
389
    result._items = items
390
    result._len = length
391
    result._maximum_size = maximum_size
392
    result._key = key
393
    result._key_width = width
394
    # XXX: InternalNodes don't really care about their size, and this will
395
    #      change if we add prefix compression
396
    result._raw_size = None # len(bytes)
397
    result._node_width = len(item_prefix)
398
    result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
399
    return result
400