/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/_btree_serializer_pyx.pyx

  • Committer: John Arbash Meinel
  • Date: 2009-10-08 04:35:01 UTC
  • mto: (4679.6.1 2.1-export-c-api)
  • mto: This revision was merged to the branch mainline in revision 4735.
  • Revision ID: john@arbash-meinel.com-20091008043501-8fgotm1vbt1zp4g6
Remove everything except for SimpleSet, and clean up its test suite.

This will be my first submission, since it doesn't directly depend on anything else.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
    Py_ssize_t PyString_Size(object p)
39
39
    Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *)
40
40
    char * PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *)
41
 
    char * PyString_AS_STRING(object)
42
 
    Py_ssize_t PyString_GET_SIZE(object)
43
41
    int PyString_AsStringAndSize_ptr(PyObject *, char **buf, Py_ssize_t *len)
44
42
    void PyString_InternInPlace(PyObject **)
45
43
    int PyTuple_CheckExact(object t)
57
55
    # void *memrchr(void *s, int c, size_t n)
58
56
    int strncmp(char *s1, char *s2, size_t n)
59
57
 
60
 
# It seems we need to import the definitions so that the pyrex compiler has
61
 
# local names to access them.
62
 
from _static_tuple_c cimport StaticTuple, \
63
 
    import_static_tuple_c, StaticTuple_New, \
64
 
    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
65
 
 
66
58
 
67
59
# TODO: Find some way to import this from _dirstate_helpers
68
60
cdef void* _my_memrchr(void *s, int c, size_t n):
102
94
    Py_DECREF_ptr(py_str)
103
95
    return result
104
96
 
105
 
from bzrlib import _static_tuple_c
106
 
cdef object _ST
107
 
_ST = _static_tuple_c.StaticTuple
108
 
# This sets up the StaticTuple C_API functionality
109
 
import_static_tuple_c()
110
 
 
111
97
 
112
98
cdef class BTreeLeafParser:
113
99
    """Parse the leaf nodes of a BTree index.
147
133
        self._cur_str = NULL
148
134
        self._end_str = NULL
149
135
        self._header_found = 0
150
 
        # keys are tuples
151
136
 
152
137
    cdef extract_key(self, char * last):
153
138
        """Extract a key.
