~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_chk_map_pyx.pyx

  • Committer: Danny van Heumen
  • Date: 2010-03-09 21:42:11 UTC
  • mto: (4634.139.5 2.0)
  • mto: This revision was merged to the branch mainline in revision 5160.
  • Revision ID: danny@dannyvanheumen.nl-20100309214211-iqh42x6qcikgd9p3
Reverted now-useless TODO list.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
29
29
 
30
30
cdef extern from "Python.h":
31
31
    ctypedef int Py_ssize_t # Required for older pyrex versions
32
 
    ctypedef struct PyObject:
 
32
    struct _PyObject:
33
33
        pass
 
34
    ctypedef _PyObject PyObject
34
35
    int PyTuple_CheckExact(object p)
35
36
    Py_ssize_t PyTuple_GET_SIZE(object t)
36
37
    int PyString_CheckExact(object)
37
38
    char *PyString_AS_STRING(object s)
38
 
    PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t)
39
39
    Py_ssize_t PyString_GET_SIZE(object)
40
 
    void PyString_InternInPlace(PyObject **)
41
 
    long PyInt_AS_LONG(object)
42
40
 
43
41
    int PyDict_SetItem(object d, object k, object v) except -1
44
42
 
 
43
    object PyTuple_New(Py_ssize_t count)
 
44
    void PyTuple_SET_ITEM(object t, Py_ssize_t offset, object)
 
45
 
45
46
    void Py_INCREF(object)
46
 
    void Py_DECREF_ptr "Py_DECREF" (PyObject *)
47
47
 
 
48
    PyObject * PyTuple_GET_ITEM_ptr "PyTuple_GET_ITEM" (object t,
 
49
                                                        Py_ssize_t offset)
 
50
    int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *p)
 
51
    Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *s)
 
52
    char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
48
53
    object PyString_FromStringAndSize(char*, Py_ssize_t)
49
54
 
50
 
# cimport all of the definitions we will need to access
51
 
from _static_tuple_c cimport StaticTuple,\
52
 
    import_static_tuple_c, StaticTuple_New, \
53
 
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact, \
54
 
    StaticTuple_GET_SIZE
55
 
 
56
 
cdef object crc32
57
 
from zlib import crc32
58
 
 
59
 
 
60
 
# Set up the StaticTuple C_API functionality
61
 
import_static_tuple_c()
62
 
 
63
 
cdef object _LeafNode
 
55
cdef extern from "zlib.h":
 
56
    ctypedef unsigned long uLong
 
57
    ctypedef unsigned int uInt
 
58
    ctypedef unsigned char Bytef
 
59
 
 
60
    uLong crc32(uLong crc, Bytef *buf, uInt len)
 
61
 
 
62
 
64
63
_LeafNode = None
65
 
cdef object _InternalNode
66
64
_InternalNode = None
67
 
cdef object _unknown
68
65
_unknown = None
69
66
 
70
67
# We shouldn't just copy this from _dirstate_helpers_pyx
82
79
    return NULL
83
80
 
84
81
 
85
 
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size):
86
 
    cdef PyObject *py_str
87
 
    if size < 0:
88
 
        raise AssertionError(
89
 
            'tried to create a string with an invalid size: %d @0x%x'
90
 
            % (size, <int>s))
91
 
    py_str = PyString_FromStringAndSize_ptr(s, size)
92
 
    PyString_InternInPlace(&py_str)
93
 
    result = <object>py_str
94
 
    # Casting a PyObject* to an <object> triggers an INCREF from Pyrex, so we
95
 
    # DECREF it to avoid geting immortal strings
96
 
    Py_DECREF_ptr(py_str)
97
 
    return result
98
 
 
99
 
 
100
82
def _search_key_16(key):
101
83
    """See chk_map._search_key_16."""
102
84
    cdef Py_ssize_t num_bits
103
85
    cdef Py_ssize_t i, j
104
86
    cdef Py_ssize_t num_out_bytes
105
 
    cdef unsigned long crc_val
 
87
    cdef Bytef *c_bit
 
88
    cdef uLong c_len
 
89
    cdef uInt crc_val
106
90
    cdef Py_ssize_t out_off
107
91
    cdef char *c_out
 
92
    cdef PyObject *bit
108
93
 
109
 
    num_bits = len(key)
 
94
    if not PyTuple_CheckExact(key):
 
95
        raise TypeError('key %r is not a tuple' % (key,))
 
96
    num_bits = PyTuple_GET_SIZE(key)
110
97
    # 4 bytes per crc32, and another 1 byte between bits
111
98
    num_out_bytes = (9 * num_bits) - 1
112
99
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
115
102
        if i > 0:
116
103
            c_out[0] = c'\x00'
117
104
            c_out = c_out + 1
118
 
        crc_val = PyInt_AS_LONG(crc32(key[i]))
 
