~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_chk_map_pyx.pyx

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-10-26 15:59:54 UTC
  • mfrom: (4679.9.25 2.1-static-tuple-chk-map)
  • Revision ID: pqm@pqm.ubuntu.com-20091026155954-r9gw2rizkikw7cg7
(jam) Start using StaticTuple as part of the chk_map code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
cdef extern from "Python.h":
31
31
    ctypedef int Py_ssize_t # Required for older pyrex versions
32
 
    struct _PyObject:
 
32
    ctypedef struct PyObject:
33
33
        pass
34
 
    ctypedef _PyObject PyObject
35
34
    int PyTuple_CheckExact(object p)
36
35
    Py_ssize_t PyTuple_GET_SIZE(object t)
37
36
    int PyString_CheckExact(object)
52
51
    char *PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *s)
53
52
    object PyString_FromStringAndSize(char*, Py_ssize_t)
54
53
 
 
54
# cimport all of the definitions we will need to access
 
55
from _static_tuple_c cimport StaticTuple,\
 
56
    import_static_tuple_c, StaticTuple_New, \
 
57
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
 
58
 
 
59
cdef extern from "_static_tuple_c.h":
 
60
    # Defined explicitly rather than cimport-ing. Trying to use cimport, the
 
61
    # type for PyObject is a different class that happens to have the same
 
62
    # name...
 
63
    PyObject * StaticTuple_GET_ITEM_ptr "StaticTuple_GET_ITEM" (StaticTuple,
 
64
                                                                Py_ssize_t)
 
65
 
55
66
cdef extern from "zlib.h":
56
67
    ctypedef unsigned long uLong
57
68
    ctypedef unsigned int uInt
60
71
    uLong crc32(uLong crc, Bytef *buf, uInt len)
61
72
 
62
73
 
 
74
# Set up the StaticTuple C_API functionality
 
75
import_static_tuple_c()
 
76
 
 
77
cdef object _LeafNode
63
78
_LeafNode = None
 
79
cdef object _InternalNode
64
80
_InternalNode = None
 
81
cdef object _unknown
65
82
_unknown = None
66
83
 
67
84
# We shouldn't just copy this from _dirstate_helpers_pyx
91
108
    cdef char *c_out
92
109
    cdef PyObject *bit
93
110
 
94
 
    if not PyTuple_CheckExact(key):
95
 
        raise TypeError('key %r is not a tuple' % (key,))
96
 
    num_bits = PyTuple_GET_SIZE(key)
 
111
    if not StaticTuple_CheckExact(key):
 
112
        raise TypeError('key %r is not a StaticTuple' % (key,))
 
113
    num_bits = len(key)
97
114
    # 4 bytes per crc32, and another 1 byte between bits
98
115
    num_out_bytes = (9 * num_bits) - 1
99
116
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
105
122
        # We use the _ptr variant, because GET_ITEM returns a borrowed
106
123
        # reference, and Pyrex assumes that returned 'object' are a new
107
124
        # reference
108
 
        bit = PyTuple_GET_ITEM_ptr(key, i)
 
125
        bit = StaticTuple_GET_ITEM_ptr(key, i)
109
126
        if not PyString_CheckExact_ptr(bit):
110
127
            raise TypeError('Bit %d of %r is not a string' % (i, key))
111
128
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
129
146
    cdef char *c_out
130
147
    cdef PyObject *bit
131
148
 
132
 
    if not PyTuple_CheckExact(key):
133
 
        raise TypeError('key %r is not a tuple' % (key,))
134
 
    num_bits = PyTuple_GET_SIZE(key)
 
149
    if not StaticTuple_CheckExact(key):
 
150
        raise TypeError('key %r is not a StaticTuple' % (key,))
 
151
    num_bits = len(key)
135
152
    # 4 bytes per crc32, and another 1 byte between bits
136
153
    num_out_bytes = (5 * num_bits) - 1
137
154
    out = PyString_FromStringAndSize(NULL, num_out_bytes)
140
157
        if i > 0:
141
158
            c_out[0] = c'\x00'
142
159
            c_out = c_out + 1
143
 
        bit = PyTuple_GET_ITEM_ptr(key, i)
 
