Patchwork [2,of,6,RFC] parsers: add fm1readmarkerswithoffset

login
register
mail settings
Submitter Jun Wu
Date May 22, 2017, 1:31 a.m.
Message ID <1a100daf74d0ea3059f5.1495416669@x1c>
Download mbox | patch
Permalink /patch/20808/
State Changes Requested
Headers show

Comments

Jun Wu - May 22, 2017, 1:31 a.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1495405286 25200
#      Sun May 21 15:21:26 2017 -0700
# Node ID 1a100daf74d0ea3059f5b841c7c6e9ff46388ad9
# Parent  fec85b1af16b0360e7bd78cd26d4d21edf678962
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 1a100daf74d0
parsers: add fm1readmarkerswithoffset

Record marker offsets so we can load markers individually without loading
the whole obsstore (which is slow).

Patch

diff --git a/mercurial/cext/parsers.c b/mercurial/cext/parsers.c
--- a/mercurial/cext/parsers.c
+++ b/mercurial/cext/parsers.c
@@ -892,4 +892,46 @@  bail:
 }
 
+static PyObject *fm1readmarkerswithoffset(PyObject *self, PyObject *args) {
+	const char *data, *dataend;
+	int datalen;
+	Py_ssize_t offset, stop;
+	PyObject *markers = NULL;
+
+	if (!PyArg_ParseTuple(args, "s#nn", &data, &datalen, &offset, &stop)) {
+		return NULL;
+	}
+	dataend = data + datalen;
+	data += offset;
+	markers = PyList_New(0);
+	if (!markers) {
+		return NULL;
+	}
+	while (offset < stop) {
+		uint32_t msize;
+		int error;
+		PyObject *markerwithoffset = NULL;
+		PyObject *record = fm1readmarker(data, dataend, &msize);
+		if (!record) {
+			goto bail;
+		}
+		markerwithoffset = Py_BuildValue("(iO)", offset, record);
+		Py_DECREF(record);
+		if (!markerwithoffset) {
+			goto bail;
+		}
+		error = PyList_Append(markers, markerwithoffset);
+		Py_DECREF(markerwithoffset);
+		if (error) {
+			goto bail;
+		}
+		data += msize;
+		offset += msize;
+	}
+	return markers;
+bail:
+	Py_DECREF(markers);
+	return NULL;
+}
+
 static char parsers_doc[] = "Efficient content parsing.";
 
@@ -918,4 +960,7 @@  static PyMethodDef methods[] = {
 	{"fm1readmarkers", fm1readmarkers, METH_VARARGS,
 			"parse v1 obsolete markers\n"},
+	{"fm1readmarkerswithoffset", fm1readmarkerswithoffset, METH_VARARGS,
+	 "like fm1readmarkers, but return [(offset, marker)] instead of "
+	 "[marker]\n"},
 	{NULL, NULL}
 };