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