/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: Andrew Bennetts
  • Date: 2009-10-08 01:50:30 UTC
  • mfrom: (4731 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4732.
  • Revision ID: andrew.bennetts@canonical.com-20091008015030-8n02kppogh8radr0
MergeĀ lp:bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 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
41
41
    int PyString_AsStringAndSize_ptr(PyObject *, char **buf, Py_ssize_t *len)
42
42
    void PyString_InternInPlace(PyObject **)
43
43
    int PyTuple_CheckExact(object t)
 
44
    object PyTuple_New(Py_ssize_t n_entries)
 
45
    void PyTuple_SET_ITEM(object, Py_ssize_t offset, object) # steals the ref
44
46
    Py_ssize_t PyTuple_GET_SIZE(object t)
45
47
    PyObject *PyTuple_GET_ITEM_ptr_object "PyTuple_GET_ITEM" (object tpl, int index)
 
48
    void Py_INCREF(object)
46
49
    void Py_DECREF_ptr "Py_DECREF" (PyObject *)
47
50
 
48
51
cdef extern from "string.h":
140
143
        cdef char *temp_ptr
141
144
        cdef int loop_counter
142
145
        # keys are tuples
143
 
        loop_counter = 0
144
 
        key_segments = []
145
 
        while loop_counter < self.key_length:
146
 
            loop_counter = loop_counter + 1
 
146
        key = PyTuple_New(self.key_length)
 
147
        for loop_counter from 0 <= loop_counter < self.key_length:
147
148
            # grab a key segment
148
149
            temp_ptr = <char*>memchr(self._start, c'\0', last - self._start)
149
150
            if temp_ptr == NULL:
150
 
                if loop_counter == self.key_length:
 
151
                if loop_counter + 1 == self.key_length:
151
152
                    # capture to last
152
153
                    temp_ptr = last
153
154
                else:
164
165
                                                         temp_ptr - self._start)
165
166
            # advance our pointer
166
167
            self._start = temp_ptr + 1
167
 
            PyList_Append(key_segments, key_element)
168
 
        return tuple(key_segments)
 
168
            Py_INCREF(key_element)
 
169
            PyTuple_SET_ITEM(key, loop_counter, key_element)
 
170
        return key
169
171
 
170
172
    cdef int process_line(self) except -1:
171
173
        """Process a line in the bytes."""
186
188
            # And the next string is right after it
187
189
            self._cur_str = last + 1
188
190
            # The last character is right before the '\n'
189
 
            last = last
190
191
 
191
192
        if last == self._start:
192
193
            # parsed it all.
193
194
            return 0
194
195
        if last < self._start:
195
196
            # Unexpected error condition - fail
196
 
            return -1
 
197
            raise AssertionError("last < self._start")
197
198
        if 0 == self._header_found:
198
199
            # The first line in a leaf node is the header "type=leaf\n"
199
200
            if strncmp("type=leaf", self._start, last - self._start) == 0:
202
203
            else:
203
204
                raise AssertionError('Node did not start with "type=leaf": %r'
204
205
                    % (safe_string_from_size(self._start, last - self._start)))
205
 
                return -1
206
206
 
207
207
        key = self.extract_key(last)
208
208
        # find the value area
209
209
        temp_ptr = <char*>_my_memrchr(self._start, c'\0', last - self._start)
210
210
        if temp_ptr == NULL:
211
211
            # Invalid line
212
 
            return -1
 
212
            raise AssertionError("Failed to find the value area")
213
213
        else:
214
214
            # capture the value string
215
215
            value = safe_string_from_size(temp_ptr + 1, last - temp_ptr - 1)
223
223
                # extract a reference list
224
224
                loop_counter = loop_counter + 1
225
225
                if last < self._start:
226
 
                    return -1
 
226
                    raise AssertionError("last < self._start")
227
227
                # find the next reference list end point:
228
228
                temp_ptr = <char*>memchr(self._start, c'\t', last - self._start)
229
229
                if temp_ptr == NULL:
230
230
                    # Only valid for the last list
231
231
                    if loop_counter != self.ref_list_length:
232
232
                        # Invalid line
233
 
                        return -1
234
 
                        raise AssertionError("invalid key")
 
233
                        raise AssertionError(
 
234
                            "invalid key, loop_counter != self.ref_list_length")
235
235
                    else:
236
236
                        # scan to the end of the ref list area
237
237
                        ref_ptr = last
257
257
        else:
258
258
            if last != self._start:
259
259
                # unexpected reference data present
260
 
                return -1
 
260
                raise AssertionError("unexpected reference data present")
261
261
            node_value = (value, ())
262
262
        PyList_Append(self.keys, (key, node_value))
263
263
        return 0