##// END OF EJS Templates
parsers: incrementally parse the revlog index in C...
Bryan O'Sullivan -
r16363:2cdd7e63 default
parent child Browse files
Show More
@@ -241,8 +241,40 b' quit:'
241 return ret;
241 return ret;
242 }
242 }
243
243
244 const char nullid[20];
244 /*
245 const int nullrev = -1;
245 * A list-like object that decodes the contents of a RevlogNG index
246 * file on demand. It has limited support for insert and delete at the
247 * last element before the end. The last entry is always a sentinel
248 * nullid.
249 */
250 typedef struct {
251 PyObject_HEAD
252 /* Type-specific fields go here. */
253 PyObject *data; /* raw bytes of index */
254 PyObject **cache; /* cached tuples */
255 const char **offsets; /* populated on demand */
256 Py_ssize_t raw_length; /* original number of elements */
257 Py_ssize_t length; /* current number of elements */
258 PyObject *added; /* populated on demand */
259 int inlined;
260 } indexObject;
261
262 static Py_ssize_t index_length(indexObject *self)
263 {
264 if (self->added == NULL)
265 return self->length;
266 return self->length + PyList_GET_SIZE(self->added);
267 }
268
269 static PyObject *nullentry;
270
271 static long inline_scan(indexObject *self, const char **offsets);
272
273 #if LONG_MAX == 0x7fffffffL
274 static const char *tuple_format = "Kiiiiiis#";
275 #else
276 static const char *tuple_format = "kiiiiiis#";
277 #endif
246
278
247 /* RevlogNG format (all in big endian, data may be inlined):
279 /* RevlogNG format (all in big endian, data may be inlined):
248 * 6 bytes: offset
280 * 6 bytes: offset
@@ -255,23 +287,60 b' const int nullrev = -1;'
255 * 4 bytes: parent 2 revision
287 * 4 bytes: parent 2 revision
256 * 32 bytes: nodeid (only 20 bytes used)
288 * 32 bytes: nodeid (only 20 bytes used)
257 */
289 */
258 static int _parse_index_ng(const char *data, int size, int inlined,
290 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
259 PyObject *index)
260 {
291 {
261 PyObject *entry;
292 uint32_t decode[8]; /* to enforce alignment with inline data */
262 int n = 0, err;
263 uint64_t offset_flags;
293 uint64_t offset_flags;
264 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
294 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
265 const char *c_node_id;
295 const char *c_node_id;
266 const char *end = data + size;
296 const char *data;
267 uint32_t decode[8]; /* to enforce alignment with inline data */
297 Py_ssize_t length = index_length(self);
298 PyObject *entry;
299
300 if (pos >= length) {
301 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
302 return NULL;
303 }
304
305 if (pos == length - 1) {
306 Py_INCREF(nullentry);
307 return nullentry;
308 }
309
310 if (pos >= self->length - 1) {
311 PyObject *obj;
312 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
313 Py_INCREF(obj);
314 return obj;
315 }
268
316
269 while (data < end) {
317 if (self->cache) {
270 unsigned int step;
318 if (self->cache[pos]) {
319 Py_INCREF(self->cache[pos]);
320 return self->cache[pos];
321 }
322 } else {
323 self->cache = calloc(self->raw_length, sizeof(PyObject *));
324 if (self->cache == NULL)
325 return PyErr_NoMemory();
326 }
271
327
272 memcpy(decode, data, 32);
328 if (self->inlined && pos > 0) {
329 if (self->offsets == NULL) {
330 self->offsets = malloc(self->raw_length *
331 sizeof(*self->offsets));
332 if (self->offsets == NULL)
333 return PyErr_NoMemory();
334 inline_scan(self, self->offsets);
335 }
336 data = self->offsets[pos];
337 } else
338 data = PyString_AS_STRING(self->data) + pos * 64;
339
340 memcpy(decode, data, 8 * sizeof(uint32_t));
341
273 offset_flags = ntohl(decode[1]);
342 offset_flags = ntohl(decode[1]);
274 if (n == 0) /* mask out version number for the first entry */
343 if (pos == 0) /* mask out version number for the first entry */
275 offset_flags &= 0xFFFF;
344 offset_flags &= 0xFFFF;
276 else {
345 else {
277 uint32_t offset_high = ntohl(decode[0]);
346 uint32_t offset_high = ntohl(decode[0]);
@@ -286,107 +355,321 b' static int _parse_index_ng(const char *d'
286 parent_2 = ntohl(decode[7]);
355 parent_2 = ntohl(decode[7]);
287 c_node_id = data + 32;
356 c_node_id = data + 32;
288
357
289 entry = Py_BuildValue("Liiiiiis#", offset_flags, comp_len,
358 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
290 uncomp_len, base_rev, link_rev,
359 uncomp_len, base_rev, link_rev,
291 parent_1, parent_2, c_node_id, 20);
360 parent_1, parent_2, c_node_id, 20);
292
361
293 if (!entry)
362 if (entry)
363 PyObject_GC_UnTrack(entry);
364
365 self->cache[pos] = entry;
366 Py_INCREF(entry);
367
368 return entry;
369 }
370
371 static PyObject *index_insert(indexObject *self, PyObject *args)
372 {
373 PyObject *obj, *node;
374 long offset;
375 Py_ssize_t len;
376
377 if (!PyArg_ParseTuple(args, "lO", &offset, &obj))
378 return NULL;
379
380 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
381 PyErr_SetString(PyExc_ValueError, "8-tuple required");
382 return NULL;
383 }
384
385 node = PyTuple_GET_ITEM(obj, 7);
386 if (!PyString_Check(node) || PyString_GET_SIZE(node) != 20) {
387 PyErr_SetString(PyExc_ValueError,
388 "20-byte hash required as last element");
389 return NULL;
390 }
391
392 len = index_length(self);
393
394 if (offset < 0)
395 offset += len;
396
397 if (offset != len - 1) {
398 PyErr_SetString(PyExc_IndexError,
399 "insert only supported at index -1");
400 return NULL;
401 }
402
403 if (self->added == NULL) {
404 self->added = PyList_New(0);
405 if (self->added == NULL)
406 return NULL;
407 }
408
409 if (PyList_Append(self->added, obj) == -1)
410 return NULL;
411
412 Py_RETURN_NONE;
413 }
414
415 static int index_assign_subscript(indexObject *self, PyObject *item,
416 PyObject *value)
417 {
418 Py_ssize_t start, stop, step, slicelength;
419 Py_ssize_t length = index_length(self);
420
421 if (!PySlice_Check(item) || value != NULL) {
422 PyErr_SetString(PyExc_TypeError,
423 "revlog index only supports slice deletion");
424 return -1;
425 }
426
427 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
428 &start, &stop, &step, &slicelength) < 0)
429 return -1;
430
431 if (slicelength <= 0)
294 return 0;
432 return 0;
295
433
296 PyObject_GC_UnTrack(entry); /* don't waste time with this */
434 if ((step < 0 && start < stop) || (step > 0 && start > stop))
435 stop = start;
297
436
298 if (inlined) {
437 if (step < 0) {
299 err = PyList_Append(index, entry);
438 stop = start + 1;
300 Py_DECREF(entry);
439 start = stop + step*(slicelength - 1) - 1;
301 if (err)
440 step = -step;
302 return 0;
441 }
303 } else
304 PyList_SET_ITEM(index, n, entry); /* steals reference */
305
442
306 n++;
443 if (step != 1) {
307 step = 64 + (inlined ? comp_len : 0);
444 PyErr_SetString(PyExc_ValueError,
308 if (data + step > end || data + step < data)
445 "revlog index delete requires step size of 1");
309 break;
446 return -1;
310 data += step;
311 }
447 }
312 if (data != end) {
448
313 if (!PyErr_Occurred())
449 if (stop != length - 1) {
314 PyErr_SetString(PyExc_ValueError, "corrupt index file");
450 PyErr_SetString(PyExc_IndexError,
451 "revlog index deletion indices are invalid");
452 return -1;
453 }
454
455 if (start < self->length) {
456 self->length = start + 1;
457 if (self->added) {
458 Py_DECREF(self->added);
459 self->added = NULL;
460 }
315 return 0;
461 return 0;
316 }
462 }
317
463
318 /* create the magic nullid entry in the index at [-1] */
464 return PyList_SetSlice(self->added, start - self->length + 1,
319 entry = Py_BuildValue("Liiiiiis#", (uint64_t)0, 0, 0, -1, -1, -1, -1, nullid, 20);
465 PyList_GET_SIZE(self->added),
466 NULL);
467 }
468
469 static long inline_scan(indexObject *self, const char **offsets)
470 {
471 const char *data = PyString_AS_STRING(self->data);
472 const char *end = data + PyString_GET_SIZE(self->data);
473 const long hdrsize = 64;
474 long incr = hdrsize;
475 Py_ssize_t len = 0;
320
476
321 if (!entry)
477 while (data + hdrsize <= end) {
322 return 0;
478 uint32_t comp_len;
479 const char *old_data;
480 /* 3rd element of header is length of compressed inline data */
481 memcpy(&comp_len, data + 8, sizeof(uint32_t));
482 incr = hdrsize + ntohl(comp_len);
483 if (incr < hdrsize)
484 break;
485 if (offsets)
486 offsets[len] = data;
487 len++;
488 old_data = data;
489 data += incr;
490 if (data <= old_data)
491 break;
492 }
323
493
324 PyObject_GC_UnTrack(entry); /* don't waste time with this */
494 if (data != end && data + hdrsize != end) {
495 if (!PyErr_Occurred())
496 PyErr_SetString(PyExc_ValueError, "corrupt index file");
497 return -1;
498 }
499
500 return len;
501 }
325
502
326 if (inlined) {
503 static int index_real_init(indexObject *self, const char *data, int size,
327 err = PyList_Append(index, entry);
504 PyObject *inlined_obj, PyObject *data_obj)
328 Py_DECREF(entry);
505 {
329 if (err)
506 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
507 self->data = data_obj;
508 self->cache = NULL;
509
510 self->added = NULL;
511 self->offsets = NULL;
512 Py_INCREF(self->data);
513
514 if (self->inlined) {
515 long len = inline_scan(self, NULL);
516 if (len == -1)
517 goto bail;
518 self->raw_length = len;
519 self->length = len + 1;
520 } else {
521 if (size % 64) {
522 PyErr_SetString(PyExc_ValueError, "corrupt index file");
523 goto bail;
524 }
525 self->raw_length = size / 64;
526 self->length = self->raw_length + 1;
527 }
528
330 return 0;
529 return 0;
331 } else
530 bail:
332 PyList_SET_ITEM(index, n, entry); /* steals reference */
531 return -1;
532 }
333
533
334 return 1;
534 static int index_init(indexObject *self, PyObject *args, PyObject *kwds)
535 {
536 const char *data;
537 int size;
538 PyObject *inlined_obj;
539
540 if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj))
541 return -1;
542
543 return index_real_init(self, data, size, inlined_obj,
544 PyTuple_GET_ITEM(args, 0));
335 }
545 }
336
546
337 /* This function parses a index file and returns a Python tuple of the
547 static void index_dealloc(indexObject *self)
338 * following format: (index, cache)
548 {
549 Py_DECREF(self->data);
550 if (self->cache) {
551 Py_ssize_t i;
552
553 for (i = 0; i < self->raw_length; i++)
554 Py_XDECREF(self->cache[i]);
555 }
556 Py_XDECREF(self->added);
557 free(self->offsets);
558 PyObject_Del(self);
559 }
560
561 static PySequenceMethods index_sequence_methods = {
562 (lenfunc)index_length, /* sq_length */
563 0, /* sq_concat */
564 0, /* sq_repeat */
565 (ssizeargfunc)index_get, /* sq_item */
566 };
567
568 static PyMappingMethods index_mapping_methods = {
569 (lenfunc)index_length, /* mp_length */
570 NULL, /* mp_subscript */
571 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
572 };
573
574 static PyMethodDef index_methods[] = {
575 {"insert", (PyCFunction)index_insert, METH_VARARGS,
576 "insert an index entry"},
577 {NULL} /* Sentinel */
578 };
579
580 static PyTypeObject indexType = {
581 PyObject_HEAD_INIT(NULL)
582 0, /* ob_size */
583 "parsers.index", /* tp_name */
584 sizeof(indexObject), /* tp_basicsize */
585 0, /* tp_itemsize */
586 (destructor)index_dealloc, /* tp_dealloc */
587 0, /* tp_print */
588 0, /* tp_getattr */
589 0, /* tp_setattr */
590 0, /* tp_compare */
591 0, /* tp_repr */
592 0, /* tp_as_number */
593 &index_sequence_methods, /* tp_as_sequence */
594 &index_mapping_methods, /* tp_as_mapping */
595 0, /* tp_hash */
596 0, /* tp_call */
597 0, /* tp_str */
598 0, /* tp_getattro */
599 0, /* tp_setattro */
600 0, /* tp_as_buffer */
601 Py_TPFLAGS_DEFAULT, /* tp_flags */
602 "revlog index", /* tp_doc */
603 0, /* tp_traverse */
604 0, /* tp_clear */
605 0, /* tp_richcompare */
606 0, /* tp_weaklistoffset */
607 0, /* tp_iter */
608 0, /* tp_iternext */
609 index_methods, /* tp_methods */
610 0, /* tp_members */
611 0, /* tp_getset */
612 0, /* tp_base */
613 0, /* tp_dict */
614 0, /* tp_descr_get */
615 0, /* tp_descr_set */
616 0, /* tp_dictoffset */
617 (initproc)index_init, /* tp_init */
618 0, /* tp_alloc */
619 PyType_GenericNew, /* tp_new */
620 };
621
622 /*
623 * returns a tuple of the form (index, None, cache) with elements as
624 * follows:
339 *
625 *
340 * index: a list of tuples containing the RevlogNG records
626 * index: an index object that lazily parses the RevlogNG records
341 * cache: if data is inlined, a tuple (index_file_content, 0) else None
627 * cache: if data is inlined, a tuple (index_file_content, 0), else None
628 *
629 * added complications are for backwards compatibility
342 */
630 */
343 static PyObject *parse_index2(PyObject *self, PyObject *args)
631 static PyObject *parse_index2(PyObject *self, PyObject *args)
344 {
632 {
345 const char *data;
633 const char *data;
346 int size, inlined;
634 int size, ret;
347 PyObject *rval = NULL, *index = NULL, *cache = NULL;
635 PyObject *inlined_obj, *tuple = NULL, *cache = NULL;
348 PyObject *data_obj = NULL, *inlined_obj;
636 indexObject *idx;
349
637
350 if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj))
638 if (!PyArg_ParseTuple(args, "s#O", &data, &size, &inlined_obj))
351 return NULL;
639 return NULL;
352 inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
640
641 idx = PyObject_New(indexObject, &indexType);
642
643 if (idx == NULL)
644 goto bail;
353
645
354 /* If no data is inlined, we know the size of the index list in
646 ret = index_real_init(idx, data, size, inlined_obj,
355 * advance: size divided by the size of one revlog record (64 bytes)
647 PyTuple_GET_ITEM(args, 0));
356 * plus one for nullid */
648 if (ret)
357 index = inlined ? PyList_New(0) : PyList_New(size / 64 + 1);
649 goto bail;
358 if (!index)
359 goto quit;
360
650
361 /* set up the cache return value */
651 if (idx->inlined) {
362 if (inlined) {
652 Py_INCREF(idx->data);
363 /* Note that the reference to data_obj is only borrowed */
653 cache = Py_BuildValue("iO", 0, idx->data);
364 data_obj = PyTuple_GET_ITEM(args, 0);
654 if (cache == NULL)
365 cache = Py_BuildValue("iO", 0, data_obj);
655 goto bail;
366 if (!cache)
367 goto quit;
368 } else {
656 } else {
369 cache = Py_None;
657 cache = Py_None;
370 Py_INCREF(Py_None);
658 Py_INCREF(cache);
371 }
659 }
372
660
373 /* actually populate the index with data */
661 tuple = Py_BuildValue("NN", idx, cache);
374 if (!_parse_index_ng(data, size, inlined, index))
662 if (!tuple)
375 goto quit;
663 goto bail;
664 return tuple;
376
665
377 rval = Py_BuildValue("NN", index, cache);
666 bail:
378 if (!rval)
667 Py_XDECREF(idx);
379 goto quit;
380 return rval;
381
382 quit:
383 Py_XDECREF(index);
384 Py_XDECREF(cache);
668 Py_XDECREF(cache);
385 Py_XDECREF(rval);
669 Py_XDECREF(tuple);
386 return NULL;
670 return NULL;
387 }
671 }
388
672
389
390 static char parsers_doc[] = "Efficient content parsing.";
673 static char parsers_doc[] = "Efficient content parsing.";
391
674
392 static PyMethodDef methods[] = {
675 static PyMethodDef methods[] = {
@@ -396,6 +679,22 b' static PyMethodDef methods[] = {'
396 {NULL, NULL}
679 {NULL, NULL}
397 };
680 };
398
681
682 static void module_init(PyObject *mod)
683 {
684 static const char nullid[20];
685
686 if (PyType_Ready(&indexType) < 0)
687 return;
688 Py_INCREF(&indexType);
689
690 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
691
692 nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
693 -1, -1, -1, -1, nullid, 20);
694 if (nullentry)
695 PyObject_GC_UnTrack(nullentry);
696 }
697
399 #ifdef IS_PY3K
698 #ifdef IS_PY3K
400 static struct PyModuleDef parsers_module = {
699 static struct PyModuleDef parsers_module = {
401 PyModuleDef_HEAD_INIT,
700 PyModuleDef_HEAD_INIT,
@@ -407,12 +706,15 b' static struct PyModuleDef parsers_module'
407
706
408 PyMODINIT_FUNC PyInit_parsers(void)
707 PyMODINIT_FUNC PyInit_parsers(void)
409 {
708 {
410 return PyModule_Create(&parsers_module);
709 PyObject *mod = PyModule_Create(&parsers_module);
710 module_init(mod);
711 return mod;
411 }
712 }
412 #else
713 #else
413 PyMODINIT_FUNC initparsers(void)
714 PyMODINIT_FUNC initparsers(void)
414 {
715 {
415 Py_InitModule3("parsers", methods, parsers_doc);
716 PyObject *mod = Py_InitModule3("parsers", methods, parsers_doc);
717 module_init(mod);
416 }
718 }
417 #endif
719 #endif
418
720
@@ -52,7 +52,6 b' def py_parseindex(data, inline) :'
52
52
53 return index, cache
53 return index, cache
54
54
55
56 data_inlined = '\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x01\x8c' \
55 data_inlined = '\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x01\x8c' \
57 '\x00\x00\x04\x07\x00\x00\x00\x00\x00\x00\x15\x15\xff\xff\xff' \
56 '\x00\x00\x04\x07\x00\x00\x00\x00\x00\x00\x15\x15\xff\xff\xff' \
58 '\xff\xff\xff\xff\xff\xebG\x97\xb7\x1fB\x04\xcf\x13V\x81\tw\x1b' \
57 '\xff\xff\xff\xff\xff\xebG\x97\xb7\x1fB\x04\xcf\x13V\x81\tw\x1b' \
@@ -94,13 +93,16 b" data_non_inlined = '\\x00\\x00\\x00\\x01\\x00"
94 '\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00' \
93 '\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00' \
95 '\x00\x00\x00\x00\x00\x00\x00\x00\x00'
94 '\x00\x00\x00\x00\x00\x00\x00\x00\x00'
96
95
97 def runtest() :
96 def parse_index2(data, inline):
97 index, chunkcache = parsers.parse_index2(data, inline)
98 return list(index), chunkcache
98
99
100 def runtest() :
99 py_res_1 = py_parseindex(data_inlined, True)
101 py_res_1 = py_parseindex(data_inlined, True)
100 c_res_1 = parsers.parse_index2(data_inlined, True)
102 c_res_1 = parse_index2(data_inlined, True)
101
103
102 py_res_2 = py_parseindex(data_non_inlined, False)
104 py_res_2 = py_parseindex(data_non_inlined, False)
103 c_res_2 = parsers.parse_index2(data_non_inlined, False)
105 c_res_2 = parse_index2(data_non_inlined, False)
104
106
105 if py_res_1 != c_res_1:
107 if py_res_1 != c_res_1:
106 print "Parse index result (with inlined data) differs!"
108 print "Parse index result (with inlined data) differs!"
General Comments 0
You need to be logged in to leave comments. Login now