4672.2.2
by John Arbash Meinel
Switch to using a for loop, and add only when checking last rather than |
1 |
# Copyright (C) 2008, 2009 Canonical Ltd
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
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
|
|
4183.7.1
by Sabin Iacob
update FSF mailing address |
15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
3641.3.29
by John Arbash Meinel
Cleanup the copyright headers |
16 |
#
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
17 |
|
18 |
"""Pyrex extensions to btree node parsing."""
|
|
19 |
||
3731.1.1
by Robert Collins
* The C extensions now build on python 2.4 (Robert Collins, #271939) |
20 |
#python2.4 support
|
21 |
cdef extern from "python-compat.h": |
|
22 |
pass
|
|
23 |
||
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
24 |
cdef extern from "stdlib.h": |
25 |
ctypedef unsigned size_t |
|
26 |
||
27 |
cdef extern from "Python.h": |
|
3641.3.32
by John Arbash Meinel
PQM's pyrex version requires Py_ssize_t to be manually defined |
28 |
ctypedef int Py_ssize_t # Required for older pyrex versions |
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
29 |
ctypedef struct PyObject: |
30 |
pass
|
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
31 |
int PyList_Append(object lst, object item) except -1 |
32 |
||
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
33 |
char *PyString_AsString(object p) except NULL |
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
34 |
object PyString_FromStringAndSize(char *, Py_ssize_t) |
4075.3.1
by John Arbash Meinel
Use PyString_InternInPlace to intern() the various parts of keys that are processed. |
35 |
PyObject *PyString_FromStringAndSize_ptr "PyString_FromStringAndSize" (char *, Py_ssize_t) |
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
36 |
int PyString_CheckExact(object s) |
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
37 |
int PyString_CheckExact_ptr "PyString_CheckExact" (PyObject *) |
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
38 |
Py_ssize_t PyString_Size(object p) |
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
39 |
Py_ssize_t PyString_GET_SIZE_ptr "PyString_GET_SIZE" (PyObject *) |
40 |
char * PyString_AS_STRING_ptr "PyString_AS_STRING" (PyObject *) |
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
41 |
char * PyString_AS_STRING(object) |
42 |
Py_ssize_t PyString_GET_SIZE(object) |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
43 |
int PyString_AsStringAndSize_ptr(PyObject *, char **buf, Py_ssize_t *len) |
4075.3.1
by John Arbash Meinel
Use PyString_InternInPlace to intern() the various parts of keys that are processed. |
44 |
void PyString_InternInPlace(PyObject **) |
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
45 |
int PyTuple_CheckExact(object t) |
4672.2.1
by John Arbash Meinel
Speed up the innermost btree parsing function. |
46 |
object PyTuple_New(Py_ssize_t n_entries) |
47 |
void PyTuple_SET_ITEM(object, Py_ssize_t offset, object) # steals the ref |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
48 |
Py_ssize_t PyTuple_GET_SIZE(object t) |
49 |
PyObject *PyTuple_GET_ITEM_ptr_object "PyTuple_GET_ITEM" (object tpl, int index) |
|
4672.2.1
by John Arbash Meinel
Speed up the innermost btree parsing function. |
50 |
void Py_INCREF(object) |
4075.3.1
by John Arbash Meinel
Use PyString_InternInPlace to intern() the various parts of keys that are processed. |
51 |
void Py_DECREF_ptr "Py_DECREF" (PyObject *) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
52 |
|
53 |
cdef extern from "string.h": |
|
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
54 |
void *memcpy(void *dest, void *src, size_t n) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
55 |
void *memchr(void *s, int c, size_t n) |
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
56 |
# GNU extension
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
57 |
# void *memrchr(void *s, int c, size_t n)
|
58 |
int strncmp(char *s1, char *s2, size_t n) |
|
59 |
||
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
60 |
# It seems we need to import the definitions so that the pyrex compiler has
|
61 |
# local names to access them.
|
|
4679.3.74
by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code. |
62 |
from _static_tuple_c cimport StaticTuple, \ |
63 |
import_static_tuple_c, STATIC_TUPLE_ALL_STRING, StaticTuple_New, \ |
|
64 |
StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact |
|
4679.3.25
by John Arbash Meinel
Using Key_New() rather than Key(PyTuple_New()) gets us to 720ms. |
65 |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
66 |
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
67 |
# TODO: Find some way to import this from _dirstate_helpers
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
68 |
cdef void* _my_memrchr(void *s, int c, size_t n): |
69 |
# memrchr seems to be a GNU extension, so we have to implement it ourselves
|
|
70 |
# It is not present in any win32 standard library
|
|
71 |
cdef char *pos |
|
72 |
cdef char *start |
|
73 |
||
74 |
start = <char*>s |
|
75 |
pos = start + n - 1 |
|
76 |
while pos >= start: |
|
77 |
if pos[0] == c: |
|
78 |
return <void*>pos |
|
79 |
pos = pos - 1 |
|
80 |
return NULL |
|
81 |
||
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
82 |
# TODO: Import this from _dirstate_helpers when it is merged
|
83 |
cdef object safe_string_from_size(char *s, Py_ssize_t size): |
|
84 |
if size < 0: |
|
85 |
raise AssertionError( |
|
86 |
'tried to create a string with an invalid size: %d @0x%x' |
|
87 |
% (size, <int>s)) |
|
88 |
return PyString_FromStringAndSize(s, size) |
|
89 |
||
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
90 |
|
4075.3.1
by John Arbash Meinel
Use PyString_InternInPlace to intern() the various parts of keys that are processed. |
91 |
cdef object safe_interned_string_from_size(char *s, Py_ssize_t size): |
92 |
cdef PyObject *py_str |
|
93 |
if size < 0: |
|
94 |
raise AssertionError( |
|
95 |
'tried to create a string with an invalid size: %d @0x%x' |
|
96 |
% (size, <int>s)) |
|
97 |
py_str = PyString_FromStringAndSize_ptr(s, size) |
|
98 |
PyString_InternInPlace(&py_str) |
|
99 |
result = <object>py_str |
|
100 |
# Casting a PyObject* to an <object> triggers an INCREF from Pyrex, so we
|
|
101 |
# DECREF it to avoid geting immortal strings
|
|
102 |
Py_DECREF_ptr(py_str) |
|
103 |
return result |
|
104 |
||
4679.3.74
by John Arbash Meinel
Revert back to before I started trying to move to pyrex/cython code. |
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() |
|
4679.3.11
by John Arbash Meinel
Get Keys into the btree implementation. |
110 |
|
4075.3.1
by John Arbash Meinel
Use PyString_InternInPlace to intern() the various parts of keys that are processed. |
111 |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
112 |
cdef class BTreeLeafParser: |
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
113 |
"""Parse the leaf nodes of a BTree index.
|
114 |
||
115 |
:ivar bytes: The PyString object containing the uncompressed text for the
|
|
116 |
node.
|
|
117 |
:ivar key_length: An integer describing how many pieces the keys have for
|
|
118 |
this index.
|
|
119 |
:ivar ref_list_length: An integer describing how many references this index
|
|
120 |
contains.
|
|
121 |
:ivar keys: A PyList of keys found in this node.
|
|
122 |
||
123 |
:ivar _cur_str: A pointer to the start of the next line to parse
|
|
124 |
:ivar _end_str: A pointer to the end of bytes
|
|
125 |
:ivar _start: Pointer to the location within the current line while
|
|
126 |
parsing.
|
|
127 |
:ivar _header_found: True when we have parsed the header for this node
|
|
128 |
"""
|
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
129 |
|
130 |
cdef object bytes |
|
131 |
cdef int key_length |
|
132 |
cdef int ref_list_length |
|
133 |
cdef object keys |
|
134 |
||
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
135 |
cdef char * _cur_str |
136 |
cdef char * _end_str |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
137 |
# The current start point for parsing
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
138 |
cdef char * _start |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
139 |
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
140 |
cdef int _header_found |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
141 |
|
142 |
def __init__(self, bytes, key_length, ref_list_length): |
|
143 |
self.bytes = bytes |
|
144 |
self.key_length = key_length |
|
145 |
self.ref_list_length = ref_list_length |
|
146 |
self.keys = [] |
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
147 |
self._cur_str = NULL |
148 |
self._end_str = NULL |
|
149 |
self._header_found = 0 |
|
4679.3.47
by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface. |
150 |
# keys are tuples
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
151 |
|
152 |
cdef extract_key(self, char * last): |
|
153 |
"""Extract a key.
|
|
154 |
||
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
155 |
:param last: points at the byte after the last byte permitted for the
|
156 |
key.
|
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
157 |
"""
|
158 |
cdef char *temp_ptr |
|
159 |
cdef int loop_counter |
|
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
160 |
cdef StaticTuple key |
4679.3.47
by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface. |
161 |
|
162 |
key = StaticTuple_New(self.key_length) |
|
4672.2.2
by John Arbash Meinel
Switch to using a for loop, and add only when checking last rather than |
163 |
for loop_counter from 0 <= loop_counter < self.key_length: |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
164 |
# grab a key segment
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
165 |
temp_ptr = <char*>memchr(self._start, c'\0', last - self._start) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
166 |
if temp_ptr == NULL: |
4672.2.2
by John Arbash Meinel
Switch to using a for loop, and add only when checking last rather than |
167 |
if loop_counter + 1 == self.key_length: |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
168 |
# capture to last
|
169 |
temp_ptr = last |
|
170 |
else: |
|
171 |
# Invalid line
|
|
172 |
failure_string = ("invalid key, wanted segment from " + |
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
173 |
repr(safe_string_from_size(self._start, |
174 |
last - self._start))) |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
175 |
raise AssertionError(failure_string) |
176 |
# capture the key string
|
|
4679.3.50
by John Arbash Meinel
Add another quick check, since key_length must be 1 for chk keys. |
177 |
if (self.key_length == 1 |
178 |
and (temp_ptr - self._start) == 45 |
|
4679.3.48
by John Arbash Meinel
Prototype of not interning sha1: strings. |
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, |
|
4075.3.1
by John Arbash Meinel
Use PyString_InternInPlace to intern() the various parts of keys that are processed. |
184 |
temp_ptr - self._start) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
185 |
# advance our pointer
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
186 |
self._start = temp_ptr + 1 |
4672.2.1
by John Arbash Meinel
Speed up the innermost btree parsing function. |
187 |
Py_INCREF(key_element) |
4679.3.47
by John Arbash Meinel
Work out how to expose the C api using the Python PyCObject interface. |
188 |
StaticTuple_SET_ITEM(key, loop_counter, key_element) |
4679.3.51
by John Arbash Meinel
Add a _static_tuple_c.pxd file to define the C api to pyrex code. |
189 |
# key->flags = key->flags | STATIC_TUPLE_ALL_STRING
|
4679.3.59
by John Arbash Meinel
Capitolize StaticTuple_Intern to conform more to the python C apis. |
190 |
key = StaticTuple_Intern(key) |
4679.3.48
by John Arbash Meinel
Prototype of not interning sha1: strings. |
191 |
return key |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
192 |
|
193 |
cdef int process_line(self) except -1: |
|
194 |
"""Process a line in the bytes."""
|
|
195 |
cdef char *last |
|
196 |
cdef char *temp_ptr |
|
197 |
cdef char *ref_ptr |
|
198 |
cdef char *next_start |
|
199 |
cdef int loop_counter |
|
200 |
||
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
201 |
self._start = self._cur_str |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
202 |
# Find the next newline
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
203 |
last = <char*>memchr(self._start, c'\n', self._end_str - self._start) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
204 |
if last == NULL: |
205 |
# Process until the end of the file
|
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
206 |
last = self._end_str |
207 |
self._cur_str = self._end_str |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
208 |
else: |
209 |
# And the next string is right after it
|
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
210 |
self._cur_str = last + 1 |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
211 |
# The last character is right before the '\n'
|
212 |
||
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
213 |
if last == self._start: |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
214 |
# parsed it all.
|
215 |
return 0 |
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
216 |
if last < self._start: |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
217 |
# Unexpected error condition - fail
|
4634.60.1
by John Arbash Meinel
Don't return -1 directly, instead raise an exception. |
218 |
raise AssertionError("last < self._start") |
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
219 |
if 0 == self._header_found: |
220 |
# The first line in a leaf node is the header "type=leaf\n"
|
|
221 |
if strncmp("type=leaf", self._start, last - self._start) == 0: |
|
222 |
self._header_found = 1 |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
223 |
return 0 |
224 |
else: |
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
225 |
raise AssertionError('Node did not start with "type=leaf": %r' |
226 |
% (safe_string_from_size(self._start, last - self._start))) |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
227 |
|
228 |
key = self.extract_key(last) |
|
229 |
# find the value area
|
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
230 |
temp_ptr = <char*>_my_memrchr(self._start, c'\0', last - self._start) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
231 |
if temp_ptr == NULL: |
232 |
# Invalid line
|
|
4634.60.1
by John Arbash Meinel
Don't return -1 directly, instead raise an exception. |
233 |
raise AssertionError("Failed to find the value area") |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
234 |
else: |
235 |
# capture the value string
|
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
236 |
value = safe_string_from_size(temp_ptr + 1, last - temp_ptr - 1) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
237 |
# shrink the references end point
|
238 |
last = temp_ptr |
|
239 |
if self.ref_list_length: |
|
240 |
ref_lists = [] |
|
241 |
loop_counter = 0 |
|
242 |
while loop_counter < self.ref_list_length: |
|
243 |
ref_list = [] |
|
244 |
# extract a reference list
|
|
245 |
loop_counter = loop_counter + 1 |
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
246 |
if last < self._start: |
4634.60.1
by John Arbash Meinel
Don't return -1 directly, instead raise an exception. |
247 |
raise AssertionError("last < self._start") |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
248 |
# find the next reference list end point:
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
249 |
temp_ptr = <char*>memchr(self._start, c'\t', last - self._start) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
250 |
if temp_ptr == NULL: |
251 |
# Only valid for the last list
|
|
252 |
if loop_counter != self.ref_list_length: |
|
253 |
# Invalid line
|
|
4634.60.1
by John Arbash Meinel
Don't return -1 directly, instead raise an exception. |
254 |
raise AssertionError( |
255 |
"invalid key, loop_counter != self.ref_list_length") |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
256 |
else: |
257 |
# scan to the end of the ref list area
|
|
258 |
ref_ptr = last |
|
259 |
next_start = last |
|
260 |
else: |
|
261 |
# scan to the end of this ref list
|
|
262 |
ref_ptr = temp_ptr |
|
263 |
next_start = temp_ptr + 1 |
|
264 |
# Now, there may be multiple keys in the ref list.
|
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
265 |
while self._start < ref_ptr: |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
266 |
# loop finding keys and extracting them
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
267 |
temp_ptr = <char*>memchr(self._start, c'\r', |
268 |
ref_ptr - self._start) |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
269 |
if temp_ptr == NULL: |
270 |
# key runs to the end
|
|
271 |
temp_ptr = ref_ptr |
|
4679.3.28
by John Arbash Meinel
Add a KEY_HAS_HASH define. |
272 |
PyList_Append(ref_list, self.extract_key(temp_ptr)) |
4679.3.59
by John Arbash Meinel
Capitolize StaticTuple_Intern to conform more to the python C apis. |
273 |
ref_list = StaticTuple_Intern(StaticTuple(*ref_list)) |
4679.3.11
by John Arbash Meinel
Get Keys into the btree implementation. |
274 |
PyList_Append(ref_lists, ref_list) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
275 |
# prepare for the next reference list
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
276 |
self._start = next_start |
4679.3.48
by John Arbash Meinel
Prototype of not interning sha1: strings. |
277 |
ref_lists = StaticTuple(*ref_lists) |
278 |
node_value = StaticTuple(value, ref_lists) |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
279 |
else: |
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
280 |
if last != self._start: |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
281 |
# unexpected reference data present
|
4634.60.1
by John Arbash Meinel
Don't return -1 directly, instead raise an exception. |
282 |
raise AssertionError("unexpected reference data present") |
4679.3.48
by John Arbash Meinel
Prototype of not interning sha1: strings. |
283 |
node_value = StaticTuple(value, StaticTuple()) |
284 |
PyList_Append(self.keys, StaticTuple(key, node_value)) |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
285 |
return 0 |
286 |
||
287 |
def parse(self): |
|
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
288 |
cdef Py_ssize_t byte_count |
289 |
if not PyString_CheckExact(self.bytes): |
|
290 |
raise AssertionError('self.bytes is not a string.') |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
291 |
byte_count = PyString_Size(self.bytes) |
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
292 |
self._cur_str = PyString_AsString(self.bytes) |
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
293 |
# This points to the last character in the string
|
3641.3.2
by John Arbash Meinel
Clean up some variable names, add some documentation. |
294 |
self._end_str = self._cur_str + byte_count |
295 |
while self._cur_str < self._end_str: |
|
3641.3.1
by John Arbash Meinel
Bring in the btree_index and chunk_writer code and their tests. |
296 |
self.process_line() |
297 |
return self.keys |
|
298 |
||
299 |
||
300 |
def _parse_leaf_lines(bytes, key_length, ref_list_length): |
|
301 |
parser = BTreeLeafParser(bytes, key_length, ref_list_length) |
|
302 |
return parser.parse() |
|
3641.3.18
by John Arbash Meinel
Start working on a compiled function for transforming |
303 |
|
304 |
||
305 |
def _flatten_node(node, reference_lists): |
|
306 |
"""Convert a node into the serialized form.
|
|
307 |
||
308 |
:param node: A tuple representing a node:
|
|
309 |
(index, key_tuple, value, references)
|
|
310 |
:param reference_lists: Does this index have reference lists?
|
|
311 |
:return: (string_key, flattened)
|
|
312 |
string_key The serialized key for referencing this node
|
|
313 |
flattened A string with the serialized form for the contents
|
|
314 |
"""
|
|
3641.3.26
by John Arbash Meinel
A couple small tweaks. |
315 |
cdef int have_reference_lists |
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
316 |
cdef Py_ssize_t flat_len |
317 |
cdef Py_ssize_t key_len |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
318 |
cdef Py_ssize_t node_len |
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
319 |
cdef char * value |
320 |
cdef Py_ssize_t value_len |
|
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
321 |
cdef char * out |
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
322 |
cdef Py_ssize_t refs_len |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
323 |
cdef Py_ssize_t next_len |
3641.3.21
by John Arbash Meinel
Flatten the outermost str.join() into memcpy's |
324 |
cdef int first_ref_list |
3641.3.22
by John Arbash Meinel
flatten the next level |
325 |
cdef int first_reference |
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
326 |
cdef int i |
327 |
cdef Py_ssize_t ref_bit_len |
|
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
328 |
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
329 |
if not PyTuple_CheckExact(node) and not StaticTuple_CheckExact(node): |
330 |
raise TypeError('We expected a tuple() or StaticTuple() for node not: %s' |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
331 |
% type(node)) |
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
332 |
node_len = len(node) |
3641.3.26
by John Arbash Meinel
A couple small tweaks. |
333 |
have_reference_lists = reference_lists |
334 |
if have_reference_lists: |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
335 |
if node_len != 4: |
336 |
raise ValueError('With ref_lists, we expected 4 entries not: %s' |
|
337 |
% len(node)) |
|
338 |
elif node_len < 3: |
|
339 |
raise ValueError('Without ref_lists, we need at least 3 entries not: %s' |
|
340 |
% len(node)) |
|
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
341 |
# I don't expect that we can do faster than string.join()
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
342 |
string_key = '\0'.join(node[1])# <object>PyTuple_GET_ITEM_ptr_object(node, 1)) |
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
343 |
|
3641.3.18
by John Arbash Meinel
Start working on a compiled function for transforming |
344 |
# TODO: instead of using string joins, precompute the final string length,
|
345 |
# and then malloc a single string and copy everything in.
|
|
3641.3.19
by John Arbash Meinel
The flatten code now handles the no-ref-list case. |
346 |
|
347 |
# TODO: We probably want to use PySequenceFast, because we have lists and
|
|
348 |
# tuples, but we aren't sure which we will get.
|
|
349 |
||
350 |
# line := string_key NULL flat_refs NULL value LF
|
|
351 |
# string_key := BYTES (NULL BYTES)*
|
|
352 |
# flat_refs := ref_list (TAB ref_list)*
|
|
353 |
# ref_list := ref (CR ref)*
|
|
354 |
# ref := BYTES (NULL BYTES)*
|
|
355 |
# value := BYTES
|
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
356 |
refs_len = 0 |
3641.3.26
by John Arbash Meinel
A couple small tweaks. |
357 |
if have_reference_lists: |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
358 |
# Figure out how many bytes it will take to store the references
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
359 |
ref_lists = node[3]# <object>PyTuple_GET_ITEM_ptr_object(node, 3) |
3641.3.24
by John Arbash Meinel
Use the compiled flatten function. |
360 |
next_len = len(ref_lists) # TODO: use a Py function |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
361 |
if next_len > 0: |
362 |
# If there are no nodes, we don't need to do any work
|
|
363 |
# Otherwise we will need (len - 1) '\t' characters to separate
|
|
364 |
# the reference lists
|
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
365 |
refs_len = refs_len + (next_len - 1) |
3641.3.24
by John Arbash Meinel
Use the compiled flatten function. |
366 |
for ref_list in ref_lists: |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
367 |
next_len = len(ref_list) |
368 |
if next_len > 0: |
|
369 |
# We will need (len - 1) '\r' characters to separate the
|
|
370 |
# references
|
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
371 |
refs_len = refs_len + (next_len - 1) |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
372 |
for reference in ref_list: |
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
373 |
if (not PyTuple_CheckExact(reference) |
374 |
and not StaticTuple_CheckExact(reference)): |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
375 |
raise TypeError( |
376 |
'We expect references to be tuples not: %s' |
|
377 |
% type(reference)) |
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
378 |
next_len = len(reference) |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
379 |
if next_len > 0: |
380 |
# We will need (len - 1) '\x00' characters to
|
|
381 |
# separate the reference key
|
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
382 |
refs_len = refs_len + (next_len - 1) |
383 |
for i from 0 <= i < next_len: |
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
384 |
ref_bit = reference[i] |
385 |
if not PyString_CheckExact(ref_bit): |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
386 |
raise TypeError('We expect reference bits' |
387 |
' to be strings not: %s' |
|
388 |
% type(<object>ref_bit)) |
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
389 |
refs_len = refs_len + PyString_GET_SIZE(ref_bit) |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
390 |
|
391 |
# So we have the (key NULL refs NULL value LF)
|
|
392 |
key_len = PyString_Size(string_key) |
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
393 |
val = node[2] # PyTuple_GET_ITEM_ptr_object(node, 2) |
394 |
if not PyString_CheckExact(val): |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
395 |
raise TypeError('Expected a plain str for value not: %s' |
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
396 |
% type(val)) |
397 |
value = PyString_AS_STRING(val) |
|
398 |
value_len = PyString_GET_SIZE(val) |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
399 |
flat_len = (key_len + 1 + refs_len + 1 + value_len + 1) |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
400 |
line = PyString_FromStringAndSize(NULL, flat_len) |
401 |
# Get a pointer to the new buffer
|
|
402 |
out = PyString_AsString(line) |
|
403 |
memcpy(out, PyString_AsString(string_key), key_len) |
|
404 |
out = out + key_len |
|
405 |
out[0] = c'\0' |
|
406 |
out = out + 1 |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
407 |
if refs_len > 0: |
3641.3.21
by John Arbash Meinel
Flatten the outermost str.join() into memcpy's |
408 |
first_ref_list = 1 |
3641.3.24
by John Arbash Meinel
Use the compiled flatten function. |
409 |
for ref_list in ref_lists: |
3641.3.21
by John Arbash Meinel
Flatten the outermost str.join() into memcpy's |
410 |
if first_ref_list == 0: |
411 |
out[0] = c'\t' |
|
412 |
out = out + 1 |
|
413 |
first_ref_list = 0 |
|
3641.3.22
by John Arbash Meinel
flatten the next level |
414 |
first_reference = 1 |
3641.3.21
by John Arbash Meinel
Flatten the outermost str.join() into memcpy's |
415 |
for reference in ref_list: |
3641.3.22
by John Arbash Meinel
flatten the next level |
416 |
if first_reference == 0: |
417 |
out[0] = c'\r' |
|
418 |
out = out + 1 |
|
419 |
first_reference = 0 |
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
420 |
next_len = len(reference) |
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
421 |
for i from 0 <= i < next_len: |
422 |
if i != 0: |
|
3641.3.23
by John Arbash Meinel
Flattened all the way down the stack. |
423 |
out[0] = c'\x00' |
424 |
out = out + 1 |
|
4679.3.49
by John Arbash Meinel
Start using StaticTuple as part of the chk_map api. |
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) |
|
3641.3.25
by John Arbash Meinel
Shave off some more time by using exact accessors. |
428 |
out = out + ref_bit_len |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
429 |
out[0] = c'\0' |
3641.3.22
by John Arbash Meinel
flatten the next level |
430 |
out = out + 1 |
3641.3.20
by John Arbash Meinel
We have a single malloc for the final output. |
431 |
memcpy(out, value, value_len) |
432 |
out = out + value_len |
|
433 |
out[0] = c'\n' |
|
3641.3.18
by John Arbash Meinel
Start working on a compiled function for transforming |
434 |
return string_key, line |