sparse-revlog: add a `index_segment_span` function in C
authorBoris Feld <boris.feld@octobus.net>
Fri, 09 Nov 2018 18:43:16 +0100
changeset 40705 4ec6a24029d2
parent 40704 7da3729d4b45
child 40706 0650be877a37
sparse-revlog: add a `index_segment_span` 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 09 18:42:58 2018 +0100
+++ b/mercurial/cext/revlog.c	Fri Nov 09 18:43:16 2018 +0100
@@ -1050,6 +1050,34 @@
 	return NULL;
 }
 
+static inline int64_t
+index_segment_span(indexObject *self, Py_ssize_t start_rev, Py_ssize_t end_rev)
+{
+	int64_t start_offset;
+	int64_t end_offset;
+	int end_size;
+	start_offset = index_get_start(self, start_rev);
+	if (start_offset < 0) {
+		return -1;
+	}
+	end_offset = index_get_start(self, end_rev);
+	if (end_offset < 0) {
+		return -1;
+	}
+	end_size = index_get_length(self, end_rev);
+	if (end_size < 0) {
+		return -1;
+	}
+	if (end_offset < start_offset) {
+		PyErr_Format(PyExc_ValueError,
+		             "corrupted revlog index: inconsistent offset "
+		             "between revisions (%zd) and (%zd)",
+		             start_rev, end_rev);
+		return -1;
+	}
+	return (end_offset - start_offset) + (int64_t)end_size;
+}
+
 static inline int nt_level(const char *node, Py_ssize_t level)
 {
 	int v = node[level >> 1];