~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_groupcompress_pyx.pyx

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 08:15:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101008081514-dviqzrdfwyzsqbz2
Split NEWS into per-release doc/en/release-notes/bzr-*.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
        unsigned long agg_offset
47
47
    struct delta_index:
48
48
        pass
49
 
    ctypedef enum delta_result:
50
 
        DELTA_OK
51
 
        DELTA_OUT_OF_MEMORY
52
 
        DELTA_INDEX_NEEDED
53
 
        DELTA_SOURCE_EMPTY
54
 
        DELTA_SOURCE_BAD
55
 
        DELTA_BUFFER_EMPTY
56
 
        DELTA_SIZE_TOO_BIG
57
 
    delta_result create_delta_index(source_info *src,
58
 
                                    delta_index *old,
59
 
                                    delta_index **fresh,
60
 
                                    int max_entries) nogil
61
 
    delta_result create_delta_index_from_delta(source_info *delta,
62
 
                                               delta_index *old,
63
 
                                               delta_index **fresh) nogil
 
49
    delta_index * create_delta_index(source_info *src, delta_index *old) nogil
 
50
    delta_index * create_delta_index_from_delta(source_info *delta,
 
51
                                                delta_index *old) nogil
64
52
    void free_delta_index(delta_index *index) nogil
65
 
    delta_result create_delta(delta_index *indexes,
66
 
                              void *buf, unsigned long bufsize,
67
 
                              unsigned long *delta_size,
68
 
                              unsigned long max_delta_size,
69
 
                              void **delta_data) nogil
 
53
    void *create_delta(delta_index *indexes,
 
54
             void *buf, unsigned long bufsize,
 
55
             unsigned long *delta_size, unsigned long max_delta_size) nogil
70
56
    unsigned long get_delta_hdr_size(unsigned char **datap,
71
57
                                     unsigned char *top) nogil
72
58
    unsigned long sizeof_delta_index(delta_index *index)
73
59
    Py_ssize_t DELTA_SIZE_MIN
74
 
    int get_hash_offset(delta_index *index, int pos, unsigned int *hash_offset)
75
 
    int get_entry_summary(delta_index *index, int pos,
76
 
                          unsigned int *global_offset, unsigned int *hash_val)
77
 
    unsigned int rabin_hash (unsigned char *data)
78
60
 
79
61
 
80
62
cdef void *safe_malloc(size_t count) except NULL:
104
86
    return DeltaIndex(source)
105
87
 
106
88
 
107
 
cdef object _translate_delta_failure(delta_result result):
108
 
    if result == DELTA_OUT_OF_MEMORY:
109
 
        return MemoryError("Delta function failed to allocate memory")
110
 
    elif result == DELTA_INDEX_NEEDED:
111
 
        return ValueError("Delta function requires delta_index param")
112
 
    elif result == DELTA_SOURCE_EMPTY:
113
 
        return ValueError("Delta function given empty source_info param")
114
 
    elif result == DELTA_SOURCE_BAD:
115
 
        return RuntimeError("Delta function given invalid source_info param")
116
 
    elif result == DELTA_BUFFER_EMPTY:
117
 
        return ValueError("Delta function given empty buffer params")
118
 
    return AssertionError("Unrecognised delta result code: %d" % result)
119
 
 
120
 
 
121
 
def _rabin_hash(content):
122
 
    if not PyString_CheckExact(content):
123
 
        raise ValueError('content must be a string')
124
 
    if len(content) < 16:
125
 
        raise ValueError('content must be at least 16 bytes long')
126
 
    # Try to cast it to an int, if it can fit
127
 
    return int(rabin_hash(<unsigned char*>(PyString_AS_STRING(content))))
128
 
 
129
 
 
130
89
cdef class DeltaIndex:
131
90
 
132
91
    # We need Pyrex 0.9.8+ to understand a 'list' definition, and this object
137
96
    cdef delta_index *_index
138
97
    cdef public unsigned long _source_offset
139
98
    cdef readonly unsigned int _max_num_sources
140
 
    cdef public int _max_bytes_to_index
141
99
 
142
 
    def __init__(self, source=None, max_bytes_to_index=None):
 
100
    def __init__(self, source=None):
143
101
        self._sources = []
144
102
        self._index = NULL
145
103
        self._max_num_sources = 65000
146
104
        self._source_infos = <source_info *>safe_malloc(sizeof(source_info)
147
105
                                                        * self._max_num_sources)
148
106
        self._source_offset = 0
149
 
        self._max_bytes_to_index = 0
150
 
        if max_bytes_to_index is not None:
151
 
            self._max_bytes_to_index = max_bytes_to_index
152
107
 
153
108
        if source is not None:
154
109
            self.add_source(source, 0)
182
137
    def _has_index(self):
183
138
        return (self._index != NULL)
184
139
 
185
 
    def _dump_index(self):
186
 
        """Dump the pointers in the index.
187
 
 
188
 
        This is an arbitrary layout, used for testing. It is not meant to be
189
 
        used in production code.
190
 
 
191
 
        :return: (hash_list, entry_list)
192
 
            hash_list   A list of offsets, so hash[i] points to the 'hash
193
 
                        bucket' starting at the given offset and going until
194
 
                        hash[i+1]
195
 
            entry_list  A list of (text_offset, hash_val). text_offset is the
196
 
                        offset in the "source" texts, and hash_val is the RABIN
197
 
                        hash for that offset.
198
 
                        Note that the entry should be in the hash bucket
199
 
                        defined by
200
 
                        hash[(hash_val & mask)] && hash[(hash_val & mask) + 1]
201
 
        """
202
 
        cdef int pos
203
 
        cdef unsigned int text_offset
204
 
        cdef unsigned int hash_val
205
 
        cdef unsigned int hash_offset
