##// END OF EJS Templates
parsers: use the correct maximum radix tree depth...
Bryan O'Sullivan -
r16641:e6dfbc5d stable
parent child Browse files
Show More
@@ -1,1186 +1,1188 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 <ctype.h>
12 12 #include <string.h>
13 13
14 14 #include "util.h"
15 15
16 16 static int hexdigit(char c)
17 17 {
18 18 if (c >= '0' && c <= '9')
19 19 return c - '0';
20 20 if (c >= 'a' && c <= 'f')
21 21 return c - 'a' + 10;
22 22 if (c >= 'A' && c <= 'F')
23 23 return c - 'A' + 10;
24 24
25 25 PyErr_SetString(PyExc_ValueError, "input contains non-hex character");
26 26 return 0;
27 27 }
28 28
29 29 /*
30 30 * Turn a hex-encoded string into binary.
31 31 */
32 32 static PyObject *unhexlify(const char *str, int len)
33 33 {
34 34 PyObject *ret;
35 35 const char *c;
36 36 char *d;
37 37
38 38 ret = PyBytes_FromStringAndSize(NULL, len / 2);
39 39
40 40 if (!ret)
41 41 return NULL;
42 42
43 43 d = PyBytes_AsString(ret);
44 44
45 45 for (c = str; c < str + len;) {
46 46 int hi = hexdigit(*c++);
47 47 int lo = hexdigit(*c++);
48 48 *d++ = (hi << 4) | lo;
49 49 }
50 50
51 51 return ret;
52 52 }
53 53
54 54 /*
55 55 * This code assumes that a manifest is stitched together with newline
56 56 * ('\n') characters.
57 57 */
58 58 static PyObject *parse_manifest(PyObject *self, PyObject *args)
59 59 {
60 60 PyObject *mfdict, *fdict;
61 61 char *str, *cur, *start, *zero;
62 62 int len;
63 63
64 64 if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest",
65 65 &PyDict_Type, &mfdict,
66 66 &PyDict_Type, &fdict,
67 67 &str, &len))
68 68 goto quit;
69 69
70 70 for (start = cur = str, zero = NULL; cur < str + len; cur++) {
71 71 PyObject *file = NULL, *node = NULL;
72 72 PyObject *flags = NULL;
73 73 int nlen;
74 74
75 75 if (!*cur) {
76 76 zero = cur;
77 77 continue;
78 78 }
79 79 else if (*cur != '\n')
80 80 continue;
81 81
82 82 if (!zero) {
83 83 PyErr_SetString(PyExc_ValueError,
84 84 "manifest entry has no separator");
85 85 goto quit;
86 86 }
87 87
88 88 file = PyBytes_FromStringAndSize(start, zero - start);
89 89
90 90 if (!file)
91 91 goto bail;
92 92
93 93 nlen = cur - zero - 1;
94 94
95 95 node = unhexlify(zero + 1, nlen > 40 ? 40 : nlen);
96 96 if (!node)
97 97 goto bail;
98 98
99 99 if (nlen > 40) {
100 100 flags = PyBytes_FromStringAndSize(zero + 41,
101 101 nlen - 40);
102 102 if (!flags)
103 103 goto bail;
104 104
105 105 if (PyDict_SetItem(fdict, file, flags) == -1)
106 106 goto bail;
107 107 }
108 108
109 109 if (PyDict_SetItem(mfdict, file, node) == -1)
110 110 goto bail;
111 111
112 112 start = cur + 1;
113 113 zero = NULL;
114 114
115 115 Py_XDECREF(flags);
116 116 Py_XDECREF(node);
117 117 Py_XDECREF(file);
118 118 continue;
119 119 bail:
120 120 Py_XDECREF(flags);
121 121 Py_XDECREF(node);
122 122 Py_XDECREF(file);
123 123 goto quit;
124 124 }
125 125
126 126 if (len > 0 && *(cur - 1) != '\n') {
127 127 PyErr_SetString(PyExc_ValueError,
128 128 "manifest contains trailing garbage");
129 129 goto quit;
130 130 }
131 131
132 132 Py_INCREF(Py_None);
133 133 return Py_None;
134 134 quit:
135 135 return NULL;
136 136 }
137 137
138 138 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
139 139 {
140 140 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
141 141 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
142 142 char *str, *cur, *end, *cpos;
143 143 int state, mode, size, mtime;
144 144 unsigned int flen;
145 145 int len;
146 146
147 147 if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate",
148 148 &PyDict_Type, &dmap,
149 149 &PyDict_Type, &cmap,
150 150 &str, &len))
151 151 goto quit;
152 152
153 153 /* read parents */
154 154 if (len < 40)
155 155 goto quit;
156 156
157 157 parents = Py_BuildValue("s#s#", str, 20, str + 20, 20);
158 158 if (!parents)
159 159 goto quit;
160 160
161 161 /* read filenames */
162 162 cur = str + 40;
163 163 end = str + len;
164 164
165 165 while (cur < end - 17) {
166 166 /* unpack header */
167 167 state = *cur;
168 168 mode = getbe32(cur + 1);
169 169 size = getbe32(cur + 5);
170 170 mtime = getbe32(cur + 9);
171 171 flen = getbe32(cur + 13);
172 172 cur += 17;
173 173 if (cur + flen > end || cur + flen < cur) {
174 174 PyErr_SetString(PyExc_ValueError, "overflow in dirstate");
175 175 goto quit;
176 176 }
177 177
178 178 entry = Py_BuildValue("ciii", state, mode, size, mtime);
179 179 if (!entry)
180 180 goto quit;
181 181 PyObject_GC_UnTrack(entry); /* don't waste time with this */
182 182
183 183 cpos = memchr(cur, 0, flen);
184 184 if (cpos) {
185 185 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
186 186 cname = PyBytes_FromStringAndSize(cpos + 1,
187 187 flen - (cpos - cur) - 1);
188 188 if (!fname || !cname ||
189 189 PyDict_SetItem(cmap, fname, cname) == -1 ||
190 190 PyDict_SetItem(dmap, fname, entry) == -1)
191 191 goto quit;
192 192 Py_DECREF(cname);
193 193 } else {
194 194 fname = PyBytes_FromStringAndSize(cur, flen);
195 195 if (!fname ||
196 196 PyDict_SetItem(dmap, fname, entry) == -1)
197 197 goto quit;
198 198 }
199 199 cur += flen;
200 200 Py_DECREF(fname);
201 201 Py_DECREF(entry);
202 202 fname = cname = entry = NULL;
203 203 }
204 204
205 205 ret = parents;
206 206 Py_INCREF(ret);
207 207 quit:
208 208 Py_XDECREF(fname);
209 209 Py_XDECREF(cname);
210 210 Py_XDECREF(entry);
211 211 Py_XDECREF(parents);
212 212 return ret;
213 213 }
214 214
215 215 /*
216 216 * A base-16 trie for fast node->rev mapping.
217 217 *
218 218 * Positive value is index of the next node in the trie
219 219 * Negative value is a leaf: -(rev + 1)
220 220 * Zero is empty
221 221 */
222 222 typedef struct {
223 223 int children[16];
224 224 } nodetree;
225 225
226 226 /*
227 227 * This class has two behaviours.
228 228 *
229 229 * When used in a list-like way (with integer keys), we decode an
230 230 * entry in a RevlogNG index file on demand. Our last entry is a
231 231 * sentinel, always a nullid. We have limited support for
232 232 * integer-keyed insert and delete, only at elements right before the
233 233 * sentinel.
234 234 *
235 235 * With string keys, we lazily perform a reverse mapping from node to
236 236 * rev, using a base-16 trie.
237 237 */
238 238 typedef struct {
239 239 PyObject_HEAD
240 240 /* Type-specific fields go here. */
241 241 PyObject *data; /* raw bytes of index */
242 242 PyObject **cache; /* cached tuples */
243 243 const char **offsets; /* populated on demand */
244 244 Py_ssize_t raw_length; /* original number of elements */
245 245 Py_ssize_t length; /* current number of elements */
246 246 PyObject *added; /* populated on demand */
247 247 nodetree *nt; /* base-16 trie */
248 248 int ntlength; /* # nodes in use */
249 249 int ntcapacity; /* # nodes allocated */
250 250 int ntdepth; /* maximum depth of tree */
251 251 int ntsplits; /* # splits performed */
252 252 int ntrev; /* last rev scanned */
253 253 int ntlookups; /* # lookups */
254 254 int ntmisses; /* # lookups that miss the cache */
255 255 int inlined;
256 256 } indexObject;
257 257
258 258 static Py_ssize_t index_length(const indexObject *self)
259 259 {
260 260 if (self->added == NULL)
261 261 return self->length;
262 262 return self->length + PyList_GET_SIZE(self->added);
263 263 }
264 264
265 265 static PyObject *nullentry;
266 266 static const char nullid[20];
267 267
268 268 static long inline_scan(indexObject *self, const char **offsets);
269 269
270 270 #if LONG_MAX == 0x7fffffffL
271 271 static char *tuple_format = "Kiiiiiis#";
272 272 #else
273 273 static char *tuple_format = "kiiiiiis#";
274 274 #endif
275 275
276 276 /*
277 277 * Return a pointer to the beginning of a RevlogNG record.
278 278 */
279 279 static const char *index_deref(indexObject *self, Py_ssize_t pos)
280 280 {
281 281 if (self->inlined && pos > 0) {
282 282 if (self->offsets == NULL) {
283 283 self->offsets = malloc(self->raw_length *
284 284 sizeof(*self->offsets));
285 285 if (self->offsets == NULL)
286 286 return (const char *)PyErr_NoMemory();
287 287 inline_scan(self, self->offsets);
288 288 }
289 289 return self->offsets[pos];
290 290 }
291 291
292 292 return PyString_AS_STRING(self->data) + pos * 64;
293 293 }
294 294
295 295 /*
296 296 * RevlogNG format (all in big endian, data may be inlined):
297 297 * 6 bytes: offset
298 298 * 2 bytes: flags
299 299 * 4 bytes: compressed length
300 300 * 4 bytes: uncompressed length
301 301 * 4 bytes: base revision
302 302 * 4 bytes: link revision
303 303 * 4 bytes: parent 1 revision
304 304 * 4 bytes: parent 2 revision
305 305 * 32 bytes: nodeid (only 20 bytes used)
306 306 */
307 307 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
308 308 {
309 309 uint64_t offset_flags;
310 310 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
311 311 const char *c_node_id;
312 312 const char *data;
313 313 Py_ssize_t length = index_length(self);
314 314 PyObject *entry;
315 315
316 316 if (pos < 0)
317 317 pos += length;
318 318
319 319 if (pos < 0 || pos >= length) {
320 320 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
321 321 return NULL;
322 322 }
323 323
324 324 if (pos == length - 1) {
325 325 Py_INCREF(nullentry);
326 326 return nullentry;
327 327 }
328 328
329 329 if (pos >= self->length - 1) {
330 330 PyObject *obj;
331 331 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
332 332 Py_INCREF(obj);
333 333 return obj;
334 334 }
335 335
336 336 if (self->cache) {
337 337 if (self->cache[pos]) {
338 338 Py_INCREF(self->cache[pos]);
339 339 return self->cache[pos];
340 340 }
341 341 } else {
342 342 self->cache = calloc(self->raw_length, sizeof(PyObject *));
343 343 if (self->cache == NULL)
344 344 return PyErr_NoMemory();
345 345 }
346 346
347 347 data = index_deref(self, pos);
348 348 if (data == NULL)
349 349 return NULL;
350 350
351 351 offset_flags = getbe32(data + 4);
352 352 if (pos == 0) /* mask out version number for the first entry */
353 353 offset_flags &= 0xFFFF;
354 354 else {
355 355 uint32_t offset_high = getbe32(data);
356 356 offset_flags |= ((uint64_t)offset_high) << 32;
357 357 }
358 358
359 359 comp_len = getbe32(data + 8);
360 360 uncomp_len = getbe32(data + 12);
361 361 base_rev = getbe32(data + 16);
362 362 link_rev = getbe32(data + 20);
363 363 parent_1 = getbe32(data + 24);
364 364 parent_2 = getbe32(data + 28);
365 365 c_node_id = data + 32;
366 366
367 367 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
368 368 uncomp_len, base_rev, link_rev,
369 369 parent_1, parent_2, c_node_id, 20);
370 370
371 371 if (entry)
372 372 PyObject_GC_UnTrack(entry);
373 373
374 374 self->cache[pos] = entry;
375 375 Py_INCREF(entry);
376 376
377 377 return entry;
378 378 }
379 379
380 380 /*
381 381 * Return the 20-byte SHA of the node corresponding to the given rev.
382 382 */
383 383 static const char *index_node(indexObject *self, Py_ssize_t pos)
384 384 {
385 385 Py_ssize_t length = index_length(self);
386 386 const char *data;
387 387
388 388 if (pos == length - 1)
389 389 return nullid;
390 390
391 391 if (pos >= length)
392 392 return NULL;
393 393
394 394 if (pos >= self->length - 1) {
395 395 PyObject *tuple, *str;
396 396 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
397 397 str = PyTuple_GetItem(tuple, 7);
398 398 return str ? PyString_AS_STRING(str) : NULL;
399 399 }
400 400
401 401 data = index_deref(self, pos);
402 402 return data ? data + 32 : NULL;
403 403 }
404 404
405 405 static int nt_insert(indexObject *self, const char *node, int rev);
406 406
407 407 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
408 408 {
409 409 if (PyString_AsStringAndSize(obj, node, nodelen) == -1)
410 410 return -1;
411 411 if (*nodelen == 20)
412 412 return 0;
413 413 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
414 414 return -1;
415 415 }
416 416
417 417 static PyObject *index_insert(indexObject *self, PyObject *args)
418 418 {
419 419 PyObject *obj;
420 420 char *node;
421 421 long offset;
422 422 Py_ssize_t len, nodelen;
423 423
424 424 if (!PyArg_ParseTuple(args, "lO", &offset, &obj))
425 425 return NULL;
426 426
427 427 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
428 428 PyErr_SetString(PyExc_TypeError, "8-tuple required");
429 429 return NULL;
430 430 }
431 431
432 432 if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1)
433 433 return NULL;
434 434
435 435 len = index_length(self);
436 436
437 437 if (offset < 0)
438 438 offset += len;
439 439
440 440 if (offset != len - 1) {
441 441 PyErr_SetString(PyExc_IndexError,
442 442 "insert only supported at index -1");
443 443 return NULL;
444 444 }
445 445
446 446 if (offset > INT_MAX) {
447 447 PyErr_SetString(PyExc_ValueError,
448 448 "currently only 2**31 revs supported");
449 449 return NULL;
450 450 }
451 451
452 452 if (self->added == NULL) {
453 453 self->added = PyList_New(0);
454 454 if (self->added == NULL)
455 455 return NULL;
456 456 }
457 457
458 458 if (PyList_Append(self->added, obj) == -1)
459 459 return NULL;
460 460
461 461 if (self->nt)
462 462 nt_insert(self, node, (int)offset);
463 463
464 464 Py_RETURN_NONE;
465 465 }
466 466
467 467 static void _index_clearcaches(indexObject *self)
468 468 {
469 469 if (self->cache) {
470 470 Py_ssize_t i;
471 471
472 472 for (i = 0; i < self->raw_length; i++) {
473 473 if (self->cache[i]) {
474 474 Py_DECREF(self->cache[i]);
475 475 self->cache[i] = NULL;
476 476 }
477 477 }
478 478 free(self->cache);
479 479 self->cache = NULL;
480 480 }
481 481 if (self->offsets) {
482 482 free(self->offsets);
483 483 self->offsets = NULL;
484 484 }
485 485 if (self->nt) {
486 486 free(self->nt);
487 487 self->nt = NULL;
488 488 }
489 489 }
490 490
491 491 static PyObject *index_clearcaches(indexObject *self)
492 492 {
493 493 _index_clearcaches(self);
494 494 self->ntlength = self->ntcapacity = 0;
495 495 self->ntdepth = self->ntsplits = 0;
496 496 self->ntrev = -1;
497 497 self->ntlookups = self->ntmisses = 0;
498 498 Py_RETURN_NONE;
499 499 }
500 500
501 501 static PyObject *index_stats(indexObject *self)
502 502 {
503 503 PyObject *obj = PyDict_New();
504 504
505 505 if (obj == NULL)
506 506 return NULL;
507 507
508 508 #define istat(__n, __d) \
509 509 if (PyDict_SetItemString(obj, __d, PyInt_FromLong(self->__n)) == -1) \
510 510 goto bail;
511 511
512 512 if (self->added) {
513 513 Py_ssize_t len = PyList_GET_SIZE(self->added);
514 514 if (PyDict_SetItemString(obj, "index entries added",
515 515 PyInt_FromLong(len)) == -1)
516 516 goto bail;
517 517 }
518 518
519 519 if (self->raw_length != self->length - 1)
520 520 istat(raw_length, "revs on disk");
521 521 istat(length, "revs in memory");
522 522 istat(ntcapacity, "node trie capacity");
523 523 istat(ntdepth, "node trie depth");
524 524 istat(ntlength, "node trie count");
525 525 istat(ntlookups, "node trie lookups");
526 526 istat(ntmisses, "node trie misses");
527 527 istat(ntrev, "node trie last rev scanned");
528 528 istat(ntsplits, "node trie splits");
529 529
530 530 #undef istat
531 531
532 532 return obj;
533 533
534 534 bail:
535 535 Py_XDECREF(obj);
536 536 return NULL;
537 537 }
538 538
539 539 static inline int nt_level(const char *node, int level)
540 540 {
541 541 int v = node[level>>1];
542 542 if (!(level & 1))
543 543 v >>= 4;
544 544 return v & 0xf;
545 545 }
546 546
547 547 static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen)
548 548 {
549 int level, off;
549 int level, maxlevel, off;
550 550
551 551 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
552 552 return -1;
553 553
554 554 if (self->nt == NULL)
555 555 return -2;
556 556
557 for (level = off = 0; level < nodelen; level++) {
557 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
558
559 for (level = off = 0; level < maxlevel; level++) {
558 560 int k = nt_level(node, level);
559 561 nodetree *n = &self->nt[off];
560 562 int v = n->children[k];
561 563
562 564 if (v < 0) {
563 565 const char *n;
564 566 v = -v - 1;
565 567 n = index_node(self, v);
566 568 if (n == NULL)
567 569 return -2;
568 570 return memcmp(node, n, nodelen > 20 ? 20 : nodelen)
569 571 ? -2 : v;
570 572 }
571 573 if (v == 0)
572 574 return -2;
573 575 off = v;
574 576 }
575 577 return -2;
576 578 }
577 579
578 580 static int nt_new(indexObject *self)
579 581 {
580 582 if (self->ntlength == self->ntcapacity) {
581 583 self->ntcapacity *= 2;
582 584 self->nt = realloc(self->nt,
583 585 self->ntcapacity * sizeof(nodetree));
584 586 if (self->nt == NULL) {
585 587 PyErr_SetString(PyExc_MemoryError, "out of memory");
586 588 return -1;
587 589 }
588 590 memset(&self->nt[self->ntlength], 0,
589 591 sizeof(nodetree) * (self->ntcapacity - self->ntlength));
590 592 }
591 593 return self->ntlength++;
592 594 }
593 595
594 596 static int nt_insert(indexObject *self, const char *node, int rev)
595 597 {
596 598 int level = 0;
597 599 int off = 0;
598 600
599 while (level < 20) {
601 while (level < 40) {
600 602 int k = nt_level(node, level);
601 603 nodetree *n;
602 604 int v;
603 605
604 606 n = &self->nt[off];
605 607 v = n->children[k];
606 608
607 609 if (v == 0) {
608 610 n->children[k] = -rev - 1;
609 611 return 0;
610 612 }
611 613 if (v < 0) {
612 614 const char *oldnode = index_node(self, -v - 1);
613 615 int noff;
614 616
615 617 if (!oldnode || !memcmp(oldnode, node, 20)) {
616 618 n->children[k] = -rev - 1;
617 619 return 0;
618 620 }
619 621 noff = nt_new(self);
620 622 if (noff == -1)
621 623 return -1;
622 624 /* self->nt may have been changed by realloc */
623 625 self->nt[off].children[k] = noff;
624 626 off = noff;
625 627 n = &self->nt[off];
626 628 n->children[nt_level(oldnode, ++level)] = v;
627 629 if (level > self->ntdepth)
628 630 self->ntdepth = level;
629 631 self->ntsplits += 1;
630 632 } else {
631 633 level += 1;
632 634 off = v;
633 635 }
634 636 }
635 637
636 638 return -1;
637 639 }
638 640
639 641 /*
640 642 * Return values:
641 643 *
642 644 * -3: error (exception set)
643 645 * -2: not found (no exception set)
644 646 * rest: valid rev
645 647 */
646 648 static int index_find_node(indexObject *self,
647 649 const char *node, Py_ssize_t nodelen)
648 650 {
649 651 int rev;
650 652
651 653 self->ntlookups++;
652 654 rev = nt_find(self, node, nodelen);
653 655 if (rev >= -1)
654 656 return rev;
655 657
656 658 if (self->nt == NULL) {
657 659 self->ntcapacity = self->raw_length < 4
658 660 ? 4 : self->raw_length / 2;
659 661 self->nt = calloc(self->ntcapacity, sizeof(nodetree));
660 662 if (self->nt == NULL) {
661 663 PyErr_SetString(PyExc_MemoryError, "out of memory");
662 664 return -3;
663 665 }
664 666 self->ntlength = 1;
665 667 self->ntrev = (int)index_length(self) - 1;
666 668 self->ntlookups = 1;
667 669 self->ntmisses = 0;
668 670 }
669 671
670 672 /*
671 673 * For the first handful of lookups, we scan the entire index,
672 674 * and cache only the matching nodes. This optimizes for cases
673 675 * like "hg tip", where only a few nodes are accessed.
674 676 *
675 677 * After that, we cache every node we visit, using a single
676 678 * scan amortized over multiple lookups. This gives the best
677 679 * bulk performance, e.g. for "hg log".
678 680 */
679 681 if (self->ntmisses++ < 4) {
680 682 for (rev = self->ntrev - 1; rev >= 0; rev--) {
681 683 const char *n = index_node(self, rev);
682 684 if (n == NULL)
683 685 return -2;
684 686 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
685 687 if (nt_insert(self, n, rev) == -1)
686 688 return -3;
687 689 break;
688 690 }
689 691 }
690 692 } else {
691 693 for (rev = self->ntrev - 1; rev >= 0; rev--) {
692 694 const char *n = index_node(self, rev);
693 695 if (n == NULL)
694 696 return -2;
695 697 if (nt_insert(self, n, rev) == -1)
696 698 return -3;
697 699 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
698 700 break;
699 701 }
700 702 }
701 703 self->ntrev = rev;
702 704 }
703 705
704 706 if (rev >= 0)
705 707 return rev;
706 708 return -2;
707 709 }
708 710
709 711 static PyObject *raise_revlog_error(void)
710 712 {
711 713 static PyObject *errclass;
712 714 PyObject *mod = NULL, *errobj;
713 715
714 716 if (errclass == NULL) {
715 717 PyObject *dict;
716 718
717 719 mod = PyImport_ImportModule("mercurial.error");
718 720 if (mod == NULL)
719 721 goto classfail;
720 722
721 723 dict = PyModule_GetDict(mod);
722 724 if (dict == NULL)
723 725 goto classfail;
724 726
725 727 errclass = PyDict_GetItemString(dict, "RevlogError");
726 728 if (errclass == NULL) {
727 729 PyErr_SetString(PyExc_SystemError,
728 730 "could not find RevlogError");
729 731 goto classfail;
730 732 }
731 733 Py_INCREF(errclass);
732 734 }
733 735
734 736 errobj = PyObject_CallFunction(errclass, NULL);
735 737 if (errobj == NULL)
736 738 return NULL;
737 739 PyErr_SetObject(errclass, errobj);
738 740 return errobj;
739 741
740 742 classfail:
741 743 Py_XDECREF(mod);
742 744 return NULL;
743 745 }
744 746
745 747 static PyObject *index_getitem(indexObject *self, PyObject *value)
746 748 {
747 749 char *node;
748 750 Py_ssize_t nodelen;
749 751 int rev;
750 752
751 753 if (PyInt_Check(value))
752 754 return index_get(self, PyInt_AS_LONG(value));
753 755
754 756 if (PyString_AsStringAndSize(value, &node, &nodelen) == -1)
755 757 return NULL;
756 758 rev = index_find_node(self, node, nodelen);
757 759 if (rev >= -1)
758 760 return PyInt_FromLong(rev);
759 761 if (rev == -2)
760 762 raise_revlog_error();
761 763 return NULL;
762 764 }
763 765
764 766 static PyObject *index_m_get(indexObject *self, PyObject *args)
765 767 {
766 768 char *node;
767 769 int nodelen, rev;
768 770
769 771 if (!PyArg_ParseTuple(args, "s#", &node, &nodelen))
770 772 return NULL;
771 773
772 774 rev = index_find_node(self, node, nodelen);
773 775 if (rev == -3)
774 776 return NULL;
775 777 if (rev == -2)
776 778 Py_RETURN_NONE;
777 779 return PyInt_FromLong(rev);
778 780 }
779 781
780 782 static int index_contains(indexObject *self, PyObject *value)
781 783 {
782 784 char *node;
783 785 Py_ssize_t nodelen;
784 786
785 787 if (PyInt_Check(value)) {
786 788 long rev = PyInt_AS_LONG(value);
787 789 return rev >= -1 && rev < index_length(self);
788 790 }
789 791
790 792 if (!PyString_Check(value))
791 793 return 0;
792 794
793 795 node = PyString_AS_STRING(value);
794 796 nodelen = PyString_GET_SIZE(value);
795 797
796 798 switch (index_find_node(self, node, nodelen)) {
797 799 case -3:
798 800 return -1;
799 801 case -2:
800 802 return 0;
801 803 default:
802 804 return 1;
803 805 }
804 806 }
805 807
806 808 /*
807 809 * Invalidate any trie entries introduced by added revs.
808 810 */
809 811 static void nt_invalidate_added(indexObject *self, Py_ssize_t start)
810 812 {
811 813 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
812 814
813 815 for (i = start; i < len; i++) {
814 816 PyObject *tuple = PyList_GET_ITEM(self->added, i);
815 817 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
816 818
817 819 nt_insert(self, PyString_AS_STRING(node), -1);
818 820 }
819 821
820 822 if (start == 0) {
821 823 Py_DECREF(self->added);
822 824 self->added = NULL;
823 825 }
824 826 }
825 827
826 828 /*
827 829 * Delete a numeric range of revs, which must be at the end of the
828 830 * range, but exclude the sentinel nullid entry.
829 831 */
830 832 static int index_slice_del(indexObject *self, PyObject *item)
831 833 {
832 834 Py_ssize_t start, stop, step, slicelength;
833 835 Py_ssize_t length = index_length(self);
834 836
835 837 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
836 838 &start, &stop, &step, &slicelength) < 0)
837 839 return -1;
838 840
839 841 if (slicelength <= 0)
840 842 return 0;
841 843
842 844 if ((step < 0 && start < stop) || (step > 0 && start > stop))
843 845 stop = start;
844 846
845 847 if (step < 0) {
846 848 stop = start + 1;
847 849 start = stop + step*(slicelength - 1) - 1;
848 850 step = -step;
849 851 }
850 852
851 853 if (step != 1) {
852 854 PyErr_SetString(PyExc_ValueError,
853 855 "revlog index delete requires step size of 1");
854 856 return -1;
855 857 }
856 858
857 859 if (stop != length - 1) {
858 860 PyErr_SetString(PyExc_IndexError,
859 861 "revlog index deletion indices are invalid");
860 862 return -1;
861 863 }
862 864
863 865 if (start < self->length - 1) {
864 866 if (self->nt) {
865 867 Py_ssize_t i;
866 868
867 869 for (i = start + 1; i < self->length - 1; i++) {
868 870 const char *node = index_node(self, i);
869 871
870 872 if (node)
871 873 nt_insert(self, node, -1);
872 874 }
873 875 if (self->added)
874 876 nt_invalidate_added(self, 0);
875 877 if (self->ntrev > start)
876 878 self->ntrev = (int)start;
877 879 }
878 880 self->length = start + 1;
879 881 return 0;
880 882 }
881 883
882 884 if (self->nt) {
883 885 nt_invalidate_added(self, start - self->length + 1);
884 886 if (self->ntrev > start)
885 887 self->ntrev = (int)start;
886 888 }
887 889 return self->added
888 890 ? PyList_SetSlice(self->added, start - self->length + 1,
889 891 PyList_GET_SIZE(self->added), NULL)
890 892 : 0;
891 893 }
892 894
893 895 /*
894 896 * Supported ops:
895 897 *
896 898 * slice deletion
897 899 * string assignment (extend node->rev mapping)
898 900 * string deletion (shrink node->rev mapping)
899 901 */
900 902 static int index_assign_subscript(indexObject *self, PyObject *item,
901 903 PyObject *value)
902 904 {
903 905 char *node;
904 906 Py_ssize_t nodelen;
905 907 long rev;
906 908
907 909 if (PySlice_Check(item) && value == NULL)
908 910 return index_slice_del(self, item);
909 911
910 912 if (node_check(item, &node, &nodelen) == -1)
911 913 return -1;
912 914
913 915 if (value == NULL)
914 916 return self->nt ? nt_insert(self, node, -1) : 0;
915 917 rev = PyInt_AsLong(value);
916 918 if (rev > INT_MAX || rev < 0) {
917 919 if (!PyErr_Occurred())
918 920 PyErr_SetString(PyExc_ValueError, "rev out of range");
919 921 return -1;
920 922 }
921 923 return nt_insert(self, node, (int)rev);
922 924 }
923 925
924 926 /*
925 927 * Find all RevlogNG entries in an index that has inline data. Update
926 928 * the optional "offsets" table with those entries.
927 929 */
928 930 static long inline_scan(indexObject *self, const char **offsets)
929 931 {
930 932 const char *data = PyString_AS_STRING(self->data);
931 933 const char *end = data + PyString_GET_SIZE(self->data);
932 934 const long hdrsize = 64;
933 935 long incr = hdrsize;
934 936 Py_ssize_t len = 0;
935 937
936 938 while (data + hdrsize <= end) {
937 939 uint32_t comp_len;
938 940 const char *old_data;
939 941 /* 3rd element of header is length of compressed inline data */
940 942 comp_len = getbe32(data + 8);
941 943 incr = hdrsize + comp_len;
942 944 if (incr < hdrsize)
943 945 break;
944 946 if (offsets)
945 947 offsets[len] = data;
946 948 len++;
947 949 old_data = data;
948 950 data += incr;
949 951 if (data <= old_data)
950 952 break;
951 953 }
952 954
953 955 if (data != end && data + hdrsize != end) {
954 956 if (!PyErr_Occurred())
955 957 PyErr_SetString(PyExc_ValueError, "corrupt index file");
956 958 return -1;
957 959 }
958 960
959 961 return len;
960 962 }
961 963
962 964 static int index_init(indexObject *self, PyObject *args)
963 965 {
964 966 PyObject *data_obj, *inlined_obj;
965 967 Py_ssize_t size;
966 968
967 969 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
968 970 return -1;
969 971 if (!PyString_Check(data_obj)) {
970 972 PyErr_SetString(PyExc_TypeError, "data is not a string");
971 973 return -1;
972 974 }
973 975 size = PyString_GET_SIZE(data_obj);
974 976
975 977 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
976 978 self->data = data_obj;
977 979 self->cache = NULL;
978 980
979 981 self->added = NULL;
980 982 self->offsets = NULL;
981 983 self->nt = NULL;
982 984 self->ntlength = self->ntcapacity = 0;
983 985 self->ntdepth = self->ntsplits = 0;
984 986 self->ntlookups = self->ntmisses = 0;
985 987 self->ntrev = -1;
986 988 Py_INCREF(self->data);
987 989
988 990 if (self->inlined) {
989 991 long len = inline_scan(self, NULL);
990 992 if (len == -1)
991 993 goto bail;
992 994 self->raw_length = len;
993 995 self->length = len + 1;
994 996 } else {
995 997 if (size % 64) {
996 998 PyErr_SetString(PyExc_ValueError, "corrupt index file");
997 999 goto bail;
998 1000 }
999 1001 self->raw_length = size / 64;
1000 1002 self->length = self->raw_length + 1;
1001 1003 }
1002 1004
1003 1005 return 0;
1004 1006 bail:
1005 1007 return -1;
1006 1008 }
1007 1009
1008 1010 static PyObject *index_nodemap(indexObject *self)
1009 1011 {
1010 1012 Py_INCREF(self);
1011 1013 return (PyObject *)self;
1012 1014 }
1013 1015
1014 1016 static void index_dealloc(indexObject *self)
1015 1017 {
1016 1018 _index_clearcaches(self);
1017 1019 Py_DECREF(self->data);
1018 1020 Py_XDECREF(self->added);
1019 1021 PyObject_Del(self);
1020 1022 }
1021 1023
1022 1024 static PySequenceMethods index_sequence_methods = {
1023 1025 (lenfunc)index_length, /* sq_length */
1024 1026 0, /* sq_concat */
1025 1027 0, /* sq_repeat */
1026 1028 (ssizeargfunc)index_get, /* sq_item */
1027 1029 0, /* sq_slice */
1028 1030 0, /* sq_ass_item */
1029 1031 0, /* sq_ass_slice */
1030 1032 (objobjproc)index_contains, /* sq_contains */
1031 1033 };
1032 1034
1033 1035 static PyMappingMethods index_mapping_methods = {
1034 1036 (lenfunc)index_length, /* mp_length */
1035 1037 (binaryfunc)index_getitem, /* mp_subscript */
1036 1038 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
1037 1039 };
1038 1040
1039 1041 static PyMethodDef index_methods[] = {
1040 1042 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
1041 1043 "clear the index caches"},
1042 1044 {"get", (PyCFunction)index_m_get, METH_VARARGS,
1043 1045 "get an index entry"},
1044 1046 {"insert", (PyCFunction)index_insert, METH_VARARGS,
1045 1047 "insert an index entry"},
1046 1048 {"stats", (PyCFunction)index_stats, METH_NOARGS,
1047 1049 "stats for the index"},
1048 1050 {NULL} /* Sentinel */
1049 1051 };
1050 1052
1051 1053 static PyGetSetDef index_getset[] = {
1052 1054 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
1053 1055 {NULL} /* Sentinel */
1054 1056 };
1055 1057
1056 1058 static PyTypeObject indexType = {
1057 1059 PyObject_HEAD_INIT(NULL)
1058 1060 0, /* ob_size */
1059 1061 "parsers.index", /* tp_name */
1060 1062 sizeof(indexObject), /* tp_basicsize */
1061 1063 0, /* tp_itemsize */
1062 1064 (destructor)index_dealloc, /* tp_dealloc */
1063 1065 0, /* tp_print */
1064 1066 0, /* tp_getattr */
1065 1067 0, /* tp_setattr */
1066 1068 0, /* tp_compare */
1067 1069 0, /* tp_repr */
1068 1070 0, /* tp_as_number */
1069 1071 &index_sequence_methods, /* tp_as_sequence */
1070 1072 &index_mapping_methods, /* tp_as_mapping */
1071 1073 0, /* tp_hash */
1072 1074 0, /* tp_call */
1073 1075 0, /* tp_str */
1074 1076 0, /* tp_getattro */
1075 1077 0, /* tp_setattro */
1076 1078 0, /* tp_as_buffer */
1077 1079 Py_TPFLAGS_DEFAULT, /* tp_flags */
1078 1080 "revlog index", /* tp_doc */
1079 1081 0, /* tp_traverse */
1080 1082 0, /* tp_clear */
1081 1083 0, /* tp_richcompare */
1082 1084 0, /* tp_weaklistoffset */
1083 1085 0, /* tp_iter */
1084 1086 0, /* tp_iternext */
1085 1087 index_methods, /* tp_methods */
1086 1088 0, /* tp_members */
1087 1089 index_getset, /* tp_getset */
1088 1090 0, /* tp_base */
1089 1091 0, /* tp_dict */
1090 1092 0, /* tp_descr_get */
1091 1093 0, /* tp_descr_set */
1092 1094 0, /* tp_dictoffset */
1093 1095 (initproc)index_init, /* tp_init */
1094 1096 0, /* tp_alloc */
1095 1097 };
1096 1098
1097 1099 /*
1098 1100 * returns a tuple of the form (index, index, cache) with elements as
1099 1101 * follows:
1100 1102 *
1101 1103 * index: an index object that lazily parses RevlogNG records
1102 1104 * cache: if data is inlined, a tuple (index_file_content, 0), else None
1103 1105 *
1104 1106 * added complications are for backwards compatibility
1105 1107 */
1106 1108 static PyObject *parse_index2(PyObject *self, PyObject *args)
1107 1109 {
1108 1110 PyObject *tuple = NULL, *cache = NULL;
1109 1111 indexObject *idx;
1110 1112 int ret;
1111 1113
1112 1114 idx = PyObject_New(indexObject, &indexType);
1113 1115 if (idx == NULL)
1114 1116 goto bail;
1115 1117
1116 1118 ret = index_init(idx, args);
1117 1119 if (ret == -1)
1118 1120 goto bail;
1119 1121
1120 1122 if (idx->inlined) {
1121 1123 cache = Py_BuildValue("iO", 0, idx->data);
1122 1124 if (cache == NULL)
1123 1125 goto bail;
1124 1126 } else {
1125 1127 cache = Py_None;
1126 1128 Py_INCREF(cache);
1127 1129 }
1128 1130
1129 1131 tuple = Py_BuildValue("NN", idx, cache);
1130 1132 if (!tuple)
1131 1133 goto bail;
1132 1134 return tuple;
1133 1135
1134 1136 bail:
1135 1137 Py_XDECREF(idx);
1136 1138 Py_XDECREF(cache);
1137 1139 Py_XDECREF(tuple);
1138 1140 return NULL;
1139 1141 }
1140 1142
1141 1143 static char parsers_doc[] = "Efficient content parsing.";
1142 1144
1143 1145 static PyMethodDef methods[] = {
1144 1146 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
1145 1147 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
1146 1148 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
1147 1149 {NULL, NULL}
1148 1150 };
1149 1151
1150 1152 static void module_init(PyObject *mod)
1151 1153 {
1152 1154 indexType.tp_new = PyType_GenericNew;
1153 1155 if (PyType_Ready(&indexType) < 0)
1154 1156 return;
1155 1157 Py_INCREF(&indexType);
1156 1158
1157 1159 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
1158 1160
1159 1161 nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
1160 1162 -1, -1, -1, -1, nullid, 20);
1161 1163 if (nullentry)
1162 1164 PyObject_GC_UnTrack(nullentry);
1163 1165 }
1164 1166
1165 1167 #ifdef IS_PY3K
1166 1168 static struct PyModuleDef parsers_module = {
1167 1169 PyModuleDef_HEAD_INIT,
1168 1170 "parsers",
1169 1171 parsers_doc,
1170 1172 -1,
1171 1173 methods
1172 1174 };
1173 1175
1174 1176 PyMODINIT_FUNC PyInit_parsers(void)
1175 1177 {
1176 1178 PyObject *mod = PyModule_Create(&parsers_module);
1177 1179 module_init(mod);
1178 1180 return mod;
1179 1181 }
1180 1182 #else
1181 1183 PyMODINIT_FUNC initparsers(void)
1182 1184 {
1183 1185 PyObject *mod = Py_InitModule3("parsers", methods, parsers_doc);
1184 1186 module_init(mod);
1185 1187 }
1186 1188 #endif
General Comments 0
You need to be logged in to leave comments. Login now