sparse-revlog: add a `index_get_start` function in C
authorBoris Feld <boris.feld@octobus.net>
Tue, 20 Nov 2018 14:49:18 +0000
changeset 40703 d5b300ec2e89
parent 40702 4240a1da4188
child 40704 7da3729d4b45
sparse-revlog: add a `index_get_start` function in C We are about to implement a native version of `slicechunktodensity`. For clarity, we introduce the helper functions first. This new function provides an efficient way to retrieve some of the information needed by `slicechunktodensity`.
mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c	Fri Nov 23 06:32:32 2018 +0100
+++ b/mercurial/cext/revlog.c	Tue Nov 20 14:49:18 2018 +0000
@@ -185,6 +185,40 @@
 	return 0;
 }
 
+static inline int64_t index_get_start(indexObject *self, Py_ssize_t rev)
+{
+	uint64_t offset;
+	if (rev >= self->length) {
+		PyObject *tuple;
+		PyObject *pylong;
+		PY_LONG_LONG tmp;
+		tuple = PyList_GET_ITEM(self->added, rev - self->length);
+		pylong = PyTuple_GET_ITEM(tuple, 0);
+		tmp = PyLong_AsLongLong(pylong);
+		if (tmp == -1 && PyErr_Occurred()) {
+			return -1;
+		}
+		if (tmp < 0) {
+			PyErr_Format(PyExc_OverflowError,
+			             "revlog entry size out of bound (%lld)",
+			             (long long)tmp);
+			return -1;
+		}
+		offset = (uint64_t)tmp;
+	} else {
+		const char *data = index_deref(self, rev);
+		offset = getbe32(data + 4);
+		if (rev == 0) {
+			/* mask out version number for the first entry */
+			offset &= 0xFFFF;
+		} else {
+			uint32_t offset_high = getbe32(data);
+			offset |= ((uint64_t)offset_high) << 32;
+		}
+	}
+	return (int64_t)(offset >> 16);
+}
+
 /*
  * RevlogNG format (all in big endian, data may be inlined):
  *    6 bytes: offset