Skip to content

Instantly share code, notes, and snippets.

@mrrodriguez
Created January 4, 2024 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrrodriguez/baaa6bf73e8a3b412970c648e208c293 to your computer and use it in GitHub Desktop.
Save mrrodriguez/baaa6bf73e8a3b412970c648e208c293 to your computer and use it in GitHub Desktop.
Externally batching and updating the working memory state
(defrecord PrevFetchedItems [item-ids])
(defrecord ToFetch [items])
(r/defrule find-items-to-fetch
[PrevFetchedItems (= ?item-ids item-ids)]
[?items <- (acc/all) :from [Item (not (contains? ?items id))]]
=>
(r/insert! (->ToFetch ?items)))
(r/defquery get-items-to-fetch []
[?to-fetch <- ToFetch])
(defn run-fetch-rules [new-items prev-fetched-ids]
(let [s (-> (r/mk-session)
(r/insert-all new-items)
(r/insert (->PrevFetchedItems prev-fetched-ids))
r/fire-rules)]
(-> s
(r/query get-items-to-fetch)
:?to-fetch
:items)))
(defn fetch-items! [items]
(do-fetching items))
(defn batched-fetching [batches]
(reduce (fn [[all-fetched-vals prev-fetched-ids] items]
(let [to-fetch-items (run-fetch-rules items prev-fetched-ids)
fetched-vals (fetch-items! to-fetch-items)]
[(into all-fetched-vals fetched-vals)
(into prev-fetched-ids (map :id) to-fetch-items)]))
[[] #{}]
batches))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment