From 2557834a967f9922dbfdb85ed54445cab7581ae3 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 22 Apr 2018 13:49:02 -0700 Subject: [PATCH] ircd::m::dbs: Factor in the event_idx for a more stable messages sort. --- ircd/m/dbs.cc | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ircd/m/dbs.cc b/ircd/m/dbs.cc index 243218fc5..6a468c190 100644 --- a/ircd/m/dbs.cc +++ b/ircd/m/dbs.cc @@ -563,21 +563,28 @@ ircd::m::dbs::desc::events__room_events__cmp b.substr(sizes[1]), }; + // These conditions are matched on some queries when the user only + // supplies a room id. + if(empty(post[0])) return true; if(empty(post[1])) return false; - // Now want just the depth... - const uint64_t depth[2] + // Distill out the depth and event_idx integers + const std::pair pair[2] { - room_events_key(post[0]).first, - room_events_key(post[1]).first, + room_events_key(post[0]), + room_events_key(post[1]) }; - // Highest to lowest sort so highest is first - return depth[1] < depth[0]; + // When two events are at the same depth sort by index (the sequence + // number given as they were admitted into the system) otherwise + // sort by depth. Note this is a reverse order comparison. + return std::get<0>(pair[1]) != std::get<0>(pair[0])? + std::get<0>(pair[1]) < std::get<0>(pair[0]): + std::get<1>(pair[1]) < std::get<1>(pair[0]); }, };