##// END OF EJS Templates
index: move index_clearcaches() further down...
Martin von Zweigbergk -
r38979:c1de6769 default
parent child Browse files
Show More
@@ -1,2191 +1,2191 b''
1 /*
1 /*
2 parsers.c - efficient content parsing
2 parsers.c - efficient content parsing
3
3
4 Copyright 2008 Matt Mackall <mpm@selenic.com> and others
4 Copyright 2008 Matt Mackall <mpm@selenic.com> and others
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8 */
8 */
9
9
10 #include <Python.h>
10 #include <Python.h>
11 #include <assert.h>
11 #include <assert.h>
12 #include <ctype.h>
12 #include <ctype.h>
13 #include <stddef.h>
13 #include <stddef.h>
14 #include <string.h>
14 #include <string.h>
15
15
16 #include "bitmanipulation.h"
16 #include "bitmanipulation.h"
17 #include "charencode.h"
17 #include "charencode.h"
18 #include "util.h"
18 #include "util.h"
19
19
20 #ifdef IS_PY3K
20 #ifdef IS_PY3K
21 /* The mapping of Python types is meant to be temporary to get Python
21 /* The mapping of Python types is meant to be temporary to get Python
22 * 3 to compile. We should remove this once Python 3 support is fully
22 * 3 to compile. We should remove this once Python 3 support is fully
23 * supported and proper types are used in the extensions themselves. */
23 * supported and proper types are used in the extensions themselves. */
24 #define PyInt_Check PyLong_Check
24 #define PyInt_Check PyLong_Check
25 #define PyInt_FromLong PyLong_FromLong
25 #define PyInt_FromLong PyLong_FromLong
26 #define PyInt_FromSsize_t PyLong_FromSsize_t
26 #define PyInt_FromSsize_t PyLong_FromSsize_t
27 #define PyInt_AS_LONG PyLong_AS_LONG
27 #define PyInt_AS_LONG PyLong_AS_LONG
28 #define PyInt_AsLong PyLong_AsLong
28 #define PyInt_AsLong PyLong_AsLong
29 #endif
29 #endif
30
30
31 typedef struct indexObjectStruct indexObject;
31 typedef struct indexObjectStruct indexObject;
32
32
33 typedef struct {
33 typedef struct {
34 int children[16];
34 int children[16];
35 } nodetreenode;
35 } nodetreenode;
36
36
37 /*
37 /*
38 * A base-16 trie for fast node->rev mapping.
38 * A base-16 trie for fast node->rev mapping.
39 *
39 *
40 * Positive value is index of the next node in the trie
40 * Positive value is index of the next node in the trie
41 * Negative value is a leaf: -(rev + 2)
41 * Negative value is a leaf: -(rev + 2)
42 * Zero is empty
42 * Zero is empty
43 */
43 */
44 typedef struct {
44 typedef struct {
45 indexObject *index;
45 indexObject *index;
46 nodetreenode *nodes;
46 nodetreenode *nodes;
47 unsigned length; /* # nodes in use */
47 unsigned length; /* # nodes in use */
48 unsigned capacity; /* # nodes allocated */
48 unsigned capacity; /* # nodes allocated */
49 int depth; /* maximum depth of tree */
49 int depth; /* maximum depth of tree */
50 int splits; /* # splits performed */
50 int splits; /* # splits performed */
51 } nodetree;
51 } nodetree;
52
52
53 /*
53 /*
54 * This class has two behaviors.
54 * This class has two behaviors.
55 *
55 *
56 * When used in a list-like way (with integer keys), we decode an
56 * When used in a list-like way (with integer keys), we decode an
57 * entry in a RevlogNG index file on demand. Our last entry is a
57 * entry in a RevlogNG index file on demand. Our last entry is a
58 * sentinel, always a nullid. We have limited support for
58 * sentinel, always a nullid. We have limited support for
59 * integer-keyed insert and delete, only at elements right before the
59 * integer-keyed insert and delete, only at elements right before the
60 * sentinel.
60 * sentinel.
61 *
61 *
62 * With string keys, we lazily perform a reverse mapping from node to
62 * With string keys, we lazily perform a reverse mapping from node to
63 * rev, using a base-16 trie.
63 * rev, using a base-16 trie.
64 */
64 */
65 struct indexObjectStruct {
65 struct indexObjectStruct {
66 PyObject_HEAD
66 PyObject_HEAD
67 /* Type-specific fields go here. */
67 /* Type-specific fields go here. */
68 PyObject *data; /* raw bytes of index */
68 PyObject *data; /* raw bytes of index */
69 Py_buffer buf; /* buffer of data */
69 Py_buffer buf; /* buffer of data */
70 PyObject **cache; /* cached tuples */
70 PyObject **cache; /* cached tuples */
71 const char **offsets; /* populated on demand */
71 const char **offsets; /* populated on demand */
72 Py_ssize_t raw_length; /* original number of elements */
72 Py_ssize_t raw_length; /* original number of elements */
73 Py_ssize_t length; /* current number of elements */
73 Py_ssize_t length; /* current number of elements */
74 PyObject *added; /* populated on demand */
74 PyObject *added; /* populated on demand */
75 PyObject *headrevs; /* cache, invalidated on changes */
75 PyObject *headrevs; /* cache, invalidated on changes */
76 PyObject *filteredrevs;/* filtered revs set */
76 PyObject *filteredrevs;/* filtered revs set */
77 nodetree *nt; /* base-16 trie */
77 nodetree *nt; /* base-16 trie */
78 int ntrev; /* last rev scanned */
78 int ntrev; /* last rev scanned */
79 int ntlookups; /* # lookups */
79 int ntlookups; /* # lookups */
80 int ntmisses; /* # lookups that miss the cache */
80 int ntmisses; /* # lookups that miss the cache */
81 int inlined;
81 int inlined;
82 };
82 };
83
83
84 static Py_ssize_t index_length(const indexObject *self)
84 static Py_ssize_t index_length(const indexObject *self)
85 {
85 {
86 if (self->added == NULL)
86 if (self->added == NULL)
87 return self->length - 1;
87 return self->length - 1;
88 return self->length + PyList_GET_SIZE(self->added) - 1;
88 return self->length + PyList_GET_SIZE(self->added) - 1;
89 }
89 }
90
90
91 static PyObject *nullentry;
91 static PyObject *nullentry;
92 static const char nullid[20];
92 static const char nullid[20];
93
93
94 static Py_ssize_t inline_scan(indexObject *self, const char **offsets);
94 static Py_ssize_t inline_scan(indexObject *self, const char **offsets);
95
95
96 #if LONG_MAX == 0x7fffffffL
96 #if LONG_MAX == 0x7fffffffL
97 static const char *const tuple_format = PY23("Kiiiiiis#", "Kiiiiiiy#");
97 static const char *const tuple_format = PY23("Kiiiiiis#", "Kiiiiiiy#");
98 #else
98 #else
99 static const char *const tuple_format = PY23("kiiiiiis#", "kiiiiiiy#");
99 static const char *const tuple_format = PY23("kiiiiiis#", "kiiiiiiy#");
100 #endif
100 #endif
101
101
102 /* A RevlogNG v1 index entry is 64 bytes long. */
102 /* A RevlogNG v1 index entry is 64 bytes long. */
103 static const long v1_hdrsize = 64;
103 static const long v1_hdrsize = 64;
104
104
105 /*
105 /*
106 * Return a pointer to the beginning of a RevlogNG record.
106 * Return a pointer to the beginning of a RevlogNG record.
107 */
107 */
108 static const char *index_deref(indexObject *self, Py_ssize_t pos)
108 static const char *index_deref(indexObject *self, Py_ssize_t pos)
109 {
109 {
110 if (self->inlined && pos > 0) {
110 if (self->inlined && pos > 0) {
111 if (self->offsets == NULL) {
111 if (self->offsets == NULL) {
112 self->offsets = PyMem_Malloc(self->raw_length *
112 self->offsets = PyMem_Malloc(self->raw_length *
113 sizeof(*self->offsets));
113 sizeof(*self->offsets));
114 if (self->offsets == NULL)
114 if (self->offsets == NULL)
115 return (const char *)PyErr_NoMemory();
115 return (const char *)PyErr_NoMemory();
116 inline_scan(self, self->offsets);
116 inline_scan(self, self->offsets);
117 }
117 }
118 return self->offsets[pos];
118 return self->offsets[pos];
119 }
119 }
120
120
121 return (const char *)(self->buf.buf) + pos * v1_hdrsize;
121 return (const char *)(self->buf.buf) + pos * v1_hdrsize;
122 }
122 }
123
123
124 static inline int index_get_parents(indexObject *self, Py_ssize_t rev,
124 static inline int index_get_parents(indexObject *self, Py_ssize_t rev,
125 int *ps, int maxrev)
125 int *ps, int maxrev)
126 {
126 {
127 if (rev >= self->length - 1) {
127 if (rev >= self->length - 1) {
128 PyObject *tuple = PyList_GET_ITEM(self->added,
128 PyObject *tuple = PyList_GET_ITEM(self->added,
129 rev - self->length + 1);
129 rev - self->length + 1);
130 ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5));
130 ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5));
131 ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6));
131 ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6));
132 } else {
132 } else {
133 const char *data = index_deref(self, rev);
133 const char *data = index_deref(self, rev);
134 ps[0] = getbe32(data + 24);
134 ps[0] = getbe32(data + 24);
135 ps[1] = getbe32(data + 28);
135 ps[1] = getbe32(data + 28);
136 }
136 }
137 /* If index file is corrupted, ps[] may point to invalid revisions. So
137 /* If index file is corrupted, ps[] may point to invalid revisions. So
138 * there is a risk of buffer overflow to trust them unconditionally. */
138 * there is a risk of buffer overflow to trust them unconditionally. */
139 if (ps[0] > maxrev || ps[1] > maxrev) {
139 if (ps[0] > maxrev || ps[1] > maxrev) {
140 PyErr_SetString(PyExc_ValueError, "parent out of range");
140 PyErr_SetString(PyExc_ValueError, "parent out of range");
141 return -1;
141 return -1;
142 }
142 }
143 return 0;
143 return 0;
144 }
144 }
145
145
146
146
147 /*
147 /*
148 * RevlogNG format (all in big endian, data may be inlined):
148 * RevlogNG format (all in big endian, data may be inlined):
149 * 6 bytes: offset
149 * 6 bytes: offset
150 * 2 bytes: flags
150 * 2 bytes: flags
151 * 4 bytes: compressed length
151 * 4 bytes: compressed length
152 * 4 bytes: uncompressed length
152 * 4 bytes: uncompressed length
153 * 4 bytes: base revision
153 * 4 bytes: base revision
154 * 4 bytes: link revision
154 * 4 bytes: link revision
155 * 4 bytes: parent 1 revision
155 * 4 bytes: parent 1 revision
156 * 4 bytes: parent 2 revision
156 * 4 bytes: parent 2 revision
157 * 32 bytes: nodeid (only 20 bytes used)
157 * 32 bytes: nodeid (only 20 bytes used)
158 */
158 */
159 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
159 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
160 {
160 {
161 uint64_t offset_flags;
161 uint64_t offset_flags;
162 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
162 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
163 const char *c_node_id;
163 const char *c_node_id;
164 const char *data;
164 const char *data;
165 Py_ssize_t length = index_length(self);
165 Py_ssize_t length = index_length(self);
166 PyObject *entry;
166 PyObject *entry;
167
167
168 if (pos == -1) {
168 if (pos == -1) {
169 Py_INCREF(nullentry);
169 Py_INCREF(nullentry);
170 return nullentry;
170 return nullentry;
171 }
171 }
172
172
173 if (pos < 0 || pos >= length) {
173 if (pos < 0 || pos >= length) {
174 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
174 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
175 return NULL;
175 return NULL;
176 }
176 }
177
177
178 if (pos >= self->length - 1) {
178 if (pos >= self->length - 1) {
179 PyObject *obj;
179 PyObject *obj;
180 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
180 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
181 Py_INCREF(obj);
181 Py_INCREF(obj);
182 return obj;
182 return obj;
183 }
183 }
184
184
185 if (self->cache) {
185 if (self->cache) {
186 if (self->cache[pos]) {
186 if (self->cache[pos]) {
187 Py_INCREF(self->cache[pos]);
187 Py_INCREF(self->cache[pos]);
188 return self->cache[pos];
188 return self->cache[pos];
189 }
189 }
190 } else {
190 } else {
191 self->cache = calloc(self->raw_length, sizeof(PyObject *));
191 self->cache = calloc(self->raw_length, sizeof(PyObject *));
192 if (self->cache == NULL)
192 if (self->cache == NULL)
193 return PyErr_NoMemory();
193 return PyErr_NoMemory();
194 }
194 }
195
195
196 data = index_deref(self, pos);
196 data = index_deref(self, pos);
197 if (data == NULL)
197 if (data == NULL)
198 return NULL;
198 return NULL;
199
199
200 offset_flags = getbe32(data + 4);
200 offset_flags = getbe32(data + 4);
201 if (pos == 0) /* mask out version number for the first entry */
201 if (pos == 0) /* mask out version number for the first entry */
202 offset_flags &= 0xFFFF;
202 offset_flags &= 0xFFFF;
203 else {
203 else {
204 uint32_t offset_high = getbe32(data);
204 uint32_t offset_high = getbe32(data);
205 offset_flags |= ((uint64_t)offset_high) << 32;
205 offset_flags |= ((uint64_t)offset_high) << 32;
206 }
206 }
207
207
208 comp_len = getbe32(data + 8);
208 comp_len = getbe32(data + 8);
209 uncomp_len = getbe32(data + 12);
209 uncomp_len = getbe32(data + 12);
210 base_rev = getbe32(data + 16);
210 base_rev = getbe32(data + 16);
211 link_rev = getbe32(data + 20);
211 link_rev = getbe32(data + 20);
212 parent_1 = getbe32(data + 24);
212 parent_1 = getbe32(data + 24);
213 parent_2 = getbe32(data + 28);
213 parent_2 = getbe32(data + 28);
214 c_node_id = data + 32;
214 c_node_id = data + 32;
215
215
216 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
216 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
217 uncomp_len, base_rev, link_rev,
217 uncomp_len, base_rev, link_rev,
218 parent_1, parent_2, c_node_id, 20);
218 parent_1, parent_2, c_node_id, 20);
219
219
220 if (entry) {
220 if (entry) {
221 PyObject_GC_UnTrack(entry);
221 PyObject_GC_UnTrack(entry);
222 Py_INCREF(entry);
222 Py_INCREF(entry);
223 }
223 }
224
224
225 self->cache[pos] = entry;
225 self->cache[pos] = entry;
226
226
227 return entry;
227 return entry;
228 }
228 }
229
229
230 /*
230 /*
231 * Return the 20-byte SHA of the node corresponding to the given rev.
231 * Return the 20-byte SHA of the node corresponding to the given rev.
232 */
232 */
233 static const char *index_node(indexObject *self, Py_ssize_t pos)
233 static const char *index_node(indexObject *self, Py_ssize_t pos)
234 {
234 {
235 Py_ssize_t length = index_length(self);
235 Py_ssize_t length = index_length(self);
236 const char *data;
236 const char *data;
237
237
238 if (pos == -1)
238 if (pos == -1)
239 return nullid;
239 return nullid;
240
240
241 if (pos >= length)
241 if (pos >= length)
242 return NULL;
242 return NULL;
243
243
244 if (pos >= self->length - 1) {
244 if (pos >= self->length - 1) {
245 PyObject *tuple, *str;
245 PyObject *tuple, *str;
246 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
246 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
247 str = PyTuple_GetItem(tuple, 7);
247 str = PyTuple_GetItem(tuple, 7);
248 return str ? PyBytes_AS_STRING(str) : NULL;
248 return str ? PyBytes_AS_STRING(str) : NULL;
249 }
249 }
250
250
251 data = index_deref(self, pos);
251 data = index_deref(self, pos);
252 return data ? data + 32 : NULL;
252 return data ? data + 32 : NULL;
253 }
253 }
254
254
255 /*
255 /*
256 * Return the 20-byte SHA of the node corresponding to the given rev. The
256 * Return the 20-byte SHA of the node corresponding to the given rev. The
257 * rev is assumed to be existing. If not, an exception is set.
257 * rev is assumed to be existing. If not, an exception is set.
258 */
258 */
259 static const char *index_node_existing(indexObject *self, Py_ssize_t pos)
259 static const char *index_node_existing(indexObject *self, Py_ssize_t pos)
260 {
260 {
261 const char *node = index_node(self, pos);
261 const char *node = index_node(self, pos);
262 if (node == NULL) {
262 if (node == NULL) {
263 PyErr_Format(PyExc_IndexError, "could not access rev %d",
263 PyErr_Format(PyExc_IndexError, "could not access rev %d",
264 (int)pos);
264 (int)pos);
265 }
265 }
266 return node;
266 return node;
267 }
267 }
268
268
269 static int nt_insert(nodetree *self, const char *node, int rev);
269 static int nt_insert(nodetree *self, const char *node, int rev);
270
270
271 static int node_check(PyObject *obj, char **node)
271 static int node_check(PyObject *obj, char **node)
272 {
272 {
273 Py_ssize_t nodelen;
273 Py_ssize_t nodelen;
274 if (PyBytes_AsStringAndSize(obj, node, &nodelen) == -1)
274 if (PyBytes_AsStringAndSize(obj, node, &nodelen) == -1)
275 return -1;
275 return -1;
276 if (nodelen == 20)
276 if (nodelen == 20)
277 return 0;
277 return 0;
278 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
278 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
279 return -1;
279 return -1;
280 }
280 }
281
281
282 static PyObject *index_append(indexObject *self, PyObject *obj)
282 static PyObject *index_append(indexObject *self, PyObject *obj)
283 {
283 {
284 char *node;
284 char *node;
285 Py_ssize_t len;
285 Py_ssize_t len;
286
286
287 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
287 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
288 PyErr_SetString(PyExc_TypeError, "8-tuple required");
288 PyErr_SetString(PyExc_TypeError, "8-tuple required");
289 return NULL;
289 return NULL;
290 }
290 }
291
291
292 if (node_check(PyTuple_GET_ITEM(obj, 7), &node) == -1)
292 if (node_check(PyTuple_GET_ITEM(obj, 7), &node) == -1)
293 return NULL;
293 return NULL;
294
294
295 len = index_length(self);
295 len = index_length(self);
296
296
297 if (self->added == NULL) {
297 if (self->added == NULL) {
298 self->added = PyList_New(0);
298 self->added = PyList_New(0);
299 if (self->added == NULL)
299 if (self->added == NULL)
300 return NULL;
300 return NULL;
301 }
301 }
302
302
303 if (PyList_Append(self->added, obj) == -1)
303 if (PyList_Append(self->added, obj) == -1)
304 return NULL;
304 return NULL;
305
305
306 if (self->nt)
306 if (self->nt)
307 nt_insert(self->nt, node, len);
307 nt_insert(self->nt, node, len);
308
308
309 Py_CLEAR(self->headrevs);
309 Py_CLEAR(self->headrevs);
310 Py_RETURN_NONE;
310 Py_RETURN_NONE;
311 }
311 }
312
312
313 static void _index_clearcaches(indexObject *self)
314 {
315 if (self->cache) {
316 Py_ssize_t i;
317
318 for (i = 0; i < self->raw_length; i++)
319 Py_CLEAR(self->cache[i]);
320 free(self->cache);
321 self->cache = NULL;
322 }
323 if (self->offsets) {
324 PyMem_Free(self->offsets);
325 self->offsets = NULL;
326 }
327 if (self->nt != NULL) {
328 free(self->nt->nodes);
329 PyMem_Free(self->nt);
330 }
331 self->nt = NULL;
332 Py_CLEAR(self->headrevs);
333 }
334
335 static PyObject *index_clearcaches(indexObject *self)
336 {
337 _index_clearcaches(self);
338 self->ntrev = -1;
339 self->ntlookups = self->ntmisses = 0;
340 Py_RETURN_NONE;
341 }
342
343 static PyObject *index_stats(indexObject *self)
313 static PyObject *index_stats(indexObject *self)
344 {
314 {
345 PyObject *obj = PyDict_New();
315 PyObject *obj = PyDict_New();
346 PyObject *t = NULL;
316 PyObject *t = NULL;
347
317
348 if (obj == NULL)
318 if (obj == NULL)
349 return NULL;
319 return NULL;
350
320
351 #define istat(__n, __d) \
321 #define istat(__n, __d) \
352 do { \
322 do { \
353 t = PyInt_FromSsize_t(self->__n); \
323 t = PyInt_FromSsize_t(self->__n); \
354 if (!t) \
324 if (!t) \
355 goto bail; \
325 goto bail; \
356 if (PyDict_SetItemString(obj, __d, t) == -1) \
326 if (PyDict_SetItemString(obj, __d, t) == -1) \
357 goto bail; \
327 goto bail; \
358 Py_DECREF(t); \
328 Py_DECREF(t); \
359 } while (0)
329 } while (0)
360
330
361 if (self->added) {
331 if (self->added) {
362 Py_ssize_t len = PyList_GET_SIZE(self->added);
332 Py_ssize_t len = PyList_GET_SIZE(self->added);
363 t = PyInt_FromSsize_t(len);
333 t = PyInt_FromSsize_t(len);
364 if (!t)
334 if (!t)
365 goto bail;
335 goto bail;
366 if (PyDict_SetItemString(obj, "index entries added", t) == -1)
336 if (PyDict_SetItemString(obj, "index entries added", t) == -1)
367 goto bail;
337 goto bail;
368 Py_DECREF(t);
338 Py_DECREF(t);
369 }
339 }
370
340
371 if (self->raw_length != self->length - 1)
341 if (self->raw_length != self->length - 1)
372 istat(raw_length, "revs on disk");
342 istat(raw_length, "revs on disk");
373 istat(length, "revs in memory");
343 istat(length, "revs in memory");
374 istat(ntlookups, "node trie lookups");
344 istat(ntlookups, "node trie lookups");
375 istat(ntmisses, "node trie misses");
345 istat(ntmisses, "node trie misses");
376 istat(ntrev, "node trie last rev scanned");
346 istat(ntrev, "node trie last rev scanned");
377 if (self->nt) {
347 if (self->nt) {
378 istat(nt->capacity, "node trie capacity");
348 istat(nt->capacity, "node trie capacity");
379 istat(nt->depth, "node trie depth");
349 istat(nt->depth, "node trie depth");
380 istat(nt->length, "node trie count");
350 istat(nt->length, "node trie count");
381 istat(nt->splits, "node trie splits");
351 istat(nt->splits, "node trie splits");
382 }
352 }
383
353
384 #undef istat
354 #undef istat
385
355
386 return obj;
356 return obj;
387
357
388 bail:
358 bail:
389 Py_XDECREF(obj);
359 Py_XDECREF(obj);
390 Py_XDECREF(t);
360 Py_XDECREF(t);
391 return NULL;
361 return NULL;
392 }
362 }
393
363
394 /*
364 /*
395 * When we cache a list, we want to be sure the caller can't mutate
365 * When we cache a list, we want to be sure the caller can't mutate
396 * the cached copy.
366 * the cached copy.
397 */
367 */
398 static PyObject *list_copy(PyObject *list)
368 static PyObject *list_copy(PyObject *list)
399 {
369 {
400 Py_ssize_t len = PyList_GET_SIZE(list);
370 Py_ssize_t len = PyList_GET_SIZE(list);
401 PyObject *newlist = PyList_New(len);
371 PyObject *newlist = PyList_New(len);
402 Py_ssize_t i;
372 Py_ssize_t i;
403
373
404 if (newlist == NULL)
374 if (newlist == NULL)
405 return NULL;
375 return NULL;
406
376
407 for (i = 0; i < len; i++) {
377 for (i = 0; i < len; i++) {
408 PyObject *obj = PyList_GET_ITEM(list, i);
378 PyObject *obj = PyList_GET_ITEM(list, i);
409 Py_INCREF(obj);
379 Py_INCREF(obj);
410 PyList_SET_ITEM(newlist, i, obj);
380 PyList_SET_ITEM(newlist, i, obj);
411 }
381 }
412
382
413 return newlist;
383 return newlist;
414 }
384 }
415
385
416 static int check_filter(PyObject *filter, Py_ssize_t arg)
386 static int check_filter(PyObject *filter, Py_ssize_t arg)
417 {
387 {
418 if (filter) {
388 if (filter) {
419 PyObject *arglist, *result;
389 PyObject *arglist, *result;
420 int isfiltered;
390 int isfiltered;
421
391
422 arglist = Py_BuildValue("(n)", arg);
392 arglist = Py_BuildValue("(n)", arg);
423 if (!arglist) {
393 if (!arglist) {
424 return -1;
394 return -1;
425 }
395 }
426
396
427 result = PyEval_CallObject(filter, arglist);
397 result = PyEval_CallObject(filter, arglist);
428 Py_DECREF(arglist);
398 Py_DECREF(arglist);
429 if (!result) {
399 if (!result) {
430 return -1;
400 return -1;
431 }
401 }
432
402
433 /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error,
403 /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error,
434 * same as this function, so we can just return it directly.*/
404 * same as this function, so we can just return it directly.*/
435 isfiltered = PyObject_IsTrue(result);
405 isfiltered = PyObject_IsTrue(result);
436 Py_DECREF(result);
406 Py_DECREF(result);
437 return isfiltered;
407 return isfiltered;
438 } else {
408 } else {
439 return 0;
409 return 0;
440 }
410 }
441 }
411 }
442
412
443 static Py_ssize_t add_roots_get_min(indexObject *self, PyObject *list,
413 static Py_ssize_t add_roots_get_min(indexObject *self, PyObject *list,
444 Py_ssize_t marker, char *phases)
414 Py_ssize_t marker, char *phases)
445 {
415 {
446 PyObject *iter = NULL;
416 PyObject *iter = NULL;
447 PyObject *iter_item = NULL;
417 PyObject *iter_item = NULL;
448 Py_ssize_t min_idx = index_length(self) + 2;
418 Py_ssize_t min_idx = index_length(self) + 2;
449 long iter_item_long;
419 long iter_item_long;
450
420
451 if (PyList_GET_SIZE(list) != 0) {
421 if (PyList_GET_SIZE(list) != 0) {
452 iter = PyObject_GetIter(list);
422 iter = PyObject_GetIter(list);
453 if (iter == NULL)
423 if (iter == NULL)
454 return -2;
424 return -2;
455 while ((iter_item = PyIter_Next(iter))) {
425 while ((iter_item = PyIter_Next(iter))) {
456 iter_item_long = PyInt_AS_LONG(iter_item);
426 iter_item_long = PyInt_AS_LONG(iter_item);
457 Py_DECREF(iter_item);
427 Py_DECREF(iter_item);
458 if (iter_item_long < min_idx)
428 if (iter_item_long < min_idx)
459 min_idx = iter_item_long;
429 min_idx = iter_item_long;
460 phases[iter_item_long] = marker;
430 phases[iter_item_long] = marker;
461 }
431 }
462 Py_DECREF(iter);
432 Py_DECREF(iter);
463 }
433 }
464
434
465 return min_idx;
435 return min_idx;
466 }
436 }
467
437
468 static inline void set_phase_from_parents(char *phases, int parent_1,
438 static inline void set_phase_from_parents(char *phases, int parent_1,
469 int parent_2, Py_ssize_t i)
439 int parent_2, Py_ssize_t i)
470 {
440 {
471 if (parent_1 >= 0 && phases[parent_1] > phases[i])
441 if (parent_1 >= 0 && phases[parent_1] > phases[i])
472 phases[i] = phases[parent_1];
442 phases[i] = phases[parent_1];
473 if (parent_2 >= 0 && phases[parent_2] > phases[i])
443 if (parent_2 >= 0 && phases[parent_2] > phases[i])
474 phases[i] = phases[parent_2];
444 phases[i] = phases[parent_2];
475 }
445 }
476
446
477 static PyObject *reachableroots2(indexObject *self, PyObject *args)
447 static PyObject *reachableroots2(indexObject *self, PyObject *args)
478 {
448 {
479
449
480 /* Input */
450 /* Input */
481 long minroot;
451 long minroot;
482 PyObject *includepatharg = NULL;
452 PyObject *includepatharg = NULL;
483 int includepath = 0;
453 int includepath = 0;
484 /* heads and roots are lists */
454 /* heads and roots are lists */
485 PyObject *heads = NULL;
455 PyObject *heads = NULL;
486 PyObject *roots = NULL;
456 PyObject *roots = NULL;
487 PyObject *reachable = NULL;
457 PyObject *reachable = NULL;
488
458
489 PyObject *val;
459 PyObject *val;
490 Py_ssize_t len = index_length(self);
460 Py_ssize_t len = index_length(self);
491 long revnum;
461 long revnum;
492 Py_ssize_t k;
462 Py_ssize_t k;
493 Py_ssize_t i;
463 Py_ssize_t i;
494 Py_ssize_t l;
464 Py_ssize_t l;
495 int r;
465 int r;
496 int parents[2];
466 int parents[2];
497
467
498 /* Internal data structure:
468 /* Internal data structure:
499 * tovisit: array of length len+1 (all revs + nullrev), filled upto lentovisit
469 * tovisit: array of length len+1 (all revs + nullrev), filled upto lentovisit
500 * revstates: array of length len+1 (all revs + nullrev) */
470 * revstates: array of length len+1 (all revs + nullrev) */
501 int *tovisit = NULL;
471 int *tovisit = NULL;
502 long lentovisit = 0;
472 long lentovisit = 0;
503 enum { RS_SEEN = 1, RS_ROOT = 2, RS_REACHABLE = 4 };
473 enum { RS_SEEN = 1, RS_ROOT = 2, RS_REACHABLE = 4 };
504 char *revstates = NULL;
474 char *revstates = NULL;
505
475
506 /* Get arguments */
476 /* Get arguments */
507 if (!PyArg_ParseTuple(args, "lO!O!O!", &minroot, &PyList_Type, &heads,
477 if (!PyArg_ParseTuple(args, "lO!O!O!", &minroot, &PyList_Type, &heads,
508 &PyList_Type, &roots,
478 &PyList_Type, &roots,
509 &PyBool_Type, &includepatharg))
479 &PyBool_Type, &includepatharg))
510 goto bail;
480 goto bail;
511
481
512 if (includepatharg == Py_True)
482 if (includepatharg == Py_True)
513 includepath = 1;
483 includepath = 1;
514
484
515 /* Initialize return set */
485 /* Initialize return set */
516 reachable = PyList_New(0);
486 reachable = PyList_New(0);
517 if (reachable == NULL)
487 if (reachable == NULL)
518 goto bail;
488 goto bail;
519
489
520 /* Initialize internal datastructures */
490 /* Initialize internal datastructures */
521 tovisit = (int *)malloc((len + 1) * sizeof(int));
491 tovisit = (int *)malloc((len + 1) * sizeof(int));
522 if (tovisit == NULL) {
492 if (tovisit == NULL) {
523 PyErr_NoMemory();
493 PyErr_NoMemory();
524 goto bail;
494 goto bail;
525 }
495 }
526
496
527 revstates = (char *)calloc(len + 1, 1);
497 revstates = (char *)calloc(len + 1, 1);
528 if (revstates == NULL) {
498 if (revstates == NULL) {
529 PyErr_NoMemory();
499 PyErr_NoMemory();
530 goto bail;
500 goto bail;
531 }
501 }
532
502
533 l = PyList_GET_SIZE(roots);
503 l = PyList_GET_SIZE(roots);
534 for (i = 0; i < l; i++) {
504 for (i = 0; i < l; i++) {
535 revnum = PyInt_AsLong(PyList_GET_ITEM(roots, i));
505 revnum = PyInt_AsLong(PyList_GET_ITEM(roots, i));
536 if (revnum == -1 && PyErr_Occurred())
506 if (revnum == -1 && PyErr_Occurred())
537 goto bail;
507 goto bail;
538 /* If root is out of range, e.g. wdir(), it must be unreachable
508 /* If root is out of range, e.g. wdir(), it must be unreachable
539 * from heads. So we can just ignore it. */
509 * from heads. So we can just ignore it. */
540 if (revnum + 1 < 0 || revnum + 1 >= len + 1)
510 if (revnum + 1 < 0 || revnum + 1 >= len + 1)
541 continue;
511 continue;
542 revstates[revnum + 1] |= RS_ROOT;
512 revstates[revnum + 1] |= RS_ROOT;
543 }
513 }
544
514
545 /* Populate tovisit with all the heads */
515 /* Populate tovisit with all the heads */
546 l = PyList_GET_SIZE(heads);
516 l = PyList_GET_SIZE(heads);
547 for (i = 0; i < l; i++) {
517 for (i = 0; i < l; i++) {
548 revnum = PyInt_AsLong(PyList_GET_ITEM(heads, i));
518 revnum = PyInt_AsLong(PyList_GET_ITEM(heads, i));
549 if (revnum == -1 && PyErr_Occurred())
519 if (revnum == -1 && PyErr_Occurred())
550 goto bail;
520 goto bail;
551 if (revnum + 1 < 0 || revnum + 1 >= len + 1) {
521 if (revnum + 1 < 0 || revnum + 1 >= len + 1) {
552 PyErr_SetString(PyExc_IndexError, "head out of range");
522 PyErr_SetString(PyExc_IndexError, "head out of range");
553 goto bail;
523 goto bail;
554 }
524 }
555 if (!(revstates[revnum + 1] & RS_SEEN)) {
525 if (!(revstates[revnum + 1] & RS_SEEN)) {
556 tovisit[lentovisit++] = (int)revnum;
526 tovisit[lentovisit++] = (int)revnum;
557 revstates[revnum + 1] |= RS_SEEN;
527 revstates[revnum + 1] |= RS_SEEN;
558 }
528 }
559 }
529 }
560
530
561 /* Visit the tovisit list and find the reachable roots */
531 /* Visit the tovisit list and find the reachable roots */
562 k = 0;
532 k = 0;
563 while (k < lentovisit) {
533 while (k < lentovisit) {
564 /* Add the node to reachable if it is a root*/
534 /* Add the node to reachable if it is a root*/
565 revnum = tovisit[k++];
535 revnum = tovisit[k++];
566 if (revstates[revnum + 1] & RS_ROOT) {
536 if (revstates[revnum + 1] & RS_ROOT) {
567 revstates[revnum + 1] |= RS_REACHABLE;
537 revstates[revnum + 1] |= RS_REACHABLE;
568 val = PyInt_FromLong(revnum);
538 val = PyInt_FromLong(revnum);
569 if (val == NULL)
539 if (val == NULL)
570 goto bail;
540 goto bail;
571 r = PyList_Append(reachable, val);
541 r = PyList_Append(reachable, val);
572 Py_DECREF(val);
542 Py_DECREF(val);
573 if (r < 0)
543 if (r < 0)
574 goto bail;
544 goto bail;
575 if (includepath == 0)
545 if (includepath == 0)
576 continue;
546 continue;
577 }
547 }
578
548
579 /* Add its parents to the list of nodes to visit */
549 /* Add its parents to the list of nodes to visit */
580 if (revnum == -1)
550 if (revnum == -1)
581 continue;
551 continue;
582 r = index_get_parents(self, revnum, parents, (int)len - 1);
552 r = index_get_parents(self, revnum, parents, (int)len - 1);
583 if (r < 0)
553 if (r < 0)
584 goto bail;
554 goto bail;
585 for (i = 0; i < 2; i++) {
555 for (i = 0; i < 2; i++) {
586 if (!(revstates[parents[i] + 1] & RS_SEEN)
556 if (!(revstates[parents[i] + 1] & RS_SEEN)
587 && parents[i] >= minroot) {
557 && parents[i] >= minroot) {
588 tovisit[lentovisit++] = parents[i];
558 tovisit[lentovisit++] = parents[i];
589 revstates[parents[i] + 1] |= RS_SEEN;
559 revstates[parents[i] + 1] |= RS_SEEN;
590 }
560 }
591 }
561 }
592 }
562 }
593
563
594 /* Find all the nodes in between the roots we found and the heads
564 /* Find all the nodes in between the roots we found and the heads
595 * and add them to the reachable set */
565 * and add them to the reachable set */
596 if (includepath == 1) {
566 if (includepath == 1) {
597 long minidx = minroot;
567 long minidx = minroot;
598 if (minidx < 0)
568 if (minidx < 0)
599 minidx = 0;
569 minidx = 0;
600 for (i = minidx; i < len; i++) {
570 for (i = minidx; i < len; i++) {
601 if (!(revstates[i + 1] & RS_SEEN))
571 if (!(revstates[i + 1] & RS_SEEN))
602 continue;
572 continue;
603 r = index_get_parents(self, i, parents, (int)len - 1);
573 r = index_get_parents(self, i, parents, (int)len - 1);
604 /* Corrupted index file, error is set from
574 /* Corrupted index file, error is set from
605 * index_get_parents */
575 * index_get_parents */
606 if (r < 0)
576 if (r < 0)
607 goto bail;
577 goto bail;
608 if (((revstates[parents[0] + 1] |
578 if (((revstates[parents[0] + 1] |
609 revstates[parents[1] + 1]) & RS_REACHABLE)
579 revstates[parents[1] + 1]) & RS_REACHABLE)
610 && !(revstates[i + 1] & RS_REACHABLE)) {
580 && !(revstates[i + 1] & RS_REACHABLE)) {
611 revstates[i + 1] |= RS_REACHABLE;
581 revstates[i + 1] |= RS_REACHABLE;
612 val = PyInt_FromLong(i);
582 val = PyInt_FromLong(i);
613 if (val == NULL)
583 if (val == NULL)
614 goto bail;
584 goto bail;
615 r = PyList_Append(reachable, val);
585 r = PyList_Append(reachable, val);
616 Py_DECREF(val);
586 Py_DECREF(val);
617 if (r < 0)
587 if (r < 0)
618 goto bail;
588 goto bail;
619 }
589 }
620 }
590 }
621 }
591 }
622
592
623 free(revstates);
593 free(revstates);
624 free(tovisit);
594 free(tovisit);
625 return reachable;
595 return reachable;
626 bail:
596 bail:
627 Py_XDECREF(reachable);
597 Py_XDECREF(reachable);
628 free(revstates);
598 free(revstates);
629 free(tovisit);
599 free(tovisit);
630 return NULL;
600 return NULL;
631 }
601 }
632
602
633 static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args)
603 static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args)
634 {
604 {
635 PyObject *roots = Py_None;
605 PyObject *roots = Py_None;
636 PyObject *ret = NULL;
606 PyObject *ret = NULL;
637 PyObject *phasessize = NULL;
607 PyObject *phasessize = NULL;
638 PyObject *phaseroots = NULL;
608 PyObject *phaseroots = NULL;
639 PyObject *phaseset = NULL;
609 PyObject *phaseset = NULL;
640 PyObject *phasessetlist = NULL;
610 PyObject *phasessetlist = NULL;
641 PyObject *rev = NULL;
611 PyObject *rev = NULL;
642 Py_ssize_t len = index_length(self);
612 Py_ssize_t len = index_length(self);
643 Py_ssize_t numphase = 0;
613 Py_ssize_t numphase = 0;
644 Py_ssize_t minrevallphases = 0;
614 Py_ssize_t minrevallphases = 0;
645 Py_ssize_t minrevphase = 0;
615 Py_ssize_t minrevphase = 0;
646 Py_ssize_t i = 0;
616 Py_ssize_t i = 0;
647 char *phases = NULL;
617 char *phases = NULL;
648 long phase;
618 long phase;
649
619
650 if (!PyArg_ParseTuple(args, "O", &roots))
620 if (!PyArg_ParseTuple(args, "O", &roots))
651 goto done;
621 goto done;
652 if (roots == NULL || !PyList_Check(roots)) {
622 if (roots == NULL || !PyList_Check(roots)) {
653 PyErr_SetString(PyExc_TypeError, "roots must be a list");
623 PyErr_SetString(PyExc_TypeError, "roots must be a list");
654 goto done;
624 goto done;
655 }
625 }
656
626
657 phases = calloc(len, 1); /* phase per rev: {0: public, 1: draft, 2: secret} */
627 phases = calloc(len, 1); /* phase per rev: {0: public, 1: draft, 2: secret} */
658 if (phases == NULL) {
628 if (phases == NULL) {
659 PyErr_NoMemory();
629 PyErr_NoMemory();
660 goto done;
630 goto done;
661 }
631 }
662 /* Put the phase information of all the roots in phases */
632 /* Put the phase information of all the roots in phases */
663 numphase = PyList_GET_SIZE(roots)+1;
633 numphase = PyList_GET_SIZE(roots)+1;
664 minrevallphases = len + 1;
634 minrevallphases = len + 1;
665 phasessetlist = PyList_New(numphase);
635 phasessetlist = PyList_New(numphase);
666 if (phasessetlist == NULL)
636 if (phasessetlist == NULL)
667 goto done;
637 goto done;
668
638
669 PyList_SET_ITEM(phasessetlist, 0, Py_None);
639 PyList_SET_ITEM(phasessetlist, 0, Py_None);
670 Py_INCREF(Py_None);
640 Py_INCREF(Py_None);
671
641
672 for (i = 0; i < numphase-1; i++) {
642 for (i = 0; i < numphase-1; i++) {
673 phaseroots = PyList_GET_ITEM(roots, i);
643 phaseroots = PyList_GET_ITEM(roots, i);
674 phaseset = PySet_New(NULL);
644 phaseset = PySet_New(NULL);
675 if (phaseset == NULL)
645 if (phaseset == NULL)
676 goto release;
646 goto release;
677 PyList_SET_ITEM(phasessetlist, i+1, phaseset);
647 PyList_SET_ITEM(phasessetlist, i+1, phaseset);
678 if (!PyList_Check(phaseroots)) {
648 if (!PyList_Check(phaseroots)) {
679 PyErr_SetString(PyExc_TypeError,
649 PyErr_SetString(PyExc_TypeError,
680 "roots item must be a list");
650 "roots item must be a list");
681 goto release;
651 goto release;
682 }
652 }
683 minrevphase = add_roots_get_min(self, phaseroots, i+1, phases);
653 minrevphase = add_roots_get_min(self, phaseroots, i+1, phases);
684 if (minrevphase == -2) /* Error from add_roots_get_min */
654 if (minrevphase == -2) /* Error from add_roots_get_min */
685 goto release;
655 goto release;
686 minrevallphases = MIN(minrevallphases, minrevphase);
656 minrevallphases = MIN(minrevallphases, minrevphase);
687 }
657 }
688 /* Propagate the phase information from the roots to the revs */
658 /* Propagate the phase information from the roots to the revs */
689 if (minrevallphases != -1) {
659 if (minrevallphases != -1) {
690 int parents[2];
660 int parents[2];
691 for (i = minrevallphases; i < len; i++) {
661 for (i = minrevallphases; i < len; i++) {
692 if (index_get_parents(self, i, parents,
662 if (index_get_parents(self, i, parents,
693 (int)len - 1) < 0)
663 (int)len - 1) < 0)
694 goto release;
664 goto release;
695 set_phase_from_parents(phases, parents[0], parents[1], i);
665 set_phase_from_parents(phases, parents[0], parents[1], i);
696 }
666 }
697 }
667 }
698 /* Transform phase list to a python list */
668 /* Transform phase list to a python list */
699 phasessize = PyInt_FromLong(len);
669 phasessize = PyInt_FromLong(len);
700 if (phasessize == NULL)
670 if (phasessize == NULL)
701 goto release;
671 goto release;
702 for (i = 0; i < len; i++) {
672 for (i = 0; i < len; i++) {
703 phase = phases[i];
673 phase = phases[i];
704 /* We only store the sets of phase for non public phase, the public phase
674 /* We only store the sets of phase for non public phase, the public phase
705 * is computed as a difference */
675 * is computed as a difference */
706 if (phase != 0) {
676 if (phase != 0) {
707 phaseset = PyList_GET_ITEM(phasessetlist, phase);
677 phaseset = PyList_GET_ITEM(phasessetlist, phase);
708 rev = PyInt_FromLong(i);
678 rev = PyInt_FromLong(i);
709 if (rev == NULL)
679 if (rev == NULL)
710 goto release;
680 goto release;
711 PySet_Add(phaseset, rev);
681 PySet_Add(phaseset, rev);
712 Py_XDECREF(rev);
682 Py_XDECREF(rev);
713 }
683 }
714 }
684 }
715 ret = PyTuple_Pack(2, phasessize, phasessetlist);
685 ret = PyTuple_Pack(2, phasessize, phasessetlist);
716
686
717 release:
687 release:
718 Py_XDECREF(phasessize);
688 Py_XDECREF(phasessize);
719 Py_XDECREF(phasessetlist);
689 Py_XDECREF(phasessetlist);
720 done:
690 done:
721 free(phases);
691 free(phases);
722 return ret;
692 return ret;
723 }
693 }
724
694
725 static PyObject *index_headrevs(indexObject *self, PyObject *args)
695 static PyObject *index_headrevs(indexObject *self, PyObject *args)
726 {
696 {
727 Py_ssize_t i, j, len;
697 Py_ssize_t i, j, len;
728 char *nothead = NULL;
698 char *nothead = NULL;
729 PyObject *heads = NULL;
699 PyObject *heads = NULL;
730 PyObject *filter = NULL;
700 PyObject *filter = NULL;
731 PyObject *filteredrevs = Py_None;
701 PyObject *filteredrevs = Py_None;
732
702
733 if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) {
703 if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) {
734 return NULL;
704 return NULL;
735 }
705 }
736
706
737 if (self->headrevs && filteredrevs == self->filteredrevs)
707 if (self->headrevs && filteredrevs == self->filteredrevs)
738 return list_copy(self->headrevs);
708 return list_copy(self->headrevs);
739
709
740 Py_DECREF(self->filteredrevs);
710 Py_DECREF(self->filteredrevs);
741 self->filteredrevs = filteredrevs;
711 self->filteredrevs = filteredrevs;
742 Py_INCREF(filteredrevs);
712 Py_INCREF(filteredrevs);
743
713
744 if (filteredrevs != Py_None) {
714 if (filteredrevs != Py_None) {
745 filter = PyObject_GetAttrString(filteredrevs, "__contains__");
715 filter = PyObject_GetAttrString(filteredrevs, "__contains__");
746 if (!filter) {
716 if (!filter) {
747 PyErr_SetString(PyExc_TypeError,
717 PyErr_SetString(PyExc_TypeError,
748 "filteredrevs has no attribute __contains__");
718 "filteredrevs has no attribute __contains__");
749 goto bail;
719 goto bail;
750 }
720 }
751 }
721 }
752
722
753 len = index_length(self);
723 len = index_length(self);
754 heads = PyList_New(0);
724 heads = PyList_New(0);
755 if (heads == NULL)
725 if (heads == NULL)
756 goto bail;
726 goto bail;
757 if (len == 0) {
727 if (len == 0) {
758 PyObject *nullid = PyInt_FromLong(-1);
728 PyObject *nullid = PyInt_FromLong(-1);
759 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
729 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
760 Py_XDECREF(nullid);
730 Py_XDECREF(nullid);
761 goto bail;
731 goto bail;
762 }
732 }
763 goto done;
733 goto done;
764 }
734 }
765
735
766 nothead = calloc(len, 1);
736 nothead = calloc(len, 1);
767 if (nothead == NULL) {
737 if (nothead == NULL) {
768 PyErr_NoMemory();
738 PyErr_NoMemory();
769 goto bail;
739 goto bail;
770 }
740 }
771
741
772 for (i = len - 1; i >= 0; i--) {
742 for (i = len - 1; i >= 0; i--) {
773 int isfiltered;
743 int isfiltered;
774 int parents[2];
744 int parents[2];
775
745
776 /* If nothead[i] == 1, it means we've seen an unfiltered child of this
746 /* If nothead[i] == 1, it means we've seen an unfiltered child of this
777 * node already, and therefore this node is not filtered. So we can skip
747 * node already, and therefore this node is not filtered. So we can skip
778 * the expensive check_filter step.
748 * the expensive check_filter step.
779 */
749 */
780 if (nothead[i] != 1) {
750 if (nothead[i] != 1) {
781 isfiltered = check_filter(filter, i);
751 isfiltered = check_filter(filter, i);
782 if (isfiltered == -1) {
752 if (isfiltered == -1) {
783 PyErr_SetString(PyExc_TypeError,
753 PyErr_SetString(PyExc_TypeError,
784 "unable to check filter");
754 "unable to check filter");
785 goto bail;
755 goto bail;
786 }
756 }
787
757
788 if (isfiltered) {
758 if (isfiltered) {
789 nothead[i] = 1;
759 nothead[i] = 1;
790 continue;
760 continue;
791 }
761 }
792 }
762 }
793
763
794 if (index_get_parents(self, i, parents, (int)len - 1) < 0)
764 if (index_get_parents(self, i, parents, (int)len - 1) < 0)
795 goto bail;
765 goto bail;
796 for (j = 0; j < 2; j++) {
766 for (j = 0; j < 2; j++) {
797 if (parents[j] >= 0)
767 if (parents[j] >= 0)
798 nothead[parents[j]] = 1;
768 nothead[parents[j]] = 1;
799 }
769 }
800 }
770 }
801
771
802 for (i = 0; i < len; i++) {
772 for (i = 0; i < len; i++) {
803 PyObject *head;
773 PyObject *head;
804
774
805 if (nothead[i])
775 if (nothead[i])
806 continue;
776 continue;
807 head = PyInt_FromSsize_t(i);
777 head = PyInt_FromSsize_t(i);
808 if (head == NULL || PyList_Append(heads, head) == -1) {
778 if (head == NULL || PyList_Append(heads, head) == -1) {
809 Py_XDECREF(head);
779 Py_XDECREF(head);
810 goto bail;
780 goto bail;
811 }
781 }
812 }
782 }
813
783
814 done:
784 done:
815 self->headrevs = heads;
785 self->headrevs = heads;
816 Py_XDECREF(filter);
786 Py_XDECREF(filter);
817 free(nothead);
787 free(nothead);
818 return list_copy(self->headrevs);
788 return list_copy(self->headrevs);
819 bail:
789 bail:
820 Py_XDECREF(filter);
790 Py_XDECREF(filter);
821 Py_XDECREF(heads);
791 Py_XDECREF(heads);
822 free(nothead);
792 free(nothead);
823 return NULL;
793 return NULL;
824 }
794 }
825
795
826 /**
796 /**
827 * Obtain the base revision index entry.
797 * Obtain the base revision index entry.
828 *
798 *
829 * Callers must ensure that rev >= 0 or illegal memory access may occur.
799 * Callers must ensure that rev >= 0 or illegal memory access may occur.
830 */
800 */
831 static inline int index_baserev(indexObject *self, int rev)
801 static inline int index_baserev(indexObject *self, int rev)
832 {
802 {
833 const char *data;
803 const char *data;
834
804
835 if (rev >= self->length - 1) {
805 if (rev >= self->length - 1) {
836 PyObject *tuple = PyList_GET_ITEM(self->added,
806 PyObject *tuple = PyList_GET_ITEM(self->added,
837 rev - self->length + 1);
807 rev - self->length + 1);
838 return (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 3));
808 return (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 3));
839 }
809 }
840 else {
810 else {
841 data = index_deref(self, rev);
811 data = index_deref(self, rev);
842 if (data == NULL) {
812 if (data == NULL) {
843 return -2;
813 return -2;
844 }
814 }
845
815
846 return getbe32(data + 16);
816 return getbe32(data + 16);
847 }
817 }
848 }
818 }
849
819
850 static PyObject *index_deltachain(indexObject *self, PyObject *args)
820 static PyObject *index_deltachain(indexObject *self, PyObject *args)
851 {
821 {
852 int rev, generaldelta;
822 int rev, generaldelta;
853 PyObject *stoparg;
823 PyObject *stoparg;
854 int stoprev, iterrev, baserev = -1;
824 int stoprev, iterrev, baserev = -1;
855 int stopped;
825 int stopped;
856 PyObject *chain = NULL, *result = NULL;
826 PyObject *chain = NULL, *result = NULL;
857 const Py_ssize_t length = index_length(self);
827 const Py_ssize_t length = index_length(self);
858
828
859 if (!PyArg_ParseTuple(args, "iOi", &rev, &stoparg, &generaldelta)) {
829 if (!PyArg_ParseTuple(args, "iOi", &rev, &stoparg, &generaldelta)) {
860 return NULL;
830 return NULL;
861 }
831 }
862
832
863 if (PyInt_Check(stoparg)) {
833 if (PyInt_Check(stoparg)) {
864 stoprev = (int)PyInt_AsLong(stoparg);
834 stoprev = (int)PyInt_AsLong(stoparg);
865 if (stoprev == -1 && PyErr_Occurred()) {
835 if (stoprev == -1 && PyErr_Occurred()) {
866 return NULL;
836 return NULL;
867 }
837 }
868 }
838 }
869 else if (stoparg == Py_None) {
839 else if (stoparg == Py_None) {
870 stoprev = -2;
840 stoprev = -2;
871 }
841 }
872 else {
842 else {
873 PyErr_SetString(PyExc_ValueError,
843 PyErr_SetString(PyExc_ValueError,
874 "stoprev must be integer or None");
844 "stoprev must be integer or None");
875 return NULL;
845 return NULL;
876 }
846 }
877
847
878 if (rev < 0 || rev >= length) {
848 if (rev < 0 || rev >= length) {
879 PyErr_SetString(PyExc_ValueError, "revlog index out of range");
849 PyErr_SetString(PyExc_ValueError, "revlog index out of range");
880 return NULL;
850 return NULL;
881 }
851 }
882
852
883 chain = PyList_New(0);
853 chain = PyList_New(0);
884 if (chain == NULL) {
854 if (chain == NULL) {
885 return NULL;
855 return NULL;
886 }
856 }
887
857
888 baserev = index_baserev(self, rev);
858 baserev = index_baserev(self, rev);
889
859
890 /* This should never happen. */
860 /* This should never happen. */
891 if (baserev <= -2) {
861 if (baserev <= -2) {
892 /* Error should be set by index_deref() */
862 /* Error should be set by index_deref() */
893 assert(PyErr_Occurred());
863 assert(PyErr_Occurred());
894 goto bail;
864 goto bail;
895 }
865 }
896
866
897 iterrev = rev;
867 iterrev = rev;
898
868
899 while (iterrev != baserev && iterrev != stoprev) {
869 while (iterrev != baserev && iterrev != stoprev) {
900 PyObject *value = PyInt_FromLong(iterrev);
870 PyObject *value = PyInt_FromLong(iterrev);
901 if (value == NULL) {
871 if (value == NULL) {
902 goto bail;
872 goto bail;
903 }
873 }
904 if (PyList_Append(chain, value)) {
874 if (PyList_Append(chain, value)) {
905 Py_DECREF(value);
875 Py_DECREF(value);
906 goto bail;
876 goto bail;
907 }
877 }
908 Py_DECREF(value);
878 Py_DECREF(value);
909
879
910 if (generaldelta) {
880 if (generaldelta) {
911 iterrev = baserev;
881 iterrev = baserev;
912 }
882 }
913 else {
883 else {
914 iterrev--;
884 iterrev--;
915 }
885 }
916
886
917 if (iterrev < 0) {
887 if (iterrev < 0) {
918 break;
888 break;
919 }
889 }
920
890
921 if (iterrev >= length) {
891 if (iterrev >= length) {
922 PyErr_SetString(PyExc_IndexError, "revision outside index");
892 PyErr_SetString(PyExc_IndexError, "revision outside index");
923 return NULL;
893 return NULL;
924 }
894 }
925
895
926 baserev = index_baserev(self, iterrev);
896 baserev = index_baserev(self, iterrev);
927
897
928 /* This should never happen. */
898 /* This should never happen. */
929 if (baserev <= -2) {
899 if (baserev <= -2) {
930 /* Error should be set by index_deref() */
900 /* Error should be set by index_deref() */
931 assert(PyErr_Occurred());
901 assert(PyErr_Occurred());
932 goto bail;
902 goto bail;
933 }
903 }
934 }
904 }
935
905
936 if (iterrev == stoprev) {
906 if (iterrev == stoprev) {
937 stopped = 1;
907 stopped = 1;
938 }
908 }
939 else {
909 else {
940 PyObject *value = PyInt_FromLong(iterrev);
910 PyObject *value = PyInt_FromLong(iterrev);
941 if (value == NULL) {
911 if (value == NULL) {
942 goto bail;
912 goto bail;
943 }
913 }
944 if (PyList_Append(chain, value)) {
914 if (PyList_Append(chain, value)) {
945 Py_DECREF(value);
915 Py_DECREF(value);
946 goto bail;
916 goto bail;
947 }
917 }
948 Py_DECREF(value);
918 Py_DECREF(value);
949
919
950 stopped = 0;
920 stopped = 0;
951 }
921 }
952
922
953 if (PyList_Reverse(chain)) {
923 if (PyList_Reverse(chain)) {
954 goto bail;
924 goto bail;
955 }
925 }
956
926
957 result = Py_BuildValue("OO", chain, stopped ? Py_True : Py_False);
927 result = Py_BuildValue("OO", chain, stopped ? Py_True : Py_False);
958 Py_DECREF(chain);
928 Py_DECREF(chain);
959 return result;
929 return result;
960
930
961 bail:
931 bail:
962 Py_DECREF(chain);
932 Py_DECREF(chain);
963 return NULL;
933 return NULL;
964 }
934 }
965
935
966 static inline int nt_level(const char *node, Py_ssize_t level)
936 static inline int nt_level(const char *node, Py_ssize_t level)
967 {
937 {
968 int v = node[level>>1];
938 int v = node[level>>1];
969 if (!(level & 1))
939 if (!(level & 1))
970 v >>= 4;
940 v >>= 4;
971 return v & 0xf;
941 return v & 0xf;
972 }
942 }
973
943
974 /*
944 /*
975 * Return values:
945 * Return values:
976 *
946 *
977 * -4: match is ambiguous (multiple candidates)
947 * -4: match is ambiguous (multiple candidates)
978 * -2: not found
948 * -2: not found
979 * rest: valid rev
949 * rest: valid rev
980 */
950 */
981 static int nt_find(nodetree *self, const char *node, Py_ssize_t nodelen,
951 static int nt_find(nodetree *self, const char *node, Py_ssize_t nodelen,
982 int hex)
952 int hex)
983 {
953 {
984 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
954 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
985 int level, maxlevel, off;
955 int level, maxlevel, off;
986
956
987 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
957 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
988 return -1;
958 return -1;
989
959
990 if (hex)
960 if (hex)
991 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
961 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
992 else
962 else
993 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
963 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
994
964
995 for (level = off = 0; level < maxlevel; level++) {
965 for (level = off = 0; level < maxlevel; level++) {
996 int k = getnybble(node, level);
966 int k = getnybble(node, level);
997 nodetreenode *n = &self->nodes[off];
967 nodetreenode *n = &self->nodes[off];
998 int v = n->children[k];
968 int v = n->children[k];
999
969
1000 if (v < 0) {
970 if (v < 0) {
1001 const char *n;
971 const char *n;
1002 Py_ssize_t i;
972 Py_ssize_t i;
1003
973
1004 v = -(v + 2);
974 v = -(v + 2);
1005 n = index_node(self->index, v);
975 n = index_node(self->index, v);
1006 if (n == NULL)
976 if (n == NULL)
1007 return -2;
977 return -2;
1008 for (i = level; i < maxlevel; i++)
978 for (i = level; i < maxlevel; i++)
1009 if (getnybble(node, i) != nt_level(n, i))
979 if (getnybble(node, i) != nt_level(n, i))
1010 return -2;
980 return -2;
1011 return v;
981 return v;
1012 }
982 }
1013 if (v == 0)
983 if (v == 0)
1014 return -2;
984 return -2;
1015 off = v;
985 off = v;
1016 }
986 }
1017 /* multiple matches against an ambiguous prefix */
987 /* multiple matches against an ambiguous prefix */
1018 return -4;
988 return -4;
1019 }
989 }
1020
990
1021 static int nt_new(nodetree *self)
991 static int nt_new(nodetree *self)
1022 {
992 {
1023 if (self->length == self->capacity) {
993 if (self->length == self->capacity) {
1024 unsigned newcapacity;
994 unsigned newcapacity;
1025 nodetreenode *newnodes;
995 nodetreenode *newnodes;
1026 if (self->capacity >= INT_MAX / (sizeof(nodetreenode) * 2)) {
996 if (self->capacity >= INT_MAX / (sizeof(nodetreenode) * 2)) {
1027 PyErr_SetString(PyExc_MemoryError,
997 PyErr_SetString(PyExc_MemoryError,
1028 "overflow in nt_new");
998 "overflow in nt_new");
1029 return -1;
999 return -1;
1030 }
1000 }
1031 newcapacity = self->capacity * 2;
1001 newcapacity = self->capacity * 2;
1032 newnodes = realloc(self->nodes, newcapacity * sizeof(nodetreenode));
1002 newnodes = realloc(self->nodes, newcapacity * sizeof(nodetreenode));
1033 if (newnodes == NULL) {
1003 if (newnodes == NULL) {
1034 PyErr_SetString(PyExc_MemoryError, "out of memory");
1004 PyErr_SetString(PyExc_MemoryError, "out of memory");
1035 return -1;
1005 return -1;
1036 }
1006 }
1037 self->capacity = newcapacity;
1007 self->capacity = newcapacity;
1038 self->nodes = newnodes;
1008 self->nodes = newnodes;
1039 memset(&self->nodes[self->length], 0,
1009 memset(&self->nodes[self->length], 0,
1040 sizeof(nodetreenode) * (self->capacity - self->length));
1010 sizeof(nodetreenode) * (self->capacity - self->length));
1041 }
1011 }
1042 return self->length++;
1012 return self->length++;
1043 }
1013 }
1044
1014
1045 static int nt_insert(nodetree *self, const char *node, int rev)
1015 static int nt_insert(nodetree *self, const char *node, int rev)
1046 {
1016 {
1047 int level = 0;
1017 int level = 0;
1048 int off = 0;
1018 int off = 0;
1049
1019
1050 while (level < 40) {
1020 while (level < 40) {
1051 int k = nt_level(node, level);
1021 int k = nt_level(node, level);
1052 nodetreenode *n;
1022 nodetreenode *n;
1053 int v;
1023 int v;
1054
1024
1055 n = &self->nodes[off];
1025 n = &self->nodes[off];
1056 v = n->children[k];
1026 v = n->children[k];
1057
1027
1058 if (v == 0) {
1028 if (v == 0) {
1059 n->children[k] = -rev - 2;
1029 n->children[k] = -rev - 2;
1060 return 0;
1030 return 0;
1061 }
1031 }
1062 if (v < 0) {
1032 if (v < 0) {
1063 const char *oldnode = index_node_existing(self->index, -(v + 2));
1033 const char *oldnode = index_node_existing(self->index, -(v + 2));
1064 int noff;
1034 int noff;
1065
1035
1066 if (oldnode == NULL)
1036 if (oldnode == NULL)
1067 return -1;
1037 return -1;
1068 if (!memcmp(oldnode, node, 20)) {
1038 if (!memcmp(oldnode, node, 20)) {
1069 n->children[k] = -rev - 2;
1039 n->children[k] = -rev - 2;
1070 return 0;
1040 return 0;
1071 }
1041 }
1072 noff = nt_new(self);
1042 noff = nt_new(self);
1073 if (noff == -1)
1043 if (noff == -1)
1074 return -1;
1044 return -1;
1075 /* self->nodes may have been changed by realloc */
1045 /* self->nodes may have been changed by realloc */
1076 self->nodes[off].children[k] = noff;
1046 self->nodes[off].children[k] = noff;
1077 off = noff;
1047 off = noff;
1078 n = &self->nodes[off];
1048 n = &self->nodes[off];
1079 n->children[nt_level(oldnode, ++level)] = v;
1049 n->children[nt_level(oldnode, ++level)] = v;
1080 if (level > self->depth)
1050 if (level > self->depth)
1081 self->depth = level;
1051 self->depth = level;
1082 self->splits += 1;
1052 self->splits += 1;
1083 } else {
1053 } else {
1084 level += 1;
1054 level += 1;
1085 off = v;
1055 off = v;
1086 }
1056 }
1087 }
1057 }
1088
1058
1089 return -1;
1059 return -1;
1090 }
1060 }
1091
1061
1092 static int nt_delete_node(nodetree *self, const char *node)
1062 static int nt_delete_node(nodetree *self, const char *node)
1093 {
1063 {
1094 /* rev==-2 happens to get encoded as 0, which is interpreted as not set */
1064 /* rev==-2 happens to get encoded as 0, which is interpreted as not set */
1095 return nt_insert(self, node, -2);
1065 return nt_insert(self, node, -2);
1096 }
1066 }
1097
1067
1098 static int nt_init(nodetree *self, indexObject *index, unsigned capacity)
1068 static int nt_init(nodetree *self, indexObject *index, unsigned capacity)
1099 {
1069 {
1100 self->index = index;
1070 self->index = index;
1101 self->capacity = capacity;
1071 self->capacity = capacity;
1102 self->depth = 0;
1072 self->depth = 0;
1103 self->splits = 0;
1073 self->splits = 0;
1104 self->nodes = calloc(self->capacity, sizeof(nodetreenode));
1074 self->nodes = calloc(self->capacity, sizeof(nodetreenode));
1105 if (self->nodes == NULL) {
1075 if (self->nodes == NULL) {
1106 PyErr_NoMemory();
1076 PyErr_NoMemory();
1107 return -1;
1077 return -1;
1108 }
1078 }
1109 self->length = 1;
1079 self->length = 1;
1110 if (nt_insert(self, nullid, -1) == -1) {
1080 if (nt_insert(self, nullid, -1) == -1) {
1111 free(self->nodes);
1081 free(self->nodes);
1112 return -1;
1082 return -1;
1113 }
1083 }
1114 return 0;
1084 return 0;
1115 }
1085 }
1116
1086
1117 static int nt_partialmatch(nodetree *self, const char *node,
1087 static int nt_partialmatch(nodetree *self, const char *node,
1118 Py_ssize_t nodelen)
1088 Py_ssize_t nodelen)
1119 {
1089 {
1120 return nt_find(self, node, nodelen, 1);
1090 return nt_find(self, node, nodelen, 1);
1121 }
1091 }
1122
1092
1123 /*
1093 /*
1124 * Find the length of the shortest unique prefix of node.
1094 * Find the length of the shortest unique prefix of node.
1125 *
1095 *
1126 * Return values:
1096 * Return values:
1127 *
1097 *
1128 * -3: error (exception set)
1098 * -3: error (exception set)
1129 * -2: not found (no exception set)
1099 * -2: not found (no exception set)
1130 * rest: length of shortest prefix
1100 * rest: length of shortest prefix
1131 */
1101 */
1132 static int nt_shortest(nodetree *self, const char *node)
1102 static int nt_shortest(nodetree *self, const char *node)
1133 {
1103 {
1134 int level, off;
1104 int level, off;
1135
1105
1136 for (level = off = 0; level < 40; level++) {
1106 for (level = off = 0; level < 40; level++) {
1137 int k, v;
1107 int k, v;
1138 nodetreenode *n = &self->nodes[off];
1108 nodetreenode *n = &self->nodes[off];
1139 k = nt_level(node, level);
1109 k = nt_level(node, level);
1140 v = n->children[k];
1110 v = n->children[k];
1141 if (v < 0) {
1111 if (v < 0) {
1142 const char *n;
1112 const char *n;
1143 v = -(v + 2);
1113 v = -(v + 2);
1144 n = index_node_existing(self->index, v);
1114 n = index_node_existing(self->index, v);
1145 if (n == NULL)
1115 if (n == NULL)
1146 return -3;
1116 return -3;
1147 if (memcmp(node, n, 20) != 0)
1117 if (memcmp(node, n, 20) != 0)
1148 /*
1118 /*
1149 * Found a unique prefix, but it wasn't for the
1119 * Found a unique prefix, but it wasn't for the
1150 * requested node (i.e the requested node does
1120 * requested node (i.e the requested node does
1151 * not exist).
1121 * not exist).
1152 */
1122 */
1153 return -2;
1123 return -2;
1154 return level + 1;
1124 return level + 1;
1155 }
1125 }
1156 if (v == 0)
1126 if (v == 0)
1157 return -2;
1127 return -2;
1158 off = v;
1128 off = v;
1159 }
1129 }
1160 /*
1130 /*
1161 * The node was still not unique after 40 hex digits, so this won't
1131 * The node was still not unique after 40 hex digits, so this won't
1162 * happen. Also, if we get here, then there's a programming error in
1132 * happen. Also, if we get here, then there's a programming error in
1163 * this file that made us insert a node longer than 40 hex digits.
1133 * this file that made us insert a node longer than 40 hex digits.
1164 */
1134 */
1165 PyErr_SetString(PyExc_Exception, "broken node tree");
1135 PyErr_SetString(PyExc_Exception, "broken node tree");
1166 return -3;
1136 return -3;
1167 }
1137 }
1168
1138
1169 static int index_init_nt(indexObject *self)
1139 static int index_init_nt(indexObject *self)
1170 {
1140 {
1171 if (self->nt == NULL) {
1141 if (self->nt == NULL) {
1172 if ((size_t)self->raw_length > INT_MAX / sizeof(nodetreenode)) {
1142 if ((size_t)self->raw_length > INT_MAX / sizeof(nodetreenode)) {
1173 PyErr_SetString(PyExc_ValueError, "overflow in index_init_nt");
1143 PyErr_SetString(PyExc_ValueError, "overflow in index_init_nt");
1174 return -1;
1144 return -1;
1175 }
1145 }
1176 self->nt = PyMem_Malloc(sizeof(nodetree));
1146 self->nt = PyMem_Malloc(sizeof(nodetree));
1177 if (self->nt == NULL) {
1147 if (self->nt == NULL) {
1178 PyErr_NoMemory();
1148 PyErr_NoMemory();
1179 return -1;
1149 return -1;
1180 }
1150 }
1181 unsigned capacity = (self->raw_length < 4 ? 4 : (int)self->raw_length / 2);
1151 unsigned capacity = (self->raw_length < 4 ? 4 : (int)self->raw_length / 2);
1182 if (nt_init(self->nt, self, capacity) == -1) {
1152 if (nt_init(self->nt, self, capacity) == -1) {
1183 PyMem_Free(self->nt);
1153 PyMem_Free(self->nt);
1184 self->nt = NULL;
1154 self->nt = NULL;
1185 return -1;
1155 return -1;
1186 }
1156 }
1187 self->ntrev = (int)index_length(self);
1157 self->ntrev = (int)index_length(self);
1188 self->ntlookups = 1;
1158 self->ntlookups = 1;
1189 self->ntmisses = 0;
1159 self->ntmisses = 0;
1190 }
1160 }
1191 return 0;
1161 return 0;
1192 }
1162 }
1193
1163
1194 /*
1164 /*
1195 * Return values:
1165 * Return values:
1196 *
1166 *
1197 * -3: error (exception set)
1167 * -3: error (exception set)
1198 * -2: not found (no exception set)
1168 * -2: not found (no exception set)
1199 * rest: valid rev
1169 * rest: valid rev
1200 */
1170 */
1201 static int index_find_node(indexObject *self,
1171 static int index_find_node(indexObject *self,
1202 const char *node, Py_ssize_t nodelen)
1172 const char *node, Py_ssize_t nodelen)
1203 {
1173 {
1204 int rev;
1174 int rev;
1205
1175
1206 if (index_init_nt(self) == -1)
1176 if (index_init_nt(self) == -1)
1207 return -3;
1177 return -3;
1208
1178
1209 self->ntlookups++;
1179 self->ntlookups++;
1210 rev = nt_find(self->nt, node, nodelen, 0);
1180 rev = nt_find(self->nt, node, nodelen, 0);
1211 if (rev >= -1)
1181 if (rev >= -1)
1212 return rev;
1182 return rev;
1213
1183
1214 /*
1184 /*
1215 * For the first handful of lookups, we scan the entire index,
1185 * For the first handful of lookups, we scan the entire index,
1216 * and cache only the matching nodes. This optimizes for cases
1186 * and cache only the matching nodes. This optimizes for cases
1217 * like "hg tip", where only a few nodes are accessed.
1187 * like "hg tip", where only a few nodes are accessed.
1218 *
1188 *
1219 * After that, we cache every node we visit, using a single
1189 * After that, we cache every node we visit, using a single
1220 * scan amortized over multiple lookups. This gives the best
1190 * scan amortized over multiple lookups. This gives the best
1221 * bulk performance, e.g. for "hg log".
1191 * bulk performance, e.g. for "hg log".
1222 */
1192 */
1223 if (self->ntmisses++ < 4) {
1193 if (self->ntmisses++ < 4) {
1224 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1194 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1225 const char *n = index_node_existing(self, rev);
1195 const char *n = index_node_existing(self, rev);
1226 if (n == NULL)
1196 if (n == NULL)
1227 return -3;
1197 return -3;
1228 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1198 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1229 if (nt_insert(self->nt, n, rev) == -1)
1199 if (nt_insert(self->nt, n, rev) == -1)
1230 return -3;
1200 return -3;
1231 break;
1201 break;
1232 }
1202 }
1233 }
1203 }
1234 } else {
1204 } else {
1235 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1205 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1236 const char *n = index_node_existing(self, rev);
1206 const char *n = index_node_existing(self, rev);
1237 if (n == NULL)
1207 if (n == NULL)
1238 return -3;
1208 return -3;
1239 if (nt_insert(self->nt, n, rev) == -1) {
1209 if (nt_insert(self->nt, n, rev) == -1) {
1240 self->ntrev = rev + 1;
1210 self->ntrev = rev + 1;
1241 return -3;
1211 return -3;
1242 }
1212 }
1243 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1213 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1244 break;
1214 break;
1245 }
1215 }
1246 }
1216 }
1247 self->ntrev = rev;
1217 self->ntrev = rev;
1248 }
1218 }
1249
1219
1250 if (rev >= 0)
1220 if (rev >= 0)
1251 return rev;
1221 return rev;
1252 return -2;
1222 return -2;
1253 }
1223 }
1254
1224
1255 static void raise_revlog_error(void)
1225 static void raise_revlog_error(void)
1256 {
1226 {
1257 PyObject *mod = NULL, *dict = NULL, *errclass = NULL;
1227 PyObject *mod = NULL, *dict = NULL, *errclass = NULL;
1258
1228
1259 mod = PyImport_ImportModule("mercurial.error");
1229 mod = PyImport_ImportModule("mercurial.error");
1260 if (mod == NULL) {
1230 if (mod == NULL) {
1261 goto cleanup;
1231 goto cleanup;
1262 }
1232 }
1263
1233
1264 dict = PyModule_GetDict(mod);
1234 dict = PyModule_GetDict(mod);
1265 if (dict == NULL) {
1235 if (dict == NULL) {
1266 goto cleanup;
1236 goto cleanup;
1267 }
1237 }
1268 Py_INCREF(dict);
1238 Py_INCREF(dict);
1269
1239
1270 errclass = PyDict_GetItemString(dict, "RevlogError");
1240 errclass = PyDict_GetItemString(dict, "RevlogError");
1271 if (errclass == NULL) {
1241 if (errclass == NULL) {
1272 PyErr_SetString(PyExc_SystemError,
1242 PyErr_SetString(PyExc_SystemError,
1273 "could not find RevlogError");
1243 "could not find RevlogError");
1274 goto cleanup;
1244 goto cleanup;
1275 }
1245 }
1276
1246
1277 /* value of exception is ignored by callers */
1247 /* value of exception is ignored by callers */
1278 PyErr_SetString(errclass, "RevlogError");
1248 PyErr_SetString(errclass, "RevlogError");
1279
1249
1280 cleanup:
1250 cleanup:
1281 Py_XDECREF(dict);
1251 Py_XDECREF(dict);
1282 Py_XDECREF(mod);
1252 Py_XDECREF(mod);
1283 }
1253 }
1284
1254
1285 static PyObject *index_getitem(indexObject *self, PyObject *value)
1255 static PyObject *index_getitem(indexObject *self, PyObject *value)
1286 {
1256 {
1287 char *node;
1257 char *node;
1288 int rev;
1258 int rev;
1289
1259
1290 if (PyInt_Check(value))
1260 if (PyInt_Check(value))
1291 return index_get(self, PyInt_AS_LONG(value));
1261 return index_get(self, PyInt_AS_LONG(value));
1292
1262
1293 if (node_check(value, &node) == -1)
1263 if (node_check(value, &node) == -1)
1294 return NULL;
1264 return NULL;
1295 rev = index_find_node(self, node, 20);
1265 rev = index_find_node(self, node, 20);
1296 if (rev >= -1)
1266 if (rev >= -1)
1297 return PyInt_FromLong(rev);
1267 return PyInt_FromLong(rev);
1298 if (rev == -2)
1268 if (rev == -2)
1299 raise_revlog_error();
1269 raise_revlog_error();
1300 return NULL;
1270 return NULL;
1301 }
1271 }
1302
1272
1303 /*
1273 /*
1304 * Fully populate the radix tree.
1274 * Fully populate the radix tree.
1305 */
1275 */
1306 static int index_populate_nt(indexObject *self) {
1276 static int index_populate_nt(indexObject *self) {
1307 int rev;
1277 int rev;
1308 if (self->ntrev > 0) {
1278 if (self->ntrev > 0) {
1309 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1279 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1310 const char *n = index_node_existing(self, rev);
1280 const char *n = index_node_existing(self, rev);
1311 if (n == NULL)
1281 if (n == NULL)
1312 return -1;
1282 return -1;
1313 if (nt_insert(self->nt, n, rev) == -1)
1283 if (nt_insert(self->nt, n, rev) == -1)
1314 return -1;
1284 return -1;
1315 }
1285 }
1316 self->ntrev = -1;
1286 self->ntrev = -1;
1317 }
1287 }
1318 return 0;
1288 return 0;
1319 }
1289 }
1320
1290
1321 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1291 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1322 {
1292 {
1323 const char *fullnode;
1293 const char *fullnode;
1324 int nodelen;
1294 int nodelen;
1325 char *node;
1295 char *node;
1326 int rev, i;
1296 int rev, i;
1327
1297
1328 if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &node, &nodelen))
1298 if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &node, &nodelen))
1329 return NULL;
1299 return NULL;
1330
1300
1331 if (nodelen < 1) {
1301 if (nodelen < 1) {
1332 PyErr_SetString(PyExc_ValueError, "key too short");
1302 PyErr_SetString(PyExc_ValueError, "key too short");
1333 return NULL;
1303 return NULL;
1334 }
1304 }
1335
1305
1336 if (nodelen > 40) {
1306 if (nodelen > 40) {
1337 PyErr_SetString(PyExc_ValueError, "key too long");
1307 PyErr_SetString(PyExc_ValueError, "key too long");
1338 return NULL;
1308 return NULL;
1339 }
1309 }
1340
1310
1341 for (i = 0; i < nodelen; i++)
1311 for (i = 0; i < nodelen; i++)
1342 hexdigit(node, i);
1312 hexdigit(node, i);
1343 if (PyErr_Occurred()) {
1313 if (PyErr_Occurred()) {
1344 /* input contains non-hex characters */
1314 /* input contains non-hex characters */
1345 PyErr_Clear();
1315 PyErr_Clear();
1346 Py_RETURN_NONE;
1316 Py_RETURN_NONE;
1347 }
1317 }
1348
1318
1349 if (index_init_nt(self) == -1)
1319 if (index_init_nt(self) == -1)
1350 return NULL;
1320 return NULL;
1351 if (index_populate_nt(self) == -1)
1321 if (index_populate_nt(self) == -1)
1352 return NULL;
1322 return NULL;
1353 rev = nt_partialmatch(self->nt, node, nodelen);
1323 rev = nt_partialmatch(self->nt, node, nodelen);
1354
1324
1355 switch (rev) {
1325 switch (rev) {
1356 case -4:
1326 case -4:
1357 raise_revlog_error();
1327 raise_revlog_error();
1358 return NULL;
1328 return NULL;
1359 case -2:
1329 case -2:
1360 Py_RETURN_NONE;
1330 Py_RETURN_NONE;
1361 case -1:
1331 case -1:
1362 return PyBytes_FromStringAndSize(nullid, 20);
1332 return PyBytes_FromStringAndSize(nullid, 20);
1363 }
1333 }
1364
1334
1365 fullnode = index_node_existing(self, rev);
1335 fullnode = index_node_existing(self, rev);
1366 if (fullnode == NULL) {
1336 if (fullnode == NULL) {
1367 return NULL;
1337 return NULL;
1368 }
1338 }
1369 return PyBytes_FromStringAndSize(fullnode, 20);
1339 return PyBytes_FromStringAndSize(fullnode, 20);
1370 }
1340 }
1371
1341
1372 static PyObject *index_shortest(indexObject *self, PyObject *args)
1342 static PyObject *index_shortest(indexObject *self, PyObject *args)
1373 {
1343 {
1374 PyObject *val;
1344 PyObject *val;
1375 char *node;
1345 char *node;
1376 int length;
1346 int length;
1377
1347
1378 if (!PyArg_ParseTuple(args, "O", &val))
1348 if (!PyArg_ParseTuple(args, "O", &val))
1379 return NULL;
1349 return NULL;
1380 if (node_check(val, &node) == -1)
1350 if (node_check(val, &node) == -1)
1381 return NULL;
1351 return NULL;
1382
1352
1383 self->ntlookups++;
1353 self->ntlookups++;
1384 if (index_init_nt(self) == -1)
1354 if (index_init_nt(self) == -1)
1385 return NULL;
1355 return NULL;
1386 if (index_populate_nt(self) == -1)
1356 if (index_populate_nt(self) == -1)
1387 return NULL;
1357 return NULL;
1388 length = nt_shortest(self->nt, node);
1358 length = nt_shortest(self->nt, node);
1389 if (length == -3)
1359 if (length == -3)
1390 return NULL;
1360 return NULL;
1391 if (length == -2) {
1361 if (length == -2) {
1392 raise_revlog_error();
1362 raise_revlog_error();
1393 return NULL;
1363 return NULL;
1394 }
1364 }
1395 return PyInt_FromLong(length);
1365 return PyInt_FromLong(length);
1396 }
1366 }
1397
1367
1398 static PyObject *index_m_get(indexObject *self, PyObject *args)
1368 static PyObject *index_m_get(indexObject *self, PyObject *args)
1399 {
1369 {
1400 PyObject *val;
1370 PyObject *val;
1401 char *node;
1371 char *node;
1402 int rev;
1372 int rev;
1403
1373
1404 if (!PyArg_ParseTuple(args, "O", &val))
1374 if (!PyArg_ParseTuple(args, "O", &val))
1405 return NULL;
1375 return NULL;
1406 if (node_check(val, &node) == -1)
1376 if (node_check(val, &node) == -1)
1407 return NULL;
1377 return NULL;
1408 rev = index_find_node(self, node, 20);
1378 rev = index_find_node(self, node, 20);
1409 if (rev == -3)
1379 if (rev == -3)
1410 return NULL;
1380 return NULL;
1411 if (rev == -2)
1381 if (rev == -2)
1412 Py_RETURN_NONE;
1382 Py_RETURN_NONE;
1413 return PyInt_FromLong(rev);
1383 return PyInt_FromLong(rev);
1414 }
1384 }
1415
1385
1416 static int index_contains(indexObject *self, PyObject *value)
1386 static int index_contains(indexObject *self, PyObject *value)
1417 {
1387 {
1418 char *node;
1388 char *node;
1419
1389
1420 if (PyInt_Check(value)) {
1390 if (PyInt_Check(value)) {
1421 long rev = PyInt_AS_LONG(value);
1391 long rev = PyInt_AS_LONG(value);
1422 return rev >= -1 && rev < index_length(self);
1392 return rev >= -1 && rev < index_length(self);
1423 }
1393 }
1424
1394
1425 if (node_check(value, &node) == -1)
1395 if (node_check(value, &node) == -1)
1426 return -1;
1396 return -1;
1427
1397
1428 switch (index_find_node(self, node, 20)) {
1398 switch (index_find_node(self, node, 20)) {
1429 case -3:
1399 case -3:
1430 return -1;
1400 return -1;
1431 case -2:
1401 case -2:
1432 return 0;
1402 return 0;
1433 default:
1403 default:
1434 return 1;
1404 return 1;
1435 }
1405 }
1436 }
1406 }
1437
1407
1438 typedef uint64_t bitmask;
1408 typedef uint64_t bitmask;
1439
1409
1440 /*
1410 /*
1441 * Given a disjoint set of revs, return all candidates for the
1411 * Given a disjoint set of revs, return all candidates for the
1442 * greatest common ancestor. In revset notation, this is the set
1412 * greatest common ancestor. In revset notation, this is the set
1443 * "heads(::a and ::b and ...)"
1413 * "heads(::a and ::b and ...)"
1444 */
1414 */
1445 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
1415 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
1446 int revcount)
1416 int revcount)
1447 {
1417 {
1448 const bitmask allseen = (1ull << revcount) - 1;
1418 const bitmask allseen = (1ull << revcount) - 1;
1449 const bitmask poison = 1ull << revcount;
1419 const bitmask poison = 1ull << revcount;
1450 PyObject *gca = PyList_New(0);
1420 PyObject *gca = PyList_New(0);
1451 int i, v, interesting;
1421 int i, v, interesting;
1452 int maxrev = -1;
1422 int maxrev = -1;
1453 bitmask sp;
1423 bitmask sp;
1454 bitmask *seen;
1424 bitmask *seen;
1455
1425
1456 if (gca == NULL)
1426 if (gca == NULL)
1457 return PyErr_NoMemory();
1427 return PyErr_NoMemory();
1458
1428
1459 for (i = 0; i < revcount; i++) {
1429 for (i = 0; i < revcount; i++) {
1460 if (revs[i] > maxrev)
1430 if (revs[i] > maxrev)
1461 maxrev = revs[i];
1431 maxrev = revs[i];
1462 }
1432 }
1463
1433
1464 seen = calloc(sizeof(*seen), maxrev + 1);
1434 seen = calloc(sizeof(*seen), maxrev + 1);
1465 if (seen == NULL) {
1435 if (seen == NULL) {
1466 Py_DECREF(gca);
1436 Py_DECREF(gca);
1467 return PyErr_NoMemory();
1437 return PyErr_NoMemory();
1468 }
1438 }
1469
1439
1470 for (i = 0; i < revcount; i++)
1440 for (i = 0; i < revcount; i++)
1471 seen[revs[i]] = 1ull << i;
1441 seen[revs[i]] = 1ull << i;
1472
1442
1473 interesting = revcount;
1443 interesting = revcount;
1474
1444
1475 for (v = maxrev; v >= 0 && interesting; v--) {
1445 for (v = maxrev; v >= 0 && interesting; v--) {
1476 bitmask sv = seen[v];
1446 bitmask sv = seen[v];
1477 int parents[2];
1447 int parents[2];
1478
1448
1479 if (!sv)
1449 if (!sv)
1480 continue;
1450 continue;
1481
1451
1482 if (sv < poison) {
1452 if (sv < poison) {
1483 interesting -= 1;
1453 interesting -= 1;
1484 if (sv == allseen) {
1454 if (sv == allseen) {
1485 PyObject *obj = PyInt_FromLong(v);
1455 PyObject *obj = PyInt_FromLong(v);
1486 if (obj == NULL)
1456 if (obj == NULL)
1487 goto bail;
1457 goto bail;
1488 if (PyList_Append(gca, obj) == -1) {
1458 if (PyList_Append(gca, obj) == -1) {
1489 Py_DECREF(obj);
1459 Py_DECREF(obj);
1490 goto bail;
1460 goto bail;
1491 }
1461 }
1492 sv |= poison;
1462 sv |= poison;
1493 for (i = 0; i < revcount; i++) {
1463 for (i = 0; i < revcount; i++) {
1494 if (revs[i] == v)
1464 if (revs[i] == v)
1495 goto done;
1465 goto done;
1496 }
1466 }
1497 }
1467 }
1498 }
1468 }
1499 if (index_get_parents(self, v, parents, maxrev) < 0)
1469 if (index_get_parents(self, v, parents, maxrev) < 0)
1500 goto bail;
1470 goto bail;
1501
1471
1502 for (i = 0; i < 2; i++) {
1472 for (i = 0; i < 2; i++) {
1503 int p = parents[i];
1473 int p = parents[i];
1504 if (p == -1)
1474 if (p == -1)
1505 continue;
1475 continue;
1506 sp = seen[p];
1476 sp = seen[p];
1507 if (sv < poison) {
1477 if (sv < poison) {
1508 if (sp == 0) {
1478 if (sp == 0) {
1509 seen[p] = sv;
1479 seen[p] = sv;
1510 interesting++;
1480 interesting++;
1511 }
1481 }
1512 else if (sp != sv)
1482 else if (sp != sv)
1513 seen[p] |= sv;
1483 seen[p] |= sv;
1514 } else {
1484 } else {
1515 if (sp && sp < poison)
1485 if (sp && sp < poison)
1516 interesting--;
1486 interesting--;
1517 seen[p] = sv;
1487 seen[p] = sv;
1518 }
1488 }
1519 }
1489 }
1520 }
1490 }
1521
1491
1522 done:
1492 done:
1523 free(seen);
1493 free(seen);
1524 return gca;
1494 return gca;
1525 bail:
1495 bail:
1526 free(seen);
1496 free(seen);
1527 Py_XDECREF(gca);
1497 Py_XDECREF(gca);
1528 return NULL;
1498 return NULL;
1529 }
1499 }
1530
1500
1531 /*
1501 /*
1532 * Given a disjoint set of revs, return the subset with the longest
1502 * Given a disjoint set of revs, return the subset with the longest
1533 * path to the root.
1503 * path to the root.
1534 */
1504 */
1535 static PyObject *find_deepest(indexObject *self, PyObject *revs)
1505 static PyObject *find_deepest(indexObject *self, PyObject *revs)
1536 {
1506 {
1537 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
1507 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
1538 static const Py_ssize_t capacity = 24;
1508 static const Py_ssize_t capacity = 24;
1539 int *depth, *interesting = NULL;
1509 int *depth, *interesting = NULL;
1540 int i, j, v, ninteresting;
1510 int i, j, v, ninteresting;
1541 PyObject *dict = NULL, *keys = NULL;
1511 PyObject *dict = NULL, *keys = NULL;
1542 long *seen = NULL;
1512 long *seen = NULL;
1543 int maxrev = -1;
1513 int maxrev = -1;
1544 long final;
1514 long final;
1545
1515
1546 if (revcount > capacity) {
1516 if (revcount > capacity) {
1547 PyErr_Format(PyExc_OverflowError,
1517 PyErr_Format(PyExc_OverflowError,
1548 "bitset size (%ld) > capacity (%ld)",
1518 "bitset size (%ld) > capacity (%ld)",
1549 (long)revcount, (long)capacity);
1519 (long)revcount, (long)capacity);
1550 return NULL;
1520 return NULL;
1551 }
1521 }
1552
1522
1553 for (i = 0; i < revcount; i++) {
1523 for (i = 0; i < revcount; i++) {
1554 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1524 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1555 if (n > maxrev)
1525 if (n > maxrev)
1556 maxrev = n;
1526 maxrev = n;
1557 }
1527 }
1558
1528
1559 depth = calloc(sizeof(*depth), maxrev + 1);
1529 depth = calloc(sizeof(*depth), maxrev + 1);
1560 if (depth == NULL)
1530 if (depth == NULL)
1561 return PyErr_NoMemory();
1531 return PyErr_NoMemory();
1562
1532
1563 seen = calloc(sizeof(*seen), maxrev + 1);
1533 seen = calloc(sizeof(*seen), maxrev + 1);
1564 if (seen == NULL) {
1534 if (seen == NULL) {
1565 PyErr_NoMemory();
1535 PyErr_NoMemory();
1566 goto bail;
1536 goto bail;
1567 }
1537 }
1568
1538
1569 interesting = calloc(sizeof(*interesting), 1 << revcount);
1539 interesting = calloc(sizeof(*interesting), 1 << revcount);
1570 if (interesting == NULL) {
1540 if (interesting == NULL) {
1571 PyErr_NoMemory();
1541 PyErr_NoMemory();
1572 goto bail;
1542 goto bail;
1573 }
1543 }
1574
1544
1575 if (PyList_Sort(revs) == -1)
1545 if (PyList_Sort(revs) == -1)
1576 goto bail;
1546 goto bail;
1577
1547
1578 for (i = 0; i < revcount; i++) {
1548 for (i = 0; i < revcount; i++) {
1579 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1549 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1580 long b = 1l << i;
1550 long b = 1l << i;
1581 depth[n] = 1;
1551 depth[n] = 1;
1582 seen[n] = b;
1552 seen[n] = b;
1583 interesting[b] = 1;
1553 interesting[b] = 1;
1584 }
1554 }
1585
1555
1586 /* invariant: ninteresting is the number of non-zero entries in
1556 /* invariant: ninteresting is the number of non-zero entries in
1587 * interesting. */
1557 * interesting. */
1588 ninteresting = (int)revcount;
1558 ninteresting = (int)revcount;
1589
1559
1590 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
1560 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
1591 int dv = depth[v];
1561 int dv = depth[v];
1592 int parents[2];
1562 int parents[2];
1593 long sv;
1563 long sv;
1594
1564
1595 if (dv == 0)
1565 if (dv == 0)
1596 continue;
1566 continue;
1597
1567
1598 sv = seen[v];
1568 sv = seen[v];
1599 if (index_get_parents(self, v, parents, maxrev) < 0)
1569 if (index_get_parents(self, v, parents, maxrev) < 0)
1600 goto bail;
1570 goto bail;
1601
1571
1602 for (i = 0; i < 2; i++) {
1572 for (i = 0; i < 2; i++) {
1603 int p = parents[i];
1573 int p = parents[i];
1604 long sp;
1574 long sp;
1605 int dp;
1575 int dp;
1606
1576
1607 if (p == -1)
1577 if (p == -1)
1608 continue;
1578 continue;
1609
1579
1610 dp = depth[p];
1580 dp = depth[p];
1611 sp = seen[p];
1581 sp = seen[p];
1612 if (dp <= dv) {
1582 if (dp <= dv) {
1613 depth[p] = dv + 1;
1583 depth[p] = dv + 1;
1614 if (sp != sv) {
1584 if (sp != sv) {
1615 interesting[sv] += 1;
1585 interesting[sv] += 1;
1616 seen[p] = sv;
1586 seen[p] = sv;
1617 if (sp) {
1587 if (sp) {
1618 interesting[sp] -= 1;
1588 interesting[sp] -= 1;
1619 if (interesting[sp] == 0)
1589 if (interesting[sp] == 0)
1620 ninteresting -= 1;
1590 ninteresting -= 1;
1621 }
1591 }
1622 }
1592 }
1623 }
1593 }
1624 else if (dv == dp - 1) {
1594 else if (dv == dp - 1) {
1625 long nsp = sp | sv;
1595 long nsp = sp | sv;
1626 if (nsp == sp)
1596 if (nsp == sp)
1627 continue;
1597 continue;
1628 seen[p] = nsp;
1598 seen[p] = nsp;
1629 interesting[sp] -= 1;
1599 interesting[sp] -= 1;
1630 if (interesting[sp] == 0)
1600 if (interesting[sp] == 0)
1631 ninteresting -= 1;
1601 ninteresting -= 1;
1632 if (interesting[nsp] == 0)
1602 if (interesting[nsp] == 0)
1633 ninteresting += 1;
1603 ninteresting += 1;
1634 interesting[nsp] += 1;
1604 interesting[nsp] += 1;
1635 }
1605 }
1636 }
1606 }
1637 interesting[sv] -= 1;
1607 interesting[sv] -= 1;
1638 if (interesting[sv] == 0)
1608 if (interesting[sv] == 0)
1639 ninteresting -= 1;
1609 ninteresting -= 1;
1640 }
1610 }
1641
1611
1642 final = 0;
1612 final = 0;
1643 j = ninteresting;
1613 j = ninteresting;
1644 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
1614 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
1645 if (interesting[i] == 0)
1615 if (interesting[i] == 0)
1646 continue;
1616 continue;
1647 final |= i;
1617 final |= i;
1648 j -= 1;
1618 j -= 1;
1649 }
1619 }
1650 if (final == 0) {
1620 if (final == 0) {
1651 keys = PyList_New(0);
1621 keys = PyList_New(0);
1652 goto bail;
1622 goto bail;
1653 }
1623 }
1654
1624
1655 dict = PyDict_New();
1625 dict = PyDict_New();
1656 if (dict == NULL)
1626 if (dict == NULL)
1657 goto bail;
1627 goto bail;
1658
1628
1659 for (i = 0; i < revcount; i++) {
1629 for (i = 0; i < revcount; i++) {
1660 PyObject *key;
1630 PyObject *key;
1661
1631
1662 if ((final & (1 << i)) == 0)
1632 if ((final & (1 << i)) == 0)
1663 continue;
1633 continue;
1664
1634
1665 key = PyList_GET_ITEM(revs, i);
1635 key = PyList_GET_ITEM(revs, i);
1666 Py_INCREF(key);
1636 Py_INCREF(key);
1667 Py_INCREF(Py_None);
1637 Py_INCREF(Py_None);
1668 if (PyDict_SetItem(dict, key, Py_None) == -1) {
1638 if (PyDict_SetItem(dict, key, Py_None) == -1) {
1669 Py_DECREF(key);
1639 Py_DECREF(key);
1670 Py_DECREF(Py_None);
1640 Py_DECREF(Py_None);
1671 goto bail;
1641 goto bail;
1672 }
1642 }
1673 }
1643 }
1674
1644
1675 keys = PyDict_Keys(dict);
1645 keys = PyDict_Keys(dict);
1676
1646
1677 bail:
1647 bail:
1678 free(depth);
1648 free(depth);
1679 free(seen);
1649 free(seen);
1680 free(interesting);
1650 free(interesting);
1681 Py_XDECREF(dict);
1651 Py_XDECREF(dict);
1682
1652
1683 return keys;
1653 return keys;
1684 }
1654 }
1685
1655
1686 /*
1656 /*
1687 * Given a (possibly overlapping) set of revs, return all the
1657 * Given a (possibly overlapping) set of revs, return all the
1688 * common ancestors heads: heads(::args[0] and ::a[1] and ...)
1658 * common ancestors heads: heads(::args[0] and ::a[1] and ...)
1689 */
1659 */
1690 static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args)
1660 static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args)
1691 {
1661 {
1692 PyObject *ret = NULL;
1662 PyObject *ret = NULL;
1693 Py_ssize_t argcount, i, len;
1663 Py_ssize_t argcount, i, len;
1694 bitmask repeat = 0;
1664 bitmask repeat = 0;
1695 int revcount = 0;
1665 int revcount = 0;
1696 int *revs;
1666 int *revs;
1697
1667
1698 argcount = PySequence_Length(args);
1668 argcount = PySequence_Length(args);
1699 revs = PyMem_Malloc(argcount * sizeof(*revs));
1669 revs = PyMem_Malloc(argcount * sizeof(*revs));
1700 if (argcount > 0 && revs == NULL)
1670 if (argcount > 0 && revs == NULL)
1701 return PyErr_NoMemory();
1671 return PyErr_NoMemory();
1702 len = index_length(self);
1672 len = index_length(self);
1703
1673
1704 for (i = 0; i < argcount; i++) {
1674 for (i = 0; i < argcount; i++) {
1705 static const int capacity = 24;
1675 static const int capacity = 24;
1706 PyObject *obj = PySequence_GetItem(args, i);
1676 PyObject *obj = PySequence_GetItem(args, i);
1707 bitmask x;
1677 bitmask x;
1708 long val;
1678 long val;
1709
1679
1710 if (!PyInt_Check(obj)) {
1680 if (!PyInt_Check(obj)) {
1711 PyErr_SetString(PyExc_TypeError,
1681 PyErr_SetString(PyExc_TypeError,
1712 "arguments must all be ints");
1682 "arguments must all be ints");
1713 Py_DECREF(obj);
1683 Py_DECREF(obj);
1714 goto bail;
1684 goto bail;
1715 }
1685 }
1716 val = PyInt_AsLong(obj);
1686 val = PyInt_AsLong(obj);
1717 Py_DECREF(obj);
1687 Py_DECREF(obj);
1718 if (val == -1) {
1688 if (val == -1) {
1719 ret = PyList_New(0);
1689 ret = PyList_New(0);
1720 goto done;
1690 goto done;
1721 }
1691 }
1722 if (val < 0 || val >= len) {
1692 if (val < 0 || val >= len) {
1723 PyErr_SetString(PyExc_IndexError,
1693 PyErr_SetString(PyExc_IndexError,
1724 "index out of range");
1694 "index out of range");
1725 goto bail;
1695 goto bail;
1726 }
1696 }
1727 /* this cheesy bloom filter lets us avoid some more
1697 /* this cheesy bloom filter lets us avoid some more
1728 * expensive duplicate checks in the common set-is-disjoint
1698 * expensive duplicate checks in the common set-is-disjoint
1729 * case */
1699 * case */
1730 x = 1ull << (val & 0x3f);
1700 x = 1ull << (val & 0x3f);
1731 if (repeat & x) {
1701 if (repeat & x) {
1732 int k;
1702 int k;
1733 for (k = 0; k < revcount; k++) {
1703 for (k = 0; k < revcount; k++) {
1734 if (val == revs[k])
1704 if (val == revs[k])
1735 goto duplicate;
1705 goto duplicate;
1736 }
1706 }
1737 }
1707 }
1738 else repeat |= x;
1708 else repeat |= x;
1739 if (revcount >= capacity) {
1709 if (revcount >= capacity) {
1740 PyErr_Format(PyExc_OverflowError,
1710 PyErr_Format(PyExc_OverflowError,
1741 "bitset size (%d) > capacity (%d)",
1711 "bitset size (%d) > capacity (%d)",
1742 revcount, capacity);
1712 revcount, capacity);
1743 goto bail;
1713 goto bail;
1744 }
1714 }
1745 revs[revcount++] = (int)val;
1715 revs[revcount++] = (int)val;
1746 duplicate:;
1716 duplicate:;
1747 }
1717 }
1748
1718
1749 if (revcount == 0) {
1719 if (revcount == 0) {
1750 ret = PyList_New(0);
1720 ret = PyList_New(0);
1751 goto done;
1721 goto done;
1752 }
1722 }
1753 if (revcount == 1) {
1723 if (revcount == 1) {
1754 PyObject *obj;
1724 PyObject *obj;
1755 ret = PyList_New(1);
1725 ret = PyList_New(1);
1756 if (ret == NULL)
1726 if (ret == NULL)
1757 goto bail;
1727 goto bail;
1758 obj = PyInt_FromLong(revs[0]);
1728 obj = PyInt_FromLong(revs[0]);
1759 if (obj == NULL)
1729 if (obj == NULL)
1760 goto bail;
1730 goto bail;
1761 PyList_SET_ITEM(ret, 0, obj);
1731 PyList_SET_ITEM(ret, 0, obj);
1762 goto done;
1732 goto done;
1763 }
1733 }
1764
1734
1765 ret = find_gca_candidates(self, revs, revcount);
1735 ret = find_gca_candidates(self, revs, revcount);
1766 if (ret == NULL)
1736 if (ret == NULL)
1767 goto bail;
1737 goto bail;
1768
1738
1769 done:
1739 done:
1770 PyMem_Free(revs);
1740 PyMem_Free(revs);
1771 return ret;
1741 return ret;
1772
1742
1773 bail:
1743 bail:
1774 PyMem_Free(revs);
1744 PyMem_Free(revs);
1775 Py_XDECREF(ret);
1745 Py_XDECREF(ret);
1776 return NULL;
1746 return NULL;
1777 }
1747 }
1778
1748
1779 /*
1749 /*
1780 * Given a (possibly overlapping) set of revs, return the greatest
1750 * Given a (possibly overlapping) set of revs, return the greatest
1781 * common ancestors: those with the longest path to the root.
1751 * common ancestors: those with the longest path to the root.
1782 */
1752 */
1783 static PyObject *index_ancestors(indexObject *self, PyObject *args)
1753 static PyObject *index_ancestors(indexObject *self, PyObject *args)
1784 {
1754 {
1785 PyObject *ret;
1755 PyObject *ret;
1786 PyObject *gca = index_commonancestorsheads(self, args);
1756 PyObject *gca = index_commonancestorsheads(self, args);
1787 if (gca == NULL)
1757 if (gca == NULL)
1788 return NULL;
1758 return NULL;
1789
1759
1790 if (PyList_GET_SIZE(gca) <= 1) {
1760 if (PyList_GET_SIZE(gca) <= 1) {
1791 return gca;
1761 return gca;
1792 }
1762 }
1793
1763
1794 ret = find_deepest(self, gca);
1764 ret = find_deepest(self, gca);
1795 Py_DECREF(gca);
1765 Py_DECREF(gca);
1796 return ret;
1766 return ret;
1797 }
1767 }
1798
1768
1799 /*
1769 /*
1800 * Invalidate any trie entries introduced by added revs.
1770 * Invalidate any trie entries introduced by added revs.
1801 */
1771 */
1802 static void index_invalidate_added(indexObject *self, Py_ssize_t start)
1772 static void index_invalidate_added(indexObject *self, Py_ssize_t start)
1803 {
1773 {
1804 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
1774 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
1805
1775
1806 for (i = start; i < len; i++) {
1776 for (i = start; i < len; i++) {
1807 PyObject *tuple = PyList_GET_ITEM(self->added, i);
1777 PyObject *tuple = PyList_GET_ITEM(self->added, i);
1808 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
1778 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
1809
1779
1810 nt_delete_node(self->nt, PyBytes_AS_STRING(node));
1780 nt_delete_node(self->nt, PyBytes_AS_STRING(node));
1811 }
1781 }
1812
1782
1813 if (start == 0)
1783 if (start == 0)
1814 Py_CLEAR(self->added);
1784 Py_CLEAR(self->added);
1815 }
1785 }
1816
1786
1817 /*
1787 /*
1818 * Delete a numeric range of revs, which must be at the end of the
1788 * Delete a numeric range of revs, which must be at the end of the
1819 * range, but exclude the sentinel nullid entry.
1789 * range, but exclude the sentinel nullid entry.
1820 */
1790 */
1821 static int index_slice_del(indexObject *self, PyObject *item)
1791 static int index_slice_del(indexObject *self, PyObject *item)
1822 {
1792 {
1823 Py_ssize_t start, stop, step, slicelength;
1793 Py_ssize_t start, stop, step, slicelength;
1824 Py_ssize_t length = index_length(self) + 1;
1794 Py_ssize_t length = index_length(self) + 1;
1825 int ret = 0;
1795 int ret = 0;
1826
1796
1827 /* Argument changed from PySliceObject* to PyObject* in Python 3. */
1797 /* Argument changed from PySliceObject* to PyObject* in Python 3. */
1828 #ifdef IS_PY3K
1798 #ifdef IS_PY3K
1829 if (PySlice_GetIndicesEx(item, length,
1799 if (PySlice_GetIndicesEx(item, length,
1830 #else
1800 #else
1831 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
1801 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
1832 #endif
1802 #endif
1833 &start, &stop, &step, &slicelength) < 0)
1803 &start, &stop, &step, &slicelength) < 0)
1834 return -1;
1804 return -1;
1835
1805
1836 if (slicelength <= 0)
1806 if (slicelength <= 0)
1837 return 0;
1807 return 0;
1838
1808
1839 if ((step < 0 && start < stop) || (step > 0 && start > stop))
1809 if ((step < 0 && start < stop) || (step > 0 && start > stop))
1840 stop = start;
1810 stop = start;
1841
1811
1842 if (step < 0) {
1812 if (step < 0) {
1843 stop = start + 1;
1813 stop = start + 1;
1844 start = stop + step*(slicelength - 1) - 1;
1814 start = stop + step*(slicelength - 1) - 1;
1845 step = -step;
1815 step = -step;
1846 }
1816 }
1847
1817
1848 if (step != 1) {
1818 if (step != 1) {
1849 PyErr_SetString(PyExc_ValueError,
1819 PyErr_SetString(PyExc_ValueError,
1850 "revlog index delete requires step size of 1");
1820 "revlog index delete requires step size of 1");
1851 return -1;
1821 return -1;
1852 }
1822 }
1853
1823
1854 if (stop != length - 1) {
1824 if (stop != length - 1) {
1855 PyErr_SetString(PyExc_IndexError,
1825 PyErr_SetString(PyExc_IndexError,
1856 "revlog index deletion indices are invalid");
1826 "revlog index deletion indices are invalid");
1857 return -1;
1827 return -1;
1858 }
1828 }
1859
1829
1860 if (start < self->length - 1) {
1830 if (start < self->length - 1) {
1861 if (self->nt) {
1831 if (self->nt) {
1862 Py_ssize_t i;
1832 Py_ssize_t i;
1863
1833
1864 for (i = start + 1; i < self->length - 1; i++) {
1834 for (i = start + 1; i < self->length - 1; i++) {
1865 const char *node = index_node_existing(self, i);
1835 const char *node = index_node_existing(self, i);
1866 if (node == NULL)
1836 if (node == NULL)
1867 return -1;
1837 return -1;
1868
1838
1869 nt_delete_node(self->nt, node);
1839 nt_delete_node(self->nt, node);
1870 }
1840 }
1871 if (self->added)
1841 if (self->added)
1872 index_invalidate_added(self, 0);
1842 index_invalidate_added(self, 0);
1873 if (self->ntrev > start)
1843 if (self->ntrev > start)
1874 self->ntrev = (int)start;
1844 self->ntrev = (int)start;
1875 }
1845 }
1876 self->length = start + 1;
1846 self->length = start + 1;
1877 if (start < self->raw_length) {
1847 if (start < self->raw_length) {
1878 if (self->cache) {
1848 if (self->cache) {
1879 Py_ssize_t i;
1849 Py_ssize_t i;
1880 for (i = start; i < self->raw_length; i++)
1850 for (i = start; i < self->raw_length; i++)
1881 Py_CLEAR(self->cache[i]);
1851 Py_CLEAR(self->cache[i]);
1882 }
1852 }
1883 self->raw_length = start;
1853 self->raw_length = start;
1884 }
1854 }
1885 goto done;
1855 goto done;
1886 }
1856 }
1887
1857
1888 if (self->nt) {
1858 if (self->nt) {
1889 index_invalidate_added(self, start - self->length + 1);
1859 index_invalidate_added(self, start - self->length + 1);
1890 if (self->ntrev > start)
1860 if (self->ntrev > start)
1891 self->ntrev = (int)start;
1861 self->ntrev = (int)start;
1892 }
1862 }
1893 if (self->added)
1863 if (self->added)
1894 ret = PyList_SetSlice(self->added, start - self->length + 1,
1864 ret = PyList_SetSlice(self->added, start - self->length + 1,
1895 PyList_GET_SIZE(self->added), NULL);
1865 PyList_GET_SIZE(self->added), NULL);
1896 done:
1866 done:
1897 Py_CLEAR(self->headrevs);
1867 Py_CLEAR(self->headrevs);
1898 return ret;
1868 return ret;
1899 }
1869 }
1900
1870
1901 /*
1871 /*
1902 * Supported ops:
1872 * Supported ops:
1903 *
1873 *
1904 * slice deletion
1874 * slice deletion
1905 * string assignment (extend node->rev mapping)
1875 * string assignment (extend node->rev mapping)
1906 * string deletion (shrink node->rev mapping)
1876 * string deletion (shrink node->rev mapping)
1907 */
1877 */
1908 static int index_assign_subscript(indexObject *self, PyObject *item,
1878 static int index_assign_subscript(indexObject *self, PyObject *item,
1909 PyObject *value)
1879 PyObject *value)
1910 {
1880 {
1911 char *node;
1881 char *node;
1912 long rev;
1882 long rev;
1913
1883
1914 if (PySlice_Check(item) && value == NULL)
1884 if (PySlice_Check(item) && value == NULL)
1915 return index_slice_del(self, item);
1885 return index_slice_del(self, item);
1916
1886
1917 if (node_check(item, &node) == -1)
1887 if (node_check(item, &node) == -1)
1918 return -1;
1888 return -1;
1919
1889
1920 if (value == NULL)
1890 if (value == NULL)
1921 return self->nt ? nt_delete_node(self->nt, node) : 0;
1891 return self->nt ? nt_delete_node(self->nt, node) : 0;
1922 rev = PyInt_AsLong(value);
1892 rev = PyInt_AsLong(value);
1923 if (rev > INT_MAX || rev < 0) {
1893 if (rev > INT_MAX || rev < 0) {
1924 if (!PyErr_Occurred())
1894 if (!PyErr_Occurred())
1925 PyErr_SetString(PyExc_ValueError, "rev out of range");
1895 PyErr_SetString(PyExc_ValueError, "rev out of range");
1926 return -1;
1896 return -1;
1927 }
1897 }
1928
1898
1929 if (index_init_nt(self) == -1)
1899 if (index_init_nt(self) == -1)
1930 return -1;
1900 return -1;
1931 return nt_insert(self->nt, node, (int)rev);
1901 return nt_insert(self->nt, node, (int)rev);
1932 }
1902 }
1933
1903
1934 /*
1904 /*
1935 * Find all RevlogNG entries in an index that has inline data. Update
1905 * Find all RevlogNG entries in an index that has inline data. Update
1936 * the optional "offsets" table with those entries.
1906 * the optional "offsets" table with those entries.
1937 */
1907 */
1938 static Py_ssize_t inline_scan(indexObject *self, const char **offsets)
1908 static Py_ssize_t inline_scan(indexObject *self, const char **offsets)
1939 {
1909 {
1940 const char *data = (const char *)self->buf.buf;
1910 const char *data = (const char *)self->buf.buf;
1941 Py_ssize_t pos = 0;
1911 Py_ssize_t pos = 0;
1942 Py_ssize_t end = self->buf.len;
1912 Py_ssize_t end = self->buf.len;
1943 long incr = v1_hdrsize;
1913 long incr = v1_hdrsize;
1944 Py_ssize_t len = 0;
1914 Py_ssize_t len = 0;
1945
1915
1946 while (pos + v1_hdrsize <= end && pos >= 0) {
1916 while (pos + v1_hdrsize <= end && pos >= 0) {
1947 uint32_t comp_len;
1917 uint32_t comp_len;
1948 /* 3rd element of header is length of compressed inline data */
1918 /* 3rd element of header is length of compressed inline data */
1949 comp_len = getbe32(data + pos + 8);
1919 comp_len = getbe32(data + pos + 8);
1950 incr = v1_hdrsize + comp_len;
1920 incr = v1_hdrsize + comp_len;
1951 if (offsets)
1921 if (offsets)
1952 offsets[len] = data + pos;
1922 offsets[len] = data + pos;
1953 len++;
1923 len++;
1954 pos += incr;
1924 pos += incr;
1955 }
1925 }
1956
1926
1957 if (pos != end) {
1927 if (pos != end) {
1958 if (!PyErr_Occurred())
1928 if (!PyErr_Occurred())
1959 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1929 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1960 return -1;
1930 return -1;
1961 }
1931 }
1962
1932
1963 return len;
1933 return len;
1964 }
1934 }
1965
1935
1966 static int index_init(indexObject *self, PyObject *args)
1936 static int index_init(indexObject *self, PyObject *args)
1967 {
1937 {
1968 PyObject *data_obj, *inlined_obj;
1938 PyObject *data_obj, *inlined_obj;
1969 Py_ssize_t size;
1939 Py_ssize_t size;
1970
1940
1971 /* Initialize before argument-checking to avoid index_dealloc() crash. */
1941 /* Initialize before argument-checking to avoid index_dealloc() crash. */
1972 self->raw_length = 0;
1942 self->raw_length = 0;
1973 self->added = NULL;
1943 self->added = NULL;
1974 self->cache = NULL;
1944 self->cache = NULL;
1975 self->data = NULL;
1945 self->data = NULL;
1976 memset(&self->buf, 0, sizeof(self->buf));
1946 memset(&self->buf, 0, sizeof(self->buf));
1977 self->headrevs = NULL;
1947 self->headrevs = NULL;
1978 self->filteredrevs = Py_None;
1948 self->filteredrevs = Py_None;
1979 Py_INCREF(Py_None);
1949 Py_INCREF(Py_None);
1980 self->nt = NULL;
1950 self->nt = NULL;
1981 self->offsets = NULL;
1951 self->offsets = NULL;
1982
1952
1983 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
1953 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
1984 return -1;
1954 return -1;
1985 if (!PyObject_CheckBuffer(data_obj)) {
1955 if (!PyObject_CheckBuffer(data_obj)) {
1986 PyErr_SetString(PyExc_TypeError,
1956 PyErr_SetString(PyExc_TypeError,
1987 "data does not support buffer interface");
1957 "data does not support buffer interface");
1988 return -1;
1958 return -1;
1989 }
1959 }
1990
1960
1991 if (PyObject_GetBuffer(data_obj, &self->buf, PyBUF_SIMPLE) == -1)
1961 if (PyObject_GetBuffer(data_obj, &self->buf, PyBUF_SIMPLE) == -1)
1992 return -1;
1962 return -1;
1993 size = self->buf.len;
1963 size = self->buf.len;
1994
1964
1995 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
1965 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
1996 self->data = data_obj;
1966 self->data = data_obj;
1997
1967
1998 self->ntlookups = self->ntmisses = 0;
1968 self->ntlookups = self->ntmisses = 0;
1999 self->ntrev = -1;
1969 self->ntrev = -1;
2000 Py_INCREF(self->data);
1970 Py_INCREF(self->data);
2001
1971
2002 if (self->inlined) {
1972 if (self->inlined) {
2003 Py_ssize_t len = inline_scan(self, NULL);
1973 Py_ssize_t len = inline_scan(self, NULL);
2004 if (len == -1)
1974 if (len == -1)
2005 goto bail;
1975 goto bail;
2006 self->raw_length = len;
1976 self->raw_length = len;
2007 self->length = len + 1;
1977 self->length = len + 1;
2008 } else {
1978 } else {
2009 if (size % v1_hdrsize) {
1979 if (size % v1_hdrsize) {
2010 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1980 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2011 goto bail;
1981 goto bail;
2012 }
1982 }
2013 self->raw_length = size / v1_hdrsize;
1983 self->raw_length = size / v1_hdrsize;
2014 self->length = self->raw_length + 1;
1984 self->length = self->raw_length + 1;
2015 }
1985 }
2016
1986
2017 return 0;
1987 return 0;
2018 bail:
1988 bail:
2019 return -1;
1989 return -1;
2020 }
1990 }
2021
1991
2022 static PyObject *index_nodemap(indexObject *self)
1992 static PyObject *index_nodemap(indexObject *self)
2023 {
1993 {
2024 Py_INCREF(self);
1994 Py_INCREF(self);
2025 return (PyObject *)self;
1995 return (PyObject *)self;
2026 }
1996 }
2027
1997
1998 static void _index_clearcaches(indexObject *self)
1999 {
2000 if (self->cache) {
2001 Py_ssize_t i;
2002
2003 for (i = 0; i < self->raw_length; i++)
2004 Py_CLEAR(self->cache[i]);
2005 free(self->cache);
2006 self->cache = NULL;
2007 }
2008 if (self->offsets) {
2009 PyMem_Free(self->offsets);
2010 self->offsets = NULL;
2011 }
2012 if (self->nt != NULL) {
2013 free(self->nt->nodes);
2014 PyMem_Free(self->nt);
2015 }
2016 self->nt = NULL;
2017 Py_CLEAR(self->headrevs);
2018 }
2019
2020 static PyObject *index_clearcaches(indexObject *self)
2021 {
2022 _index_clearcaches(self);
2023 self->ntrev = -1;
2024 self->ntlookups = self->ntmisses = 0;
2025 Py_RETURN_NONE;
2026 }
2027
2028 static void index_dealloc(indexObject *self)
2028 static void index_dealloc(indexObject *self)
2029 {
2029 {
2030 _index_clearcaches(self);
2030 _index_clearcaches(self);
2031 Py_XDECREF(self->filteredrevs);
2031 Py_XDECREF(self->filteredrevs);
2032 if (self->buf.buf) {
2032 if (self->buf.buf) {
2033 PyBuffer_Release(&self->buf);
2033 PyBuffer_Release(&self->buf);
2034 memset(&self->buf, 0, sizeof(self->buf));
2034 memset(&self->buf, 0, sizeof(self->buf));
2035 }
2035 }
2036 Py_XDECREF(self->data);
2036 Py_XDECREF(self->data);
2037 Py_XDECREF(self->added);
2037 Py_XDECREF(self->added);
2038 PyObject_Del(self);
2038 PyObject_Del(self);
2039 }
2039 }
2040
2040
2041 static PySequenceMethods index_sequence_methods = {
2041 static PySequenceMethods index_sequence_methods = {
2042 (lenfunc)index_length, /* sq_length */
2042 (lenfunc)index_length, /* sq_length */
2043 0, /* sq_concat */
2043 0, /* sq_concat */
2044 0, /* sq_repeat */
2044 0, /* sq_repeat */
2045 (ssizeargfunc)index_get, /* sq_item */
2045 (ssizeargfunc)index_get, /* sq_item */
2046 0, /* sq_slice */
2046 0, /* sq_slice */
2047 0, /* sq_ass_item */
2047 0, /* sq_ass_item */
2048 0, /* sq_ass_slice */
2048 0, /* sq_ass_slice */
2049 (objobjproc)index_contains, /* sq_contains */
2049 (objobjproc)index_contains, /* sq_contains */
2050 };
2050 };
2051
2051
2052 static PyMappingMethods index_mapping_methods = {
2052 static PyMappingMethods index_mapping_methods = {
2053 (lenfunc)index_length, /* mp_length */
2053 (lenfunc)index_length, /* mp_length */
2054 (binaryfunc)index_getitem, /* mp_subscript */
2054 (binaryfunc)index_getitem, /* mp_subscript */
2055 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
2055 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
2056 };
2056 };
2057
2057
2058 static PyMethodDef index_methods[] = {
2058 static PyMethodDef index_methods[] = {
2059 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
2059 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
2060 "return the gca set of the given revs"},
2060 "return the gca set of the given revs"},
2061 {"commonancestorsheads", (PyCFunction)index_commonancestorsheads,
2061 {"commonancestorsheads", (PyCFunction)index_commonancestorsheads,
2062 METH_VARARGS,
2062 METH_VARARGS,
2063 "return the heads of the common ancestors of the given revs"},
2063 "return the heads of the common ancestors of the given revs"},
2064 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
2064 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
2065 "clear the index caches"},
2065 "clear the index caches"},
2066 {"get", (PyCFunction)index_m_get, METH_VARARGS,
2066 {"get", (PyCFunction)index_m_get, METH_VARARGS,
2067 "get an index entry"},
2067 "get an index entry"},
2068 {"computephasesmapsets", (PyCFunction)compute_phases_map_sets,
2068 {"computephasesmapsets", (PyCFunction)compute_phases_map_sets,
2069 METH_VARARGS, "compute phases"},
2069 METH_VARARGS, "compute phases"},
2070 {"reachableroots2", (PyCFunction)reachableroots2, METH_VARARGS,
2070 {"reachableroots2", (PyCFunction)reachableroots2, METH_VARARGS,
2071 "reachableroots"},
2071 "reachableroots"},
2072 {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS,
2072 {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS,
2073 "get head revisions"}, /* Can do filtering since 3.2 */
2073 "get head revisions"}, /* Can do filtering since 3.2 */
2074 {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS,
2074 {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS,
2075 "get filtered head revisions"}, /* Can always do filtering */
2075 "get filtered head revisions"}, /* Can always do filtering */
2076 {"deltachain", (PyCFunction)index_deltachain, METH_VARARGS,
2076 {"deltachain", (PyCFunction)index_deltachain, METH_VARARGS,
2077 "determine revisions with deltas to reconstruct fulltext"},
2077 "determine revisions with deltas to reconstruct fulltext"},
2078 {"append", (PyCFunction)index_append, METH_O,
2078 {"append", (PyCFunction)index_append, METH_O,
2079 "append an index entry"},
2079 "append an index entry"},
2080 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
2080 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
2081 "match a potentially ambiguous node ID"},
2081 "match a potentially ambiguous node ID"},
2082 {"shortest", (PyCFunction)index_shortest, METH_VARARGS,
2082 {"shortest", (PyCFunction)index_shortest, METH_VARARGS,
2083 "find length of shortest hex nodeid of a binary ID"},
2083 "find length of shortest hex nodeid of a binary ID"},
2084 {"stats", (PyCFunction)index_stats, METH_NOARGS,
2084 {"stats", (PyCFunction)index_stats, METH_NOARGS,
2085 "stats for the index"},
2085 "stats for the index"},
2086 {NULL} /* Sentinel */
2086 {NULL} /* Sentinel */
2087 };
2087 };
2088
2088
2089 static PyGetSetDef index_getset[] = {
2089 static PyGetSetDef index_getset[] = {
2090 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
2090 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
2091 {NULL} /* Sentinel */
2091 {NULL} /* Sentinel */
2092 };
2092 };
2093
2093
2094 static PyTypeObject indexType = {
2094 static PyTypeObject indexType = {
2095 PyVarObject_HEAD_INIT(NULL, 0) /* header */
2095 PyVarObject_HEAD_INIT(NULL, 0) /* header */
2096 "parsers.index", /* tp_name */
2096 "parsers.index", /* tp_name */
2097 sizeof(indexObject), /* tp_basicsize */
2097 sizeof(indexObject), /* tp_basicsize */
2098 0, /* tp_itemsize */
2098 0, /* tp_itemsize */
2099 (destructor)index_dealloc, /* tp_dealloc */
2099 (destructor)index_dealloc, /* tp_dealloc */
2100 0, /* tp_print */
2100 0, /* tp_print */
2101 0, /* tp_getattr */
2101 0, /* tp_getattr */
2102 0, /* tp_setattr */
2102 0, /* tp_setattr */
2103 0, /* tp_compare */
2103 0, /* tp_compare */
2104 0, /* tp_repr */
2104 0, /* tp_repr */
2105 0, /* tp_as_number */
2105 0, /* tp_as_number */
2106 &index_sequence_methods, /* tp_as_sequence */
2106 &index_sequence_methods, /* tp_as_sequence */
2107 &index_mapping_methods, /* tp_as_mapping */
2107 &index_mapping_methods, /* tp_as_mapping */
2108 0, /* tp_hash */
2108 0, /* tp_hash */
2109 0, /* tp_call */
2109 0, /* tp_call */
2110 0, /* tp_str */
2110 0, /* tp_str */
2111 0, /* tp_getattro */
2111 0, /* tp_getattro */
2112 0, /* tp_setattro */
2112 0, /* tp_setattro */
2113 0, /* tp_as_buffer */
2113 0, /* tp_as_buffer */
2114 Py_TPFLAGS_DEFAULT, /* tp_flags */
2114 Py_TPFLAGS_DEFAULT, /* tp_flags */
2115 "revlog index", /* tp_doc */
2115 "revlog index", /* tp_doc */
2116 0, /* tp_traverse */
2116 0, /* tp_traverse */
2117 0, /* tp_clear */
2117 0, /* tp_clear */
2118 0, /* tp_richcompare */
2118 0, /* tp_richcompare */
2119 0, /* tp_weaklistoffset */
2119 0, /* tp_weaklistoffset */
2120 0, /* tp_iter */
2120 0, /* tp_iter */
2121 0, /* tp_iternext */
2121 0, /* tp_iternext */
2122 index_methods, /* tp_methods */
2122 index_methods, /* tp_methods */
2123 0, /* tp_members */
2123 0, /* tp_members */
2124 index_getset, /* tp_getset */
2124 index_getset, /* tp_getset */
2125 0, /* tp_base */
2125 0, /* tp_base */
2126 0, /* tp_dict */
2126 0, /* tp_dict */
2127 0, /* tp_descr_get */
2127 0, /* tp_descr_get */
2128 0, /* tp_descr_set */
2128 0, /* tp_descr_set */
2129 0, /* tp_dictoffset */
2129 0, /* tp_dictoffset */
2130 (initproc)index_init, /* tp_init */
2130 (initproc)index_init, /* tp_init */
2131 0, /* tp_alloc */
2131 0, /* tp_alloc */
2132 };
2132 };
2133
2133
2134 /*
2134 /*
2135 * returns a tuple of the form (index, index, cache) with elements as
2135 * returns a tuple of the form (index, index, cache) with elements as
2136 * follows:
2136 * follows:
2137 *
2137 *
2138 * index: an index object that lazily parses RevlogNG records
2138 * index: an index object that lazily parses RevlogNG records
2139 * cache: if data is inlined, a tuple (0, index_file_content), else None
2139 * cache: if data is inlined, a tuple (0, index_file_content), else None
2140 * index_file_content could be a string, or a buffer
2140 * index_file_content could be a string, or a buffer
2141 *
2141 *
2142 * added complications are for backwards compatibility
2142 * added complications are for backwards compatibility
2143 */
2143 */
2144 PyObject *parse_index2(PyObject *self, PyObject *args)
2144 PyObject *parse_index2(PyObject *self, PyObject *args)
2145 {
2145 {
2146 PyObject *tuple = NULL, *cache = NULL;
2146 PyObject *tuple = NULL, *cache = NULL;
2147 indexObject *idx;
2147 indexObject *idx;
2148 int ret;
2148 int ret;
2149
2149
2150 idx = PyObject_New(indexObject, &indexType);
2150 idx = PyObject_New(indexObject, &indexType);
2151 if (idx == NULL)
2151 if (idx == NULL)
2152 goto bail;
2152 goto bail;
2153
2153
2154 ret = index_init(idx, args);
2154 ret = index_init(idx, args);
2155 if (ret == -1)
2155 if (ret == -1)
2156 goto bail;
2156 goto bail;
2157
2157
2158 if (idx->inlined) {
2158 if (idx->inlined) {
2159 cache = Py_BuildValue("iO", 0, idx->data);
2159 cache = Py_BuildValue("iO", 0, idx->data);
2160 if (cache == NULL)
2160 if (cache == NULL)
2161 goto bail;
2161 goto bail;
2162 } else {
2162 } else {
2163 cache = Py_None;
2163 cache = Py_None;
2164 Py_INCREF(cache);
2164 Py_INCREF(cache);
2165 }
2165 }
2166
2166
2167 tuple = Py_BuildValue("NN", idx, cache);
2167 tuple = Py_BuildValue("NN", idx, cache);
2168 if (!tuple)
2168 if (!tuple)
2169 goto bail;
2169 goto bail;
2170 return tuple;
2170 return tuple;
2171
2171
2172 bail:
2172 bail:
2173 Py_XDECREF(idx);
2173 Py_XDECREF(idx);
2174 Py_XDECREF(cache);
2174 Py_XDECREF(cache);
2175 Py_XDECREF(tuple);
2175 Py_XDECREF(tuple);
2176 return NULL;
2176 return NULL;
2177 }
2177 }
2178
2178
2179 void revlog_module_init(PyObject *mod)
2179 void revlog_module_init(PyObject *mod)
2180 {
2180 {
2181 indexType.tp_new = PyType_GenericNew;
2181 indexType.tp_new = PyType_GenericNew;
2182 if (PyType_Ready(&indexType) < 0)
2182 if (PyType_Ready(&indexType) < 0)
2183 return;
2183 return;
2184 Py_INCREF(&indexType);
2184 Py_INCREF(&indexType);
2185 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
2185 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
2186
2186
2187 nullentry = Py_BuildValue(PY23("iiiiiiis#", "iiiiiiiy#"), 0, 0, 0,
2187 nullentry = Py_BuildValue(PY23("iiiiiiis#", "iiiiiiiy#"), 0, 0, 0,
2188 -1, -1, -1, -1, nullid, 20);
2188 -1, -1, -1, -1, nullid, 20);
2189 if (nullentry)
2189 if (nullentry)
2190 PyObject_GC_UnTrack(nullentry);
2190 PyObject_GC_UnTrack(nullentry);
2191 }
2191 }
General Comments 0
You need to be logged in to leave comments. Login now