105
        # We use the _ptr variant, because GET_ITEM returns a borrowed
 
106
        # reference, and Pyrex assumes that returned 'object' are a new
 
107
        # reference
 
108
        bit = PyTuple_GET_ITEM_ptr(key, i)
 
109
        if not PyString_CheckExact_ptr(bit):
 
110
            raise TypeError('Bit %d of %r is not a string' % (i, key))
 
111
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
 
112
        c_len = PyString_GET_SIZE_ptr(bit)
 
113
        crc_val = crc32(0, c_bit, c_len)
119
114
        # Hex(val) order
120
115
        sprintf(c_out, '%08X', crc_val)
121
116
        c_out = c_out + 8
127
122
    cdef Py_ssize_t num_bits
128
123
    cdef Py_ssize_t i, j
129
124
    cdef Py_ssize_t num_out_bytes
130
 
    cdef unsigned long crc_val
 
125
    cdef Bytef *c_bit
 
126
    cdef uLong c_len
 
127
    cdef uInt crc_val
131
128
    cdef Py_ssize_t out_off
132
129
    cdef char *c_out
 
130
    cdef PyObject *bit
133
131
 
134
 
    num_bits = len(key)
 
132
    if not PyTuple_CheckExact(key):
 
133
        raise TypeError('key %r is not a tuple' % (key,))
 
134
    num_bits = PyTuple_GET_SIZE(key)
135
135
    # 4 bytes per crc32, and another 1 byte between bits
136
136
    num_out_bytes = (5 * num_bits) - 1
137
137
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
140
140
        if i > 0:
141
141
            c_out[0] = c'\x00'
142
142
            c_out = c_out + 1
143
 
        crc_val = PyInt_AS_LONG(crc32(key[i]))
 
143
        bit = PyTuple_GET_ITEM_ptr(key, i)
 
144
        if not PyString_CheckExact_ptr(bit):
 
145
            raise TypeError('Bit %d of %r is not a string: %r' % (i, key,
 
146
            <object>bit))
 
147
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
 
148
        c_len = PyString_GET_SIZE_ptr(bit)
 
149
        crc_val = crc32(0, c_bit, c_len)
144
150
        # MSB order
145
151
        c_out[0] = (crc_val >> 24) & 0xFF
146
152
        c_out[1] = (crc_val >> 16) & 0xFF
175
181
    return value
176
182
 
177
183
 
178
 
cdef _import_globals():
179
 
    """Set the global attributes. Done lazy to avoid recursive import loops."""
180
 
    global _LeafNode, _InternalNode, _unknown
181
 
 
182
 
    from bzrlib import chk_map
183
 
    _LeafNode = chk_map.LeafNode
184
 
    _InternalNode = chk_map.InternalNode
185
 
    _unknown = chk_map._unknown