157
142
        """
158
143
        cdef char *temp_ptr
159
144
        cdef int loop_counter
160
 
        cdef StaticTuple key
161
 
        
162
 
        key = StaticTuple_New(self.key_length)
 
145
        # keys are tuples
 
146
        key = PyTuple_New(self.key_length)
163
147
        for loop_counter from 0 <= loop_counter < self.key_length:
164
148
            # grab a key segment
165
149
            temp_ptr = <char*>memchr(self._start, c'\0', last - self._start)
174
158
                                                   last - self._start)))
175
159
                    raise AssertionError(failure_string)
176
160
            # capture the key string
177
 
            if (self.key_length == 1 
178
 
                and (temp_ptr - self._start) == 45
179
 
                and strncmp(self._start, 'sha1:', 5) == 0):
180
 
                key_element = safe_string_from_size(self._start,
181
 
                                                    temp_ptr - self._start)
182
 
            else:
183
 
                key_element = safe_interned_string_from_size(self._start,
 
161
            # TODO: Consider using PyIntern_FromString, the only caveat is that
 
162
            # it assumes a NULL-terminated string, so we have to check if
 
163
            # temp_ptr[0] == c'\0' or some other char.
 
164
            key_element = safe_interned_string_from_size(self._start,
184
165
                                                         temp_ptr - self._start)
185
166
            # advance our pointer
186
167
            self._start = temp_ptr + 1
187
168
            Py_INCREF(key_element)
188
 
            StaticTuple_SET_ITEM(key, loop_counter, key_element)
189
 
        key = StaticTuple_Intern(key)
 
169
            PyTuple_SET_ITEM(key, loop_counter, key_element)
190
170
        return key
191
171
 
192
172
    cdef int process_line(self) except -1:
236
216
            # shrink the references end point
237
217
            last = temp_ptr
238
218
        if self.ref_list_length:
239
 
            ref_lists = StaticTuple_New(self.ref_list_length)
 
219
            ref_lists = []
240
220
            loop_counter = 0
241
221
            while loop_counter < self.ref_list_length:
242
222
                ref_list = []
268
248
                    if temp_ptr == NULL:
269
249
                        # key runs to the end
270
250
                        temp_ptr = ref_ptr
271
 
                                        
272
251
                    PyList_Append(ref_list, self.extract_key(temp_ptr))
273
 
                ref_list = StaticTuple_Intern(StaticTuple(*ref_list))
274
 
                Py_INCREF(ref_list)
275
 
                StaticTuple_SET_ITEM(ref_lists, loop_counter - 1, ref_list)
 
252
                PyList_Append(ref_lists, tuple(ref_list))
276
253
                # prepare for the next reference list
277
254
                self._start = next_start
278
 
            node_value = StaticTuple(value, ref_lists)
 
255
            ref_lists = tuple(ref_lists)
 
256
            node_value = (value, ref_lists)
279
257
        else:
280
258
            if last != self._start:
281
259
                # unexpected reference data present
282
260
                raise AssertionError("unexpected reference data present")
283
 
            node_value = StaticTuple(value, StaticTuple())
284
 
        PyList_Append(self.keys, StaticTuple(key, node_value))
 
261
            node_value = (value, ())
 
262
        PyList_Append(self.keys, (key, node_value))
285
263
        return 0
286
264
 
287
265
    def parse(self):
316
294
    cdef Py_ssize_t flat_len
317
295
    cdef Py_ssize_t key_len
318
296
    cdef Py_ssize_t node_len
 
297
    cdef PyObject * val
319
298
    cdef char * value
320
299
    cdef Py_ssize_t value_len
321
300
    cdef char * out
324
303
    cdef int first_ref_list
325
304
    cdef int first_reference
326
305
    cdef int i
 
306
    cdef PyObject *ref_bit
327
307
    cdef Py_ssize_t ref_bit_len
328
308
 
329
 
    if not PyTuple_CheckExact(node) and not StaticTuple_CheckExact(node):
330
 
        raise TypeError('We expected a tuple() or StaticTuple() for node not: %s'
 
309
    if not PyTuple_CheckExact(node):
 
310
        raise TypeError('We expected a tuple() for node not: %s'
331
311
            % type(node))
332
 
    node_len = len(node)
 
312
    node_len = PyTuple_GET_SIZE(node)
333
313
    have_reference_lists = reference_lists
334
314
    if have_reference_lists:
335
315
        if node_len != 4:
339
319
        raise ValueError('Without ref_lists, we need at least 3 entries not: %s'
340
320
            % len(node))
341
321
    # I don't expect that we can do faster than string.join()
342
 
    string_key = '\0'.join(node[1])# <object>PyTuple_GET_ITEM_ptr_object(node, 1))
 
322
    string_key = '\0'.join(<object>PyTuple_GET_ITEM_ptr_object(node, 1))
343
323
 
344
324
    # TODO: instead of using string joins, precompute the final string length,
345
325
    #       and then malloc a single string and copy everything in.
356
336
    refs_len = 0
357
337
    if have_reference_lists:
358
338
        # Figure out how many bytes it will take to store the references
359
 
        ref_lists = node[3]# <object>PyTuple_GET_ITEM_ptr_object(node, 3)
 
339
        ref_lists = <object>PyTuple_GET_ITEM_ptr_object(node, 3)
360
340
        next_len = len(ref_lists) # TODO: use a Py function
361
341
        if next_len > 0:
362
342
            # If there are no nodes, we don't need to do any work
370
350
                    # references
371
351
                    refs_len = refs_len + (next_len - 1)
372
352
                    for reference in ref_list:
373
 
                        if (not PyTuple_CheckExact(reference)
374
 
                            and not StaticTuple_CheckExact(reference)):
 
353
                        if not PyTuple_CheckExact(reference):
375
354
                            raise TypeError(
376
355
                                'We expect references to be tuples not: %s'
377
356
                                % type(reference))
378
 
                        next_len = len(reference)
 
357
                        next_len = PyTuple_GET_SIZE(reference)
379
358
                        if next_len > 0:
380
359
                            # We will need (len - 1) '\x00' characters to
381
360
                            # separate the reference key
382
361
                            refs_len = refs_len + (next_len - 1)
383
362
                            for i from 0 <= i < next_len:
384
 
                                ref_bit = reference[i]
385
 
                                if not PyString_CheckExact(ref_bit):
 
363
                                ref_bit = PyTuple_GET_ITEM_ptr_object(reference, i)
 
364
                                if not PyString_CheckExact_ptr(ref_bit):
386
365
                                    raise TypeError('We expect reference bits'
387
366
                                        ' to be strings not: %s'
388
367
                                        % type(<object>ref_bit))
389
 
                                refs_len = refs_len + PyString_GET_SIZE(ref_bit)
 
368
                                refs_len = refs_len + PyString_GET_SIZE_ptr(ref_bit)
390
369
 
391
370
    # So we have the (key NULL refs NULL value LF)
392
371
    key_len = PyString_Size(string_key)
393
 
    val = node[2] # PyTuple_GET_ITEM_ptr_object(node, 2)
394
 
    if not PyString_CheckExact(val):
 
372
    val = PyTuple_GET_ITEM_ptr_object(node, 2)
 
373
    if not PyString_CheckExact_ptr(val):
395
374
        raise TypeError('Expected a plain str for value not: %s'
396
 
                        % type(val))
397
 
    value = PyString_AS_STRING(val)
398
 
    value_len = PyString_GET_SIZE(val)
 
375
                        % type(<object>val))
 
376
    value = PyString_AS_STRING_ptr(val)
 
377
    value_len = PyString_GET_SIZE_ptr(val)
399
378
    flat_len = (key_len + 1 + refs_len + 1 + value_len + 1)
400
379
    line = PyString_FromStringAndSize(NULL, flat_len)
401
380
    # Get a pointer to the new buffer
417
396
                    out[0] = c'\r'
418
397
                    out = out + 1
419
398
                first_reference = 0
420
 
                next_len = len(reference)
 
399
                next_len = PyTuple_GET_SIZE(reference)
421
400
                for i from 0 <= i < next_len:
422
401
                    if i != 0:
423
402
                        out[0] = c'\x00'
424
403
                        out = out + 1
425
 
                    ref_bit = reference[i] #PyTuple_GET_ITEM_ptr_object(reference, i)
426
 
                    ref_bit_len = PyString_GET_SIZE(ref_bit)
427
 
                    memcpy(out, PyString_AS_STRING(ref_bit), ref_bit_len)
 
404
                    ref_bit = PyTuple_GET_ITEM_ptr_object(reference, i)
 
405
                    ref_bit_len = PyString_GET_SIZE_ptr(ref_bit)
 
406
                    memcpy(out, PyString_AS_STRING_ptr(ref_bit), ref_bit_len)
428
407
                    out = out + ref_bit_len
429
408
    out[0] = c'\0'
430
409
    out = out  + 1