Created
January 4, 2024 17:25
-
-
Save mrrodriguez/baaa6bf73e8a3b412970c648e208c293 to your computer and use it in GitHub Desktop.
Externally batching and updating the working memory state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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