186
 
 
187
 
 
188
184
def _deserialise_leaf_node(bytes, key, search_key_func=None):
189
185
    """Deserialise bytes, with key key, into a LeafNode.
190
186
 
199
195
    cdef char *prefix, *value_start, *prefix_tail
200
196
    cdef char *next_null, *last_null, *line_start
201
197
    cdef char *c_entry, *entry_start
202
 
    cdef StaticTuple entry_bits
203
198
 
204
199
    if _LeafNode is None:
205
 
        _import_globals()
 
200
        from bzrlib import chk_map
 
201
        _LeafNode = chk_map.LeafNode
 
202
        _InternalNode = chk_map.InternalNode
 
203
        _unknown = chk_map._unknown
206
204
 
207
205
    result = _LeafNode(search_key_func=search_key_func)
208
206
    # Splitlines can split on '\r' so don't use it, split('\n') adds an
267
265
            if next_line == NULL:
268
266
                raise ValueError('missing trailing newline')
269
267
            cur = next_line + 1
270
 
        entry_bits = StaticTuple_New(width)
 
268
        entry_bits = PyTuple_New(width)
271
269
        for i from 0 <= i < num_prefix_bits:
272
 
            # TODO: Use PyList_GetItem, or turn prefix_bits into a
273
 
            #       tuple/StaticTuple
274
270
            entry = prefix_bits[i]
275
271
            # SET_ITEM 'steals' a reference
276
272
            Py_INCREF(entry)
277
 
            StaticTuple_SET_ITEM(entry_bits, i, entry)
 
273
            PyTuple_SET_ITEM(entry_bits, i, entry)
278
274
        value = PyString_FromStringAndSize(value_start, next_line - value_start)
279
275
        # The next entry bit needs the 'tail' from the prefix, and first part
280
276
        # of the line
292
288
            memcpy(c_entry + prefix_tail_len, line_start, next_null - line_start)
293
289
        Py_INCREF(entry)
294
290
        i = num_prefix_bits
295
 
        StaticTuple_SET_ITEM(entry_bits, i, entry)
 
291
        PyTuple_SET_ITEM(entry_bits, i, entry)
296
292
        while next_null != last_null: # We have remaining bits
297
293
            i = i + 1
298
294
            if i > width:
305
301
            entry = PyString_FromStringAndSize(entry_start,
306
302
                                               next_null - entry_start)
307
303
            Py_INCREF(entry)
308
 
            StaticTuple_SET_ITEM(entry_bits, i, entry)
309
 
        if StaticTuple_GET_SIZE(entry_bits) != width:
 
304
            PyTuple_SET_ITEM(entry_bits, i, entry)
 
305
        if len(entry_bits) != width:
310
306
            raise AssertionError(
311
307
                'Incorrect number of elements (%d vs %d)'
312
308
                % (len(entry_bits)+1, width + 1))
313
 
        entry_bits = StaticTuple_Intern(entry_bits)
314
309
        PyDict_SetItem(items, entry_bits, value)
315
310
    if len(items) != length:
316
311
        raise ValueError("item count (%d) mismatch for key %s,"
342
337
    cdef char *prefix, *line_prefix, *next_null, *c_item_prefix
343
338
 
344
339
    if _InternalNode is None:
345
 
        _import_globals()
 
340
        from bzrlib import chk_map
 
341
        _LeafNode = chk_map.LeafNode
 
342
        _InternalNode = chk_map.InternalNode
 
343
        _unknown = chk_map._unknown
346
344
    result = _InternalNode(search_key_func=search_key_func)
347
345
 
348
 
    if not StaticTuple_CheckExact(key):
349
 
        raise TypeError('key %r is not a StaticTuple' % (key,))
350
346
    if not PyString_CheckExact(bytes):
351
347
        raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
352
348
 
388
384
        memcpy(c_item_prefix + prefix_length, cur, next_null - cur)
389
385
        flat_key = PyString_FromStringAndSize(next_null + 1,
390
386
                                              next_line - next_null - 1)
391
 
        flat_key = StaticTuple(flat_key).intern()
392
 
        PyDict_SetItem(items, item_prefix, flat_key)
 
387
        PyDict_SetItem(items, item_prefix, (flat_key,))
393
388
        cur = next_line + 1
394
389
    assert len(items) > 0
395
390
    result._items = items
404
399
    result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
405
400
    return result
406
401
 
407
 
 
408
 
def _bytes_to_text_key(bytes):
409
 
    """Take a CHKInventory value string and return a (file_id, rev_id) tuple"""
410
 
    cdef StaticTuple key
411
 
    cdef char *byte_str, *cur_end, *file_id_str, *byte_end
412
 
    cdef char *revision_str
413
 
    cdef Py_ssize_t byte_size, pos, file_id_len
414
 
 
415
 
    if not PyString_CheckExact(bytes):
416
 
        raise TypeError('bytes must be a string')
417
 
    byte_str = PyString_AS_STRING(bytes)
418
 
    byte_size = PyString_GET_SIZE(bytes)
419
 
    byte_end = byte_str + byte_size
420
 
    cur_end = <char*>memchr(byte_str, c':', byte_size)
421
 
    if cur_end == NULL:
422
 
        raise ValueError('No kind section found.')
423
 
    if cur_end[1] != c' ':
424
 
        raise ValueError('Kind section should end with ": "')
425
 
    file_id_str = cur_end + 2
426
 
    # file_id is now the data up until the next newline
427
 
    cur_end = <char*>memchr(file_id_str, c'\n', byte_end - file_id_str)
428
 
    if cur_end == NULL:
429
 
        raise ValueError('no newline after file-id')
430
 
    file_id = safe_interned_string_from_size(file_id_str,
431
 
                                             cur_end - file_id_str)
432
 
    # this is the end of the parent_str
433
 
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
434
 
    if cur_end == NULL:
435
 
        raise ValueError('no newline after parent_str')
436
 
    # end of the name str
437
 
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
438
 
    if cur_end == NULL:
439
 
        raise ValueError('no newline after name str')
440
 
    # the next section is the revision info
441
 
    revision_str = cur_end + 1
442
 
    cur_end = <char*>memchr(cur_end + 1, c'\n', byte_end - cur_end - 1)
443
 
    if cur_end == NULL:
444
 
        # This is probably a dir: entry, which has revision as the last item
445
 
        cur_end = byte_end
446
 
    revision = safe_interned_string_from_size(revision_str,
447
 
        cur_end - revision_str)
448
 
    key = StaticTuple_New(2)
449
 
    Py_INCREF(file_id)
450
 
    StaticTuple_SET_ITEM(key, 0, file_id) 
451
 
    Py_INCREF(revision)
452
 
    StaticTuple_SET_ITEM(key, 1, revision) 
453
 
    return StaticTuple_Intern(key)