206
 
        if self._index == NULL:
207
 
            return None
208
 
        hash_list = []
209
 
        pos = 0
210
 
        while get_hash_offset(self._index, pos, &hash_offset):
211
 
            hash_list.append(int(hash_offset))
212
 
            pos += 1
213
 
        entry_list = []
214
 
        pos = 0
215
 
        while get_entry_summary(self._index, pos, &text_offset, &hash_val):
216
 
            # Map back using 'int' so that we don't get Long everywhere, when
217
 
            # almost everything is <2**31.
218
 
            val = tuple(map(int, [text_offset, hash_val]))
219
 
            entry_list.append(val)
220
 
            pos += 1
221
 
        return hash_list, entry_list
222
 
 
223
140
    def add_delta_source(self, delta, unadded_bytes):
224
141
        """Add a new delta to the source texts.
225
142
 
230
147
        cdef char *c_delta
231
148
        cdef Py_ssize_t c_delta_size
232
149
        cdef delta_index *index
233
 
        cdef delta_result res
234
150
        cdef unsigned int source_location
235
151
        cdef source_info *src
236
152
        cdef unsigned int num_indexes
249
165
        src.size = c_delta_size
250
166
        src.agg_offset = self._source_offset + unadded_bytes
251
167
        with nogil:
252
 
            res = create_delta_index_from_delta(src, self._index, &index)
253
 
        if res != DELTA_OK:
254
 
            raise _translate_delta_failure(res)
 
168
            index = create_delta_index_from_delta(src, self._index)
255
169
        self._source_offset = src.agg_offset + src.size
256
 
        if index != self._index:
 
170
        if index != NULL:
257
171
            free_delta_index(self._index)
258
172
            self._index = index
259
173
 
263
177
        :param source: The text in question, this must be a byte string
264
178
        :param unadded_bytes: Assume there are this many bytes that didn't get
265
179
            added between this source and the end of the previous source.
266
 
        :param max_pointers: Add no more than this many entries to the index.
267
 
            By default, we sample every 16 bytes, if that would require more
268
 
            than max_entries, we will reduce the sampling rate.
269
 
            A value of 0 means unlimited, None means use the default limit.
270
180
        """
271
181
        cdef char *c_source
272
182
        cdef Py_ssize_t c_source_size
273
183
        cdef delta_index *index
274
 
        cdef delta_result res
275
184
        cdef unsigned int source_location
276
185
        cdef source_info *src
277
186
        cdef unsigned int num_indexes
278
 
        cdef int max_num_entries
279
187
 
280
188
        if not PyString_CheckExact(source):
281
189
            raise TypeError('source is not a str')
298
206
        # We delay creating the index on the first insert
299
207
        if source_location != 0:
300
208
            with nogil:
301
 
                res = create_delta_index(src, self._index, &index,
302
 
                                         self._max_bytes_to_index)
303
 
            if res != DELTA_OK:
304
 
                raise _translate_delta_failure(res)
305
 
            if index != self._index:
 
209
                index = create_delta_index(src, self._index)
 
210
            if index != NULL:
306
211
                free_delta_index(self._index)
307
212
                self._index = index
308
213
 
309
214
    cdef _populate_first_index(self):
310
215
        cdef delta_index *index
311
 
        cdef delta_result res
312
216
        if len(self._sources) != 1 or self._index != NULL:
313
217
            raise AssertionError('_populate_first_index should only be'
314
218
                ' called when we have a single source and no index yet')
315
219
 
316
 
        # We know that self._index is already NULL, so create_delta_index
317
 
        # will always create a new index unless there's a malloc failure
 
220
        # We know that self._index is already NULL, so whatever
 
221
        # create_delta_index returns is fine
318
222
        with nogil:
319
 
            res = create_delta_index(&self._source_infos[0], NULL, &index,
320
 
                                     self._max_bytes_to_index)
321
 
        if res != DELTA_OK:
322
 
            raise _translate_delta_failure(res)
323
 
        self._index = index
 
223
            self._index = create_delta_index(&self._source_infos[0], NULL)
 
224
        assert self._index != NULL
324
225
 
325
226
    cdef _expand_sources(self):
326
227
        raise RuntimeError('if we move self._source_infos, then we need to'
337
238
        cdef void * delta
338
239
        cdef unsigned long delta_size
339
240
        cdef unsigned long c_max_delta_size
340
 
        cdef delta_result res
341
241
 
342
242
        if self._index == NULL:
343
243
            if len(self._sources) == 0:
356
256
        #       allocate the bytes into the final string
357
257
        c_max_delta_size = max_delta_size
358
258
        with nogil:
359
 
            res = create_delta(self._index, target, target_size,
360
 
                               &delta_size, c_max_delta_size, &delta)
 
259
            delta = create_delta(self._index,
 
260
                                 target, target_size,
 
261
                                 &delta_size, c_max_delta_size)
361
262
        result = None
362
 
        if res == DELTA_OK:
 
263
        if delta:
363
264
            result = PyString_FromStringAndSize(<char *>delta, delta_size)
364
265
            free(delta)
365
 
        elif res != DELTA_SIZE_TOO_BIG:
366
 
            raise _translate_delta_failure(res)
367
266
        return result
368
267
 
369
268
 
470
369
                # Copy instruction
471
370
                data = _decode_copy_instruction(data, cmd, &cp_off, &cp_size)
472
371
                if (cp_off + cp_size < cp_size or
473
 
                    cp_off + cp_size > <unsigned int>source_size or
474
 
                    cp_size > <unsigned int>size):
 
372
                    cp_off + cp_size > source_size or
 
373
                    cp_size > size):
475
374
                    failed = 1
476
375
                    break
477
376
                memcpy(out, source + cp_off, cp_size)