Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/codax/operations.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))

5 changes: 5 additions & 0 deletions src/codax/store.clj
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@
false)))


(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]
Expand Down