Query for mid-point of two databases

I’m writing a git bisect-like utility, and as part of this I’m looking to query for the transaction midway between two databases. Here is what I have so far:

(d/q '[:find (median ?t)
       :in $earlier $later
       :where [$later ?t :db/txInstant _]
              [(q '[:find (max ?et)
                    :where [?et :db/txInstant _]]
                  $earlier) [[?min-t]]]
              [(<= ?min-t ?t)]]
       earlier-db
       later-db)

It seems to be correct, but I’m wondering if there are ways to simplify the query. Could it be re-written without the sub-query?

1 Like