Last active
June 2, 2021 09:23
-
-
Save kordano/b7fae1b8855615f6009b1551dec75cc8 to your computer and use it in GitHub Desktop.
Upsert with noise
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
| (require '[datahike.api :as d]) | |
| (defn random-uuid [] | |
| (java.util.UUID/randomUUID)) | |
| (def ascii-ish | |
| (map char (concat (range 48 58) (range 65 91) (range 97 123)))) | |
| (defn random-char [] | |
| (rand-nth ascii-ish)) | |
| (defn random-string [length] | |
| (apply str (repeatedly length random-char))) | |
| (defn ent-ids [db] | |
| (d/q '[:find [?e ...] | |
| :where | |
| [?e :ent/id]] | |
| db)) | |
| (defn active-status [db id] | |
| (d/q '[:find ?v ?t ?op | |
| :in $ ?e | |
| :where [?e :ent/active? ?v ?t ?op]] | |
| db id)) | |
| (defn active-status-history [db id] | |
| (->> (active-status (d/history db) id) | |
| (sort (fn [[_ t1 _] [_ t2 _]] (< t1 t2))))) | |
| (defn tx-history [db id tx] | |
| (d/q '[:find ?a ?v ?t ?op | |
| :in $ ?e ?t | |
| :where | |
| [?e ?a ?v ?t ?op]] (d/history db) id tx)) | |
| (defn init-data [conn] | |
| (d/transact conn {:tx-data [{:db/ident :ent/id | |
| :db/valueType :db.type/uuid | |
| :db/cardinality :db.cardinality/one | |
| :db/unique :db.unique/identity | |
| :db/doc "The entity ID."} | |
| {:db/ident :ent/active? | |
| :db/valueType :db.type/boolean | |
| :db/cardinality :db.cardinality/one | |
| :db/doc "Whether the entity is active."} | |
| {:db/ident :meta/space-taker | |
| :db/valueType :db.type/string | |
| :db/cardinality :db.cardinality/one | |
| :db/doc "Takes up some space in the db"}]}) | |
| (d/transact conn {:tx-data (map (fn [_] | |
| {:ent/id (random-uuid)}) | |
| (range 8))}) | |
| (d/transact conn {:tx-data (map (fn [_] | |
| {:meta/space-taker (random-string 250)}) | |
| (range 1000))}) | |
| (d/transact conn {:tx-data (map (fn [eid] | |
| {:db/id eid | |
| :ent/active? true}) | |
| (ent-ids @conn))}) | |
| (d/transact conn {:tx-data (->> (ent-ids @conn) | |
| sort | |
| (take 5) | |
| (map (fn [eid] | |
| [:db/add eid :ent/active? false])))})) | |
| ;; FILE | |
| (def file-cfg {:store {:backend :file :path "/tmp/upsert-with-noise"}}) | |
| (d/delete-database file-cfg) | |
| (when-not (d/database-exists? file-cfg) | |
| (d/create-database file-cfg)) | |
| (def file-conn (d/connect file-cfg)) | |
| (init-data file-conn) | |
| (def cached-file-db @file-conn) | |
| (def fresh-file-db @(d/connect file-cfg)) | |
| (active-status fresh-file-db 4);; => #{[true 536870916 true]} | |
| (active-status cached-file-db 4);; => #{[false 536870917 true]} | |
| (active-status-history fresh-file-db 4);; => ([true 536870916 true] [false 536870916 false] [false 536870917 true]) | |
| (active-status-history cached-file-db 4);; => ([true 536870916 true] [false 536870917 true] [true 536870917 false]) | |
| (tx-history fresh-file-db 4 536870917);; => #{[:ent/active? false 536870917 true]} | |
| (tx-history cached-file-db 4 536870917);; => #{[:ent/active? true 536870917 false] [:ent/active? false 536870917 true]} | |
| ;; MEM | |
| (def mem-cfg {:store {:backend :mem :id "upsert-with-noise"}}) | |
| (d/delete-database mem-cfg) | |
| (when-not (d/database-exists? mem-cfg) | |
| (d/create-database mem-cfg)) | |
| (def mem-conn (d/connect mem-cfg)) | |
| (init-data mem-conn) | |
| (def cached-mem-db @mem-conn) | |
| (def fresh-mem-db @(d/connect mem-cfg)) | |
| (active-status fresh-mem-db 4);; => #{[false 536870917 true]} | |
| (active-status cached-mem-db 4);; => #{[false 536870917 true]} | |
| (active-status-history fresh-mem-db 4);; => ([true 536870916 true] [false 536870917 true] [true 536870917 false]) | |
| (active-status-history cached-mem-db 4);; => ([true 536870916 true] [false 536870917 true] [true 536870917 false]) | |
| (tx-history fresh-mem-db 4 536870917);; => #{[:ent/active? true 536870917 false] [:ent/active? false 536870917 true]} | |
| (tx-history cached-mem-db 4 536870917);; => #{[:ent/active? true 536870917 false] [:ent/active? false 536870917 true]} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment