From 8269ca59cdbdf3cf2629323c2e78381e2d58c454 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Thu, 23 Nov 2023 13:19:07 -0800 Subject: [PATCH] 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 --- nostrdb/nostrdb.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/nostrdb/nostrdb.c b/nostrdb/nostrdb.c index 5497468e..368ff4d9 100644 --- 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;