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