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)
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
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)
105
from bzrlib import _static_tuple_c
107
_ST = _static_tuple_c.StaticTuple
108
# This sets up the StaticTuple C_API functionality
109
import_static_tuple_c()
112
98
cdef class BTreeLeafParser:
113
99
"""Parse the leaf nodes of a BTree index.
158
143
cdef char *temp_ptr
159
144
cdef int loop_counter
162
key = StaticTuple_New(self.key_length)
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)
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)
192
172
cdef int process_line(self) except -1:
268
248
if temp_ptr == NULL:
269
249
# key runs to the end
270
250
temp_ptr = ref_ptr
272
251
PyList_Append(ref_list, self.extract_key(temp_ptr))
273
ref_list = StaticTuple_Intern(StaticTuple(*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)
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))
324
303
cdef int first_ref_list
325
304
cdef int first_reference
306
cdef PyObject *ref_bit
327
307
cdef Py_ssize_t ref_bit_len
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'
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'
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))
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.
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
362
342
# If there are no nodes, we don't need to do any work
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):
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)
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)
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'
397
value = PyString_AS_STRING(val)
398
value_len = PyString_GET_SIZE(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
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:
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