commit 8269ca59cdbdf3cf2629323c2e78381e2d58c454
parent 0f9d55d4f9c012c10dd0c26aa3db2a59bba805c5
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 23 Nov 2023 13:19:07 -0800
nostrdb/index: add u64_timestamp lmdb comparator
custom kind+timestamp comparison function. This is used by lmdb to
perform b+ tree searches over the kind+timestamp index.
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
1 file changed, 27 insertions(+), 0 deletions(-)
diff --git a/nostrdb/nostrdb.c b/nostrdb/nostrdb.c
@@ -127,6 +127,12 @@ struct ndb_tsid {
 	uint64_t timestamp;
 };
 
+// A u64 + timestamp id. Just using this for kinds at the moment.
+struct ndb_u64_tsid {
+	uint64_t u64; // kind, etc
+	uint64_t timestamp;
+};
+
 // Copies only lowercase characters to the destination string and fills the rest with null bytes.
 // `dst` and `src` are pointers to the destination and source strings, respectively.
 // `n` is the maximum number of characters to copy.
@@ -753,6 +759,27 @@ static int mdb_cmp_memn(const MDB_val *a, const MDB_val *b) {
 	return diff ? diff : len_diff<0 ? -1 : len_diff;
 }
 
+// custom kind+timestamp comparison function. This is used by lmdb to perform
+// b+ tree searches over the kind+timestamp index
+static int ndb_u64_tsid_compare(const MDB_val *a, const MDB_val *b)
+{
+	struct ndb_u64_tsid *tsa, *tsb;
+	tsa = a->mv_data;
+	tsb = b->mv_data;
+
+	if (tsa->u64 < tsb->u64)
+		return -1;
+	else if (tsa->u64 > tsb->u64)
+		return 1;
+
+	if (tsa->timestamp < tsb->timestamp)
+		return -1;
+	else if (tsa->timestamp > tsb->timestamp)
+		return 1;
+
+	return 0;
+}
+
 static int ndb_tsid_compare(const MDB_val *a, const MDB_val *b)
 {
 	struct ndb_tsid *tsa, *tsb;