##// END OF EJS Templates
rust: look up HgRevlogIndex_GetParents() from symbol table...
Yuya Nishihara -
r40897:54a60968 default
parent child Browse files
Show More
@@ -1,2901 +1,2889 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 <limits.h>
13 #include <limits.h>
14 #include <stddef.h>
14 #include <stddef.h>
15 #include <stdlib.h>
15 #include <stdlib.h>
16 #include <string.h>
16 #include <string.h>
17
17
18 #include "bitmanipulation.h"
18 #include "bitmanipulation.h"
19 #include "charencode.h"
19 #include "charencode.h"
20 #include "revlog.h"
20 #include "revlog.h"
21 #include "util.h"
21 #include "util.h"
22
22
23 #ifdef IS_PY3K
23 #ifdef IS_PY3K
24 /* The mapping of Python types is meant to be temporary to get Python
24 /* The mapping of Python types is meant to be temporary to get Python
25 * 3 to compile. We should remove this once Python 3 support is fully
25 * 3 to compile. We should remove this once Python 3 support is fully
26 * supported and proper types are used in the extensions themselves. */
26 * supported and proper types are used in the extensions themselves. */
27 #define PyInt_Check PyLong_Check
27 #define PyInt_Check PyLong_Check
28 #define PyInt_FromLong PyLong_FromLong
28 #define PyInt_FromLong PyLong_FromLong
29 #define PyInt_FromSsize_t PyLong_FromSsize_t
29 #define PyInt_FromSsize_t PyLong_FromSsize_t
30 #define PyInt_AsLong PyLong_AsLong
30 #define PyInt_AsLong PyLong_AsLong
31 #endif
31 #endif
32
32
33 typedef struct indexObjectStruct indexObject;
33 typedef struct indexObjectStruct indexObject;
34
34
35 typedef struct {
35 typedef struct {
36 int children[16];
36 int children[16];
37 } nodetreenode;
37 } nodetreenode;
38
38
39 /*
39 /*
40 * A base-16 trie for fast node->rev mapping.
40 * A base-16 trie for fast node->rev mapping.
41 *
41 *
42 * Positive value is index of the next node in the trie
42 * Positive value is index of the next node in the trie
43 * Negative value is a leaf: -(rev + 2)
43 * Negative value is a leaf: -(rev + 2)
44 * Zero is empty
44 * Zero is empty
45 */
45 */
46 typedef struct {
46 typedef struct {
47 indexObject *index;
47 indexObject *index;
48 nodetreenode *nodes;
48 nodetreenode *nodes;
49 unsigned length; /* # nodes in use */
49 unsigned length; /* # nodes in use */
50 unsigned capacity; /* # nodes allocated */
50 unsigned capacity; /* # nodes allocated */
51 int depth; /* maximum depth of tree */
51 int depth; /* maximum depth of tree */
52 int splits; /* # splits performed */
52 int splits; /* # splits performed */
53 } nodetree;
53 } nodetree;
54
54
55 typedef struct {
55 typedef struct {
56 PyObject_HEAD /* ; */
56 PyObject_HEAD /* ; */
57 nodetree nt;
57 nodetree nt;
58 } nodetreeObject;
58 } nodetreeObject;
59
59
60 /*
60 /*
61 * This class has two behaviors.
61 * This class has two behaviors.
62 *
62 *
63 * When used in a list-like way (with integer keys), we decode an
63 * When used in a list-like way (with integer keys), we decode an
64 * entry in a RevlogNG index file on demand. Our last entry is a
64 * entry in a RevlogNG index file on demand. Our last entry is a
65 * sentinel, always a nullid. We have limited support for
65 * sentinel, always a nullid. We have limited support for
66 * integer-keyed insert and delete, only at elements right before the
66 * integer-keyed insert and delete, only at elements right before the
67 * sentinel.
67 * sentinel.
68 *
68 *
69 * With string keys, we lazily perform a reverse mapping from node to
69 * With string keys, we lazily perform a reverse mapping from node to
70 * rev, using a base-16 trie.
70 * rev, using a base-16 trie.
71 */
71 */
72 struct indexObjectStruct {
72 struct indexObjectStruct {
73 PyObject_HEAD
73 PyObject_HEAD
74 /* Type-specific fields go here. */
74 /* Type-specific fields go here. */
75 PyObject *data; /* raw bytes of index */
75 PyObject *data; /* raw bytes of index */
76 Py_buffer buf; /* buffer of data */
76 Py_buffer buf; /* buffer of data */
77 PyObject **cache; /* cached tuples */
77 PyObject **cache; /* cached tuples */
78 const char **offsets; /* populated on demand */
78 const char **offsets; /* populated on demand */
79 Py_ssize_t raw_length; /* original number of elements */
79 Py_ssize_t raw_length; /* original number of elements */
80 Py_ssize_t length; /* current number of elements */
80 Py_ssize_t length; /* current number of elements */
81 PyObject *added; /* populated on demand */
81 PyObject *added; /* populated on demand */
82 PyObject *headrevs; /* cache, invalidated on changes */
82 PyObject *headrevs; /* cache, invalidated on changes */
83 PyObject *filteredrevs; /* filtered revs set */
83 PyObject *filteredrevs; /* filtered revs set */
84 nodetree nt; /* base-16 trie */
84 nodetree nt; /* base-16 trie */
85 int ntinitialized; /* 0 or 1 */
85 int ntinitialized; /* 0 or 1 */
86 int ntrev; /* last rev scanned */
86 int ntrev; /* last rev scanned */
87 int ntlookups; /* # lookups */
87 int ntlookups; /* # lookups */
88 int ntmisses; /* # lookups that miss the cache */
88 int ntmisses; /* # lookups that miss the cache */
89 int inlined;
89 int inlined;
90 };
90 };
91
91
92 static Py_ssize_t index_length(const indexObject *self)
92 static Py_ssize_t index_length(const indexObject *self)
93 {
93 {
94 if (self->added == NULL)
94 if (self->added == NULL)
95 return self->length;
95 return self->length;
96 return self->length + PyList_GET_SIZE(self->added);
96 return self->length + PyList_GET_SIZE(self->added);
97 }
97 }
98
98
99 static PyObject *nullentry = NULL;
99 static PyObject *nullentry = NULL;
100 static const char nullid[20] = {0};
100 static const char nullid[20] = {0};
101
101
102 static Py_ssize_t inline_scan(indexObject *self, const char **offsets);
102 static Py_ssize_t inline_scan(indexObject *self, const char **offsets);
103
103
104 #if LONG_MAX == 0x7fffffffL
104 #if LONG_MAX == 0x7fffffffL
105 static const char *const tuple_format = PY23("Kiiiiiis#", "Kiiiiiiy#");
105 static const char *const tuple_format = PY23("Kiiiiiis#", "Kiiiiiiy#");
106 #else
106 #else
107 static const char *const tuple_format = PY23("kiiiiiis#", "kiiiiiiy#");
107 static const char *const tuple_format = PY23("kiiiiiis#", "kiiiiiiy#");
108 #endif
108 #endif
109
109
110 /* A RevlogNG v1 index entry is 64 bytes long. */
110 /* A RevlogNG v1 index entry is 64 bytes long. */
111 static const long v1_hdrsize = 64;
111 static const long v1_hdrsize = 64;
112
112
113 static void raise_revlog_error(void)
113 static void raise_revlog_error(void)
114 {
114 {
115 PyObject *mod = NULL, *dict = NULL, *errclass = NULL;
115 PyObject *mod = NULL, *dict = NULL, *errclass = NULL;
116
116
117 mod = PyImport_ImportModule("mercurial.error");
117 mod = PyImport_ImportModule("mercurial.error");
118 if (mod == NULL) {
118 if (mod == NULL) {
119 goto cleanup;
119 goto cleanup;
120 }
120 }
121
121
122 dict = PyModule_GetDict(mod);
122 dict = PyModule_GetDict(mod);
123 if (dict == NULL) {
123 if (dict == NULL) {
124 goto cleanup;
124 goto cleanup;
125 }
125 }
126 Py_INCREF(dict);
126 Py_INCREF(dict);
127
127
128 errclass = PyDict_GetItemString(dict, "RevlogError");
128 errclass = PyDict_GetItemString(dict, "RevlogError");
129 if (errclass == NULL) {
129 if (errclass == NULL) {
130 PyErr_SetString(PyExc_SystemError,
130 PyErr_SetString(PyExc_SystemError,
131 "could not find RevlogError");
131 "could not find RevlogError");
132 goto cleanup;
132 goto cleanup;
133 }
133 }
134
134
135 /* value of exception is ignored by callers */
135 /* value of exception is ignored by callers */
136 PyErr_SetString(errclass, "RevlogError");
136 PyErr_SetString(errclass, "RevlogError");
137
137
138 cleanup:
138 cleanup:
139 Py_XDECREF(dict);
139 Py_XDECREF(dict);
140 Py_XDECREF(mod);
140 Py_XDECREF(mod);
141 }
141 }
142
142
143 /*
143 /*
144 * Return a pointer to the beginning of a RevlogNG record.
144 * Return a pointer to the beginning of a RevlogNG record.
145 */
145 */
146 static const char *index_deref(indexObject *self, Py_ssize_t pos)
146 static const char *index_deref(indexObject *self, Py_ssize_t pos)
147 {
147 {
148 if (self->inlined && pos > 0) {
148 if (self->inlined && pos > 0) {
149 if (self->offsets == NULL) {
149 if (self->offsets == NULL) {
150 self->offsets = PyMem_Malloc(self->raw_length *
150 self->offsets = PyMem_Malloc(self->raw_length *
151 sizeof(*self->offsets));
151 sizeof(*self->offsets));
152 if (self->offsets == NULL)
152 if (self->offsets == NULL)
153 return (const char *)PyErr_NoMemory();
153 return (const char *)PyErr_NoMemory();
154 inline_scan(self, self->offsets);
154 inline_scan(self, self->offsets);
155 }
155 }
156 return self->offsets[pos];
156 return self->offsets[pos];
157 }
157 }
158
158
159 return (const char *)(self->buf.buf) + pos * v1_hdrsize;
159 return (const char *)(self->buf.buf) + pos * v1_hdrsize;
160 }
160 }
161
161
162 /*
162 /*
163 * Get parents of the given rev.
163 * Get parents of the given rev.
164 *
164 *
165 * The specified rev must be valid and must not be nullrev. A returned
165 * The specified rev must be valid and must not be nullrev. A returned
166 * parent revision may be nullrev, but is guaranteed to be in valid range.
166 * parent revision may be nullrev, but is guaranteed to be in valid range.
167 */
167 */
168 static inline int index_get_parents(indexObject *self, Py_ssize_t rev, int *ps,
168 static inline int index_get_parents(indexObject *self, Py_ssize_t rev, int *ps,
169 int maxrev)
169 int maxrev)
170 {
170 {
171 if (rev >= self->length) {
171 if (rev >= self->length) {
172 long tmp;
172 long tmp;
173 PyObject *tuple =
173 PyObject *tuple =
174 PyList_GET_ITEM(self->added, rev - self->length);
174 PyList_GET_ITEM(self->added, rev - self->length);
175 if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 5), &tmp)) {
175 if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 5), &tmp)) {
176 return -1;
176 return -1;
177 }
177 }
178 ps[0] = (int)tmp;
178 ps[0] = (int)tmp;
179 if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 6), &tmp)) {
179 if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 6), &tmp)) {
180 return -1;
180 return -1;
181 }
181 }
182 ps[1] = (int)tmp;
182 ps[1] = (int)tmp;
183 } else {
183 } else {
184 const char *data = index_deref(self, rev);
184 const char *data = index_deref(self, rev);
185 ps[0] = getbe32(data + 24);
185 ps[0] = getbe32(data + 24);
186 ps[1] = getbe32(data + 28);
186 ps[1] = getbe32(data + 28);
187 }
187 }
188 /* If index file is corrupted, ps[] may point to invalid revisions. So
188 /* If index file is corrupted, ps[] may point to invalid revisions. So
189 * there is a risk of buffer overflow to trust them unconditionally. */
189 * there is a risk of buffer overflow to trust them unconditionally. */
190 if (ps[0] < -1 || ps[0] > maxrev || ps[1] < -1 || ps[1] > maxrev) {
190 if (ps[0] < -1 || ps[0] > maxrev || ps[1] < -1 || ps[1] > maxrev) {
191 PyErr_SetString(PyExc_ValueError, "parent out of range");
191 PyErr_SetString(PyExc_ValueError, "parent out of range");
192 return -1;
192 return -1;
193 }
193 }
194 return 0;
194 return 0;
195 }
195 }
196
196
197 /*
197 /*
198 * Get parents of the given rev.
198 * Get parents of the given rev.
199 *
199 *
200 * If the specified rev is out of range, IndexError will be raised. If the
200 * If the specified rev is out of range, IndexError will be raised. If the
201 * revlog entry is corrupted, ValueError may be raised.
201 * revlog entry is corrupted, ValueError may be raised.
202 *
202 *
203 * Returns 0 on success or -1 on failure.
203 * Returns 0 on success or -1 on failure.
204 */
204 */
205 int HgRevlogIndex_GetParents(PyObject *op, int rev, int *ps)
205 int HgRevlogIndex_GetParents(PyObject *op, int rev, int *ps)
206 {
206 {
207 int tiprev;
207 int tiprev;
208 if (!op || !HgRevlogIndex_Check(op) || !ps) {
208 if (!op || !HgRevlogIndex_Check(op) || !ps) {
209 PyErr_BadInternalCall();
209 PyErr_BadInternalCall();
210 return -1;
210 return -1;
211 }
211 }
212 tiprev = (int)index_length((indexObject *)op) - 1;
212 tiprev = (int)index_length((indexObject *)op) - 1;
213 if (rev < -1 || rev > tiprev) {
213 if (rev < -1 || rev > tiprev) {
214 PyErr_Format(PyExc_IndexError, "rev out of range: %d", rev);
214 PyErr_Format(PyExc_IndexError, "rev out of range: %d", rev);
215 return -1;
215 return -1;
216 } else if (rev == -1) {
216 } else if (rev == -1) {
217 ps[0] = ps[1] = -1;
217 ps[0] = ps[1] = -1;
218 return 0;
218 return 0;
219 } else {
219 } else {
220 return index_get_parents((indexObject *)op, rev, ps, tiprev);
220 return index_get_parents((indexObject *)op, rev, ps, tiprev);
221 }
221 }
222 }
222 }
223
223
224 static inline int64_t index_get_start(indexObject *self, Py_ssize_t rev)
224 static inline int64_t index_get_start(indexObject *self, Py_ssize_t rev)
225 {
225 {
226 uint64_t offset;
226 uint64_t offset;
227 if (rev >= self->length) {
227 if (rev >= self->length) {
228 PyObject *tuple;
228 PyObject *tuple;
229 PyObject *pylong;
229 PyObject *pylong;
230 PY_LONG_LONG tmp;
230 PY_LONG_LONG tmp;
231 tuple = PyList_GET_ITEM(self->added, rev - self->length);
231 tuple = PyList_GET_ITEM(self->added, rev - self->length);
232 pylong = PyTuple_GET_ITEM(tuple, 0);
232 pylong = PyTuple_GET_ITEM(tuple, 0);
233 tmp = PyLong_AsLongLong(pylong);
233 tmp = PyLong_AsLongLong(pylong);
234 if (tmp == -1 && PyErr_Occurred()) {
234 if (tmp == -1 && PyErr_Occurred()) {
235 return -1;
235 return -1;
236 }
236 }
237 if (tmp < 0) {
237 if (tmp < 0) {
238 PyErr_Format(PyExc_OverflowError,
238 PyErr_Format(PyExc_OverflowError,
239 "revlog entry size out of bound (%lld)",
239 "revlog entry size out of bound (%lld)",
240 (long long)tmp);
240 (long long)tmp);
241 return -1;
241 return -1;
242 }
242 }
243 offset = (uint64_t)tmp;
243 offset = (uint64_t)tmp;
244 } else {
244 } else {
245 const char *data = index_deref(self, rev);
245 const char *data = index_deref(self, rev);
246 offset = getbe32(data + 4);
246 offset = getbe32(data + 4);
247 if (rev == 0) {
247 if (rev == 0) {
248 /* mask out version number for the first entry */
248 /* mask out version number for the first entry */
249 offset &= 0xFFFF;
249 offset &= 0xFFFF;
250 } else {
250 } else {
251 uint32_t offset_high = getbe32(data);
251 uint32_t offset_high = getbe32(data);
252 offset |= ((uint64_t)offset_high) << 32;
252 offset |= ((uint64_t)offset_high) << 32;
253 }
253 }
254 }
254 }
255 return (int64_t)(offset >> 16);
255 return (int64_t)(offset >> 16);
256 }
256 }
257
257
258 static inline int index_get_length(indexObject *self, Py_ssize_t rev)
258 static inline int index_get_length(indexObject *self, Py_ssize_t rev)
259 {
259 {
260 if (rev >= self->length) {
260 if (rev >= self->length) {
261 PyObject *tuple;
261 PyObject *tuple;
262 PyObject *pylong;
262 PyObject *pylong;
263 long ret;
263 long ret;
264 tuple = PyList_GET_ITEM(self->added, rev - self->length);
264 tuple = PyList_GET_ITEM(self->added, rev - self->length);
265 pylong = PyTuple_GET_ITEM(tuple, 1);
265 pylong = PyTuple_GET_ITEM(tuple, 1);
266 ret = PyInt_AsLong(pylong);
266 ret = PyInt_AsLong(pylong);
267 if (ret == -1 && PyErr_Occurred()) {
267 if (ret == -1 && PyErr_Occurred()) {
268 return -1;
268 return -1;
269 }
269 }
270 if (ret < 0 || ret > (long)INT_MAX) {
270 if (ret < 0 || ret > (long)INT_MAX) {
271 PyErr_Format(PyExc_OverflowError,
271 PyErr_Format(PyExc_OverflowError,
272 "revlog entry size out of bound (%ld)",
272 "revlog entry size out of bound (%ld)",
273 ret);
273 ret);
274 return -1;
274 return -1;
275 }
275 }
276 return (int)ret;
276 return (int)ret;
277 } else {
277 } else {
278 const char *data = index_deref(self, rev);
278 const char *data = index_deref(self, rev);
279 int tmp = (int)getbe32(data + 8);
279 int tmp = (int)getbe32(data + 8);
280 if (tmp < 0) {
280 if (tmp < 0) {
281 PyErr_Format(PyExc_OverflowError,
281 PyErr_Format(PyExc_OverflowError,
282 "revlog entry size out of bound (%d)",
282 "revlog entry size out of bound (%d)",
283 tmp);
283 tmp);
284 return -1;
284 return -1;
285 }
285 }
286 return tmp;
286 return tmp;
287 }
287 }
288 }
288 }
289
289
290 /*
290 /*
291 * RevlogNG format (all in big endian, data may be inlined):
291 * RevlogNG format (all in big endian, data may be inlined):
292 * 6 bytes: offset
292 * 6 bytes: offset
293 * 2 bytes: flags
293 * 2 bytes: flags
294 * 4 bytes: compressed length
294 * 4 bytes: compressed length
295 * 4 bytes: uncompressed length
295 * 4 bytes: uncompressed length
296 * 4 bytes: base revision
296 * 4 bytes: base revision
297 * 4 bytes: link revision
297 * 4 bytes: link revision
298 * 4 bytes: parent 1 revision
298 * 4 bytes: parent 1 revision
299 * 4 bytes: parent 2 revision
299 * 4 bytes: parent 2 revision
300 * 32 bytes: nodeid (only 20 bytes used)
300 * 32 bytes: nodeid (only 20 bytes used)
301 */
301 */
302 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
302 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
303 {
303 {
304 uint64_t offset_flags;
304 uint64_t offset_flags;
305 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
305 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
306 const char *c_node_id;
306 const char *c_node_id;
307 const char *data;
307 const char *data;
308 Py_ssize_t length = index_length(self);
308 Py_ssize_t length = index_length(self);
309 PyObject *entry;
309 PyObject *entry;
310
310
311 if (pos == -1) {
311 if (pos == -1) {
312 Py_INCREF(nullentry);
312 Py_INCREF(nullentry);
313 return nullentry;
313 return nullentry;
314 }
314 }
315
315
316 if (pos < 0 || pos >= length) {
316 if (pos < 0 || pos >= length) {
317 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
317 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
318 return NULL;
318 return NULL;
319 }
319 }
320
320
321 if (pos >= self->length) {
321 if (pos >= self->length) {
322 PyObject *obj;
322 PyObject *obj;
323 obj = PyList_GET_ITEM(self->added, pos - self->length);
323 obj = PyList_GET_ITEM(self->added, pos - self->length);
324 Py_INCREF(obj);
324 Py_INCREF(obj);
325 return obj;
325 return obj;
326 }
326 }
327
327
328 if (self->cache) {
328 if (self->cache) {
329 if (self->cache[pos]) {
329 if (self->cache[pos]) {
330 Py_INCREF(self->cache[pos]);
330 Py_INCREF(self->cache[pos]);
331 return self->cache[pos];
331 return self->cache[pos];
332 }
332 }
333 } else {
333 } else {
334 self->cache = calloc(self->raw_length, sizeof(PyObject *));
334 self->cache = calloc(self->raw_length, sizeof(PyObject *));
335 if (self->cache == NULL)
335 if (self->cache == NULL)
336 return PyErr_NoMemory();
336 return PyErr_NoMemory();
337 }
337 }
338
338
339 data = index_deref(self, pos);
339 data = index_deref(self, pos);
340 if (data == NULL)
340 if (data == NULL)
341 return NULL;
341 return NULL;
342
342
343 offset_flags = getbe32(data + 4);
343 offset_flags = getbe32(data + 4);
344 if (pos == 0) /* mask out version number for the first entry */
344 if (pos == 0) /* mask out version number for the first entry */
345 offset_flags &= 0xFFFF;
345 offset_flags &= 0xFFFF;
346 else {
346 else {
347 uint32_t offset_high = getbe32(data);
347 uint32_t offset_high = getbe32(data);
348 offset_flags |= ((uint64_t)offset_high) << 32;
348 offset_flags |= ((uint64_t)offset_high) << 32;
349 }
349 }
350
350
351 comp_len = getbe32(data + 8);
351 comp_len = getbe32(data + 8);
352 uncomp_len = getbe32(data + 12);
352 uncomp_len = getbe32(data + 12);
353 base_rev = getbe32(data + 16);
353 base_rev = getbe32(data + 16);
354 link_rev = getbe32(data + 20);
354 link_rev = getbe32(data + 20);
355 parent_1 = getbe32(data + 24);
355 parent_1 = getbe32(data + 24);
356 parent_2 = getbe32(data + 28);
356 parent_2 = getbe32(data + 28);
357 c_node_id = data + 32;
357 c_node_id = data + 32;
358
358
359 entry = Py_BuildValue(tuple_format, offset_flags, comp_len, uncomp_len,
359 entry = Py_BuildValue(tuple_format, offset_flags, comp_len, uncomp_len,
360 base_rev, link_rev, parent_1, parent_2, c_node_id,
360 base_rev, link_rev, parent_1, parent_2, c_node_id,
361 20);
361 20);
362
362
363 if (entry) {
363 if (entry) {
364 PyObject_GC_UnTrack(entry);
364 PyObject_GC_UnTrack(entry);
365 Py_INCREF(entry);
365 Py_INCREF(entry);
366 }
366 }
367
367
368 self->cache[pos] = entry;
368 self->cache[pos] = entry;
369
369
370 return entry;
370 return entry;
371 }
371 }
372
372
373 /*
373 /*
374 * Return the 20-byte SHA of the node corresponding to the given rev.
374 * Return the 20-byte SHA of the node corresponding to the given rev.
375 */
375 */
376 static const char *index_node(indexObject *self, Py_ssize_t pos)
376 static const char *index_node(indexObject *self, Py_ssize_t pos)
377 {
377 {
378 Py_ssize_t length = index_length(self);
378 Py_ssize_t length = index_length(self);
379 const char *data;
379 const char *data;
380
380
381 if (pos == -1)
381 if (pos == -1)
382 return nullid;
382 return nullid;
383
383
384 if (pos >= length)
384 if (pos >= length)
385 return NULL;
385 return NULL;
386
386
387 if (pos >= self->length) {
387 if (pos >= self->length) {
388 PyObject *tuple, *str;
388 PyObject *tuple, *str;
389 tuple = PyList_GET_ITEM(self->added, pos - self->length);
389 tuple = PyList_GET_ITEM(self->added, pos - self->length);
390 str = PyTuple_GetItem(tuple, 7);
390 str = PyTuple_GetItem(tuple, 7);
391 return str ? PyBytes_AS_STRING(str) : NULL;
391 return str ? PyBytes_AS_STRING(str) : NULL;
392 }
392 }
393
393
394 data = index_deref(self, pos);
394 data = index_deref(self, pos);
395 return data ? data + 32 : NULL;
395 return data ? data + 32 : NULL;
396 }
396 }
397
397
398 /*
398 /*
399 * Return the 20-byte SHA of the node corresponding to the given rev. The
399 * Return the 20-byte SHA of the node corresponding to the given rev. The
400 * rev is assumed to be existing. If not, an exception is set.
400 * rev is assumed to be existing. If not, an exception is set.
401 */
401 */
402 static const char *index_node_existing(indexObject *self, Py_ssize_t pos)
402 static const char *index_node_existing(indexObject *self, Py_ssize_t pos)
403 {
403 {
404 const char *node = index_node(self, pos);
404 const char *node = index_node(self, pos);
405 if (node == NULL) {
405 if (node == NULL) {
406 PyErr_Format(PyExc_IndexError, "could not access rev %d",
406 PyErr_Format(PyExc_IndexError, "could not access rev %d",
407 (int)pos);
407 (int)pos);
408 }
408 }
409 return node;
409 return node;
410 }
410 }
411
411
412 static int nt_insert(nodetree *self, const char *node, int rev);
412 static int nt_insert(nodetree *self, const char *node, int rev);
413
413
414 static int node_check(PyObject *obj, char **node)
414 static int node_check(PyObject *obj, char **node)
415 {
415 {
416 Py_ssize_t nodelen;
416 Py_ssize_t nodelen;
417 if (PyBytes_AsStringAndSize(obj, node, &nodelen) == -1)
417 if (PyBytes_AsStringAndSize(obj, node, &nodelen) == -1)
418 return -1;
418 return -1;
419 if (nodelen == 20)
419 if (nodelen == 20)
420 return 0;
420 return 0;
421 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
421 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
422 return -1;
422 return -1;
423 }
423 }
424
424
425 static PyObject *index_append(indexObject *self, PyObject *obj)
425 static PyObject *index_append(indexObject *self, PyObject *obj)
426 {
426 {
427 char *node;
427 char *node;
428 Py_ssize_t len;
428 Py_ssize_t len;
429
429
430 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
430 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
431 PyErr_SetString(PyExc_TypeError, "8-tuple required");
431 PyErr_SetString(PyExc_TypeError, "8-tuple required");
432 return NULL;
432 return NULL;
433 }
433 }
434
434
435 if (node_check(PyTuple_GET_ITEM(obj, 7), &node) == -1)
435 if (node_check(PyTuple_GET_ITEM(obj, 7), &node) == -1)
436 return NULL;
436 return NULL;
437
437
438 len = index_length(self);
438 len = index_length(self);
439
439
440 if (self->added == NULL) {
440 if (self->added == NULL) {
441 self->added = PyList_New(0);
441 self->added = PyList_New(0);
442 if (self->added == NULL)
442 if (self->added == NULL)
443 return NULL;
443 return NULL;
444 }
444 }
445
445
446 if (PyList_Append(self->added, obj) == -1)
446 if (PyList_Append(self->added, obj) == -1)
447 return NULL;
447 return NULL;
448
448
449 if (self->ntinitialized)
449 if (self->ntinitialized)
450 nt_insert(&self->nt, node, (int)len);
450 nt_insert(&self->nt, node, (int)len);
451
451
452 Py_CLEAR(self->headrevs);
452 Py_CLEAR(self->headrevs);
453 Py_RETURN_NONE;
453 Py_RETURN_NONE;
454 }
454 }
455
455
456 static PyObject *index_stats(indexObject *self)
456 static PyObject *index_stats(indexObject *self)
457 {
457 {
458 PyObject *obj = PyDict_New();
458 PyObject *obj = PyDict_New();
459 PyObject *s = NULL;
459 PyObject *s = NULL;
460 PyObject *t = NULL;
460 PyObject *t = NULL;
461
461
462 if (obj == NULL)
462 if (obj == NULL)
463 return NULL;
463 return NULL;
464
464
465 #define istat(__n, __d) \
465 #define istat(__n, __d) \
466 do { \
466 do { \
467 s = PyBytes_FromString(__d); \
467 s = PyBytes_FromString(__d); \
468 t = PyInt_FromSsize_t(self->__n); \
468 t = PyInt_FromSsize_t(self->__n); \
469 if (!s || !t) \
469 if (!s || !t) \
470 goto bail; \
470 goto bail; \
471 if (PyDict_SetItem(obj, s, t) == -1) \
471 if (PyDict_SetItem(obj, s, t) == -1) \
472 goto bail; \
472 goto bail; \
473 Py_CLEAR(s); \
473 Py_CLEAR(s); \
474 Py_CLEAR(t); \
474 Py_CLEAR(t); \
475 } while (0)
475 } while (0)
476
476
477 if (self->added) {
477 if (self->added) {
478 Py_ssize_t len = PyList_GET_SIZE(self->added);
478 Py_ssize_t len = PyList_GET_SIZE(self->added);
479 s = PyBytes_FromString("index entries added");
479 s = PyBytes_FromString("index entries added");
480 t = PyInt_FromSsize_t(len);
480 t = PyInt_FromSsize_t(len);
481 if (!s || !t)
481 if (!s || !t)
482 goto bail;
482 goto bail;
483 if (PyDict_SetItem(obj, s, t) == -1)
483 if (PyDict_SetItem(obj, s, t) == -1)
484 goto bail;
484 goto bail;
485 Py_CLEAR(s);
485 Py_CLEAR(s);
486 Py_CLEAR(t);
486 Py_CLEAR(t);
487 }
487 }
488
488
489 if (self->raw_length != self->length)
489 if (self->raw_length != self->length)
490 istat(raw_length, "revs on disk");
490 istat(raw_length, "revs on disk");
491 istat(length, "revs in memory");
491 istat(length, "revs in memory");
492 istat(ntlookups, "node trie lookups");
492 istat(ntlookups, "node trie lookups");
493 istat(ntmisses, "node trie misses");
493 istat(ntmisses, "node trie misses");
494 istat(ntrev, "node trie last rev scanned");
494 istat(ntrev, "node trie last rev scanned");
495 if (self->ntinitialized) {
495 if (self->ntinitialized) {
496 istat(nt.capacity, "node trie capacity");
496 istat(nt.capacity, "node trie capacity");
497 istat(nt.depth, "node trie depth");
497 istat(nt.depth, "node trie depth");
498 istat(nt.length, "node trie count");
498 istat(nt.length, "node trie count");
499 istat(nt.splits, "node trie splits");
499 istat(nt.splits, "node trie splits");
500 }
500 }
501
501
502 #undef istat
502 #undef istat
503
503
504 return obj;
504 return obj;
505
505
506 bail:
506 bail:
507 Py_XDECREF(obj);
507 Py_XDECREF(obj);
508 Py_XDECREF(s);
508 Py_XDECREF(s);
509 Py_XDECREF(t);
509 Py_XDECREF(t);
510 return NULL;
510 return NULL;
511 }
511 }
512
512
513 /*
513 /*
514 * When we cache a list, we want to be sure the caller can't mutate
514 * When we cache a list, we want to be sure the caller can't mutate
515 * the cached copy.
515 * the cached copy.
516 */
516 */
517 static PyObject *list_copy(PyObject *list)
517 static PyObject *list_copy(PyObject *list)
518 {
518 {
519 Py_ssize_t len = PyList_GET_SIZE(list);
519 Py_ssize_t len = PyList_GET_SIZE(list);
520 PyObject *newlist = PyList_New(len);
520 PyObject *newlist = PyList_New(len);
521 Py_ssize_t i;
521 Py_ssize_t i;
522
522
523 if (newlist == NULL)
523 if (newlist == NULL)
524 return NULL;
524 return NULL;
525
525
526 for (i = 0; i < len; i++) {
526 for (i = 0; i < len; i++) {
527 PyObject *obj = PyList_GET_ITEM(list, i);
527 PyObject *obj = PyList_GET_ITEM(list, i);
528 Py_INCREF(obj);
528 Py_INCREF(obj);
529 PyList_SET_ITEM(newlist, i, obj);
529 PyList_SET_ITEM(newlist, i, obj);
530 }
530 }
531
531
532 return newlist;
532 return newlist;
533 }
533 }
534
534
535 static int check_filter(PyObject *filter, Py_ssize_t arg)
535 static int check_filter(PyObject *filter, Py_ssize_t arg)
536 {
536 {
537 if (filter) {
537 if (filter) {
538 PyObject *arglist, *result;
538 PyObject *arglist, *result;
539 int isfiltered;
539 int isfiltered;
540
540
541 arglist = Py_BuildValue("(n)", arg);
541 arglist = Py_BuildValue("(n)", arg);
542 if (!arglist) {
542 if (!arglist) {
543 return -1;
543 return -1;
544 }
544 }
545
545
546 result = PyEval_CallObject(filter, arglist);
546 result = PyEval_CallObject(filter, arglist);
547 Py_DECREF(arglist);
547 Py_DECREF(arglist);
548 if (!result) {
548 if (!result) {
549 return -1;
549 return -1;
550 }
550 }
551
551
552 /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error,
552 /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error,
553 * same as this function, so we can just return it directly.*/
553 * same as this function, so we can just return it directly.*/
554 isfiltered = PyObject_IsTrue(result);
554 isfiltered = PyObject_IsTrue(result);
555 Py_DECREF(result);
555 Py_DECREF(result);
556 return isfiltered;
556 return isfiltered;
557 } else {
557 } else {
558 return 0;
558 return 0;
559 }
559 }
560 }
560 }
561
561
562 static Py_ssize_t add_roots_get_min(indexObject *self, PyObject *list,
562 static Py_ssize_t add_roots_get_min(indexObject *self, PyObject *list,
563 Py_ssize_t marker, char *phases)
563 Py_ssize_t marker, char *phases)
564 {
564 {
565 PyObject *iter = NULL;
565 PyObject *iter = NULL;
566 PyObject *iter_item = NULL;
566 PyObject *iter_item = NULL;
567 Py_ssize_t min_idx = index_length(self) + 2;
567 Py_ssize_t min_idx = index_length(self) + 2;
568 long iter_item_long;
568 long iter_item_long;
569
569
570 if (PyList_GET_SIZE(list) != 0) {
570 if (PyList_GET_SIZE(list) != 0) {
571 iter = PyObject_GetIter(list);
571 iter = PyObject_GetIter(list);
572 if (iter == NULL)
572 if (iter == NULL)
573 return -2;
573 return -2;
574 while ((iter_item = PyIter_Next(iter))) {
574 while ((iter_item = PyIter_Next(iter))) {
575 if (!pylong_to_long(iter_item, &iter_item_long)) {
575 if (!pylong_to_long(iter_item, &iter_item_long)) {
576 Py_DECREF(iter_item);
576 Py_DECREF(iter_item);
577 return -2;
577 return -2;
578 }
578 }
579 Py_DECREF(iter_item);
579 Py_DECREF(iter_item);
580 if (iter_item_long < min_idx)
580 if (iter_item_long < min_idx)
581 min_idx = iter_item_long;
581 min_idx = iter_item_long;
582 phases[iter_item_long] = (char)marker;
582 phases[iter_item_long] = (char)marker;
583 }
583 }
584 Py_DECREF(iter);
584 Py_DECREF(iter);
585 }
585 }
586
586
587 return min_idx;
587 return min_idx;
588 }
588 }
589
589
590 static inline void set_phase_from_parents(char *phases, int parent_1,
590 static inline void set_phase_from_parents(char *phases, int parent_1,
591 int parent_2, Py_ssize_t i)
591 int parent_2, Py_ssize_t i)
592 {
592 {
593 if (parent_1 >= 0 && phases[parent_1] > phases[i])
593 if (parent_1 >= 0 && phases[parent_1] > phases[i])
594 phases[i] = phases[parent_1];
594 phases[i] = phases[parent_1];
595 if (parent_2 >= 0 && phases[parent_2] > phases[i])
595 if (parent_2 >= 0 && phases[parent_2] > phases[i])
596 phases[i] = phases[parent_2];
596 phases[i] = phases[parent_2];
597 }
597 }
598
598
599 static PyObject *reachableroots2(indexObject *self, PyObject *args)
599 static PyObject *reachableroots2(indexObject *self, PyObject *args)
600 {
600 {
601
601
602 /* Input */
602 /* Input */
603 long minroot;
603 long minroot;
604 PyObject *includepatharg = NULL;
604 PyObject *includepatharg = NULL;
605 int includepath = 0;
605 int includepath = 0;
606 /* heads and roots are lists */
606 /* heads and roots are lists */
607 PyObject *heads = NULL;
607 PyObject *heads = NULL;
608 PyObject *roots = NULL;
608 PyObject *roots = NULL;
609 PyObject *reachable = NULL;
609 PyObject *reachable = NULL;
610
610
611 PyObject *val;
611 PyObject *val;
612 Py_ssize_t len = index_length(self);
612 Py_ssize_t len = index_length(self);
613 long revnum;
613 long revnum;
614 Py_ssize_t k;
614 Py_ssize_t k;
615 Py_ssize_t i;
615 Py_ssize_t i;
616 Py_ssize_t l;
616 Py_ssize_t l;
617 int r;
617 int r;
618 int parents[2];
618 int parents[2];
619
619
620 /* Internal data structure:
620 /* Internal data structure:
621 * tovisit: array of length len+1 (all revs + nullrev), filled upto
621 * tovisit: array of length len+1 (all revs + nullrev), filled upto
622 * lentovisit
622 * lentovisit
623 *
623 *
624 * revstates: array of length len+1 (all revs + nullrev) */
624 * revstates: array of length len+1 (all revs + nullrev) */
625 int *tovisit = NULL;
625 int *tovisit = NULL;
626 long lentovisit = 0;
626 long lentovisit = 0;
627 enum { RS_SEEN = 1, RS_ROOT = 2, RS_REACHABLE = 4 };
627 enum { RS_SEEN = 1, RS_ROOT = 2, RS_REACHABLE = 4 };
628 char *revstates = NULL;
628 char *revstates = NULL;
629
629
630 /* Get arguments */
630 /* Get arguments */
631 if (!PyArg_ParseTuple(args, "lO!O!O!", &minroot, &PyList_Type, &heads,
631 if (!PyArg_ParseTuple(args, "lO!O!O!", &minroot, &PyList_Type, &heads,
632 &PyList_Type, &roots, &PyBool_Type,
632 &PyList_Type, &roots, &PyBool_Type,
633 &includepatharg))
633 &includepatharg))
634 goto bail;
634 goto bail;
635
635
636 if (includepatharg == Py_True)
636 if (includepatharg == Py_True)
637 includepath = 1;
637 includepath = 1;
638
638
639 /* Initialize return set */
639 /* Initialize return set */
640 reachable = PyList_New(0);
640 reachable = PyList_New(0);
641 if (reachable == NULL)
641 if (reachable == NULL)
642 goto bail;
642 goto bail;
643
643
644 /* Initialize internal datastructures */
644 /* Initialize internal datastructures */
645 tovisit = (int *)malloc((len + 1) * sizeof(int));
645 tovisit = (int *)malloc((len + 1) * sizeof(int));
646 if (tovisit == NULL) {
646 if (tovisit == NULL) {
647 PyErr_NoMemory();
647 PyErr_NoMemory();
648 goto bail;
648 goto bail;
649 }
649 }
650
650
651 revstates = (char *)calloc(len + 1, 1);
651 revstates = (char *)calloc(len + 1, 1);
652 if (revstates == NULL) {
652 if (revstates == NULL) {
653 PyErr_NoMemory();
653 PyErr_NoMemory();
654 goto bail;
654 goto bail;
655 }
655 }
656
656
657 l = PyList_GET_SIZE(roots);
657 l = PyList_GET_SIZE(roots);
658 for (i = 0; i < l; i++) {
658 for (i = 0; i < l; i++) {
659 revnum = PyInt_AsLong(PyList_GET_ITEM(roots, i));
659 revnum = PyInt_AsLong(PyList_GET_ITEM(roots, i));
660 if (revnum == -1 && PyErr_Occurred())
660 if (revnum == -1 && PyErr_Occurred())
661 goto bail;
661 goto bail;
662 /* If root is out of range, e.g. wdir(), it must be unreachable
662 /* If root is out of range, e.g. wdir(), it must be unreachable
663 * from heads. So we can just ignore it. */
663 * from heads. So we can just ignore it. */
664 if (revnum + 1 < 0 || revnum + 1 >= len + 1)
664 if (revnum + 1 < 0 || revnum + 1 >= len + 1)
665 continue;
665 continue;
666 revstates[revnum + 1] |= RS_ROOT;
666 revstates[revnum + 1] |= RS_ROOT;
667 }
667 }
668
668
669 /* Populate tovisit with all the heads */
669 /* Populate tovisit with all the heads */
670 l = PyList_GET_SIZE(heads);
670 l = PyList_GET_SIZE(heads);
671 for (i = 0; i < l; i++) {
671 for (i = 0; i < l; i++) {
672 revnum = PyInt_AsLong(PyList_GET_ITEM(heads, i));
672 revnum = PyInt_AsLong(PyList_GET_ITEM(heads, i));
673 if (revnum == -1 && PyErr_Occurred())
673 if (revnum == -1 && PyErr_Occurred())
674 goto bail;
674 goto bail;
675 if (revnum + 1 < 0 || revnum + 1 >= len + 1) {
675 if (revnum + 1 < 0 || revnum + 1 >= len + 1) {
676 PyErr_SetString(PyExc_IndexError, "head out of range");
676 PyErr_SetString(PyExc_IndexError, "head out of range");
677 goto bail;
677 goto bail;
678 }
678 }
679 if (!(revstates[revnum + 1] & RS_SEEN)) {
679 if (!(revstates[revnum + 1] & RS_SEEN)) {
680 tovisit[lentovisit++] = (int)revnum;
680 tovisit[lentovisit++] = (int)revnum;
681 revstates[revnum + 1] |= RS_SEEN;
681 revstates[revnum + 1] |= RS_SEEN;
682 }
682 }
683 }
683 }
684
684
685 /* Visit the tovisit list and find the reachable roots */
685 /* Visit the tovisit list and find the reachable roots */
686 k = 0;
686 k = 0;
687 while (k < lentovisit) {
687 while (k < lentovisit) {
688 /* Add the node to reachable if it is a root*/
688 /* Add the node to reachable if it is a root*/
689 revnum = tovisit[k++];
689 revnum = tovisit[k++];
690 if (revstates[revnum + 1] & RS_ROOT) {
690 if (revstates[revnum + 1] & RS_ROOT) {
691 revstates[revnum + 1] |= RS_REACHABLE;
691 revstates[revnum + 1] |= RS_REACHABLE;
692 val = PyInt_FromLong(revnum);
692 val = PyInt_FromLong(revnum);
693 if (val == NULL)
693 if (val == NULL)
694 goto bail;
694 goto bail;
695 r = PyList_Append(reachable, val);
695 r = PyList_Append(reachable, val);
696 Py_DECREF(val);
696 Py_DECREF(val);
697 if (r < 0)
697 if (r < 0)
698 goto bail;
698 goto bail;
699 if (includepath == 0)
699 if (includepath == 0)
700 continue;
700 continue;
701 }
701 }
702
702
703 /* Add its parents to the list of nodes to visit */
703 /* Add its parents to the list of nodes to visit */
704 if (revnum == -1)
704 if (revnum == -1)
705 continue;
705 continue;
706 r = index_get_parents(self, revnum, parents, (int)len - 1);
706 r = index_get_parents(self, revnum, parents, (int)len - 1);
707 if (r < 0)
707 if (r < 0)
708 goto bail;
708 goto bail;
709 for (i = 0; i < 2; i++) {
709 for (i = 0; i < 2; i++) {
710 if (!(revstates[parents[i] + 1] & RS_SEEN) &&
710 if (!(revstates[parents[i] + 1] & RS_SEEN) &&
711 parents[i] >= minroot) {
711 parents[i] >= minroot) {
712 tovisit[lentovisit++] = parents[i];
712 tovisit[lentovisit++] = parents[i];
713 revstates[parents[i] + 1] |= RS_SEEN;
713 revstates[parents[i] + 1] |= RS_SEEN;
714 }
714 }
715 }
715 }
716 }
716 }
717
717
718 /* Find all the nodes in between the roots we found and the heads
718 /* Find all the nodes in between the roots we found and the heads
719 * and add them to the reachable set */
719 * and add them to the reachable set */
720 if (includepath == 1) {
720 if (includepath == 1) {
721 long minidx = minroot;
721 long minidx = minroot;
722 if (minidx < 0)
722 if (minidx < 0)
723 minidx = 0;
723 minidx = 0;
724 for (i = minidx; i < len; i++) {
724 for (i = minidx; i < len; i++) {
725 if (!(revstates[i + 1] & RS_SEEN))
725 if (!(revstates[i + 1] & RS_SEEN))
726 continue;
726 continue;
727 r = index_get_parents(self, i, parents, (int)len - 1);
727 r = index_get_parents(self, i, parents, (int)len - 1);
728 /* Corrupted index file, error is set from
728 /* Corrupted index file, error is set from
729 * index_get_parents */
729 * index_get_parents */
730 if (r < 0)
730 if (r < 0)
731 goto bail;
731 goto bail;
732 if (((revstates[parents[0] + 1] |
732 if (((revstates[parents[0] + 1] |
733 revstates[parents[1] + 1]) &
733 revstates[parents[1] + 1]) &
734 RS_REACHABLE) &&
734 RS_REACHABLE) &&
735 !(revstates[i + 1] & RS_REACHABLE)) {
735 !(revstates[i + 1] & RS_REACHABLE)) {
736 revstates[i + 1] |= RS_REACHABLE;
736 revstates[i + 1] |= RS_REACHABLE;
737 val = PyInt_FromSsize_t(i);
737 val = PyInt_FromSsize_t(i);
738 if (val == NULL)
738 if (val == NULL)
739 goto bail;
739 goto bail;
740 r = PyList_Append(reachable, val);
740 r = PyList_Append(reachable, val);
741 Py_DECREF(val);
741 Py_DECREF(val);
742 if (r < 0)
742 if (r < 0)
743 goto bail;
743 goto bail;
744 }
744 }
745 }
745 }
746 }
746 }
747
747
748 free(revstates);
748 free(revstates);
749 free(tovisit);
749 free(tovisit);
750 return reachable;
750 return reachable;
751 bail:
751 bail:
752 Py_XDECREF(reachable);
752 Py_XDECREF(reachable);
753 free(revstates);
753 free(revstates);
754 free(tovisit);
754 free(tovisit);
755 return NULL;
755 return NULL;
756 }
756 }
757
757
758 static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args)
758 static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args)
759 {
759 {
760 PyObject *roots = Py_None;
760 PyObject *roots = Py_None;
761 PyObject *ret = NULL;
761 PyObject *ret = NULL;
762 PyObject *phasessize = NULL;
762 PyObject *phasessize = NULL;
763 PyObject *phaseroots = NULL;
763 PyObject *phaseroots = NULL;
764 PyObject *phaseset = NULL;
764 PyObject *phaseset = NULL;
765 PyObject *phasessetlist = NULL;
765 PyObject *phasessetlist = NULL;
766 PyObject *rev = NULL;
766 PyObject *rev = NULL;
767 Py_ssize_t len = index_length(self);
767 Py_ssize_t len = index_length(self);
768 Py_ssize_t numphase = 0;
768 Py_ssize_t numphase = 0;
769 Py_ssize_t minrevallphases = 0;
769 Py_ssize_t minrevallphases = 0;
770 Py_ssize_t minrevphase = 0;
770 Py_ssize_t minrevphase = 0;
771 Py_ssize_t i = 0;
771 Py_ssize_t i = 0;
772 char *phases = NULL;
772 char *phases = NULL;
773 long phase;
773 long phase;
774
774
775 if (!PyArg_ParseTuple(args, "O", &roots))
775 if (!PyArg_ParseTuple(args, "O", &roots))
776 goto done;
776 goto done;
777 if (roots == NULL || !PyList_Check(roots)) {
777 if (roots == NULL || !PyList_Check(roots)) {
778 PyErr_SetString(PyExc_TypeError, "roots must be a list");
778 PyErr_SetString(PyExc_TypeError, "roots must be a list");
779 goto done;
779 goto done;
780 }
780 }
781
781
782 phases = calloc(
782 phases = calloc(
783 len, 1); /* phase per rev: {0: public, 1: draft, 2: secret} */
783 len, 1); /* phase per rev: {0: public, 1: draft, 2: secret} */
784 if (phases == NULL) {
784 if (phases == NULL) {
785 PyErr_NoMemory();
785 PyErr_NoMemory();
786 goto done;
786 goto done;
787 }
787 }
788 /* Put the phase information of all the roots in phases */
788 /* Put the phase information of all the roots in phases */
789 numphase = PyList_GET_SIZE(roots) + 1;
789 numphase = PyList_GET_SIZE(roots) + 1;
790 minrevallphases = len + 1;
790 minrevallphases = len + 1;
791 phasessetlist = PyList_New(numphase);
791 phasessetlist = PyList_New(numphase);
792 if (phasessetlist == NULL)
792 if (phasessetlist == NULL)
793 goto done;
793 goto done;
794
794
795 PyList_SET_ITEM(phasessetlist, 0, Py_None);
795 PyList_SET_ITEM(phasessetlist, 0, Py_None);
796 Py_INCREF(Py_None);
796 Py_INCREF(Py_None);
797
797
798 for (i = 0; i < numphase - 1; i++) {
798 for (i = 0; i < numphase - 1; i++) {
799 phaseroots = PyList_GET_ITEM(roots, i);
799 phaseroots = PyList_GET_ITEM(roots, i);
800 phaseset = PySet_New(NULL);
800 phaseset = PySet_New(NULL);
801 if (phaseset == NULL)
801 if (phaseset == NULL)
802 goto release;
802 goto release;
803 PyList_SET_ITEM(phasessetlist, i + 1, phaseset);
803 PyList_SET_ITEM(phasessetlist, i + 1, phaseset);
804 if (!PyList_Check(phaseroots)) {
804 if (!PyList_Check(phaseroots)) {
805 PyErr_SetString(PyExc_TypeError,
805 PyErr_SetString(PyExc_TypeError,
806 "roots item must be a list");
806 "roots item must be a list");
807 goto release;
807 goto release;
808 }
808 }
809 minrevphase =
809 minrevphase =
810 add_roots_get_min(self, phaseroots, i + 1, phases);
810 add_roots_get_min(self, phaseroots, i + 1, phases);
811 if (minrevphase == -2) /* Error from add_roots_get_min */
811 if (minrevphase == -2) /* Error from add_roots_get_min */
812 goto release;
812 goto release;
813 minrevallphases = MIN(minrevallphases, minrevphase);
813 minrevallphases = MIN(minrevallphases, minrevphase);
814 }
814 }
815 /* Propagate the phase information from the roots to the revs */
815 /* Propagate the phase information from the roots to the revs */
816 if (minrevallphases != -1) {
816 if (minrevallphases != -1) {
817 int parents[2];
817 int parents[2];
818 for (i = minrevallphases; i < len; i++) {
818 for (i = minrevallphases; i < len; i++) {
819 if (index_get_parents(self, i, parents, (int)len - 1) <
819 if (index_get_parents(self, i, parents, (int)len - 1) <
820 0)
820 0)
821 goto release;
821 goto release;
822 set_phase_from_parents(phases, parents[0], parents[1],
822 set_phase_from_parents(phases, parents[0], parents[1],
823 i);
823 i);
824 }
824 }
825 }
825 }
826 /* Transform phase list to a python list */
826 /* Transform phase list to a python list */
827 phasessize = PyInt_FromSsize_t(len);
827 phasessize = PyInt_FromSsize_t(len);
828 if (phasessize == NULL)
828 if (phasessize == NULL)
829 goto release;
829 goto release;
830 for (i = 0; i < len; i++) {
830 for (i = 0; i < len; i++) {
831 phase = phases[i];
831 phase = phases[i];
832 /* We only store the sets of phase for non public phase, the
832 /* We only store the sets of phase for non public phase, the
833 * public phase is computed as a difference */
833 * public phase is computed as a difference */
834 if (phase != 0) {
834 if (phase != 0) {
835 phaseset = PyList_GET_ITEM(phasessetlist, phase);
835 phaseset = PyList_GET_ITEM(phasessetlist, phase);
836 rev = PyInt_FromSsize_t(i);
836 rev = PyInt_FromSsize_t(i);
837 if (rev == NULL)
837 if (rev == NULL)
838 goto release;
838 goto release;
839 PySet_Add(phaseset, rev);
839 PySet_Add(phaseset, rev);
840 Py_XDECREF(rev);
840 Py_XDECREF(rev);
841 }
841 }
842 }
842 }
843 ret = PyTuple_Pack(2, phasessize, phasessetlist);
843 ret = PyTuple_Pack(2, phasessize, phasessetlist);
844
844
845 release:
845 release:
846 Py_XDECREF(phasessize);
846 Py_XDECREF(phasessize);
847 Py_XDECREF(phasessetlist);
847 Py_XDECREF(phasessetlist);
848 done:
848 done:
849 free(phases);
849 free(phases);
850 return ret;
850 return ret;
851 }
851 }
852
852
853 static PyObject *index_headrevs(indexObject *self, PyObject *args)
853 static PyObject *index_headrevs(indexObject *self, PyObject *args)
854 {
854 {
855 Py_ssize_t i, j, len;
855 Py_ssize_t i, j, len;
856 char *nothead = NULL;
856 char *nothead = NULL;
857 PyObject *heads = NULL;
857 PyObject *heads = NULL;
858 PyObject *filter = NULL;
858 PyObject *filter = NULL;
859 PyObject *filteredrevs = Py_None;
859 PyObject *filteredrevs = Py_None;
860
860
861 if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) {
861 if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) {
862 return NULL;
862 return NULL;
863 }
863 }
864
864
865 if (self->headrevs && filteredrevs == self->filteredrevs)
865 if (self->headrevs && filteredrevs == self->filteredrevs)
866 return list_copy(self->headrevs);
866 return list_copy(self->headrevs);
867
867
868 Py_DECREF(self->filteredrevs);
868 Py_DECREF(self->filteredrevs);
869 self->filteredrevs = filteredrevs;
869 self->filteredrevs = filteredrevs;
870 Py_INCREF(filteredrevs);
870 Py_INCREF(filteredrevs);
871
871
872 if (filteredrevs != Py_None) {
872 if (filteredrevs != Py_None) {
873 filter = PyObject_GetAttrString(filteredrevs, "__contains__");
873 filter = PyObject_GetAttrString(filteredrevs, "__contains__");
874 if (!filter) {
874 if (!filter) {
875 PyErr_SetString(
875 PyErr_SetString(
876 PyExc_TypeError,
876 PyExc_TypeError,
877 "filteredrevs has no attribute __contains__");
877 "filteredrevs has no attribute __contains__");
878 goto bail;
878 goto bail;
879 }
879 }
880 }
880 }
881
881
882 len = index_length(self);
882 len = index_length(self);
883 heads = PyList_New(0);
883 heads = PyList_New(0);
884 if (heads == NULL)
884 if (heads == NULL)
885 goto bail;
885 goto bail;
886 if (len == 0) {
886 if (len == 0) {
887 PyObject *nullid = PyInt_FromLong(-1);
887 PyObject *nullid = PyInt_FromLong(-1);
888 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
888 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
889 Py_XDECREF(nullid);
889 Py_XDECREF(nullid);
890 goto bail;
890 goto bail;
891 }
891 }
892 goto done;
892 goto done;
893 }
893 }
894
894
895 nothead = calloc(len, 1);
895 nothead = calloc(len, 1);
896 if (nothead == NULL) {
896 if (nothead == NULL) {
897 PyErr_NoMemory();
897 PyErr_NoMemory();
898 goto bail;
898 goto bail;
899 }
899 }
900
900
901 for (i = len - 1; i >= 0; i--) {
901 for (i = len - 1; i >= 0; i--) {
902 int isfiltered;
902 int isfiltered;
903 int parents[2];
903 int parents[2];
904
904
905 /* If nothead[i] == 1, it means we've seen an unfiltered child
905 /* If nothead[i] == 1, it means we've seen an unfiltered child
906 * of this node already, and therefore this node is not
906 * of this node already, and therefore this node is not
907 * filtered. So we can skip the expensive check_filter step.
907 * filtered. So we can skip the expensive check_filter step.
908 */
908 */
909 if (nothead[i] != 1) {
909 if (nothead[i] != 1) {
910 isfiltered = check_filter(filter, i);
910 isfiltered = check_filter(filter, i);
911 if (isfiltered == -1) {
911 if (isfiltered == -1) {
912 PyErr_SetString(PyExc_TypeError,
912 PyErr_SetString(PyExc_TypeError,
913 "unable to check filter");
913 "unable to check filter");
914 goto bail;
914 goto bail;
915 }
915 }
916
916
917 if (isfiltered) {
917 if (isfiltered) {
918 nothead[i] = 1;
918 nothead[i] = 1;
919 continue;
919 continue;
920 }
920 }
921 }
921 }
922
922
923 if (index_get_parents(self, i, parents, (int)len - 1) < 0)
923 if (index_get_parents(self, i, parents, (int)len - 1) < 0)
924 goto bail;
924 goto bail;
925 for (j = 0; j < 2; j++) {
925 for (j = 0; j < 2; j++) {
926 if (parents[j] >= 0)
926 if (parents[j] >= 0)
927 nothead[parents[j]] = 1;
927 nothead[parents[j]] = 1;
928 }
928 }
929 }
929 }
930
930
931 for (i = 0; i < len; i++) {
931 for (i = 0; i < len; i++) {
932 PyObject *head;
932 PyObject *head;
933
933
934 if (nothead[i])
934 if (nothead[i])
935 continue;
935 continue;
936 head = PyInt_FromSsize_t(i);
936 head = PyInt_FromSsize_t(i);
937 if (head == NULL || PyList_Append(heads, head) == -1) {
937 if (head == NULL || PyList_Append(heads, head) == -1) {
938 Py_XDECREF(head);
938 Py_XDECREF(head);
939 goto bail;
939 goto bail;
940 }
940 }
941 }
941 }
942
942
943 done:
943 done:
944 self->headrevs = heads;
944 self->headrevs = heads;
945 Py_XDECREF(filter);
945 Py_XDECREF(filter);
946 free(nothead);
946 free(nothead);
947 return list_copy(self->headrevs);
947 return list_copy(self->headrevs);
948 bail:
948 bail:
949 Py_XDECREF(filter);
949 Py_XDECREF(filter);
950 Py_XDECREF(heads);
950 Py_XDECREF(heads);
951 free(nothead);
951 free(nothead);
952 return NULL;
952 return NULL;
953 }
953 }
954
954
955 /**
955 /**
956 * Obtain the base revision index entry.
956 * Obtain the base revision index entry.
957 *
957 *
958 * Callers must ensure that rev >= 0 or illegal memory access may occur.
958 * Callers must ensure that rev >= 0 or illegal memory access may occur.
959 */
959 */
960 static inline int index_baserev(indexObject *self, int rev)
960 static inline int index_baserev(indexObject *self, int rev)
961 {
961 {
962 const char *data;
962 const char *data;
963
963
964 if (rev >= self->length) {
964 if (rev >= self->length) {
965 PyObject *tuple =
965 PyObject *tuple =
966 PyList_GET_ITEM(self->added, rev - self->length);
966 PyList_GET_ITEM(self->added, rev - self->length);
967 long ret;
967 long ret;
968 if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 3), &ret)) {
968 if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 3), &ret)) {
969 return -2;
969 return -2;
970 }
970 }
971 return (int)ret;
971 return (int)ret;
972 } else {
972 } else {
973 data = index_deref(self, rev);
973 data = index_deref(self, rev);
974 if (data == NULL) {
974 if (data == NULL) {
975 return -2;
975 return -2;
976 }
976 }
977
977
978 return getbe32(data + 16);
978 return getbe32(data + 16);
979 }
979 }
980 }
980 }
981
981
982 static PyObject *index_deltachain(indexObject *self, PyObject *args)
982 static PyObject *index_deltachain(indexObject *self, PyObject *args)
983 {
983 {
984 int rev, generaldelta;
984 int rev, generaldelta;
985 PyObject *stoparg;
985 PyObject *stoparg;
986 int stoprev, iterrev, baserev = -1;
986 int stoprev, iterrev, baserev = -1;
987 int stopped;
987 int stopped;
988 PyObject *chain = NULL, *result = NULL;
988 PyObject *chain = NULL, *result = NULL;
989 const Py_ssize_t length = index_length(self);
989 const Py_ssize_t length = index_length(self);
990
990
991 if (!PyArg_ParseTuple(args, "iOi", &rev, &stoparg, &generaldelta)) {
991 if (!PyArg_ParseTuple(args, "iOi", &rev, &stoparg, &generaldelta)) {
992 return NULL;
992 return NULL;
993 }
993 }
994
994
995 if (PyInt_Check(stoparg)) {
995 if (PyInt_Check(stoparg)) {
996 stoprev = (int)PyInt_AsLong(stoparg);
996 stoprev = (int)PyInt_AsLong(stoparg);
997 if (stoprev == -1 && PyErr_Occurred()) {
997 if (stoprev == -1 && PyErr_Occurred()) {
998 return NULL;
998 return NULL;
999 }
999 }
1000 } else if (stoparg == Py_None) {
1000 } else if (stoparg == Py_None) {
1001 stoprev = -2;
1001 stoprev = -2;
1002 } else {
1002 } else {
1003 PyErr_SetString(PyExc_ValueError,
1003 PyErr_SetString(PyExc_ValueError,
1004 "stoprev must be integer or None");
1004 "stoprev must be integer or None");
1005 return NULL;
1005 return NULL;
1006 }
1006 }
1007
1007
1008 if (rev < 0 || rev >= length) {
1008 if (rev < 0 || rev >= length) {
1009 PyErr_SetString(PyExc_ValueError, "revlog index out of range");
1009 PyErr_SetString(PyExc_ValueError, "revlog index out of range");
1010 return NULL;
1010 return NULL;
1011 }
1011 }
1012
1012
1013 chain = PyList_New(0);
1013 chain = PyList_New(0);
1014 if (chain == NULL) {
1014 if (chain == NULL) {
1015 return NULL;
1015 return NULL;
1016 }
1016 }
1017
1017
1018 baserev = index_baserev(self, rev);
1018 baserev = index_baserev(self, rev);
1019
1019
1020 /* This should never happen. */
1020 /* This should never happen. */
1021 if (baserev <= -2) {
1021 if (baserev <= -2) {
1022 /* Error should be set by index_deref() */
1022 /* Error should be set by index_deref() */
1023 assert(PyErr_Occurred());
1023 assert(PyErr_Occurred());
1024 goto bail;
1024 goto bail;
1025 }
1025 }
1026
1026
1027 iterrev = rev;
1027 iterrev = rev;
1028
1028
1029 while (iterrev != baserev && iterrev != stoprev) {
1029 while (iterrev != baserev && iterrev != stoprev) {
1030 PyObject *value = PyInt_FromLong(iterrev);
1030 PyObject *value = PyInt_FromLong(iterrev);
1031 if (value == NULL) {
1031 if (value == NULL) {
1032 goto bail;
1032 goto bail;
1033 }
1033 }
1034 if (PyList_Append(chain, value)) {
1034 if (PyList_Append(chain, value)) {
1035 Py_DECREF(value);
1035 Py_DECREF(value);
1036 goto bail;
1036 goto bail;
1037 }
1037 }
1038 Py_DECREF(value);
1038 Py_DECREF(value);
1039
1039
1040 if (generaldelta) {
1040 if (generaldelta) {
1041 iterrev = baserev;
1041 iterrev = baserev;
1042 } else {
1042 } else {
1043 iterrev--;
1043 iterrev--;
1044 }
1044 }
1045
1045
1046 if (iterrev < 0) {
1046 if (iterrev < 0) {
1047 break;
1047 break;
1048 }
1048 }
1049
1049
1050 if (iterrev >= length) {
1050 if (iterrev >= length) {
1051 PyErr_SetString(PyExc_IndexError,
1051 PyErr_SetString(PyExc_IndexError,
1052 "revision outside index");
1052 "revision outside index");
1053 return NULL;
1053 return NULL;
1054 }
1054 }
1055
1055
1056 baserev = index_baserev(self, iterrev);
1056 baserev = index_baserev(self, iterrev);
1057
1057
1058 /* This should never happen. */
1058 /* This should never happen. */
1059 if (baserev <= -2) {
1059 if (baserev <= -2) {
1060 /* Error should be set by index_deref() */
1060 /* Error should be set by index_deref() */
1061 assert(PyErr_Occurred());
1061 assert(PyErr_Occurred());
1062 goto bail;
1062 goto bail;
1063 }
1063 }
1064 }
1064 }
1065
1065
1066 if (iterrev == stoprev) {
1066 if (iterrev == stoprev) {
1067 stopped = 1;
1067 stopped = 1;
1068 } else {
1068 } else {
1069 PyObject *value = PyInt_FromLong(iterrev);
1069 PyObject *value = PyInt_FromLong(iterrev);
1070 if (value == NULL) {
1070 if (value == NULL) {
1071 goto bail;
1071 goto bail;
1072 }
1072 }
1073 if (PyList_Append(chain, value)) {
1073 if (PyList_Append(chain, value)) {
1074 Py_DECREF(value);
1074 Py_DECREF(value);
1075 goto bail;
1075 goto bail;
1076 }
1076 }
1077 Py_DECREF(value);
1077 Py_DECREF(value);
1078
1078
1079 stopped = 0;
1079 stopped = 0;
1080 }
1080 }
1081
1081
1082 if (PyList_Reverse(chain)) {
1082 if (PyList_Reverse(chain)) {
1083 goto bail;
1083 goto bail;
1084 }
1084 }
1085
1085
1086 result = Py_BuildValue("OO", chain, stopped ? Py_True : Py_False);
1086 result = Py_BuildValue("OO", chain, stopped ? Py_True : Py_False);
1087 Py_DECREF(chain);
1087 Py_DECREF(chain);
1088 return result;
1088 return result;
1089
1089
1090 bail:
1090 bail:
1091 Py_DECREF(chain);
1091 Py_DECREF(chain);
1092 return NULL;
1092 return NULL;
1093 }
1093 }
1094
1094
1095 static inline int64_t
1095 static inline int64_t
1096 index_segment_span(indexObject *self, Py_ssize_t start_rev, Py_ssize_t end_rev)
1096 index_segment_span(indexObject *self, Py_ssize_t start_rev, Py_ssize_t end_rev)
1097 {
1097 {
1098 int64_t start_offset;
1098 int64_t start_offset;
1099 int64_t end_offset;
1099 int64_t end_offset;
1100 int end_size;
1100 int end_size;
1101 start_offset = index_get_start(self, start_rev);
1101 start_offset = index_get_start(self, start_rev);
1102 if (start_offset < 0) {
1102 if (start_offset < 0) {
1103 return -1;
1103 return -1;
1104 }
1104 }
1105 end_offset = index_get_start(self, end_rev);
1105 end_offset = index_get_start(self, end_rev);
1106 if (end_offset < 0) {
1106 if (end_offset < 0) {
1107 return -1;
1107 return -1;
1108 }
1108 }
1109 end_size = index_get_length(self, end_rev);
1109 end_size = index_get_length(self, end_rev);
1110 if (end_size < 0) {
1110 if (end_size < 0) {
1111 return -1;
1111 return -1;
1112 }
1112 }
1113 if (end_offset < start_offset) {
1113 if (end_offset < start_offset) {
1114 PyErr_Format(PyExc_ValueError,
1114 PyErr_Format(PyExc_ValueError,
1115 "corrupted revlog index: inconsistent offset "
1115 "corrupted revlog index: inconsistent offset "
1116 "between revisions (%zd) and (%zd)",
1116 "between revisions (%zd) and (%zd)",
1117 start_rev, end_rev);
1117 start_rev, end_rev);
1118 return -1;
1118 return -1;
1119 }
1119 }
1120 return (end_offset - start_offset) + (int64_t)end_size;
1120 return (end_offset - start_offset) + (int64_t)end_size;
1121 }
1121 }
1122
1122
1123 /* returns endidx so that revs[startidx:endidx] has no empty trailing revs */
1123 /* returns endidx so that revs[startidx:endidx] has no empty trailing revs */
1124 static Py_ssize_t trim_endidx(indexObject *self, const Py_ssize_t *revs,
1124 static Py_ssize_t trim_endidx(indexObject *self, const Py_ssize_t *revs,
1125 Py_ssize_t startidx, Py_ssize_t endidx)
1125 Py_ssize_t startidx, Py_ssize_t endidx)
1126 {
1126 {
1127 int length;
1127 int length;
1128 while (endidx > 1 && endidx > startidx) {
1128 while (endidx > 1 && endidx > startidx) {
1129 length = index_get_length(self, revs[endidx - 1]);
1129 length = index_get_length(self, revs[endidx - 1]);
1130 if (length < 0) {
1130 if (length < 0) {
1131 return -1;
1131 return -1;
1132 }
1132 }
1133 if (length != 0) {
1133 if (length != 0) {
1134 break;
1134 break;
1135 }
1135 }
1136 endidx -= 1;
1136 endidx -= 1;
1137 }
1137 }
1138 return endidx;
1138 return endidx;
1139 }
1139 }
1140
1140
1141 struct Gap {
1141 struct Gap {
1142 int64_t size;
1142 int64_t size;
1143 Py_ssize_t idx;
1143 Py_ssize_t idx;
1144 };
1144 };
1145
1145
1146 static int gap_compare(const void *left, const void *right)
1146 static int gap_compare(const void *left, const void *right)
1147 {
1147 {
1148 const struct Gap *l_left = ((const struct Gap *)left);
1148 const struct Gap *l_left = ((const struct Gap *)left);
1149 const struct Gap *l_right = ((const struct Gap *)right);
1149 const struct Gap *l_right = ((const struct Gap *)right);
1150 if (l_left->size < l_right->size) {
1150 if (l_left->size < l_right->size) {
1151 return -1;
1151 return -1;
1152 } else if (l_left->size > l_right->size) {
1152 } else if (l_left->size > l_right->size) {
1153 return 1;
1153 return 1;
1154 }
1154 }
1155 return 0;
1155 return 0;
1156 }
1156 }
1157 static int Py_ssize_t_compare(const void *left, const void *right)
1157 static int Py_ssize_t_compare(const void *left, const void *right)
1158 {
1158 {
1159 const Py_ssize_t l_left = *(const Py_ssize_t *)left;
1159 const Py_ssize_t l_left = *(const Py_ssize_t *)left;
1160 const Py_ssize_t l_right = *(const Py_ssize_t *)right;
1160 const Py_ssize_t l_right = *(const Py_ssize_t *)right;
1161 if (l_left < l_right) {
1161 if (l_left < l_right) {
1162 return -1;
1162 return -1;
1163 } else if (l_left > l_right) {
1163 } else if (l_left > l_right) {
1164 return 1;
1164 return 1;
1165 }
1165 }
1166 return 0;
1166 return 0;
1167 }
1167 }
1168
1168
1169 static PyObject *index_slicechunktodensity(indexObject *self, PyObject *args)
1169 static PyObject *index_slicechunktodensity(indexObject *self, PyObject *args)
1170 {
1170 {
1171 /* method arguments */
1171 /* method arguments */
1172 PyObject *list_revs = NULL; /* revisions in the chain */
1172 PyObject *list_revs = NULL; /* revisions in the chain */
1173 double targetdensity = 0; /* min density to achieve */
1173 double targetdensity = 0; /* min density to achieve */
1174 Py_ssize_t mingapsize = 0; /* threshold to ignore gaps */
1174 Py_ssize_t mingapsize = 0; /* threshold to ignore gaps */
1175
1175
1176 /* other core variables */
1176 /* other core variables */
1177 Py_ssize_t idxlen = index_length(self);
1177 Py_ssize_t idxlen = index_length(self);
1178 Py_ssize_t i; /* used for various iteration */
1178 Py_ssize_t i; /* used for various iteration */
1179 PyObject *result = NULL; /* the final return of the function */
1179 PyObject *result = NULL; /* the final return of the function */
1180
1180
1181 /* generic information about the delta chain being slice */
1181 /* generic information about the delta chain being slice */
1182 Py_ssize_t num_revs = 0; /* size of the full delta chain */
1182 Py_ssize_t num_revs = 0; /* size of the full delta chain */
1183 Py_ssize_t *revs = NULL; /* native array of revision in the chain */
1183 Py_ssize_t *revs = NULL; /* native array of revision in the chain */
1184 int64_t chainpayload = 0; /* sum of all delta in the chain */
1184 int64_t chainpayload = 0; /* sum of all delta in the chain */
1185 int64_t deltachainspan = 0; /* distance from first byte to last byte */
1185 int64_t deltachainspan = 0; /* distance from first byte to last byte */
1186
1186
1187 /* variable used for slicing the delta chain */
1187 /* variable used for slicing the delta chain */
1188 int64_t readdata = 0; /* amount of data currently planned to be read */
1188 int64_t readdata = 0; /* amount of data currently planned to be read */
1189 double density = 0; /* ration of payload data compared to read ones */
1189 double density = 0; /* ration of payload data compared to read ones */
1190 int64_t previous_end;
1190 int64_t previous_end;
1191 struct Gap *gaps = NULL; /* array of notable gap in the chain */
1191 struct Gap *gaps = NULL; /* array of notable gap in the chain */
1192 Py_ssize_t num_gaps =
1192 Py_ssize_t num_gaps =
1193 0; /* total number of notable gap recorded so far */
1193 0; /* total number of notable gap recorded so far */
1194 Py_ssize_t *selected_indices = NULL; /* indices of gap skipped over */
1194 Py_ssize_t *selected_indices = NULL; /* indices of gap skipped over */
1195 Py_ssize_t num_selected = 0; /* number of gaps skipped */
1195 Py_ssize_t num_selected = 0; /* number of gaps skipped */
1196 PyObject *chunk = NULL; /* individual slice */
1196 PyObject *chunk = NULL; /* individual slice */
1197 PyObject *allchunks = NULL; /* all slices */
1197 PyObject *allchunks = NULL; /* all slices */
1198 Py_ssize_t previdx;
1198 Py_ssize_t previdx;
1199
1199
1200 /* parsing argument */
1200 /* parsing argument */
1201 if (!PyArg_ParseTuple(args, "O!dn", &PyList_Type, &list_revs,
1201 if (!PyArg_ParseTuple(args, "O!dn", &PyList_Type, &list_revs,
1202 &targetdensity, &mingapsize)) {
1202 &targetdensity, &mingapsize)) {
1203 goto bail;
1203 goto bail;
1204 }
1204 }
1205
1205
1206 /* If the delta chain contains a single element, we do not need slicing
1206 /* If the delta chain contains a single element, we do not need slicing
1207 */
1207 */
1208 num_revs = PyList_GET_SIZE(list_revs);
1208 num_revs = PyList_GET_SIZE(list_revs);
1209 if (num_revs <= 1) {
1209 if (num_revs <= 1) {
1210 result = PyTuple_Pack(1, list_revs);
1210 result = PyTuple_Pack(1, list_revs);
1211 goto done;
1211 goto done;
1212 }
1212 }
1213
1213
1214 /* Turn the python list into a native integer array (for efficiency) */
1214 /* Turn the python list into a native integer array (for efficiency) */
1215 revs = (Py_ssize_t *)calloc(num_revs, sizeof(Py_ssize_t));
1215 revs = (Py_ssize_t *)calloc(num_revs, sizeof(Py_ssize_t));
1216 if (revs == NULL) {
1216 if (revs == NULL) {
1217 PyErr_NoMemory();
1217 PyErr_NoMemory();
1218 goto bail;
1218 goto bail;
1219 }
1219 }
1220 for (i = 0; i < num_revs; i++) {
1220 for (i = 0; i < num_revs; i++) {
1221 Py_ssize_t revnum = PyInt_AsLong(PyList_GET_ITEM(list_revs, i));
1221 Py_ssize_t revnum = PyInt_AsLong(PyList_GET_ITEM(list_revs, i));
1222 if (revnum == -1 && PyErr_Occurred()) {
1222 if (revnum == -1 && PyErr_Occurred()) {
1223 goto bail;
1223 goto bail;
1224 }
1224 }
1225 if (revnum < 0 || revnum >= idxlen) {
1225 if (revnum < 0 || revnum >= idxlen) {
1226 PyErr_Format(PyExc_IndexError,
1226 PyErr_Format(PyExc_IndexError,
1227 "index out of range: %zd", revnum);
1227 "index out of range: %zd", revnum);
1228 goto bail;
1228 goto bail;
1229 }
1229 }
1230 revs[i] = revnum;
1230 revs[i] = revnum;
1231 }
1231 }
1232
1232
1233 /* Compute and check various property of the unsliced delta chain */
1233 /* Compute and check various property of the unsliced delta chain */
1234 deltachainspan = index_segment_span(self, revs[0], revs[num_revs - 1]);
1234 deltachainspan = index_segment_span(self, revs[0], revs[num_revs - 1]);
1235 if (deltachainspan < 0) {
1235 if (deltachainspan < 0) {
1236 goto bail;
1236 goto bail;
1237 }
1237 }
1238
1238
1239 if (deltachainspan <= mingapsize) {
1239 if (deltachainspan <= mingapsize) {
1240 result = PyTuple_Pack(1, list_revs);
1240 result = PyTuple_Pack(1, list_revs);
1241 goto done;
1241 goto done;
1242 }
1242 }
1243 chainpayload = 0;
1243 chainpayload = 0;
1244 for (i = 0; i < num_revs; i++) {
1244 for (i = 0; i < num_revs; i++) {
1245 int tmp = index_get_length(self, revs[i]);
1245 int tmp = index_get_length(self, revs[i]);
1246 if (tmp < 0) {
1246 if (tmp < 0) {
1247 goto bail;
1247 goto bail;
1248 }
1248 }
1249 chainpayload += tmp;
1249 chainpayload += tmp;
1250 }
1250 }
1251
1251
1252 readdata = deltachainspan;
1252 readdata = deltachainspan;
1253 density = 1.0;
1253 density = 1.0;
1254
1254
1255 if (0 < deltachainspan) {
1255 if (0 < deltachainspan) {
1256 density = (double)chainpayload / (double)deltachainspan;
1256 density = (double)chainpayload / (double)deltachainspan;
1257 }
1257 }
1258
1258
1259 if (density >= targetdensity) {
1259 if (density >= targetdensity) {
1260 result = PyTuple_Pack(1, list_revs);
1260 result = PyTuple_Pack(1, list_revs);
1261 goto done;
1261 goto done;
1262 }
1262 }
1263
1263
1264 /* if chain is too sparse, look for relevant gaps */
1264 /* if chain is too sparse, look for relevant gaps */
1265 gaps = (struct Gap *)calloc(num_revs, sizeof(struct Gap));
1265 gaps = (struct Gap *)calloc(num_revs, sizeof(struct Gap));
1266 if (gaps == NULL) {
1266 if (gaps == NULL) {
1267 PyErr_NoMemory();
1267 PyErr_NoMemory();
1268 goto bail;
1268 goto bail;
1269 }
1269 }
1270
1270
1271 previous_end = -1;
1271 previous_end = -1;
1272 for (i = 0; i < num_revs; i++) {
1272 for (i = 0; i < num_revs; i++) {
1273 int64_t revstart;
1273 int64_t revstart;
1274 int revsize;
1274 int revsize;
1275 revstart = index_get_start(self, revs[i]);
1275 revstart = index_get_start(self, revs[i]);
1276 if (revstart < 0) {
1276 if (revstart < 0) {
1277 goto bail;
1277 goto bail;
1278 };
1278 };
1279 revsize = index_get_length(self, revs[i]);
1279 revsize = index_get_length(self, revs[i]);
1280 if (revsize < 0) {
1280 if (revsize < 0) {
1281 goto bail;
1281 goto bail;
1282 };
1282 };
1283 if (revsize == 0) {
1283 if (revsize == 0) {
1284 continue;
1284 continue;
1285 }
1285 }
1286 if (previous_end >= 0) {
1286 if (previous_end >= 0) {
1287 int64_t gapsize = revstart - previous_end;
1287 int64_t gapsize = revstart - previous_end;
1288 if (gapsize > mingapsize) {
1288 if (gapsize > mingapsize) {
1289 gaps[num_gaps].size = gapsize;
1289 gaps[num_gaps].size = gapsize;
1290 gaps[num_gaps].idx = i;
1290 gaps[num_gaps].idx = i;
1291 num_gaps += 1;
1291 num_gaps += 1;
1292 }
1292 }
1293 }
1293 }
1294 previous_end = revstart + revsize;
1294 previous_end = revstart + revsize;
1295 }
1295 }
1296 if (num_gaps == 0) {
1296 if (num_gaps == 0) {
1297 result = PyTuple_Pack(1, list_revs);
1297 result = PyTuple_Pack(1, list_revs);
1298 goto done;
1298 goto done;
1299 }
1299 }
1300 qsort(gaps, num_gaps, sizeof(struct Gap), &gap_compare);
1300 qsort(gaps, num_gaps, sizeof(struct Gap), &gap_compare);
1301
1301
1302 /* Slice the largest gap first, they improve the density the most */
1302 /* Slice the largest gap first, they improve the density the most */
1303 selected_indices =
1303 selected_indices =
1304 (Py_ssize_t *)malloc((num_gaps + 1) * sizeof(Py_ssize_t));
1304 (Py_ssize_t *)malloc((num_gaps + 1) * sizeof(Py_ssize_t));
1305 if (selected_indices == NULL) {
1305 if (selected_indices == NULL) {
1306 PyErr_NoMemory();
1306 PyErr_NoMemory();
1307 goto bail;
1307 goto bail;
1308 }
1308 }
1309
1309
1310 for (i = num_gaps - 1; i >= 0; i--) {
1310 for (i = num_gaps - 1; i >= 0; i--) {
1311 selected_indices[num_selected] = gaps[i].idx;
1311 selected_indices[num_selected] = gaps[i].idx;
1312 readdata -= gaps[i].size;
1312 readdata -= gaps[i].size;
1313 num_selected += 1;
1313 num_selected += 1;
1314 if (readdata <= 0) {
1314 if (readdata <= 0) {
1315 density = 1.0;
1315 density = 1.0;
1316 } else {
1316 } else {
1317 density = (double)chainpayload / (double)readdata;
1317 density = (double)chainpayload / (double)readdata;
1318 }
1318 }
1319 if (density >= targetdensity) {
1319 if (density >= targetdensity) {
1320 break;
1320 break;
1321 }
1321 }
1322 }
1322 }
1323 qsort(selected_indices, num_selected, sizeof(Py_ssize_t),
1323 qsort(selected_indices, num_selected, sizeof(Py_ssize_t),
1324 &Py_ssize_t_compare);
1324 &Py_ssize_t_compare);
1325
1325
1326 /* create the resulting slice */
1326 /* create the resulting slice */
1327 allchunks = PyList_New(0);
1327 allchunks = PyList_New(0);
1328 if (allchunks == NULL) {
1328 if (allchunks == NULL) {
1329 goto bail;
1329 goto bail;
1330 }
1330 }
1331 previdx = 0;
1331 previdx = 0;
1332 selected_indices[num_selected] = num_revs;
1332 selected_indices[num_selected] = num_revs;
1333 for (i = 0; i <= num_selected; i++) {
1333 for (i = 0; i <= num_selected; i++) {
1334 Py_ssize_t idx = selected_indices[i];
1334 Py_ssize_t idx = selected_indices[i];
1335 Py_ssize_t endidx = trim_endidx(self, revs, previdx, idx);
1335 Py_ssize_t endidx = trim_endidx(self, revs, previdx, idx);
1336 if (endidx < 0) {
1336 if (endidx < 0) {
1337 goto bail;
1337 goto bail;
1338 }
1338 }
1339 if (previdx < endidx) {
1339 if (previdx < endidx) {
1340 chunk = PyList_GetSlice(list_revs, previdx, endidx);
1340 chunk = PyList_GetSlice(list_revs, previdx, endidx);
1341 if (chunk == NULL) {
1341 if (chunk == NULL) {
1342 goto bail;
1342 goto bail;
1343 }
1343 }
1344 if (PyList_Append(allchunks, chunk) == -1) {
1344 if (PyList_Append(allchunks, chunk) == -1) {
1345 goto bail;
1345 goto bail;
1346 }
1346 }
1347 Py_DECREF(chunk);
1347 Py_DECREF(chunk);
1348 chunk = NULL;
1348 chunk = NULL;
1349 }
1349 }
1350 previdx = idx;
1350 previdx = idx;
1351 }
1351 }
1352 result = allchunks;
1352 result = allchunks;
1353 goto done;
1353 goto done;
1354
1354
1355 bail:
1355 bail:
1356 Py_XDECREF(allchunks);
1356 Py_XDECREF(allchunks);
1357 Py_XDECREF(chunk);
1357 Py_XDECREF(chunk);
1358 done:
1358 done:
1359 free(revs);
1359 free(revs);
1360 free(gaps);
1360 free(gaps);
1361 free(selected_indices);
1361 free(selected_indices);
1362 return result;
1362 return result;
1363 }
1363 }
1364
1364
1365 static inline int nt_level(const char *node, Py_ssize_t level)
1365 static inline int nt_level(const char *node, Py_ssize_t level)
1366 {
1366 {
1367 int v = node[level >> 1];
1367 int v = node[level >> 1];
1368 if (!(level & 1))
1368 if (!(level & 1))
1369 v >>= 4;
1369 v >>= 4;
1370 return v & 0xf;
1370 return v & 0xf;
1371 }
1371 }
1372
1372
1373 /*
1373 /*
1374 * Return values:
1374 * Return values:
1375 *
1375 *
1376 * -4: match is ambiguous (multiple candidates)
1376 * -4: match is ambiguous (multiple candidates)
1377 * -2: not found
1377 * -2: not found
1378 * rest: valid rev
1378 * rest: valid rev
1379 */
1379 */
1380 static int nt_find(nodetree *self, const char *node, Py_ssize_t nodelen,
1380 static int nt_find(nodetree *self, const char *node, Py_ssize_t nodelen,
1381 int hex)
1381 int hex)
1382 {
1382 {
1383 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
1383 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
1384 int level, maxlevel, off;
1384 int level, maxlevel, off;
1385
1385
1386 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
1386 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
1387 return -1;
1387 return -1;
1388
1388
1389 if (hex)
1389 if (hex)
1390 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
1390 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
1391 else
1391 else
1392 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
1392 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
1393
1393
1394 for (level = off = 0; level < maxlevel; level++) {
1394 for (level = off = 0; level < maxlevel; level++) {
1395 int k = getnybble(node, level);
1395 int k = getnybble(node, level);
1396 nodetreenode *n = &self->nodes[off];
1396 nodetreenode *n = &self->nodes[off];
1397 int v = n->children[k];
1397 int v = n->children[k];
1398
1398
1399 if (v < 0) {
1399 if (v < 0) {
1400 const char *n;
1400 const char *n;
1401 Py_ssize_t i;
1401 Py_ssize_t i;
1402
1402
1403 v = -(v + 2);
1403 v = -(v + 2);
1404 n = index_node(self->index, v);
1404 n = index_node(self->index, v);
1405 if (n == NULL)
1405 if (n == NULL)
1406 return -2;
1406 return -2;
1407 for (i = level; i < maxlevel; i++)
1407 for (i = level; i < maxlevel; i++)
1408 if (getnybble(node, i) != nt_level(n, i))
1408 if (getnybble(node, i) != nt_level(n, i))
1409 return -2;
1409 return -2;
1410 return v;
1410 return v;
1411 }
1411 }
1412 if (v == 0)
1412 if (v == 0)
1413 return -2;
1413 return -2;
1414 off = v;
1414 off = v;
1415 }
1415 }
1416 /* multiple matches against an ambiguous prefix */
1416 /* multiple matches against an ambiguous prefix */
1417 return -4;
1417 return -4;
1418 }
1418 }
1419
1419
1420 static int nt_new(nodetree *self)
1420 static int nt_new(nodetree *self)
1421 {
1421 {
1422 if (self->length == self->capacity) {
1422 if (self->length == self->capacity) {
1423 unsigned newcapacity;
1423 unsigned newcapacity;
1424 nodetreenode *newnodes;
1424 nodetreenode *newnodes;
1425 newcapacity = self->capacity * 2;
1425 newcapacity = self->capacity * 2;
1426 if (newcapacity >= INT_MAX / sizeof(nodetreenode)) {
1426 if (newcapacity >= INT_MAX / sizeof(nodetreenode)) {
1427 PyErr_SetString(PyExc_MemoryError,
1427 PyErr_SetString(PyExc_MemoryError,
1428 "overflow in nt_new");
1428 "overflow in nt_new");
1429 return -1;
1429 return -1;
1430 }
1430 }
1431 newnodes =
1431 newnodes =
1432 realloc(self->nodes, newcapacity * sizeof(nodetreenode));
1432 realloc(self->nodes, newcapacity * sizeof(nodetreenode));
1433 if (newnodes == NULL) {
1433 if (newnodes == NULL) {
1434 PyErr_SetString(PyExc_MemoryError, "out of memory");
1434 PyErr_SetString(PyExc_MemoryError, "out of memory");
1435 return -1;
1435 return -1;
1436 }
1436 }
1437 self->capacity = newcapacity;
1437 self->capacity = newcapacity;
1438 self->nodes = newnodes;
1438 self->nodes = newnodes;
1439 memset(&self->nodes[self->length], 0,
1439 memset(&self->nodes[self->length], 0,
1440 sizeof(nodetreenode) * (self->capacity - self->length));
1440 sizeof(nodetreenode) * (self->capacity - self->length));
1441 }
1441 }
1442 return self->length++;
1442 return self->length++;
1443 }
1443 }
1444
1444
1445 static int nt_insert(nodetree *self, const char *node, int rev)
1445 static int nt_insert(nodetree *self, const char *node, int rev)
1446 {
1446 {
1447 int level = 0;
1447 int level = 0;
1448 int off = 0;
1448 int off = 0;
1449
1449
1450 while (level < 40) {
1450 while (level < 40) {
1451 int k = nt_level(node, level);
1451 int k = nt_level(node, level);
1452 nodetreenode *n;
1452 nodetreenode *n;
1453 int v;
1453 int v;
1454
1454
1455 n = &self->nodes[off];
1455 n = &self->nodes[off];
1456 v = n->children[k];
1456 v = n->children[k];
1457
1457
1458 if (v == 0) {
1458 if (v == 0) {
1459 n->children[k] = -rev - 2;
1459 n->children[k] = -rev - 2;
1460 return 0;
1460 return 0;
1461 }
1461 }
1462 if (v < 0) {
1462 if (v < 0) {
1463 const char *oldnode =
1463 const char *oldnode =
1464 index_node_existing(self->index, -(v + 2));
1464 index_node_existing(self->index, -(v + 2));
1465 int noff;
1465 int noff;
1466
1466
1467 if (oldnode == NULL)
1467 if (oldnode == NULL)
1468 return -1;
1468 return -1;
1469 if (!memcmp(oldnode, node, 20)) {
1469 if (!memcmp(oldnode, node, 20)) {
1470 n->children[k] = -rev - 2;
1470 n->children[k] = -rev - 2;
1471 return 0;
1471 return 0;
1472 }
1472 }
1473 noff = nt_new(self);
1473 noff = nt_new(self);
1474 if (noff == -1)
1474 if (noff == -1)
1475 return -1;
1475 return -1;
1476 /* self->nodes may have been changed by realloc */
1476 /* self->nodes may have been changed by realloc */
1477 self->nodes[off].children[k] = noff;
1477 self->nodes[off].children[k] = noff;
1478 off = noff;
1478 off = noff;
1479 n = &self->nodes[off];
1479 n = &self->nodes[off];
1480 n->children[nt_level(oldnode, ++level)] = v;
1480 n->children[nt_level(oldnode, ++level)] = v;
1481 if (level > self->depth)
1481 if (level > self->depth)
1482 self->depth = level;
1482 self->depth = level;
1483 self->splits += 1;
1483 self->splits += 1;
1484 } else {
1484 } else {
1485 level += 1;
1485 level += 1;
1486 off = v;
1486 off = v;
1487 }
1487 }
1488 }
1488 }
1489
1489
1490 return -1;
1490 return -1;
1491 }
1491 }
1492
1492
1493 static PyObject *ntobj_insert(nodetreeObject *self, PyObject *args)
1493 static PyObject *ntobj_insert(nodetreeObject *self, PyObject *args)
1494 {
1494 {
1495 Py_ssize_t rev;
1495 Py_ssize_t rev;
1496 const char *node;
1496 const char *node;
1497 Py_ssize_t length;
1497 Py_ssize_t length;
1498 if (!PyArg_ParseTuple(args, "n", &rev))
1498 if (!PyArg_ParseTuple(args, "n", &rev))
1499 return NULL;
1499 return NULL;
1500 length = index_length(self->nt.index);
1500 length = index_length(self->nt.index);
1501 if (rev < 0 || rev >= length) {
1501 if (rev < 0 || rev >= length) {
1502 PyErr_SetString(PyExc_ValueError, "revlog index out of range");
1502 PyErr_SetString(PyExc_ValueError, "revlog index out of range");
1503 return NULL;
1503 return NULL;
1504 }
1504 }
1505 node = index_node_existing(self->nt.index, rev);
1505 node = index_node_existing(self->nt.index, rev);
1506 if (nt_insert(&self->nt, node, (int)rev) == -1)
1506 if (nt_insert(&self->nt, node, (int)rev) == -1)
1507 return NULL;
1507 return NULL;
1508 Py_RETURN_NONE;
1508 Py_RETURN_NONE;
1509 }
1509 }
1510
1510
1511 static int nt_delete_node(nodetree *self, const char *node)
1511 static int nt_delete_node(nodetree *self, const char *node)
1512 {
1512 {
1513 /* rev==-2 happens to get encoded as 0, which is interpreted as not set
1513 /* rev==-2 happens to get encoded as 0, which is interpreted as not set
1514 */
1514 */
1515 return nt_insert(self, node, -2);
1515 return nt_insert(self, node, -2);
1516 }
1516 }
1517
1517
1518 static int nt_init(nodetree *self, indexObject *index, unsigned capacity)
1518 static int nt_init(nodetree *self, indexObject *index, unsigned capacity)
1519 {
1519 {
1520 /* Initialize before overflow-checking to avoid nt_dealloc() crash. */
1520 /* Initialize before overflow-checking to avoid nt_dealloc() crash. */
1521 self->nodes = NULL;
1521 self->nodes = NULL;
1522
1522
1523 self->index = index;
1523 self->index = index;
1524 /* The input capacity is in terms of revisions, while the field is in
1524 /* The input capacity is in terms of revisions, while the field is in
1525 * terms of nodetree nodes. */
1525 * terms of nodetree nodes. */
1526 self->capacity = (capacity < 4 ? 4 : capacity / 2);
1526 self->capacity = (capacity < 4 ? 4 : capacity / 2);
1527 self->depth = 0;
1527 self->depth = 0;
1528 self->splits = 0;
1528 self->splits = 0;
1529 if ((size_t)self->capacity > INT_MAX / sizeof(nodetreenode)) {
1529 if ((size_t)self->capacity > INT_MAX / sizeof(nodetreenode)) {
1530 PyErr_SetString(PyExc_ValueError, "overflow in init_nt");
1530 PyErr_SetString(PyExc_ValueError, "overflow in init_nt");
1531 return -1;
1531 return -1;
1532 }
1532 }
1533 self->nodes = calloc(self->capacity, sizeof(nodetreenode));
1533 self->nodes = calloc(self->capacity, sizeof(nodetreenode));
1534 if (self->nodes == NULL) {
1534 if (self->nodes == NULL) {
1535 PyErr_NoMemory();
1535 PyErr_NoMemory();
1536 return -1;
1536 return -1;
1537 }
1537 }
1538 self->length = 1;
1538 self->length = 1;
1539 return 0;
1539 return 0;
1540 }
1540 }
1541
1541
1542 static int ntobj_init(nodetreeObject *self, PyObject *args)
1542 static int ntobj_init(nodetreeObject *self, PyObject *args)
1543 {
1543 {
1544 PyObject *index;
1544 PyObject *index;
1545 unsigned capacity;
1545 unsigned capacity;
1546 if (!PyArg_ParseTuple(args, "O!I", &HgRevlogIndex_Type, &index,
1546 if (!PyArg_ParseTuple(args, "O!I", &HgRevlogIndex_Type, &index,
1547 &capacity))
1547 &capacity))
1548 return -1;
1548 return -1;
1549 Py_INCREF(index);
1549 Py_INCREF(index);
1550 return nt_init(&self->nt, (indexObject *)index, capacity);
1550 return nt_init(&self->nt, (indexObject *)index, capacity);
1551 }
1551 }
1552
1552
1553 static int nt_partialmatch(nodetree *self, const char *node, Py_ssize_t nodelen)
1553 static int nt_partialmatch(nodetree *self, const char *node, Py_ssize_t nodelen)
1554 {
1554 {
1555 return nt_find(self, node, nodelen, 1);
1555 return nt_find(self, node, nodelen, 1);
1556 }
1556 }
1557
1557
1558 /*
1558 /*
1559 * Find the length of the shortest unique prefix of node.
1559 * Find the length of the shortest unique prefix of node.
1560 *
1560 *
1561 * Return values:
1561 * Return values:
1562 *
1562 *
1563 * -3: error (exception set)
1563 * -3: error (exception set)
1564 * -2: not found (no exception set)
1564 * -2: not found (no exception set)
1565 * rest: length of shortest prefix
1565 * rest: length of shortest prefix
1566 */
1566 */
1567 static int nt_shortest(nodetree *self, const char *node)
1567 static int nt_shortest(nodetree *self, const char *node)
1568 {
1568 {
1569 int level, off;
1569 int level, off;
1570
1570
1571 for (level = off = 0; level < 40; level++) {
1571 for (level = off = 0; level < 40; level++) {
1572 int k, v;
1572 int k, v;
1573 nodetreenode *n = &self->nodes[off];
1573 nodetreenode *n = &self->nodes[off];
1574 k = nt_level(node, level);
1574 k = nt_level(node, level);
1575 v = n->children[k];
1575 v = n->children[k];
1576 if (v < 0) {
1576 if (v < 0) {
1577 const char *n;
1577 const char *n;
1578 v = -(v + 2);
1578 v = -(v + 2);
1579 n = index_node_existing(self->index, v);
1579 n = index_node_existing(self->index, v);
1580 if (n == NULL)
1580 if (n == NULL)
1581 return -3;
1581 return -3;
1582 if (memcmp(node, n, 20) != 0)
1582 if (memcmp(node, n, 20) != 0)
1583 /*
1583 /*
1584 * Found a unique prefix, but it wasn't for the
1584 * Found a unique prefix, but it wasn't for the
1585 * requested node (i.e the requested node does
1585 * requested node (i.e the requested node does
1586 * not exist).
1586 * not exist).
1587 */
1587 */
1588 return -2;
1588 return -2;
1589 return level + 1;
1589 return level + 1;
1590 }
1590 }
1591 if (v == 0)
1591 if (v == 0)
1592 return -2;
1592 return -2;
1593 off = v;
1593 off = v;
1594 }
1594 }
1595 /*
1595 /*
1596 * The node was still not unique after 40 hex digits, so this won't
1596 * The node was still not unique after 40 hex digits, so this won't
1597 * happen. Also, if we get here, then there's a programming error in
1597 * happen. Also, if we get here, then there's a programming error in
1598 * this file that made us insert a node longer than 40 hex digits.
1598 * this file that made us insert a node longer than 40 hex digits.
1599 */
1599 */
1600 PyErr_SetString(PyExc_Exception, "broken node tree");
1600 PyErr_SetString(PyExc_Exception, "broken node tree");
1601 return -3;
1601 return -3;
1602 }
1602 }
1603
1603
1604 static PyObject *ntobj_shortest(nodetreeObject *self, PyObject *args)
1604 static PyObject *ntobj_shortest(nodetreeObject *self, PyObject *args)
1605 {
1605 {
1606 PyObject *val;
1606 PyObject *val;
1607 char *node;
1607 char *node;
1608 int length;
1608 int length;
1609
1609
1610 if (!PyArg_ParseTuple(args, "O", &val))
1610 if (!PyArg_ParseTuple(args, "O", &val))
1611 return NULL;
1611 return NULL;
1612 if (node_check(val, &node) == -1)
1612 if (node_check(val, &node) == -1)
1613 return NULL;
1613 return NULL;
1614
1614
1615 length = nt_shortest(&self->nt, node);
1615 length = nt_shortest(&self->nt, node);
1616 if (length == -3)
1616 if (length == -3)
1617 return NULL;
1617 return NULL;
1618 if (length == -2) {
1618 if (length == -2) {
1619 raise_revlog_error();
1619 raise_revlog_error();
1620 return NULL;
1620 return NULL;
1621 }
1621 }
1622 return PyInt_FromLong(length);
1622 return PyInt_FromLong(length);
1623 }
1623 }
1624
1624
1625 static void nt_dealloc(nodetree *self)
1625 static void nt_dealloc(nodetree *self)
1626 {
1626 {
1627 free(self->nodes);
1627 free(self->nodes);
1628 self->nodes = NULL;
1628 self->nodes = NULL;
1629 }
1629 }
1630
1630
1631 static void ntobj_dealloc(nodetreeObject *self)
1631 static void ntobj_dealloc(nodetreeObject *self)
1632 {
1632 {
1633 Py_XDECREF(self->nt.index);
1633 Py_XDECREF(self->nt.index);
1634 nt_dealloc(&self->nt);
1634 nt_dealloc(&self->nt);
1635 PyObject_Del(self);
1635 PyObject_Del(self);
1636 }
1636 }
1637
1637
1638 static PyMethodDef ntobj_methods[] = {
1638 static PyMethodDef ntobj_methods[] = {
1639 {"insert", (PyCFunction)ntobj_insert, METH_VARARGS,
1639 {"insert", (PyCFunction)ntobj_insert, METH_VARARGS,
1640 "insert an index entry"},
1640 "insert an index entry"},
1641 {"shortest", (PyCFunction)ntobj_shortest, METH_VARARGS,
1641 {"shortest", (PyCFunction)ntobj_shortest, METH_VARARGS,
1642 "find length of shortest hex nodeid of a binary ID"},
1642 "find length of shortest hex nodeid of a binary ID"},
1643 {NULL} /* Sentinel */
1643 {NULL} /* Sentinel */
1644 };
1644 };
1645
1645
1646 static PyTypeObject nodetreeType = {
1646 static PyTypeObject nodetreeType = {
1647 PyVarObject_HEAD_INIT(NULL, 0) /* header */
1647 PyVarObject_HEAD_INIT(NULL, 0) /* header */
1648 "parsers.nodetree", /* tp_name */
1648 "parsers.nodetree", /* tp_name */
1649 sizeof(nodetreeObject), /* tp_basicsize */
1649 sizeof(nodetreeObject), /* tp_basicsize */
1650 0, /* tp_itemsize */
1650 0, /* tp_itemsize */
1651 (destructor)ntobj_dealloc, /* tp_dealloc */
1651 (destructor)ntobj_dealloc, /* tp_dealloc */
1652 0, /* tp_print */
1652 0, /* tp_print */
1653 0, /* tp_getattr */
1653 0, /* tp_getattr */
1654 0, /* tp_setattr */
1654 0, /* tp_setattr */
1655 0, /* tp_compare */
1655 0, /* tp_compare */
1656 0, /* tp_repr */
1656 0, /* tp_repr */
1657 0, /* tp_as_number */
1657 0, /* tp_as_number */
1658 0, /* tp_as_sequence */
1658 0, /* tp_as_sequence */
1659 0, /* tp_as_mapping */
1659 0, /* tp_as_mapping */
1660 0, /* tp_hash */
1660 0, /* tp_hash */
1661 0, /* tp_call */
1661 0, /* tp_call */
1662 0, /* tp_str */
1662 0, /* tp_str */
1663 0, /* tp_getattro */
1663 0, /* tp_getattro */
1664 0, /* tp_setattro */
1664 0, /* tp_setattro */
1665 0, /* tp_as_buffer */
1665 0, /* tp_as_buffer */
1666 Py_TPFLAGS_DEFAULT, /* tp_flags */
1666 Py_TPFLAGS_DEFAULT, /* tp_flags */
1667 "nodetree", /* tp_doc */
1667 "nodetree", /* tp_doc */
1668 0, /* tp_traverse */
1668 0, /* tp_traverse */
1669 0, /* tp_clear */
1669 0, /* tp_clear */
1670 0, /* tp_richcompare */
1670 0, /* tp_richcompare */
1671 0, /* tp_weaklistoffset */
1671 0, /* tp_weaklistoffset */
1672 0, /* tp_iter */
1672 0, /* tp_iter */
1673 0, /* tp_iternext */
1673 0, /* tp_iternext */
1674 ntobj_methods, /* tp_methods */
1674 ntobj_methods, /* tp_methods */
1675 0, /* tp_members */
1675 0, /* tp_members */
1676 0, /* tp_getset */
1676 0, /* tp_getset */
1677 0, /* tp_base */
1677 0, /* tp_base */
1678 0, /* tp_dict */
1678 0, /* tp_dict */
1679 0, /* tp_descr_get */
1679 0, /* tp_descr_get */
1680 0, /* tp_descr_set */
1680 0, /* tp_descr_set */
1681 0, /* tp_dictoffset */
1681 0, /* tp_dictoffset */
1682 (initproc)ntobj_init, /* tp_init */
1682 (initproc)ntobj_init, /* tp_init */
1683 0, /* tp_alloc */
1683 0, /* tp_alloc */
1684 };
1684 };
1685
1685
1686 static int index_init_nt(indexObject *self)
1686 static int index_init_nt(indexObject *self)
1687 {
1687 {
1688 if (!self->ntinitialized) {
1688 if (!self->ntinitialized) {
1689 if (nt_init(&self->nt, self, (int)self->raw_length) == -1) {
1689 if (nt_init(&self->nt, self, (int)self->raw_length) == -1) {
1690 nt_dealloc(&self->nt);
1690 nt_dealloc(&self->nt);
1691 return -1;
1691 return -1;
1692 }
1692 }
1693 if (nt_insert(&self->nt, nullid, -1) == -1) {
1693 if (nt_insert(&self->nt, nullid, -1) == -1) {
1694 nt_dealloc(&self->nt);
1694 nt_dealloc(&self->nt);
1695 return -1;
1695 return -1;
1696 }
1696 }
1697 self->ntinitialized = 1;
1697 self->ntinitialized = 1;
1698 self->ntrev = (int)index_length(self);
1698 self->ntrev = (int)index_length(self);
1699 self->ntlookups = 1;
1699 self->ntlookups = 1;
1700 self->ntmisses = 0;
1700 self->ntmisses = 0;
1701 }
1701 }
1702 return 0;
1702 return 0;
1703 }
1703 }
1704
1704
1705 /*
1705 /*
1706 * Return values:
1706 * Return values:
1707 *
1707 *
1708 * -3: error (exception set)
1708 * -3: error (exception set)
1709 * -2: not found (no exception set)
1709 * -2: not found (no exception set)
1710 * rest: valid rev
1710 * rest: valid rev
1711 */
1711 */
1712 static int index_find_node(indexObject *self, const char *node,
1712 static int index_find_node(indexObject *self, const char *node,
1713 Py_ssize_t nodelen)
1713 Py_ssize_t nodelen)
1714 {
1714 {
1715 int rev;
1715 int rev;
1716
1716
1717 if (index_init_nt(self) == -1)
1717 if (index_init_nt(self) == -1)
1718 return -3;
1718 return -3;
1719
1719
1720 self->ntlookups++;
1720 self->ntlookups++;
1721 rev = nt_find(&self->nt, node, nodelen, 0);
1721 rev = nt_find(&self->nt, node, nodelen, 0);
1722 if (rev >= -1)
1722 if (rev >= -1)
1723 return rev;
1723 return rev;
1724
1724
1725 /*
1725 /*
1726 * For the first handful of lookups, we scan the entire index,
1726 * For the first handful of lookups, we scan the entire index,
1727 * and cache only the matching nodes. This optimizes for cases
1727 * and cache only the matching nodes. This optimizes for cases
1728 * like "hg tip", where only a few nodes are accessed.
1728 * like "hg tip", where only a few nodes are accessed.
1729 *
1729 *
1730 * After that, we cache every node we visit, using a single
1730 * After that, we cache every node we visit, using a single
1731 * scan amortized over multiple lookups. This gives the best
1731 * scan amortized over multiple lookups. This gives the best
1732 * bulk performance, e.g. for "hg log".
1732 * bulk performance, e.g. for "hg log".
1733 */
1733 */
1734 if (self->ntmisses++ < 4) {
1734 if (self->ntmisses++ < 4) {
1735 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1735 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1736 const char *n = index_node_existing(self, rev);
1736 const char *n = index_node_existing(self, rev);
1737 if (n == NULL)
1737 if (n == NULL)
1738 return -3;
1738 return -3;
1739 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1739 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1740 if (nt_insert(&self->nt, n, rev) == -1)
1740 if (nt_insert(&self->nt, n, rev) == -1)
1741 return -3;
1741 return -3;
1742 break;
1742 break;
1743 }
1743 }
1744 }
1744 }
1745 } else {
1745 } else {
1746 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1746 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1747 const char *n = index_node_existing(self, rev);
1747 const char *n = index_node_existing(self, rev);
1748 if (n == NULL)
1748 if (n == NULL)
1749 return -3;
1749 return -3;
1750 if (nt_insert(&self->nt, n, rev) == -1) {
1750 if (nt_insert(&self->nt, n, rev) == -1) {
1751 self->ntrev = rev + 1;
1751 self->ntrev = rev + 1;
1752 return -3;
1752 return -3;
1753 }
1753 }
1754 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1754 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1755 break;
1755 break;
1756 }
1756 }
1757 }
1757 }
1758 self->ntrev = rev;
1758 self->ntrev = rev;
1759 }
1759 }
1760
1760
1761 if (rev >= 0)
1761 if (rev >= 0)
1762 return rev;
1762 return rev;
1763 return -2;
1763 return -2;
1764 }
1764 }
1765
1765
1766 static PyObject *index_getitem(indexObject *self, PyObject *value)
1766 static PyObject *index_getitem(indexObject *self, PyObject *value)
1767 {
1767 {
1768 char *node;
1768 char *node;
1769 int rev;
1769 int rev;
1770
1770
1771 if (PyInt_Check(value)) {
1771 if (PyInt_Check(value)) {
1772 long idx;
1772 long idx;
1773 if (!pylong_to_long(value, &idx)) {
1773 if (!pylong_to_long(value, &idx)) {
1774 return NULL;
1774 return NULL;
1775 }
1775 }
1776 return index_get(self, idx);
1776 return index_get(self, idx);
1777 }
1777 }
1778
1778
1779 if (node_check(value, &node) == -1)
1779 if (node_check(value, &node) == -1)
1780 return NULL;
1780 return NULL;
1781 rev = index_find_node(self, node, 20);
1781 rev = index_find_node(self, node, 20);
1782 if (rev >= -1)
1782 if (rev >= -1)
1783 return PyInt_FromLong(rev);
1783 return PyInt_FromLong(rev);
1784 if (rev == -2)
1784 if (rev == -2)
1785 raise_revlog_error();
1785 raise_revlog_error();
1786 return NULL;
1786 return NULL;
1787 }
1787 }
1788
1788
1789 /*
1789 /*
1790 * Fully populate the radix tree.
1790 * Fully populate the radix tree.
1791 */
1791 */
1792 static int index_populate_nt(indexObject *self)
1792 static int index_populate_nt(indexObject *self)
1793 {
1793 {
1794 int rev;
1794 int rev;
1795 if (self->ntrev > 0) {
1795 if (self->ntrev > 0) {
1796 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1796 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1797 const char *n = index_node_existing(self, rev);
1797 const char *n = index_node_existing(self, rev);
1798 if (n == NULL)
1798 if (n == NULL)
1799 return -1;
1799 return -1;
1800 if (nt_insert(&self->nt, n, rev) == -1)
1800 if (nt_insert(&self->nt, n, rev) == -1)
1801 return -1;
1801 return -1;
1802 }
1802 }
1803 self->ntrev = -1;
1803 self->ntrev = -1;
1804 }
1804 }
1805 return 0;
1805 return 0;
1806 }
1806 }
1807
1807
1808 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1808 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1809 {
1809 {
1810 const char *fullnode;
1810 const char *fullnode;
1811 int nodelen;
1811 int nodelen;
1812 char *node;
1812 char *node;
1813 int rev, i;
1813 int rev, i;
1814
1814
1815 if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &node, &nodelen))
1815 if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &node, &nodelen))
1816 return NULL;
1816 return NULL;
1817
1817
1818 if (nodelen < 1) {
1818 if (nodelen < 1) {
1819 PyErr_SetString(PyExc_ValueError, "key too short");
1819 PyErr_SetString(PyExc_ValueError, "key too short");
1820 return NULL;
1820 return NULL;
1821 }
1821 }
1822
1822
1823 if (nodelen > 40) {
1823 if (nodelen > 40) {
1824 PyErr_SetString(PyExc_ValueError, "key too long");
1824 PyErr_SetString(PyExc_ValueError, "key too long");
1825 return NULL;
1825 return NULL;
1826 }
1826 }
1827
1827
1828 for (i = 0; i < nodelen; i++)
1828 for (i = 0; i < nodelen; i++)
1829 hexdigit(node, i);
1829 hexdigit(node, i);
1830 if (PyErr_Occurred()) {
1830 if (PyErr_Occurred()) {
1831 /* input contains non-hex characters */
1831 /* input contains non-hex characters */
1832 PyErr_Clear();
1832 PyErr_Clear();
1833 Py_RETURN_NONE;
1833 Py_RETURN_NONE;
1834 }
1834 }
1835
1835
1836 if (index_init_nt(self) == -1)
1836 if (index_init_nt(self) == -1)
1837 return NULL;
1837 return NULL;
1838 if (index_populate_nt(self) == -1)
1838 if (index_populate_nt(self) == -1)
1839 return NULL;
1839 return NULL;
1840 rev = nt_partialmatch(&self->nt, node, nodelen);
1840 rev = nt_partialmatch(&self->nt, node, nodelen);
1841
1841
1842 switch (rev) {
1842 switch (rev) {
1843 case -4:
1843 case -4:
1844 raise_revlog_error();
1844 raise_revlog_error();
1845 return NULL;
1845 return NULL;
1846 case -2:
1846 case -2:
1847 Py_RETURN_NONE;
1847 Py_RETURN_NONE;
1848 case -1:
1848 case -1:
1849 return PyBytes_FromStringAndSize(nullid, 20);
1849 return PyBytes_FromStringAndSize(nullid, 20);
1850 }
1850 }
1851
1851
1852 fullnode = index_node_existing(self, rev);
1852 fullnode = index_node_existing(self, rev);
1853 if (fullnode == NULL) {
1853 if (fullnode == NULL) {
1854 return NULL;
1854 return NULL;
1855 }
1855 }
1856 return PyBytes_FromStringAndSize(fullnode, 20);
1856 return PyBytes_FromStringAndSize(fullnode, 20);
1857 }
1857 }
1858
1858
1859 static PyObject *index_shortest(indexObject *self, PyObject *args)
1859 static PyObject *index_shortest(indexObject *self, PyObject *args)
1860 {
1860 {
1861 PyObject *val;
1861 PyObject *val;
1862 char *node;
1862 char *node;
1863 int length;
1863 int length;
1864
1864
1865 if (!PyArg_ParseTuple(args, "O", &val))
1865 if (!PyArg_ParseTuple(args, "O", &val))
1866 return NULL;
1866 return NULL;
1867 if (node_check(val, &node) == -1)
1867 if (node_check(val, &node) == -1)
1868 return NULL;
1868 return NULL;
1869
1869
1870 self->ntlookups++;
1870 self->ntlookups++;
1871 if (index_init_nt(self) == -1)
1871 if (index_init_nt(self) == -1)
1872 return NULL;
1872 return NULL;
1873 if (index_populate_nt(self) == -1)
1873 if (index_populate_nt(self) == -1)
1874 return NULL;
1874 return NULL;
1875 length = nt_shortest(&self->nt, node);
1875 length = nt_shortest(&self->nt, node);
1876 if (length == -3)
1876 if (length == -3)
1877 return NULL;
1877 return NULL;
1878 if (length == -2) {
1878 if (length == -2) {
1879 raise_revlog_error();
1879 raise_revlog_error();
1880 return NULL;
1880 return NULL;
1881 }
1881 }
1882 return PyInt_FromLong(length);
1882 return PyInt_FromLong(length);
1883 }
1883 }
1884
1884
1885 static PyObject *index_m_get(indexObject *self, PyObject *args)
1885 static PyObject *index_m_get(indexObject *self, PyObject *args)
1886 {
1886 {
1887 PyObject *val;
1887 PyObject *val;
1888 char *node;
1888 char *node;
1889 int rev;
1889 int rev;
1890
1890
1891 if (!PyArg_ParseTuple(args, "O", &val))
1891 if (!PyArg_ParseTuple(args, "O", &val))
1892 return NULL;
1892 return NULL;
1893 if (node_check(val, &node) == -1)
1893 if (node_check(val, &node) == -1)
1894 return NULL;
1894 return NULL;
1895 rev = index_find_node(self, node, 20);
1895 rev = index_find_node(self, node, 20);
1896 if (rev == -3)
1896 if (rev == -3)
1897 return NULL;
1897 return NULL;
1898 if (rev == -2)
1898 if (rev == -2)
1899 Py_RETURN_NONE;
1899 Py_RETURN_NONE;
1900 return PyInt_FromLong(rev);
1900 return PyInt_FromLong(rev);
1901 }
1901 }
1902
1902
1903 static int index_contains(indexObject *self, PyObject *value)
1903 static int index_contains(indexObject *self, PyObject *value)
1904 {
1904 {
1905 char *node;
1905 char *node;
1906
1906
1907 if (PyInt_Check(value)) {
1907 if (PyInt_Check(value)) {
1908 long rev;
1908 long rev;
1909 if (!pylong_to_long(value, &rev)) {
1909 if (!pylong_to_long(value, &rev)) {
1910 return -1;
1910 return -1;
1911 }
1911 }
1912 return rev >= -1 && rev < index_length(self);
1912 return rev >= -1 && rev < index_length(self);
1913 }
1913 }
1914
1914
1915 if (node_check(value, &node) == -1)
1915 if (node_check(value, &node) == -1)
1916 return -1;
1916 return -1;
1917
1917
1918 switch (index_find_node(self, node, 20)) {
1918 switch (index_find_node(self, node, 20)) {
1919 case -3:
1919 case -3:
1920 return -1;
1920 return -1;
1921 case -2:
1921 case -2:
1922 return 0;
1922 return 0;
1923 default:
1923 default:
1924 return 1;
1924 return 1;
1925 }
1925 }
1926 }
1926 }
1927
1927
1928 typedef uint64_t bitmask;
1928 typedef uint64_t bitmask;
1929
1929
1930 /*
1930 /*
1931 * Given a disjoint set of revs, return all candidates for the
1931 * Given a disjoint set of revs, return all candidates for the
1932 * greatest common ancestor. In revset notation, this is the set
1932 * greatest common ancestor. In revset notation, this is the set
1933 * "heads(::a and ::b and ...)"
1933 * "heads(::a and ::b and ...)"
1934 */
1934 */
1935 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
1935 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
1936 int revcount)
1936 int revcount)
1937 {
1937 {
1938 const bitmask allseen = (1ull << revcount) - 1;
1938 const bitmask allseen = (1ull << revcount) - 1;
1939 const bitmask poison = 1ull << revcount;
1939 const bitmask poison = 1ull << revcount;
1940 PyObject *gca = PyList_New(0);
1940 PyObject *gca = PyList_New(0);
1941 int i, v, interesting;
1941 int i, v, interesting;
1942 int maxrev = -1;
1942 int maxrev = -1;
1943 bitmask sp;
1943 bitmask sp;
1944 bitmask *seen;
1944 bitmask *seen;
1945
1945
1946 if (gca == NULL)
1946 if (gca == NULL)
1947 return PyErr_NoMemory();
1947 return PyErr_NoMemory();
1948
1948
1949 for (i = 0; i < revcount; i++) {
1949 for (i = 0; i < revcount; i++) {
1950 if (revs[i] > maxrev)
1950 if (revs[i] > maxrev)
1951 maxrev = revs[i];
1951 maxrev = revs[i];
1952 }
1952 }
1953
1953
1954 seen = calloc(sizeof(*seen), maxrev + 1);
1954 seen = calloc(sizeof(*seen), maxrev + 1);
1955 if (seen == NULL) {
1955 if (seen == NULL) {
1956 Py_DECREF(gca);
1956 Py_DECREF(gca);
1957 return PyErr_NoMemory();
1957 return PyErr_NoMemory();
1958 }
1958 }
1959
1959
1960 for (i = 0; i < revcount; i++)
1960 for (i = 0; i < revcount; i++)
1961 seen[revs[i]] = 1ull << i;
1961 seen[revs[i]] = 1ull << i;
1962
1962
1963 interesting = revcount;
1963 interesting = revcount;
1964
1964
1965 for (v = maxrev; v >= 0 && interesting; v--) {
1965 for (v = maxrev; v >= 0 && interesting; v--) {
1966 bitmask sv = seen[v];
1966 bitmask sv = seen[v];
1967 int parents[2];
1967 int parents[2];
1968
1968
1969 if (!sv)
1969 if (!sv)
1970 continue;
1970 continue;
1971
1971
1972 if (sv < poison) {
1972 if (sv < poison) {
1973 interesting -= 1;
1973 interesting -= 1;
1974 if (sv == allseen) {
1974 if (sv == allseen) {
1975 PyObject *obj = PyInt_FromLong(v);
1975 PyObject *obj = PyInt_FromLong(v);
1976 if (obj == NULL)
1976 if (obj == NULL)
1977 goto bail;
1977 goto bail;
1978 if (PyList_Append(gca, obj) == -1) {
1978 if (PyList_Append(gca, obj) == -1) {
1979 Py_DECREF(obj);
1979 Py_DECREF(obj);
1980 goto bail;
1980 goto bail;
1981 }
1981 }
1982 sv |= poison;
1982 sv |= poison;
1983 for (i = 0; i < revcount; i++) {
1983 for (i = 0; i < revcount; i++) {
1984 if (revs[i] == v)
1984 if (revs[i] == v)
1985 goto done;
1985 goto done;
1986 }
1986 }
1987 }
1987 }
1988 }
1988 }
1989 if (index_get_parents(self, v, parents, maxrev) < 0)
1989 if (index_get_parents(self, v, parents, maxrev) < 0)
1990 goto bail;
1990 goto bail;
1991
1991
1992 for (i = 0; i < 2; i++) {
1992 for (i = 0; i < 2; i++) {
1993 int p = parents[i];
1993 int p = parents[i];
1994 if (p == -1)
1994 if (p == -1)
1995 continue;
1995 continue;
1996 sp = seen[p];
1996 sp = seen[p];
1997 if (sv < poison) {
1997 if (sv < poison) {
1998 if (sp == 0) {
1998 if (sp == 0) {
1999 seen[p] = sv;
1999 seen[p] = sv;
2000 interesting++;
2000 interesting++;
2001 } else if (sp != sv)
2001 } else if (sp != sv)
2002 seen[p] |= sv;
2002 seen[p] |= sv;
2003 } else {
2003 } else {
2004 if (sp && sp < poison)
2004 if (sp && sp < poison)
2005 interesting--;
2005 interesting--;
2006 seen[p] = sv;
2006 seen[p] = sv;
2007 }
2007 }
2008 }
2008 }
2009 }
2009 }
2010
2010
2011 done:
2011 done:
2012 free(seen);
2012 free(seen);
2013 return gca;
2013 return gca;
2014 bail:
2014 bail:
2015 free(seen);
2015 free(seen);
2016 Py_XDECREF(gca);
2016 Py_XDECREF(gca);
2017 return NULL;
2017 return NULL;
2018 }
2018 }
2019
2019
2020 /*
2020 /*
2021 * Given a disjoint set of revs, return the subset with the longest
2021 * Given a disjoint set of revs, return the subset with the longest
2022 * path to the root.
2022 * path to the root.
2023 */
2023 */
2024 static PyObject *find_deepest(indexObject *self, PyObject *revs)
2024 static PyObject *find_deepest(indexObject *self, PyObject *revs)
2025 {
2025 {
2026 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
2026 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
2027 static const Py_ssize_t capacity = 24;
2027 static const Py_ssize_t capacity = 24;
2028 int *depth, *interesting = NULL;
2028 int *depth, *interesting = NULL;
2029 int i, j, v, ninteresting;
2029 int i, j, v, ninteresting;
2030 PyObject *dict = NULL, *keys = NULL;
2030 PyObject *dict = NULL, *keys = NULL;
2031 long *seen = NULL;
2031 long *seen = NULL;
2032 int maxrev = -1;
2032 int maxrev = -1;
2033 long final;
2033 long final;
2034
2034
2035 if (revcount > capacity) {
2035 if (revcount > capacity) {
2036 PyErr_Format(PyExc_OverflowError,
2036 PyErr_Format(PyExc_OverflowError,
2037 "bitset size (%ld) > capacity (%ld)",
2037 "bitset size (%ld) > capacity (%ld)",
2038 (long)revcount, (long)capacity);
2038 (long)revcount, (long)capacity);
2039 return NULL;
2039 return NULL;
2040 }
2040 }
2041
2041
2042 for (i = 0; i < revcount; i++) {
2042 for (i = 0; i < revcount; i++) {
2043 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
2043 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
2044 if (n > maxrev)
2044 if (n > maxrev)
2045 maxrev = n;
2045 maxrev = n;
2046 }
2046 }
2047
2047
2048 depth = calloc(sizeof(*depth), maxrev + 1);
2048 depth = calloc(sizeof(*depth), maxrev + 1);
2049 if (depth == NULL)
2049 if (depth == NULL)
2050 return PyErr_NoMemory();
2050 return PyErr_NoMemory();
2051
2051
2052 seen = calloc(sizeof(*seen), maxrev + 1);
2052 seen = calloc(sizeof(*seen), maxrev + 1);
2053 if (seen == NULL) {
2053 if (seen == NULL) {
2054 PyErr_NoMemory();
2054 PyErr_NoMemory();
2055 goto bail;
2055 goto bail;
2056 }
2056 }
2057
2057
2058 interesting = calloc(sizeof(*interesting), ((size_t)1) << revcount);
2058 interesting = calloc(sizeof(*interesting), ((size_t)1) << revcount);
2059 if (interesting == NULL) {
2059 if (interesting == NULL) {
2060 PyErr_NoMemory();
2060 PyErr_NoMemory();
2061 goto bail;
2061 goto bail;
2062 }
2062 }
2063
2063
2064 if (PyList_Sort(revs) == -1)
2064 if (PyList_Sort(revs) == -1)
2065 goto bail;
2065 goto bail;
2066
2066
2067 for (i = 0; i < revcount; i++) {
2067 for (i = 0; i < revcount; i++) {
2068 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
2068 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
2069 long b = 1l << i;
2069 long b = 1l << i;
2070 depth[n] = 1;
2070 depth[n] = 1;
2071 seen[n] = b;
2071 seen[n] = b;
2072 interesting[b] = 1;
2072 interesting[b] = 1;
2073 }
2073 }
2074
2074
2075 /* invariant: ninteresting is the number of non-zero entries in
2075 /* invariant: ninteresting is the number of non-zero entries in
2076 * interesting. */
2076 * interesting. */
2077 ninteresting = (int)revcount;
2077 ninteresting = (int)revcount;
2078
2078
2079 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
2079 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
2080 int dv = depth[v];
2080 int dv = depth[v];
2081 int parents[2];
2081 int parents[2];
2082 long sv;
2082 long sv;
2083
2083
2084 if (dv == 0)
2084 if (dv == 0)
2085 continue;
2085 continue;
2086
2086
2087 sv = seen[v];
2087 sv = seen[v];
2088 if (index_get_parents(self, v, parents, maxrev) < 0)
2088 if (index_get_parents(self, v, parents, maxrev) < 0)
2089 goto bail;
2089 goto bail;
2090
2090
2091 for (i = 0; i < 2; i++) {
2091 for (i = 0; i < 2; i++) {
2092 int p = parents[i];
2092 int p = parents[i];
2093 long sp;
2093 long sp;
2094 int dp;
2094 int dp;
2095
2095
2096 if (p == -1)
2096 if (p == -1)
2097 continue;
2097 continue;
2098
2098
2099 dp = depth[p];
2099 dp = depth[p];
2100 sp = seen[p];
2100 sp = seen[p];
2101 if (dp <= dv) {
2101 if (dp <= dv) {
2102 depth[p] = dv + 1;
2102 depth[p] = dv + 1;
2103 if (sp != sv) {
2103 if (sp != sv) {
2104 interesting[sv] += 1;
2104 interesting[sv] += 1;
2105 seen[p] = sv;
2105 seen[p] = sv;
2106 if (sp) {
2106 if (sp) {
2107 interesting[sp] -= 1;
2107 interesting[sp] -= 1;
2108 if (interesting[sp] == 0)
2108 if (interesting[sp] == 0)
2109 ninteresting -= 1;
2109 ninteresting -= 1;
2110 }
2110 }
2111 }
2111 }
2112 } else if (dv == dp - 1) {
2112 } else if (dv == dp - 1) {
2113 long nsp = sp | sv;
2113 long nsp = sp | sv;
2114 if (nsp == sp)
2114 if (nsp == sp)
2115 continue;
2115 continue;
2116 seen[p] = nsp;
2116 seen[p] = nsp;
2117 interesting[sp] -= 1;
2117 interesting[sp] -= 1;
2118 if (interesting[sp] == 0)
2118 if (interesting[sp] == 0)
2119 ninteresting -= 1;
2119 ninteresting -= 1;
2120 if (interesting[nsp] == 0)
2120 if (interesting[nsp] == 0)
2121 ninteresting += 1;
2121 ninteresting += 1;
2122 interesting[nsp] += 1;
2122 interesting[nsp] += 1;
2123 }
2123 }
2124 }
2124 }
2125 interesting[sv] -= 1;
2125 interesting[sv] -= 1;
2126 if (interesting[sv] == 0)
2126 if (interesting[sv] == 0)
2127 ninteresting -= 1;
2127 ninteresting -= 1;
2128 }
2128 }
2129
2129
2130 final = 0;
2130 final = 0;
2131 j = ninteresting;
2131 j = ninteresting;
2132 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
2132 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
2133 if (interesting[i] == 0)
2133 if (interesting[i] == 0)
2134 continue;
2134 continue;
2135 final |= i;
2135 final |= i;
2136 j -= 1;
2136 j -= 1;
2137 }
2137 }
2138 if (final == 0) {
2138 if (final == 0) {
2139 keys = PyList_New(0);
2139 keys = PyList_New(0);
2140 goto bail;
2140 goto bail;
2141 }
2141 }
2142
2142
2143 dict = PyDict_New();
2143 dict = PyDict_New();
2144 if (dict == NULL)
2144 if (dict == NULL)
2145 goto bail;
2145 goto bail;
2146
2146
2147 for (i = 0; i < revcount; i++) {
2147 for (i = 0; i < revcount; i++) {
2148 PyObject *key;
2148 PyObject *key;
2149
2149
2150 if ((final & (1 << i)) == 0)
2150 if ((final & (1 << i)) == 0)
2151 continue;
2151 continue;
2152
2152
2153 key = PyList_GET_ITEM(revs, i);
2153 key = PyList_GET_ITEM(revs, i);
2154 Py_INCREF(key);
2154 Py_INCREF(key);
2155 Py_INCREF(Py_None);
2155 Py_INCREF(Py_None);
2156 if (PyDict_SetItem(dict, key, Py_None) == -1) {
2156 if (PyDict_SetItem(dict, key, Py_None) == -1) {
2157 Py_DECREF(key);
2157 Py_DECREF(key);
2158 Py_DECREF(Py_None);
2158 Py_DECREF(Py_None);
2159 goto bail;
2159 goto bail;
2160 }
2160 }
2161 }
2161 }
2162
2162
2163 keys = PyDict_Keys(dict);
2163 keys = PyDict_Keys(dict);
2164
2164
2165 bail:
2165 bail:
2166 free(depth);
2166 free(depth);
2167 free(seen);
2167 free(seen);
2168 free(interesting);
2168 free(interesting);
2169 Py_XDECREF(dict);
2169 Py_XDECREF(dict);
2170
2170
2171 return keys;
2171 return keys;
2172 }
2172 }
2173
2173
2174 /*
2174 /*
2175 * Given a (possibly overlapping) set of revs, return all the
2175 * Given a (possibly overlapping) set of revs, return all the
2176 * common ancestors heads: heads(::args[0] and ::a[1] and ...)
2176 * common ancestors heads: heads(::args[0] and ::a[1] and ...)
2177 */
2177 */
2178 static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args)
2178 static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args)
2179 {
2179 {
2180 PyObject *ret = NULL;
2180 PyObject *ret = NULL;
2181 Py_ssize_t argcount, i, len;
2181 Py_ssize_t argcount, i, len;
2182 bitmask repeat = 0;
2182 bitmask repeat = 0;
2183 int revcount = 0;
2183 int revcount = 0;
2184 int *revs;
2184 int *revs;
2185
2185
2186 argcount = PySequence_Length(args);
2186 argcount = PySequence_Length(args);
2187 revs = PyMem_Malloc(argcount * sizeof(*revs));
2187 revs = PyMem_Malloc(argcount * sizeof(*revs));
2188 if (argcount > 0 && revs == NULL)
2188 if (argcount > 0 && revs == NULL)
2189 return PyErr_NoMemory();
2189 return PyErr_NoMemory();
2190 len = index_length(self);
2190 len = index_length(self);
2191
2191
2192 for (i = 0; i < argcount; i++) {
2192 for (i = 0; i < argcount; i++) {
2193 static const int capacity = 24;
2193 static const int capacity = 24;
2194 PyObject *obj = PySequence_GetItem(args, i);
2194 PyObject *obj = PySequence_GetItem(args, i);
2195 bitmask x;
2195 bitmask x;
2196 long val;
2196 long val;
2197
2197
2198 if (!PyInt_Check(obj)) {
2198 if (!PyInt_Check(obj)) {
2199 PyErr_SetString(PyExc_TypeError,
2199 PyErr_SetString(PyExc_TypeError,
2200 "arguments must all be ints");
2200 "arguments must all be ints");
2201 Py_DECREF(obj);
2201 Py_DECREF(obj);
2202 goto bail;
2202 goto bail;
2203 }
2203 }
2204 val = PyInt_AsLong(obj);
2204 val = PyInt_AsLong(obj);
2205 Py_DECREF(obj);
2205 Py_DECREF(obj);
2206 if (val == -1) {
2206 if (val == -1) {
2207 ret = PyList_New(0);
2207 ret = PyList_New(0);
2208 goto done;
2208 goto done;
2209 }
2209 }
2210 if (val < 0 || val >= len) {
2210 if (val < 0 || val >= len) {
2211 PyErr_SetString(PyExc_IndexError, "index out of range");
2211 PyErr_SetString(PyExc_IndexError, "index out of range");
2212 goto bail;
2212 goto bail;
2213 }
2213 }
2214 /* this cheesy bloom filter lets us avoid some more
2214 /* this cheesy bloom filter lets us avoid some more
2215 * expensive duplicate checks in the common set-is-disjoint
2215 * expensive duplicate checks in the common set-is-disjoint
2216 * case */
2216 * case */
2217 x = 1ull << (val & 0x3f);
2217 x = 1ull << (val & 0x3f);
2218 if (repeat & x) {
2218 if (repeat & x) {
2219 int k;
2219 int k;
2220 for (k = 0; k < revcount; k++) {
2220 for (k = 0; k < revcount; k++) {
2221 if (val == revs[k])
2221 if (val == revs[k])
2222 goto duplicate;
2222 goto duplicate;
2223 }
2223 }
2224 } else
2224 } else
2225 repeat |= x;
2225 repeat |= x;
2226 if (revcount >= capacity) {
2226 if (revcount >= capacity) {
2227 PyErr_Format(PyExc_OverflowError,
2227 PyErr_Format(PyExc_OverflowError,
2228 "bitset size (%d) > capacity (%d)",
2228 "bitset size (%d) > capacity (%d)",
2229 revcount, capacity);
2229 revcount, capacity);
2230 goto bail;
2230 goto bail;
2231 }
2231 }
2232 revs[revcount++] = (int)val;
2232 revs[revcount++] = (int)val;
2233 duplicate:;
2233 duplicate:;
2234 }
2234 }
2235
2235
2236 if (revcount == 0) {
2236 if (revcount == 0) {
2237 ret = PyList_New(0);
2237 ret = PyList_New(0);
2238 goto done;
2238 goto done;
2239 }
2239 }
2240 if (revcount == 1) {
2240 if (revcount == 1) {
2241 PyObject *obj;
2241 PyObject *obj;
2242 ret = PyList_New(1);
2242 ret = PyList_New(1);
2243 if (ret == NULL)
2243 if (ret == NULL)
2244 goto bail;
2244 goto bail;
2245 obj = PyInt_FromLong(revs[0]);
2245 obj = PyInt_FromLong(revs[0]);
2246 if (obj == NULL)
2246 if (obj == NULL)
2247 goto bail;
2247 goto bail;
2248 PyList_SET_ITEM(ret, 0, obj);
2248 PyList_SET_ITEM(ret, 0, obj);
2249 goto done;
2249 goto done;
2250 }
2250 }
2251
2251
2252 ret = find_gca_candidates(self, revs, revcount);
2252 ret = find_gca_candidates(self, revs, revcount);
2253 if (ret == NULL)
2253 if (ret == NULL)
2254 goto bail;
2254 goto bail;
2255
2255
2256 done:
2256 done:
2257 PyMem_Free(revs);
2257 PyMem_Free(revs);
2258 return ret;
2258 return ret;
2259
2259
2260 bail:
2260 bail:
2261 PyMem_Free(revs);
2261 PyMem_Free(revs);
2262 Py_XDECREF(ret);
2262 Py_XDECREF(ret);
2263 return NULL;
2263 return NULL;
2264 }
2264 }
2265
2265
2266 /*
2266 /*
2267 * Given a (possibly overlapping) set of revs, return the greatest
2267 * Given a (possibly overlapping) set of revs, return the greatest
2268 * common ancestors: those with the longest path to the root.
2268 * common ancestors: those with the longest path to the root.
2269 */
2269 */
2270 static PyObject *index_ancestors(indexObject *self, PyObject *args)
2270 static PyObject *index_ancestors(indexObject *self, PyObject *args)
2271 {
2271 {
2272 PyObject *ret;
2272 PyObject *ret;
2273 PyObject *gca = index_commonancestorsheads(self, args);
2273 PyObject *gca = index_commonancestorsheads(self, args);
2274 if (gca == NULL)
2274 if (gca == NULL)
2275 return NULL;
2275 return NULL;
2276
2276
2277 if (PyList_GET_SIZE(gca) <= 1) {
2277 if (PyList_GET_SIZE(gca) <= 1) {
2278 return gca;
2278 return gca;
2279 }
2279 }
2280
2280
2281 ret = find_deepest(self, gca);
2281 ret = find_deepest(self, gca);
2282 Py_DECREF(gca);
2282 Py_DECREF(gca);
2283 return ret;
2283 return ret;
2284 }
2284 }
2285
2285
2286 /*
2286 /*
2287 * Invalidate any trie entries introduced by added revs.
2287 * Invalidate any trie entries introduced by added revs.
2288 */
2288 */
2289 static void index_invalidate_added(indexObject *self, Py_ssize_t start)
2289 static void index_invalidate_added(indexObject *self, Py_ssize_t start)
2290 {
2290 {
2291 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
2291 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
2292
2292
2293 for (i = start; i < len; i++) {
2293 for (i = start; i < len; i++) {
2294 PyObject *tuple = PyList_GET_ITEM(self->added, i);
2294 PyObject *tuple = PyList_GET_ITEM(self->added, i);
2295 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
2295 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
2296
2296
2297 nt_delete_node(&self->nt, PyBytes_AS_STRING(node));
2297 nt_delete_node(&self->nt, PyBytes_AS_STRING(node));
2298 }
2298 }
2299
2299
2300 if (start == 0)
2300 if (start == 0)
2301 Py_CLEAR(self->added);
2301 Py_CLEAR(self->added);
2302 }
2302 }
2303
2303
2304 /*
2304 /*
2305 * Delete a numeric range of revs, which must be at the end of the
2305 * Delete a numeric range of revs, which must be at the end of the
2306 * range, but exclude the sentinel nullid entry.
2306 * range, but exclude the sentinel nullid entry.
2307 */
2307 */
2308 static int index_slice_del(indexObject *self, PyObject *item)
2308 static int index_slice_del(indexObject *self, PyObject *item)
2309 {
2309 {
2310 Py_ssize_t start, stop, step, slicelength;
2310 Py_ssize_t start, stop, step, slicelength;
2311 Py_ssize_t length = index_length(self) + 1;
2311 Py_ssize_t length = index_length(self) + 1;
2312 int ret = 0;
2312 int ret = 0;
2313
2313
2314 /* Argument changed from PySliceObject* to PyObject* in Python 3. */
2314 /* Argument changed from PySliceObject* to PyObject* in Python 3. */
2315 #ifdef IS_PY3K
2315 #ifdef IS_PY3K
2316 if (PySlice_GetIndicesEx(item, length, &start, &stop, &step,
2316 if (PySlice_GetIndicesEx(item, length, &start, &stop, &step,
2317 &slicelength) < 0)
2317 &slicelength) < 0)
2318 #else
2318 #else
2319 if (PySlice_GetIndicesEx((PySliceObject *)item, length, &start, &stop,
2319 if (PySlice_GetIndicesEx((PySliceObject *)item, length, &start, &stop,
2320 &step, &slicelength) < 0)
2320 &step, &slicelength) < 0)
2321 #endif
2321 #endif
2322 return -1;
2322 return -1;
2323
2323
2324 if (slicelength <= 0)
2324 if (slicelength <= 0)
2325 return 0;
2325 return 0;
2326
2326
2327 if ((step < 0 && start < stop) || (step > 0 && start > stop))
2327 if ((step < 0 && start < stop) || (step > 0 && start > stop))
2328 stop = start;
2328 stop = start;
2329
2329
2330 if (step < 0) {
2330 if (step < 0) {
2331 stop = start + 1;
2331 stop = start + 1;
2332 start = stop + step * (slicelength - 1) - 1;
2332 start = stop + step * (slicelength - 1) - 1;
2333 step = -step;
2333 step = -step;
2334 }
2334 }
2335
2335
2336 if (step != 1) {
2336 if (step != 1) {
2337 PyErr_SetString(PyExc_ValueError,
2337 PyErr_SetString(PyExc_ValueError,
2338 "revlog index delete requires step size of 1");
2338 "revlog index delete requires step size of 1");
2339 return -1;
2339 return -1;
2340 }
2340 }
2341
2341
2342 if (stop != length - 1) {
2342 if (stop != length - 1) {
2343 PyErr_SetString(PyExc_IndexError,
2343 PyErr_SetString(PyExc_IndexError,
2344 "revlog index deletion indices are invalid");
2344 "revlog index deletion indices are invalid");
2345 return -1;
2345 return -1;
2346 }
2346 }
2347
2347
2348 if (start < self->length) {
2348 if (start < self->length) {
2349 if (self->ntinitialized) {
2349 if (self->ntinitialized) {
2350 Py_ssize_t i;
2350 Py_ssize_t i;
2351
2351
2352 for (i = start + 1; i < self->length; i++) {
2352 for (i = start + 1; i < self->length; i++) {
2353 const char *node = index_node_existing(self, i);
2353 const char *node = index_node_existing(self, i);
2354 if (node == NULL)
2354 if (node == NULL)
2355 return -1;
2355 return -1;
2356
2356
2357 nt_delete_node(&self->nt, node);
2357 nt_delete_node(&self->nt, node);
2358 }
2358 }
2359 if (self->added)
2359 if (self->added)
2360 index_invalidate_added(self, 0);
2360 index_invalidate_added(self, 0);
2361 if (self->ntrev > start)
2361 if (self->ntrev > start)
2362 self->ntrev = (int)start;
2362 self->ntrev = (int)start;
2363 }
2363 }
2364 self->length = start;
2364 self->length = start;
2365 if (start < self->raw_length) {
2365 if (start < self->raw_length) {
2366 if (self->cache) {
2366 if (self->cache) {
2367 Py_ssize_t i;
2367 Py_ssize_t i;
2368 for (i = start; i < self->raw_length; i++)
2368 for (i = start; i < self->raw_length; i++)
2369 Py_CLEAR(self->cache[i]);
2369 Py_CLEAR(self->cache[i]);
2370 }
2370 }
2371 self->raw_length = start;
2371 self->raw_length = start;
2372 }
2372 }
2373 goto done;
2373 goto done;
2374 }
2374 }
2375
2375
2376 if (self->ntinitialized) {
2376 if (self->ntinitialized) {
2377 index_invalidate_added(self, start - self->length);
2377 index_invalidate_added(self, start - self->length);
2378 if (self->ntrev > start)
2378 if (self->ntrev > start)
2379 self->ntrev = (int)start;
2379 self->ntrev = (int)start;
2380 }
2380 }
2381 if (self->added)
2381 if (self->added)
2382 ret = PyList_SetSlice(self->added, start - self->length,
2382 ret = PyList_SetSlice(self->added, start - self->length,
2383 PyList_GET_SIZE(self->added), NULL);
2383 PyList_GET_SIZE(self->added), NULL);
2384 done:
2384 done:
2385 Py_CLEAR(self->headrevs);
2385 Py_CLEAR(self->headrevs);
2386 return ret;
2386 return ret;
2387 }
2387 }
2388
2388
2389 /*
2389 /*
2390 * Supported ops:
2390 * Supported ops:
2391 *
2391 *
2392 * slice deletion
2392 * slice deletion
2393 * string assignment (extend node->rev mapping)
2393 * string assignment (extend node->rev mapping)
2394 * string deletion (shrink node->rev mapping)
2394 * string deletion (shrink node->rev mapping)
2395 */
2395 */
2396 static int index_assign_subscript(indexObject *self, PyObject *item,
2396 static int index_assign_subscript(indexObject *self, PyObject *item,
2397 PyObject *value)
2397 PyObject *value)
2398 {
2398 {
2399 char *node;
2399 char *node;
2400 long rev;
2400 long rev;
2401
2401
2402 if (PySlice_Check(item) && value == NULL)
2402 if (PySlice_Check(item) && value == NULL)
2403 return index_slice_del(self, item);
2403 return index_slice_del(self, item);
2404
2404
2405 if (node_check(item, &node) == -1)
2405 if (node_check(item, &node) == -1)
2406 return -1;
2406 return -1;
2407
2407
2408 if (value == NULL)
2408 if (value == NULL)
2409 return self->ntinitialized ? nt_delete_node(&self->nt, node)
2409 return self->ntinitialized ? nt_delete_node(&self->nt, node)
2410 : 0;
2410 : 0;
2411 rev = PyInt_AsLong(value);
2411 rev = PyInt_AsLong(value);
2412 if (rev > INT_MAX || rev < 0) {
2412 if (rev > INT_MAX || rev < 0) {
2413 if (!PyErr_Occurred())
2413 if (!PyErr_Occurred())
2414 PyErr_SetString(PyExc_ValueError, "rev out of range");
2414 PyErr_SetString(PyExc_ValueError, "rev out of range");
2415 return -1;
2415 return -1;
2416 }
2416 }
2417
2417
2418 if (index_init_nt(self) == -1)
2418 if (index_init_nt(self) == -1)
2419 return -1;
2419 return -1;
2420 return nt_insert(&self->nt, node, (int)rev);
2420 return nt_insert(&self->nt, node, (int)rev);
2421 }
2421 }
2422
2422
2423 /*
2423 /*
2424 * Find all RevlogNG entries in an index that has inline data. Update
2424 * Find all RevlogNG entries in an index that has inline data. Update
2425 * the optional "offsets" table with those entries.
2425 * the optional "offsets" table with those entries.
2426 */
2426 */
2427 static Py_ssize_t inline_scan(indexObject *self, const char **offsets)
2427 static Py_ssize_t inline_scan(indexObject *self, const char **offsets)
2428 {
2428 {
2429 const char *data = (const char *)self->buf.buf;
2429 const char *data = (const char *)self->buf.buf;
2430 Py_ssize_t pos = 0;
2430 Py_ssize_t pos = 0;
2431 Py_ssize_t end = self->buf.len;
2431 Py_ssize_t end = self->buf.len;
2432 long incr = v1_hdrsize;
2432 long incr = v1_hdrsize;
2433 Py_ssize_t len = 0;
2433 Py_ssize_t len = 0;
2434
2434
2435 while (pos + v1_hdrsize <= end && pos >= 0) {
2435 while (pos + v1_hdrsize <= end && pos >= 0) {
2436 uint32_t comp_len;
2436 uint32_t comp_len;
2437 /* 3rd element of header is length of compressed inline data */
2437 /* 3rd element of header is length of compressed inline data */
2438 comp_len = getbe32(data + pos + 8);
2438 comp_len = getbe32(data + pos + 8);
2439 incr = v1_hdrsize + comp_len;
2439 incr = v1_hdrsize + comp_len;
2440 if (offsets)
2440 if (offsets)
2441 offsets[len] = data + pos;
2441 offsets[len] = data + pos;
2442 len++;
2442 len++;
2443 pos += incr;
2443 pos += incr;
2444 }
2444 }
2445
2445
2446 if (pos != end) {
2446 if (pos != end) {
2447 if (!PyErr_Occurred())
2447 if (!PyErr_Occurred())
2448 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2448 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2449 return -1;
2449 return -1;
2450 }
2450 }
2451
2451
2452 return len;
2452 return len;
2453 }
2453 }
2454
2454
2455 static int index_init(indexObject *self, PyObject *args)
2455 static int index_init(indexObject *self, PyObject *args)
2456 {
2456 {
2457 PyObject *data_obj, *inlined_obj;
2457 PyObject *data_obj, *inlined_obj;
2458 Py_ssize_t size;
2458 Py_ssize_t size;
2459
2459
2460 /* Initialize before argument-checking to avoid index_dealloc() crash.
2460 /* Initialize before argument-checking to avoid index_dealloc() crash.
2461 */
2461 */
2462 self->raw_length = 0;
2462 self->raw_length = 0;
2463 self->added = NULL;
2463 self->added = NULL;
2464 self->cache = NULL;
2464 self->cache = NULL;
2465 self->data = NULL;
2465 self->data = NULL;
2466 memset(&self->buf, 0, sizeof(self->buf));
2466 memset(&self->buf, 0, sizeof(self->buf));
2467 self->headrevs = NULL;
2467 self->headrevs = NULL;
2468 self->filteredrevs = Py_None;
2468 self->filteredrevs = Py_None;
2469 Py_INCREF(Py_None);
2469 Py_INCREF(Py_None);
2470 self->ntinitialized = 0;
2470 self->ntinitialized = 0;
2471 self->offsets = NULL;
2471 self->offsets = NULL;
2472
2472
2473 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
2473 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
2474 return -1;
2474 return -1;
2475 if (!PyObject_CheckBuffer(data_obj)) {
2475 if (!PyObject_CheckBuffer(data_obj)) {
2476 PyErr_SetString(PyExc_TypeError,
2476 PyErr_SetString(PyExc_TypeError,
2477 "data does not support buffer interface");
2477 "data does not support buffer interface");
2478 return -1;
2478 return -1;
2479 }
2479 }
2480
2480
2481 if (PyObject_GetBuffer(data_obj, &self->buf, PyBUF_SIMPLE) == -1)
2481 if (PyObject_GetBuffer(data_obj, &self->buf, PyBUF_SIMPLE) == -1)
2482 return -1;
2482 return -1;
2483 size = self->buf.len;
2483 size = self->buf.len;
2484
2484
2485 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
2485 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
2486 self->data = data_obj;
2486 self->data = data_obj;
2487
2487
2488 self->ntlookups = self->ntmisses = 0;
2488 self->ntlookups = self->ntmisses = 0;
2489 self->ntrev = -1;
2489 self->ntrev = -1;
2490 Py_INCREF(self->data);
2490 Py_INCREF(self->data);
2491
2491
2492 if (self->inlined) {
2492 if (self->inlined) {
2493 Py_ssize_t len = inline_scan(self, NULL);
2493 Py_ssize_t len = inline_scan(self, NULL);
2494 if (len == -1)
2494 if (len == -1)
2495 goto bail;
2495 goto bail;
2496 self->raw_length = len;
2496 self->raw_length = len;
2497 self->length = len;
2497 self->length = len;
2498 } else {
2498 } else {
2499 if (size % v1_hdrsize) {
2499 if (size % v1_hdrsize) {
2500 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2500 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2501 goto bail;
2501 goto bail;
2502 }
2502 }
2503 self->raw_length = size / v1_hdrsize;
2503 self->raw_length = size / v1_hdrsize;
2504 self->length = self->raw_length;
2504 self->length = self->raw_length;
2505 }
2505 }
2506
2506
2507 return 0;
2507 return 0;
2508 bail:
2508 bail:
2509 return -1;
2509 return -1;
2510 }
2510 }
2511
2511
2512 static PyObject *index_nodemap(indexObject *self)
2512 static PyObject *index_nodemap(indexObject *self)
2513 {
2513 {
2514 Py_INCREF(self);
2514 Py_INCREF(self);
2515 return (PyObject *)self;
2515 return (PyObject *)self;
2516 }
2516 }
2517
2517
2518 static void _index_clearcaches(indexObject *self)
2518 static void _index_clearcaches(indexObject *self)
2519 {
2519 {
2520 if (self->cache) {
2520 if (self->cache) {
2521 Py_ssize_t i;
2521 Py_ssize_t i;
2522
2522
2523 for (i = 0; i < self->raw_length; i++)
2523 for (i = 0; i < self->raw_length; i++)
2524 Py_CLEAR(self->cache[i]);
2524 Py_CLEAR(self->cache[i]);
2525 free(self->cache);
2525 free(self->cache);
2526 self->cache = NULL;
2526 self->cache = NULL;
2527 }
2527 }
2528 if (self->offsets) {
2528 if (self->offsets) {
2529 PyMem_Free((void *)self->offsets);
2529 PyMem_Free((void *)self->offsets);
2530 self->offsets = NULL;
2530 self->offsets = NULL;
2531 }
2531 }
2532 if (self->ntinitialized) {
2532 if (self->ntinitialized) {
2533 nt_dealloc(&self->nt);
2533 nt_dealloc(&self->nt);
2534 }
2534 }
2535 self->ntinitialized = 0;
2535 self->ntinitialized = 0;
2536 Py_CLEAR(self->headrevs);
2536 Py_CLEAR(self->headrevs);
2537 }
2537 }
2538
2538
2539 static PyObject *index_clearcaches(indexObject *self)
2539 static PyObject *index_clearcaches(indexObject *self)
2540 {
2540 {
2541 _index_clearcaches(self);
2541 _index_clearcaches(self);
2542 self->ntrev = -1;
2542 self->ntrev = -1;
2543 self->ntlookups = self->ntmisses = 0;
2543 self->ntlookups = self->ntmisses = 0;
2544 Py_RETURN_NONE;
2544 Py_RETURN_NONE;
2545 }
2545 }
2546
2546
2547 static void index_dealloc(indexObject *self)
2547 static void index_dealloc(indexObject *self)
2548 {
2548 {
2549 _index_clearcaches(self);
2549 _index_clearcaches(self);
2550 Py_XDECREF(self->filteredrevs);
2550 Py_XDECREF(self->filteredrevs);
2551 if (self->buf.buf) {
2551 if (self->buf.buf) {
2552 PyBuffer_Release(&self->buf);
2552 PyBuffer_Release(&self->buf);
2553 memset(&self->buf, 0, sizeof(self->buf));
2553 memset(&self->buf, 0, sizeof(self->buf));
2554 }
2554 }
2555 Py_XDECREF(self->data);
2555 Py_XDECREF(self->data);
2556 Py_XDECREF(self->added);
2556 Py_XDECREF(self->added);
2557 PyObject_Del(self);
2557 PyObject_Del(self);
2558 }
2558 }
2559
2559
2560 static PySequenceMethods index_sequence_methods = {
2560 static PySequenceMethods index_sequence_methods = {
2561 (lenfunc)index_length, /* sq_length */
2561 (lenfunc)index_length, /* sq_length */
2562 0, /* sq_concat */
2562 0, /* sq_concat */
2563 0, /* sq_repeat */
2563 0, /* sq_repeat */
2564 (ssizeargfunc)index_get, /* sq_item */
2564 (ssizeargfunc)index_get, /* sq_item */
2565 0, /* sq_slice */
2565 0, /* sq_slice */
2566 0, /* sq_ass_item */
2566 0, /* sq_ass_item */
2567 0, /* sq_ass_slice */
2567 0, /* sq_ass_slice */
2568 (objobjproc)index_contains, /* sq_contains */
2568 (objobjproc)index_contains, /* sq_contains */
2569 };
2569 };
2570
2570
2571 static PyMappingMethods index_mapping_methods = {
2571 static PyMappingMethods index_mapping_methods = {
2572 (lenfunc)index_length, /* mp_length */
2572 (lenfunc)index_length, /* mp_length */
2573 (binaryfunc)index_getitem, /* mp_subscript */
2573 (binaryfunc)index_getitem, /* mp_subscript */
2574 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
2574 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
2575 };
2575 };
2576
2576
2577 static PyMethodDef index_methods[] = {
2577 static PyMethodDef index_methods[] = {
2578 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
2578 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
2579 "return the gca set of the given revs"},
2579 "return the gca set of the given revs"},
2580 {"commonancestorsheads", (PyCFunction)index_commonancestorsheads,
2580 {"commonancestorsheads", (PyCFunction)index_commonancestorsheads,
2581 METH_VARARGS,
2581 METH_VARARGS,
2582 "return the heads of the common ancestors of the given revs"},
2582 "return the heads of the common ancestors of the given revs"},
2583 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
2583 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
2584 "clear the index caches"},
2584 "clear the index caches"},
2585 {"get", (PyCFunction)index_m_get, METH_VARARGS, "get an index entry"},
2585 {"get", (PyCFunction)index_m_get, METH_VARARGS, "get an index entry"},
2586 {"computephasesmapsets", (PyCFunction)compute_phases_map_sets, METH_VARARGS,
2586 {"computephasesmapsets", (PyCFunction)compute_phases_map_sets, METH_VARARGS,
2587 "compute phases"},
2587 "compute phases"},
2588 {"reachableroots2", (PyCFunction)reachableroots2, METH_VARARGS,
2588 {"reachableroots2", (PyCFunction)reachableroots2, METH_VARARGS,
2589 "reachableroots"},
2589 "reachableroots"},
2590 {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS,
2590 {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS,
2591 "get head revisions"}, /* Can do filtering since 3.2 */
2591 "get head revisions"}, /* Can do filtering since 3.2 */
2592 {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS,
2592 {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS,
2593 "get filtered head revisions"}, /* Can always do filtering */
2593 "get filtered head revisions"}, /* Can always do filtering */
2594 {"deltachain", (PyCFunction)index_deltachain, METH_VARARGS,
2594 {"deltachain", (PyCFunction)index_deltachain, METH_VARARGS,
2595 "determine revisions with deltas to reconstruct fulltext"},
2595 "determine revisions with deltas to reconstruct fulltext"},
2596 {"slicechunktodensity", (PyCFunction)index_slicechunktodensity,
2596 {"slicechunktodensity", (PyCFunction)index_slicechunktodensity,
2597 METH_VARARGS, "determine revisions with deltas to reconstruct fulltext"},
2597 METH_VARARGS, "determine revisions with deltas to reconstruct fulltext"},
2598 {"append", (PyCFunction)index_append, METH_O, "append an index entry"},
2598 {"append", (PyCFunction)index_append, METH_O, "append an index entry"},
2599 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
2599 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
2600 "match a potentially ambiguous node ID"},
2600 "match a potentially ambiguous node ID"},
2601 {"shortest", (PyCFunction)index_shortest, METH_VARARGS,
2601 {"shortest", (PyCFunction)index_shortest, METH_VARARGS,
2602 "find length of shortest hex nodeid of a binary ID"},
2602 "find length of shortest hex nodeid of a binary ID"},
2603 {"stats", (PyCFunction)index_stats, METH_NOARGS, "stats for the index"},
2603 {"stats", (PyCFunction)index_stats, METH_NOARGS, "stats for the index"},
2604 {NULL} /* Sentinel */
2604 {NULL} /* Sentinel */
2605 };
2605 };
2606
2606
2607 static PyGetSetDef index_getset[] = {
2607 static PyGetSetDef index_getset[] = {
2608 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
2608 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
2609 {NULL} /* Sentinel */
2609 {NULL} /* Sentinel */
2610 };
2610 };
2611
2611
2612 PyTypeObject HgRevlogIndex_Type = {
2612 PyTypeObject HgRevlogIndex_Type = {
2613 PyVarObject_HEAD_INIT(NULL, 0) /* header */
2613 PyVarObject_HEAD_INIT(NULL, 0) /* header */
2614 "parsers.index", /* tp_name */
2614 "parsers.index", /* tp_name */
2615 sizeof(indexObject), /* tp_basicsize */
2615 sizeof(indexObject), /* tp_basicsize */
2616 0, /* tp_itemsize */
2616 0, /* tp_itemsize */
2617 (destructor)index_dealloc, /* tp_dealloc */
2617 (destructor)index_dealloc, /* tp_dealloc */
2618 0, /* tp_print */
2618 0, /* tp_print */
2619 0, /* tp_getattr */
2619 0, /* tp_getattr */
2620 0, /* tp_setattr */
2620 0, /* tp_setattr */
2621 0, /* tp_compare */
2621 0, /* tp_compare */
2622 0, /* tp_repr */
2622 0, /* tp_repr */
2623 0, /* tp_as_number */
2623 0, /* tp_as_number */
2624 &index_sequence_methods, /* tp_as_sequence */
2624 &index_sequence_methods, /* tp_as_sequence */
2625 &index_mapping_methods, /* tp_as_mapping */
2625 &index_mapping_methods, /* tp_as_mapping */
2626 0, /* tp_hash */
2626 0, /* tp_hash */
2627 0, /* tp_call */
2627 0, /* tp_call */
2628 0, /* tp_str */
2628 0, /* tp_str */
2629 0, /* tp_getattro */
2629 0, /* tp_getattro */
2630 0, /* tp_setattro */
2630 0, /* tp_setattro */
2631 0, /* tp_as_buffer */
2631 0, /* tp_as_buffer */
2632 Py_TPFLAGS_DEFAULT, /* tp_flags */
2632 Py_TPFLAGS_DEFAULT, /* tp_flags */
2633 "revlog index", /* tp_doc */
2633 "revlog index", /* tp_doc */
2634 0, /* tp_traverse */
2634 0, /* tp_traverse */
2635 0, /* tp_clear */
2635 0, /* tp_clear */
2636 0, /* tp_richcompare */
2636 0, /* tp_richcompare */
2637 0, /* tp_weaklistoffset */
2637 0, /* tp_weaklistoffset */
2638 0, /* tp_iter */
2638 0, /* tp_iter */
2639 0, /* tp_iternext */
2639 0, /* tp_iternext */
2640 index_methods, /* tp_methods */
2640 index_methods, /* tp_methods */
2641 0, /* tp_members */
2641 0, /* tp_members */
2642 index_getset, /* tp_getset */
2642 index_getset, /* tp_getset */
2643 0, /* tp_base */
2643 0, /* tp_base */
2644 0, /* tp_dict */
2644 0, /* tp_dict */
2645 0, /* tp_descr_get */
2645 0, /* tp_descr_get */
2646 0, /* tp_descr_set */
2646 0, /* tp_descr_set */
2647 0, /* tp_dictoffset */
2647 0, /* tp_dictoffset */
2648 (initproc)index_init, /* tp_init */
2648 (initproc)index_init, /* tp_init */
2649 0, /* tp_alloc */
2649 0, /* tp_alloc */
2650 };
2650 };
2651
2651
2652 /*
2652 /*
2653 * returns a tuple of the form (index, index, cache) with elements as
2653 * returns a tuple of the form (index, index, cache) with elements as
2654 * follows:
2654 * follows:
2655 *
2655 *
2656 * index: an index object that lazily parses RevlogNG records
2656 * index: an index object that lazily parses RevlogNG records
2657 * cache: if data is inlined, a tuple (0, index_file_content), else None
2657 * cache: if data is inlined, a tuple (0, index_file_content), else None
2658 * index_file_content could be a string, or a buffer
2658 * index_file_content could be a string, or a buffer
2659 *
2659 *
2660 * added complications are for backwards compatibility
2660 * added complications are for backwards compatibility
2661 */
2661 */
2662 PyObject *parse_index2(PyObject *self, PyObject *args)
2662 PyObject *parse_index2(PyObject *self, PyObject *args)
2663 {
2663 {
2664 PyObject *tuple = NULL, *cache = NULL;
2664 PyObject *tuple = NULL, *cache = NULL;
2665 indexObject *idx;
2665 indexObject *idx;
2666 int ret;
2666 int ret;
2667
2667
2668 idx = PyObject_New(indexObject, &HgRevlogIndex_Type);
2668 idx = PyObject_New(indexObject, &HgRevlogIndex_Type);
2669 if (idx == NULL)
2669 if (idx == NULL)
2670 goto bail;
2670 goto bail;
2671
2671
2672 ret = index_init(idx, args);
2672 ret = index_init(idx, args);
2673 if (ret == -1)
2673 if (ret == -1)
2674 goto bail;
2674 goto bail;
2675
2675
2676 if (idx->inlined) {
2676 if (idx->inlined) {
2677 cache = Py_BuildValue("iO", 0, idx->data);
2677 cache = Py_BuildValue("iO", 0, idx->data);
2678 if (cache == NULL)
2678 if (cache == NULL)
2679 goto bail;
2679 goto bail;
2680 } else {
2680 } else {
2681 cache = Py_None;
2681 cache = Py_None;
2682 Py_INCREF(cache);
2682 Py_INCREF(cache);
2683 }
2683 }
2684
2684
2685 tuple = Py_BuildValue("NN", idx, cache);
2685 tuple = Py_BuildValue("NN", idx, cache);
2686 if (!tuple)
2686 if (!tuple)
2687 goto bail;
2687 goto bail;
2688 return tuple;
2688 return tuple;
2689
2689
2690 bail:
2690 bail:
2691 Py_XDECREF(idx);
2691 Py_XDECREF(idx);
2692 Py_XDECREF(cache);
2692 Py_XDECREF(cache);
2693 Py_XDECREF(tuple);
2693 Py_XDECREF(tuple);
2694 return NULL;
2694 return NULL;
2695 }
2695 }
2696
2696
2697 #ifdef WITH_RUST
2697 #ifdef WITH_RUST
2698
2698
2699 /* rustlazyancestors: iteration over ancestors implemented in Rust
2699 /* rustlazyancestors: iteration over ancestors implemented in Rust
2700 *
2700 *
2701 * This class holds a reference to an index and to the Rust iterator.
2701 * This class holds a reference to an index and to the Rust iterator.
2702 */
2702 */
2703 typedef struct rustlazyancestorsObjectStruct rustlazyancestorsObject;
2703 typedef struct rustlazyancestorsObjectStruct rustlazyancestorsObject;
2704
2704
2705 struct rustlazyancestorsObjectStruct {
2705 struct rustlazyancestorsObjectStruct {
2706 PyObject_HEAD
2706 PyObject_HEAD
2707 /* Type-specific fields go here. */
2707 /* Type-specific fields go here. */
2708 indexObject *index; /* Ref kept to avoid GC'ing the index */
2708 indexObject *index; /* Ref kept to avoid GC'ing the index */
2709 void *iter; /* Rust iterator */
2709 void *iter; /* Rust iterator */
2710 };
2710 };
2711
2711
2712 /* FFI exposed from Rust code */
2712 /* FFI exposed from Rust code */
2713 rustlazyancestorsObject *
2713 rustlazyancestorsObject *rustlazyancestors_init(indexObject *index,
2714 rustlazyancestors_init(indexObject *index,
2714 /* intrevs vector */
2715 /* to pass index_get_parents() */
2715 Py_ssize_t initrevslen,
2716 int (*)(indexObject *, Py_ssize_t, int *, int),
2716 long *initrevs, long stoprev,
2717 /* intrevs vector */
2717 int inclusive);
2718 Py_ssize_t initrevslen, long *initrevs, long stoprev,
2719 int inclusive);
2720 void rustlazyancestors_drop(rustlazyancestorsObject *self);
2718 void rustlazyancestors_drop(rustlazyancestorsObject *self);
2721 int rustlazyancestors_next(rustlazyancestorsObject *self);
2719 int rustlazyancestors_next(rustlazyancestorsObject *self);
2722 int rustlazyancestors_contains(rustlazyancestorsObject *self, long rev);
2720 int rustlazyancestors_contains(rustlazyancestorsObject *self, long rev);
2723
2721
2724 static int index_get_parents_checked(indexObject *self, Py_ssize_t rev, int *ps,
2725 int maxrev)
2726 {
2727 if (rev < 0 || rev >= index_length(self)) {
2728 PyErr_SetString(PyExc_ValueError, "rev out of range");
2729 return -1;
2730 }
2731 return index_get_parents(self, rev, ps, maxrev);
2732 }
2733
2734 /* CPython instance methods */
2722 /* CPython instance methods */
2735 static int rustla_init(rustlazyancestorsObject *self, PyObject *args)
2723 static int rustla_init(rustlazyancestorsObject *self, PyObject *args)
2736 {
2724 {
2737 PyObject *initrevsarg = NULL;
2725 PyObject *initrevsarg = NULL;
2738 PyObject *inclusivearg = NULL;
2726 PyObject *inclusivearg = NULL;
2739 long stoprev = 0;
2727 long stoprev = 0;
2740 long *initrevs = NULL;
2728 long *initrevs = NULL;
2741 int inclusive = 0;
2729 int inclusive = 0;
2742 Py_ssize_t i;
2730 Py_ssize_t i;
2743
2731
2744 indexObject *index;
2732 indexObject *index;
2745 if (!PyArg_ParseTuple(args, "O!O!lO!", &HgRevlogIndex_Type, &index,
2733 if (!PyArg_ParseTuple(args, "O!O!lO!", &HgRevlogIndex_Type, &index,
2746 &PyList_Type, &initrevsarg, &stoprev,
2734 &PyList_Type, &initrevsarg, &stoprev,
2747 &PyBool_Type, &inclusivearg))
2735 &PyBool_Type, &inclusivearg))
2748 return -1;
2736 return -1;
2749
2737
2750 Py_INCREF(index);
2738 Py_INCREF(index);
2751 self->index = index;
2739 self->index = index;
2752
2740
2753 if (inclusivearg == Py_True)
2741 if (inclusivearg == Py_True)
2754 inclusive = 1;
2742 inclusive = 1;
2755
2743
2756 Py_ssize_t linit = PyList_GET_SIZE(initrevsarg);
2744 Py_ssize_t linit = PyList_GET_SIZE(initrevsarg);
2757
2745
2758 initrevs = (long *)calloc(linit, sizeof(long));
2746 initrevs = (long *)calloc(linit, sizeof(long));
2759
2747
2760 if (initrevs == NULL) {
2748 if (initrevs == NULL) {
2761 PyErr_NoMemory();
2749 PyErr_NoMemory();
2762 goto bail;
2750 goto bail;
2763 }
2751 }
2764
2752
2765 for (i = 0; i < linit; i++) {
2753 for (i = 0; i < linit; i++) {
2766 initrevs[i] = PyInt_AsLong(PyList_GET_ITEM(initrevsarg, i));
2754 initrevs[i] = PyInt_AsLong(PyList_GET_ITEM(initrevsarg, i));
2767 }
2755 }
2768 if (PyErr_Occurred())
2756 if (PyErr_Occurred())
2769 goto bail;
2757 goto bail;
2770
2758
2771 self->iter = rustlazyancestors_init(index, index_get_parents, linit,
2759 self->iter =
2772 initrevs, stoprev, inclusive);
2760 rustlazyancestors_init(index, linit, initrevs, stoprev, inclusive);
2773 if (self->iter == NULL) {
2761 if (self->iter == NULL) {
2774 /* if this is because of GraphError::ParentOutOfRange
2762 /* if this is because of GraphError::ParentOutOfRange
2775 * index_get_parents_checked() has already set the proper
2763 * HgRevlogIndex_GetParents() has already set the proper
2776 * ValueError */
2764 * exception */
2777 goto bail;
2765 goto bail;
2778 }
2766 }
2779
2767
2780 free(initrevs);
2768 free(initrevs);
2781 return 0;
2769 return 0;
2782
2770
2783 bail:
2771 bail:
2784 free(initrevs);
2772 free(initrevs);
2785 return -1;
2773 return -1;
2786 };
2774 };
2787
2775
2788 static void rustla_dealloc(rustlazyancestorsObject *self)
2776 static void rustla_dealloc(rustlazyancestorsObject *self)
2789 {
2777 {
2790 Py_XDECREF(self->index);
2778 Py_XDECREF(self->index);
2791 if (self->iter != NULL) { /* can happen if rustla_init failed */
2779 if (self->iter != NULL) { /* can happen if rustla_init failed */
2792 rustlazyancestors_drop(self->iter);
2780 rustlazyancestors_drop(self->iter);
2793 }
2781 }
2794 PyObject_Del(self);
2782 PyObject_Del(self);
2795 }
2783 }
2796
2784
2797 static PyObject *rustla_next(rustlazyancestorsObject *self)
2785 static PyObject *rustla_next(rustlazyancestorsObject *self)
2798 {
2786 {
2799 int res = rustlazyancestors_next(self->iter);
2787 int res = rustlazyancestors_next(self->iter);
2800 if (res == -1) {
2788 if (res == -1) {
2801 /* Setting an explicit exception seems unnecessary
2789 /* Setting an explicit exception seems unnecessary
2802 * as examples from Python source code (Objects/rangeobjets.c
2790 * as examples from Python source code (Objects/rangeobjets.c
2803 * and Modules/_io/stringio.c) seem to demonstrate.
2791 * and Modules/_io/stringio.c) seem to demonstrate.
2804 */
2792 */
2805 return NULL;
2793 return NULL;
2806 }
2794 }
2807 return PyInt_FromLong(res);
2795 return PyInt_FromLong(res);
2808 }
2796 }
2809
2797
2810 static int rustla_contains(rustlazyancestorsObject *self, PyObject *rev)
2798 static int rustla_contains(rustlazyancestorsObject *self, PyObject *rev)
2811 {
2799 {
2812 long lrev;
2800 long lrev;
2813 if (!pylong_to_long(rev, &lrev)) {
2801 if (!pylong_to_long(rev, &lrev)) {
2814 PyErr_Clear();
2802 PyErr_Clear();
2815 return 0;
2803 return 0;
2816 }
2804 }
2817 return rustlazyancestors_contains(self->iter, lrev);
2805 return rustlazyancestors_contains(self->iter, lrev);
2818 }
2806 }
2819
2807
2820 static PySequenceMethods rustla_sequence_methods = {
2808 static PySequenceMethods rustla_sequence_methods = {
2821 0, /* sq_length */
2809 0, /* sq_length */
2822 0, /* sq_concat */
2810 0, /* sq_concat */
2823 0, /* sq_repeat */
2811 0, /* sq_repeat */
2824 0, /* sq_item */
2812 0, /* sq_item */
2825 0, /* sq_slice */
2813 0, /* sq_slice */
2826 0, /* sq_ass_item */
2814 0, /* sq_ass_item */
2827 0, /* sq_ass_slice */
2815 0, /* sq_ass_slice */
2828 (objobjproc)rustla_contains, /* sq_contains */
2816 (objobjproc)rustla_contains, /* sq_contains */
2829 };
2817 };
2830
2818
2831 static PyTypeObject rustlazyancestorsType = {
2819 static PyTypeObject rustlazyancestorsType = {
2832 PyVarObject_HEAD_INIT(NULL, 0) /* header */
2820 PyVarObject_HEAD_INIT(NULL, 0) /* header */
2833 "parsers.rustlazyancestors", /* tp_name */
2821 "parsers.rustlazyancestors", /* tp_name */
2834 sizeof(rustlazyancestorsObject), /* tp_basicsize */
2822 sizeof(rustlazyancestorsObject), /* tp_basicsize */
2835 0, /* tp_itemsize */
2823 0, /* tp_itemsize */
2836 (destructor)rustla_dealloc, /* tp_dealloc */
2824 (destructor)rustla_dealloc, /* tp_dealloc */
2837 0, /* tp_print */
2825 0, /* tp_print */
2838 0, /* tp_getattr */
2826 0, /* tp_getattr */
2839 0, /* tp_setattr */
2827 0, /* tp_setattr */
2840 0, /* tp_compare */
2828 0, /* tp_compare */
2841 0, /* tp_repr */
2829 0, /* tp_repr */
2842 0, /* tp_as_number */
2830 0, /* tp_as_number */
2843 &rustla_sequence_methods, /* tp_as_sequence */
2831 &rustla_sequence_methods, /* tp_as_sequence */
2844 0, /* tp_as_mapping */
2832 0, /* tp_as_mapping */
2845 0, /* tp_hash */
2833 0, /* tp_hash */
2846 0, /* tp_call */
2834 0, /* tp_call */
2847 0, /* tp_str */
2835 0, /* tp_str */
2848 0, /* tp_getattro */
2836 0, /* tp_getattro */
2849 0, /* tp_setattro */
2837 0, /* tp_setattro */
2850 0, /* tp_as_buffer */
2838 0, /* tp_as_buffer */
2851 Py_TPFLAGS_DEFAULT, /* tp_flags */
2839 Py_TPFLAGS_DEFAULT, /* tp_flags */
2852 "Iterator over ancestors, implemented in Rust", /* tp_doc */
2840 "Iterator over ancestors, implemented in Rust", /* tp_doc */
2853 0, /* tp_traverse */
2841 0, /* tp_traverse */
2854 0, /* tp_clear */
2842 0, /* tp_clear */
2855 0, /* tp_richcompare */
2843 0, /* tp_richcompare */
2856 0, /* tp_weaklistoffset */
2844 0, /* tp_weaklistoffset */
2857 0, /* tp_iter */
2845 0, /* tp_iter */
2858 (iternextfunc)rustla_next, /* tp_iternext */
2846 (iternextfunc)rustla_next, /* tp_iternext */
2859 0, /* tp_methods */
2847 0, /* tp_methods */
2860 0, /* tp_members */
2848 0, /* tp_members */
2861 0, /* tp_getset */
2849 0, /* tp_getset */
2862 0, /* tp_base */
2850 0, /* tp_base */
2863 0, /* tp_dict */
2851 0, /* tp_dict */
2864 0, /* tp_descr_get */
2852 0, /* tp_descr_get */
2865 0, /* tp_descr_set */
2853 0, /* tp_descr_set */
2866 0, /* tp_dictoffset */
2854 0, /* tp_dictoffset */
2867 (initproc)rustla_init, /* tp_init */
2855 (initproc)rustla_init, /* tp_init */
2868 0, /* tp_alloc */
2856 0, /* tp_alloc */
2869 };
2857 };
2870 #endif /* WITH_RUST */
2858 #endif /* WITH_RUST */
2871
2859
2872 void revlog_module_init(PyObject *mod)
2860 void revlog_module_init(PyObject *mod)
2873 {
2861 {
2874 HgRevlogIndex_Type.tp_new = PyType_GenericNew;
2862 HgRevlogIndex_Type.tp_new = PyType_GenericNew;
2875 if (PyType_Ready(&HgRevlogIndex_Type) < 0)
2863 if (PyType_Ready(&HgRevlogIndex_Type) < 0)
2876 return;
2864 return;
2877 Py_INCREF(&HgRevlogIndex_Type);
2865 Py_INCREF(&HgRevlogIndex_Type);
2878 PyModule_AddObject(mod, "index", (PyObject *)&HgRevlogIndex_Type);
2866 PyModule_AddObject(mod, "index", (PyObject *)&HgRevlogIndex_Type);
2879
2867
2880 nodetreeType.tp_new = PyType_GenericNew;
2868 nodetreeType.tp_new = PyType_GenericNew;
2881 if (PyType_Ready(&nodetreeType) < 0)
2869 if (PyType_Ready(&nodetreeType) < 0)
2882 return;
2870 return;
2883 Py_INCREF(&nodetreeType);
2871 Py_INCREF(&nodetreeType);
2884 PyModule_AddObject(mod, "nodetree", (PyObject *)&nodetreeType);
2872 PyModule_AddObject(mod, "nodetree", (PyObject *)&nodetreeType);
2885
2873
2886 if (!nullentry) {
2874 if (!nullentry) {
2887 nullentry = Py_BuildValue(PY23("iiiiiiis#", "iiiiiiiy#"), 0, 0,
2875 nullentry = Py_BuildValue(PY23("iiiiiiis#", "iiiiiiiy#"), 0, 0,
2888 0, -1, -1, -1, -1, nullid, 20);
2876 0, -1, -1, -1, -1, nullid, 20);
2889 }
2877 }
2890 if (nullentry)
2878 if (nullentry)
2891 PyObject_GC_UnTrack(nullentry);
2879 PyObject_GC_UnTrack(nullentry);
2892
2880
2893 #ifdef WITH_RUST
2881 #ifdef WITH_RUST
2894 rustlazyancestorsType.tp_new = PyType_GenericNew;
2882 rustlazyancestorsType.tp_new = PyType_GenericNew;
2895 if (PyType_Ready(&rustlazyancestorsType) < 0)
2883 if (PyType_Ready(&rustlazyancestorsType) < 0)
2896 return;
2884 return;
2897 Py_INCREF(&rustlazyancestorsType);
2885 Py_INCREF(&rustlazyancestorsType);
2898 PyModule_AddObject(mod, "rustlazyancestors",
2886 PyModule_AddObject(mod, "rustlazyancestors",
2899 (PyObject *)&rustlazyancestorsType);
2887 (PyObject *)&rustlazyancestorsType);
2900 #endif
2888 #endif
2901 }
2889 }
@@ -1,265 +1,267 b''
1 // Copyright 2018 Georges Racinet <gracinet@anybox.fr>
1 // Copyright 2018 Georges Racinet <gracinet@anybox.fr>
2 //
2 //
3 // This software may be used and distributed according to the terms of the
3 // This software may be used and distributed according to the terms of the
4 // GNU General Public License version 2 or any later version.
4 // GNU General Public License version 2 or any later version.
5
5
6 //! Bindings for CPython extension code
6 //! Bindings for CPython extension code
7 //!
7 //!
8 //! This exposes methods to build and use a `rustlazyancestors` iterator
8 //! This exposes methods to build and use a `rustlazyancestors` iterator
9 //! from C code, using an index and its parents function that are passed
9 //! from C code, using an index and its parents function that are passed
10 //! from the caller at instantiation.
10 //! from the caller at instantiation.
11
11
12 use hg::AncestorsIterator;
12 use hg::AncestorsIterator;
13 use hg::{Graph, GraphError, Revision, NULL_REVISION};
13 use hg::{Graph, GraphError, Revision, NULL_REVISION};
14 use libc::{c_int, c_long, c_void, ssize_t};
14 use libc::{c_int, c_long, c_void, ssize_t};
15 use std::ptr::null_mut;
15 use std::ptr::null_mut;
16 use std::slice;
16 use std::slice;
17
17
18 type IndexPtr = *mut c_void;
18 type IndexPtr = *mut c_void;
19 type IndexParentsFn =
19
20 unsafe extern "C" fn(index: IndexPtr, rev: ssize_t, ps: *mut [c_int; 2], max_rev: c_int)
20 extern "C" {
21 -> c_int;
21 fn HgRevlogIndex_GetParents(
22 op: IndexPtr,
23 rev: c_int,
24 parents: *mut [c_int; 2],
25 ) -> c_int;
26 }
22
27
23 /// A Graph backed up by objects and functions from revlog.c
28 /// A Graph backed up by objects and functions from revlog.c
24 ///
29 ///
25 /// This implementation of the Graph trait, relies on (pointers to)
30 /// This implementation of the Graph trait, relies on (pointers to)
26 /// - the C index object (`index` member)
31 /// - the C index object (`index` member)
27 /// - the `index_get_parents()` function (`parents` member)
32 /// - the `index_get_parents()` function (`parents` member)
28 pub struct Index {
33 pub struct Index {
29 index: IndexPtr,
34 index: IndexPtr,
30 parents: IndexParentsFn,
31 }
35 }
32
36
33 impl Index {
37 impl Index {
34 pub fn new(index: IndexPtr, parents: IndexParentsFn) -> Self {
38 pub fn new(index: IndexPtr) -> Self {
35 Index {
39 Index {
36 index: index,
40 index: index,
37 parents: parents,
38 }
41 }
39 }
42 }
40 }
43 }
41
44
42 impl Graph for Index {
45 impl Graph for Index {
43 /// wrap a call to the C extern parents function
46 /// wrap a call to the C extern parents function
44 fn parents(&self, rev: Revision) -> Result<(Revision, Revision), GraphError> {
47 fn parents(&self, rev: Revision) -> Result<(Revision, Revision), GraphError> {
45 let mut res: [c_int; 2] = [0; 2];
48 let mut res: [c_int; 2] = [0; 2];
46 let code =
49 let code =
47 unsafe { (self.parents)(self.index, rev as ssize_t, &mut res as *mut [c_int; 2], rev) };
50 unsafe { HgRevlogIndex_GetParents(self.index, rev, &mut res as *mut [c_int; 2]) };
48 match code {
51 match code {
49 0 => Ok((res[0], res[1])),
52 0 => Ok((res[0], res[1])),
50 _ => Err(GraphError::ParentOutOfRange(rev)),
53 _ => Err(GraphError::ParentOutOfRange(rev)),
51 }
54 }
52 }
55 }
53 }
56 }
54
57
55 /// Wrapping of AncestorsIterator<Index> constructor, for C callers.
58 /// Wrapping of AncestorsIterator<Index> constructor, for C callers.
56 ///
59 ///
57 /// Besides `initrevs`, `stoprev` and `inclusive`, that are converted
60 /// Besides `initrevs`, `stoprev` and `inclusive`, that are converted
58 /// we receive the index and the parents function as pointers
61 /// we receive the index and the parents function as pointers
59 #[no_mangle]
62 #[no_mangle]
60 pub extern "C" fn rustlazyancestors_init(
63 pub extern "C" fn rustlazyancestors_init(
61 index: IndexPtr,
64 index: IndexPtr,
62 parents: IndexParentsFn,
63 initrevslen: ssize_t,
65 initrevslen: ssize_t,
64 initrevs: *mut c_long,
66 initrevs: *mut c_long,
65 stoprev: c_long,
67 stoprev: c_long,
66 inclusive: c_int,
68 inclusive: c_int,
67 ) -> *mut AncestorsIterator<Index> {
69 ) -> *mut AncestorsIterator<Index> {
68 assert!(initrevslen >= 0);
70 assert!(initrevslen >= 0);
69 unsafe {
71 unsafe {
70 raw_init(
72 raw_init(
71 Index::new(index, parents),
73 Index::new(index),
72 initrevslen as usize,
74 initrevslen as usize,
73 initrevs,
75 initrevs,
74 stoprev,
76 stoprev,
75 inclusive,
77 inclusive,
76 )
78 )
77 }
79 }
78 }
80 }
79
81
80 /// Testable (for any Graph) version of rustlazyancestors_init
82 /// Testable (for any Graph) version of rustlazyancestors_init
81 #[inline]
83 #[inline]
82 unsafe fn raw_init<G: Graph>(
84 unsafe fn raw_init<G: Graph>(
83 graph: G,
85 graph: G,
84 initrevslen: usize,
86 initrevslen: usize,
85 initrevs: *mut c_long,
87 initrevs: *mut c_long,
86 stoprev: c_long,
88 stoprev: c_long,
87 inclusive: c_int,
89 inclusive: c_int,
88 ) -> *mut AncestorsIterator<G> {
90 ) -> *mut AncestorsIterator<G> {
89 let inclb = match inclusive {
91 let inclb = match inclusive {
90 0 => false,
92 0 => false,
91 1 => true,
93 1 => true,
92 _ => {
94 _ => {
93 return null_mut();
95 return null_mut();
94 }
96 }
95 };
97 };
96
98
97 let slice = slice::from_raw_parts(initrevs, initrevslen);
99 let slice = slice::from_raw_parts(initrevs, initrevslen);
98
100
99 Box::into_raw(Box::new(match AncestorsIterator::new(
101 Box::into_raw(Box::new(match AncestorsIterator::new(
100 graph,
102 graph,
101 slice.into_iter().map(|&r| r as Revision),
103 slice.into_iter().map(|&r| r as Revision),
102 stoprev as Revision,
104 stoprev as Revision,
103 inclb,
105 inclb,
104 ) {
106 ) {
105 Ok(it) => it,
107 Ok(it) => it,
106 Err(_) => {
108 Err(_) => {
107 return null_mut();
109 return null_mut();
108 }
110 }
109 }))
111 }))
110 }
112 }
111
113
112 /// Deallocator to be called from C code
114 /// Deallocator to be called from C code
113 #[no_mangle]
115 #[no_mangle]
114 pub extern "C" fn rustlazyancestors_drop(raw_iter: *mut AncestorsIterator<Index>) {
116 pub extern "C" fn rustlazyancestors_drop(raw_iter: *mut AncestorsIterator<Index>) {
115 raw_drop(raw_iter);
117 raw_drop(raw_iter);
116 }
118 }
117
119
118 /// Testable (for any Graph) version of rustlazayancestors_drop
120 /// Testable (for any Graph) version of rustlazayancestors_drop
119 #[inline]
121 #[inline]
120 fn raw_drop<G: Graph>(raw_iter: *mut AncestorsIterator<G>) {
122 fn raw_drop<G: Graph>(raw_iter: *mut AncestorsIterator<G>) {
121 unsafe {
123 unsafe {
122 Box::from_raw(raw_iter);
124 Box::from_raw(raw_iter);
123 }
125 }
124 }
126 }
125
127
126 /// Iteration main method to be called from C code
128 /// Iteration main method to be called from C code
127 ///
129 ///
128 /// We convert the end of iteration into NULL_REVISION,
130 /// We convert the end of iteration into NULL_REVISION,
129 /// it will be up to the C wrapper to convert that back into a Python end of
131 /// it will be up to the C wrapper to convert that back into a Python end of
130 /// iteration
132 /// iteration
131 #[no_mangle]
133 #[no_mangle]
132 pub extern "C" fn rustlazyancestors_next(raw: *mut AncestorsIterator<Index>) -> c_long {
134 pub extern "C" fn rustlazyancestors_next(raw: *mut AncestorsIterator<Index>) -> c_long {
133 raw_next(raw)
135 raw_next(raw)
134 }
136 }
135
137
136 /// Testable (for any Graph) version of rustlazayancestors_next
138 /// Testable (for any Graph) version of rustlazayancestors_next
137 #[inline]
139 #[inline]
138 fn raw_next<G: Graph>(raw: *mut AncestorsIterator<G>) -> c_long {
140 fn raw_next<G: Graph>(raw: *mut AncestorsIterator<G>) -> c_long {
139 let as_ref = unsafe { &mut *raw };
141 let as_ref = unsafe { &mut *raw };
140 as_ref.next().unwrap_or(NULL_REVISION) as c_long
142 as_ref.next().unwrap_or(NULL_REVISION) as c_long
141 }
143 }
142
144
143 #[no_mangle]
145 #[no_mangle]
144 pub extern "C" fn rustlazyancestors_contains(
146 pub extern "C" fn rustlazyancestors_contains(
145 raw: *mut AncestorsIterator<Index>,
147 raw: *mut AncestorsIterator<Index>,
146 target: c_long,
148 target: c_long,
147 ) -> c_int {
149 ) -> c_int {
148 raw_contains(raw, target)
150 raw_contains(raw, target)
149 }
151 }
150
152
151 /// Testable (for any Graph) version of rustlazayancestors_next
153 /// Testable (for any Graph) version of rustlazayancestors_next
152 #[inline]
154 #[inline]
153 fn raw_contains<G: Graph>(
155 fn raw_contains<G: Graph>(
154 raw: *mut AncestorsIterator<G>,
156 raw: *mut AncestorsIterator<G>,
155 target: c_long,
157 target: c_long,
156 ) -> c_int {
158 ) -> c_int {
157 let as_ref = unsafe { &mut *raw };
159 let as_ref = unsafe { &mut *raw };
158 if as_ref.contains(target as Revision) {
160 if as_ref.contains(target as Revision) {
159 return 1;
161 return 1;
160 }
162 }
161 0
163 0
162 }
164 }
163
165
164 #[cfg(test)]
166 #[cfg(test)]
165 mod tests {
167 mod tests {
166 use super::*;
168 use super::*;
167 use std::thread;
169 use std::thread;
168
170
169 #[derive(Clone, Debug)]
171 #[derive(Clone, Debug)]
170 struct Stub;
172 struct Stub;
171
173
172 impl Graph for Stub {
174 impl Graph for Stub {
173 fn parents(&self, r: Revision) -> Result<(Revision, Revision), GraphError> {
175 fn parents(&self, r: Revision) -> Result<(Revision, Revision), GraphError> {
174 match r {
176 match r {
175 25 => Err(GraphError::ParentOutOfRange(25)),
177 25 => Err(GraphError::ParentOutOfRange(25)),
176 _ => Ok((1, 2)),
178 _ => Ok((1, 2)),
177 }
179 }
178 }
180 }
179 }
181 }
180
182
181 /// Helper for test_init_next()
183 /// Helper for test_init_next()
182 fn stub_raw_init(
184 fn stub_raw_init(
183 initrevslen: usize,
185 initrevslen: usize,
184 initrevs: usize,
186 initrevs: usize,
185 stoprev: c_long,
187 stoprev: c_long,
186 inclusive: c_int,
188 inclusive: c_int,
187 ) -> usize {
189 ) -> usize {
188 unsafe {
190 unsafe {
189 raw_init(
191 raw_init(
190 Stub,
192 Stub,
191 initrevslen,
193 initrevslen,
192 initrevs as *mut c_long,
194 initrevs as *mut c_long,
193 stoprev,
195 stoprev,
194 inclusive,
196 inclusive,
195 ) as usize
197 ) as usize
196 }
198 }
197 }
199 }
198
200
199 fn stub_raw_init_from_vec(
201 fn stub_raw_init_from_vec(
200 mut initrevs: Vec<c_long>,
202 mut initrevs: Vec<c_long>,
201 stoprev: c_long,
203 stoprev: c_long,
202 inclusive: c_int,
204 inclusive: c_int,
203 ) -> *mut AncestorsIterator<Stub> {
205 ) -> *mut AncestorsIterator<Stub> {
204 unsafe {
206 unsafe {
205 raw_init(
207 raw_init(
206 Stub,
208 Stub,
207 initrevs.len(),
209 initrevs.len(),
208 initrevs.as_mut_ptr(),
210 initrevs.as_mut_ptr(),
209 stoprev,
211 stoprev,
210 inclusive,
212 inclusive,
211 )
213 )
212 }
214 }
213 }
215 }
214
216
215 #[test]
217 #[test]
216 // Test what happens when we init an Iterator as with the exposed C ABI
218 // Test what happens when we init an Iterator as with the exposed C ABI
217 // and try to use it afterwards
219 // and try to use it afterwards
218 // We spawn new threads, in order to make memory consistency harder
220 // We spawn new threads, in order to make memory consistency harder
219 // but this forces us to convert the pointers into shareable usizes.
221 // but this forces us to convert the pointers into shareable usizes.
220 fn test_init_next() {
222 fn test_init_next() {
221 let mut initrevs: Vec<c_long> = vec![11, 13];
223 let mut initrevs: Vec<c_long> = vec![11, 13];
222 let initrevs_len = initrevs.len();
224 let initrevs_len = initrevs.len();
223 let initrevs_ptr = initrevs.as_mut_ptr() as usize;
225 let initrevs_ptr = initrevs.as_mut_ptr() as usize;
224 let handler = thread::spawn(move || stub_raw_init(initrevs_len, initrevs_ptr, 0, 1));
226 let handler = thread::spawn(move || stub_raw_init(initrevs_len, initrevs_ptr, 0, 1));
225 let raw = handler.join().unwrap() as *mut AncestorsIterator<Stub>;
227 let raw = handler.join().unwrap() as *mut AncestorsIterator<Stub>;
226
228
227 assert_eq!(raw_next(raw), 13);
229 assert_eq!(raw_next(raw), 13);
228 assert_eq!(raw_next(raw), 11);
230 assert_eq!(raw_next(raw), 11);
229 assert_eq!(raw_next(raw), 2);
231 assert_eq!(raw_next(raw), 2);
230 assert_eq!(raw_next(raw), 1);
232 assert_eq!(raw_next(raw), 1);
231 assert_eq!(raw_next(raw), NULL_REVISION as c_long);
233 assert_eq!(raw_next(raw), NULL_REVISION as c_long);
232 raw_drop(raw);
234 raw_drop(raw);
233 }
235 }
234
236
235 #[test]
237 #[test]
236 fn test_init_wrong_bool() {
238 fn test_init_wrong_bool() {
237 assert_eq!(stub_raw_init_from_vec(vec![11, 13], 0, 2), null_mut());
239 assert_eq!(stub_raw_init_from_vec(vec![11, 13], 0, 2), null_mut());
238 }
240 }
239
241
240 #[test]
242 #[test]
241 fn test_empty() {
243 fn test_empty() {
242 let raw = stub_raw_init_from_vec(vec![], 0, 1);
244 let raw = stub_raw_init_from_vec(vec![], 0, 1);
243 assert_eq!(raw_next(raw), NULL_REVISION as c_long);
245 assert_eq!(raw_next(raw), NULL_REVISION as c_long);
244 raw_drop(raw);
246 raw_drop(raw);
245 }
247 }
246
248
247 #[test]
249 #[test]
248 fn test_init_err_out_of_range() {
250 fn test_init_err_out_of_range() {
249 assert!(stub_raw_init_from_vec(vec![25], 0, 0).is_null());
251 assert!(stub_raw_init_from_vec(vec![25], 0, 0).is_null());
250 }
252 }
251
253
252 #[test]
254 #[test]
253 fn test_contains() {
255 fn test_contains() {
254 let raw = stub_raw_init_from_vec(vec![5, 6], 0, 1);
256 let raw = stub_raw_init_from_vec(vec![5, 6], 0, 1);
255 assert_eq!(raw_contains(raw, 5), 1);
257 assert_eq!(raw_contains(raw, 5), 1);
256 assert_eq!(raw_contains(raw, 2), 1);
258 assert_eq!(raw_contains(raw, 2), 1);
257 }
259 }
258
260
259 #[test]
261 #[test]
260 fn test_contains_exclusive() {
262 fn test_contains_exclusive() {
261 let raw = stub_raw_init_from_vec(vec![5, 6], 0, 0);
263 let raw = stub_raw_init_from_vec(vec![5, 6], 0, 0);
262 assert_eq!(raw_contains(raw, 5), 0);
264 assert_eq!(raw_contains(raw, 5), 0);
263 assert_eq!(raw_contains(raw, 2), 1);
265 assert_eq!(raw_contains(raw, 2), 1);
264 }
266 }
265 }
267 }
General Comments 0
You need to be logged in to leave comments. Login now