160
        bit = StaticTuple_GET_ITEM_ptr(key, i)
144
161
        if not PyString_CheckExact_ptr(bit):
145
 
            raise TypeError('Bit %d of %r is not a string: %r' % (i, key,
146
 
            <object>bit))
 
162
            raise TypeError('Bit %d of %r is not a string: %r'
 
163
                            % (i, key, <object>bit))
147
164
        c_bit = <Bytef *>PyString_AS_STRING_ptr(bit)
148
165
        c_len = PyString_GET_SIZE_ptr(bit)
149
166
        crc_val = crc32(0, c_bit, c_len)
195
212
    cdef char *prefix, *value_start, *prefix_tail
196
213
    cdef char *next_null, *last_null, *line_start
197
214
    cdef char *c_entry, *entry_start
 
215
    cdef StaticTuple entry_bits
198
216
 
199
217
    if _LeafNode is None:
200
218
        from bzrlib import chk_map
265
283
            if next_line == NULL:
266
284
                raise ValueError('missing trailing newline')
267
285
            cur = next_line + 1
268
 
        entry_bits = PyTuple_New(width)
 
286
        entry_bits = StaticTuple_New(width)
269
287
        for i from 0 <= i < num_prefix_bits:
 
288
            # TODO: Use PyList_GetItem, or turn prefix_bits into a
 
289
            #       tuple/StaticTuple
270
290
            entry = prefix_bits[i]
271
291
            # SET_ITEM 'steals' a reference
272
292
            Py_INCREF(entry)
273
 
            PyTuple_SET_ITEM(entry_bits, i, entry)
 
293
            StaticTuple_SET_ITEM(entry_bits, i, entry)
274
294
        value = PyString_FromStringAndSize(value_start, next_line - value_start)
275
295
        # The next entry bit needs the 'tail' from the prefix, and first part
276
296
        # of the line
288
308
            memcpy(c_entry + prefix_tail_len, line_start, next_null - line_start)
289
309
        Py_INCREF(entry)
290
310
        i = num_prefix_bits
291
 
        PyTuple_SET_ITEM(entry_bits, i, entry)
 
311
        StaticTuple_SET_ITEM(entry_bits, i, entry)
292
312
        while next_null != last_null: # We have remaining bits
293
313
            i = i + 1
294
314
            if i > width:
301
321
            entry = PyString_FromStringAndSize(entry_start,
302
322
                                               next_null - entry_start)
303
323
            Py_INCREF(entry)
304
 
            PyTuple_SET_ITEM(entry_bits, i, entry)
 
324
            StaticTuple_SET_ITEM(entry_bits, i, entry)
305
325
        if len(entry_bits) != width:
306
326
            raise AssertionError(
307
327
                'Incorrect number of elements (%d vs %d)'
308
328
                % (len(entry_bits)+1, width + 1))
 
329
        entry_bits = StaticTuple_Intern(entry_bits)
309
330
        PyDict_SetItem(items, entry_bits, value)
310
331
    if len(items) != length:
311
332
        raise ValueError("item count (%d) mismatch for key %s,"
343
364
        _unknown = chk_map._unknown
344
365
    result = _InternalNode(search_key_func=search_key_func)
345
366
 
 
367
    if not StaticTuple_CheckExact(key):
 
368
        raise TypeError('key %r is not a StaticTuple' % (key,))
346
369
    if not PyString_CheckExact(bytes):
347
370
        raise TypeError('bytes must be a plain string not %s' % (type(bytes),))
348
371
 
384
407
        memcpy(c_item_prefix + prefix_length, cur, next_null - cur)
385
408
        flat_key = PyString_FromStringAndSize(next_null + 1,
386
409
                                              next_line - next_null - 1)
387
 
        PyDict_SetItem(items, item_prefix, (flat_key,))
 
410
        flat_key = StaticTuple(flat_key).intern()
 
411
        PyDict_SetItem(items, item_prefix, flat_key)
388
412
        cur = next_line + 1
389
413
    assert len(items) > 0
390
414
    result._items = items
398
422
    result._node_width = len(item_prefix)
399
423
    result._search_prefix = PyString_FromStringAndSize(prefix, prefix_length)
400
424
    return result
401