Skip to content

Commit

Permalink
roar
Browse files Browse the repository at this point in the history
  • Loading branch information
stopachka committed Dec 19, 2024
1 parent 6555d49 commit 9645d3b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
23 changes: 10 additions & 13 deletions server/src/instant/db/datalog.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1107,13 +1107,6 @@
(defn has-prev-tbl [table]
(kw table :-has-prev))

(defn joins-on-value? [prev-table [_ & conds]]
(let [conds-set (set conds)
prev-value-uuid (kw prev-table :-value-uuid)]
(or
(contains? conds-set [:= :entity-id prev-value-uuid])
(contains? conds-set [:= prev-value-uuid :entity-id]))))

(defn add-page-info
"Updates the cte with pagination constraints."
[{:keys [next-idx
Expand Down Expand Up @@ -1142,10 +1135,14 @@
false
page-pattern)
prev-table (kw prefix (dec next-idx))
entity-id-col-name (if (joins-on-value? prev-table (:where query))
:-value-uuid
:-entity-id)
entity-id-col (kw prev-table entity-id-col-name)

entity-id-col (if (= :created-at-timestamp order-col-type)
:entity-id
(kw prev-table :-entity-id))

entity-id-col-full-name (if (= :created-at-timestamp order-col-type)
(kw table :-entity-id)
(kw prev-table :-entity-id))
sym-component-type (component-type-of-sym named-pattern order-sym)
sym-triple-idx (get (set/map-invert idx->component-type)
sym-component-type)
Expand Down Expand Up @@ -1208,14 +1205,14 @@
first-row-table (kw table :-first)
last-row-table (kw table :-last)
first-row-cte [first-row-table
{:select [[entity-id-col :e]
{:select [[entity-id-col-full-name :e]
[(kw table :- (if (= :value order-col-name)
:value-blob
order-col-name)) :sym]]
:from table
:limit 1}]
last-row-cte [last-row-table
{:select [[entity-id-col :e]
{:select [[entity-id-col-full-name :e]
[(kw table :- (if (= :value order-col-name)
:value-blob
order-col-name)) :sym]]
Expand Down
40 changes: 33 additions & 7 deletions server/test/instant/db/instaql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@
:data
:datalog-result
:page-info)]

(testing "after"
(is-pretty-eq? (query-pretty {:users {:$ {:limit 1
:after end-cursor
Expand Down Expand Up @@ -4030,18 +4029,33 @@

;; Create conversations, messages
convo-id-aid (random-uuid)
group-id-aid (random-uuid)
convo-group-aid (random-uuid)
msg-id-aid (random-uuid)
msg-time-aid (random-uuid)
msg-convos-id (random-uuid)
_ (tx/transact! (aurora/conn-pool)
(attr-model/get-by-app-id app-id)
app-id
[[:add-attr {:id convo-id-aid
[[:add-attr {:id group-id-aid
:forward-identity [(random-uuid) "groups" "id"]
:unique? true
:index? true
:value-type :blob
:cardinality :one}]
[:add-attr {:id convo-id-aid
:forward-identity [(random-uuid) "conversations" "id"]
:unique? true
:index? true
:value-type :blob
:cardinality :one}]
[:add-attr {:id convo-group-aid
:forward-identity [(random-uuid) "conversations" "groups"]
:reverse-identity [(random-uuid) "groups" "conversations"]
:unique? false
:index? false
:value-type :ref
:cardinality :one}]
[:add-attr {:id msg-id-aid
:forward-identity [(random-uuid) "messages" "id"]
:unique? true
Expand All @@ -4063,10 +4077,10 @@
:value-type :ref
:cardinality :one}]])

;; add 3 conversations, with 5 messages each

add-conversation (fn [c-id]
[[:add-triple c-id convo-id-aid (str c-id)]])
;; add 1 group, 3 conversations, with 5 messages each
add-conversation (fn [g-id c-id]
[[:add-triple c-id convo-id-aid (str c-id)]
[:add-triple c-id convo-group-aid (str g-id)]])

add-message (fn [convo-id m-id t]
[[:add-triple m-id msg-id-aid m-id]
Expand All @@ -4083,9 +4097,10 @@
:msg-idx i})
(repeatedly 20 random-uuid))})))

group-id (random-uuid)
steps-of-steps (map
(fn [{:keys [convo-id convo-idx messages]}]
(concat (add-conversation convo-id)
(concat (add-conversation group-id convo-id)
(mapcat (fn [{:keys [msg-id msg-idx]}]
(add-message convo-id msg-id (+ convo-idx msg-idx)))
messages)))
Expand All @@ -4099,6 +4114,7 @@
steps))

steps-of-steps)

ctx (make-ctx)]
(is (= 3
(-> (instaql-nodes->object-tree
Expand All @@ -4115,6 +4131,16 @@
{:conversations {:$ {:limit 2
:where {:messages.time {:$gte 0}}}}}))
(get "conversations")
count)))

(is (= 2
(-> (instaql-nodes->object-tree
ctx
(iq/query ctx
{:conversations {:$ {:limit 2
:where {:groups group-id
:messages.time {:$gte 0}}}}}))
(get "conversations")
count)))))))

(comment
Expand Down

0 comments on commit 9645d3b

Please sign in to comment.