From f5ed1408c847be69ef1b3b1b452f8374ff581b0b Mon Sep 17 00:00:00 2001 From: dbushenko Date: Fri, 13 Oct 2017 20:35:06 +0300 Subject: [PATCH 1/2] Added idempotent function codax.store/connection --- src/codax/store.clj | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/codax/store.clj b/src/codax/store.clj index c205112..39e2c14 100644 --- a/src/codax/store.clj +++ b/src/codax/store.clj @@ -284,6 +284,11 @@ (swap! open-databases assoc path db) db))) +(defn connection [ path ] + (let [full-path (to-canonical-path-string path) + con (get @open-databases full-path)] + (or con (open-database path)))) + ;;;;; Transactions (defn- update-database! [{:keys [db root-id id-counter manifest]} nodes-offset manifest-delta nodes-by-address] From 68d2d33af6759959f98c47398df0c2607324e922 Mon Sep 17 00:00:00 2001 From: Date: Sun, 1 Apr 2018 23:21:38 +0300 Subject: [PATCH 2/2] Introduced function next-id! --- src/codax/operations.clj | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/codax/operations.clj b/src/codax/operations.clj index df80732..2b8022b 100644 --- a/src/codax/operations.clj +++ b/src/codax/operations.clj @@ -228,3 +228,31 @@ (when (empty? path) (throw (ex-info "Invalid Path" {:cause :empty-path :message "You cannot clear the empty (root) path."}))) (clear-path tx path)) + + +;;;;; + +(defn next-id! + "Function generates auto incremented id starting from 0 for the specified entity. + Parameters: + db -- opened database + entity -- the entity for which the id should be generated. + Return: + The value of the next available id for the specified entity. + Usage examples: + (next-id! db :person) => 0 + (-> db (next-id! :person)) => 1 + " + [ db entity ] + (let [n (atom nil)] + (c/with-write-transaction [db tx] + + ;; Read previous index value. If it is absent -- use 0 instead; + (reset! n (or (c/get-at! db [:codax/indicies entity]) 0)) + + ;; Create or update the next index value with incremented previous value + (c/assoc-at tx [:codax/indicies entity] (inc @n))) + + ;; Return original index value + @n)) +