This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclj_bdb.clj
48 lines (38 loc) · 1.7 KB
/
clj_bdb.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(ns clj-bdb
(:import (com.sleepycat.je Database DatabaseConfig Environment EnvironmentConfig DatabaseEntry OperationStatus LockMode))
(:import java.io.File)
(:import com.sleepycat.bind.tuple.TupleBinding)
)
;------------------ HELPERS
(defn- to-database-entry [what]
(let [binder (TupleBinding/getPrimitiveBinding (.getClass what))]
(if binder
(let [result (DatabaseEntry.)] (.objectToEntry binder what result) result)
(DatabaseEntry. what))))
;------------------ PUBLIC API
(defn open-or-create [env-path name]
(let [db-env (Environment. (File. env-path) (doto (EnvironmentConfig.) (.setAllowCreate true)))]
(.openDatabase db-env nil name (doto (DatabaseConfig.) (.setAllowCreate true)))))
(defn db-sync [#^Database db]
(doto db .sync))
(defn db-close [#^Database db]
(.close db))
(defn db-put [#^Database db key value]
(.put db nil (to-database-entry key) (DatabaseEntry. value))
db)
(defn db-get [#^Database db key]
(let [value (DatabaseEntry.)]
(when (= OperationStatus/SUCCESS (.get db nil (to-database-entry key) value LockMode/DEFAULT))
(.getData value))))
(defn db-delete-record [#^Database db key]
(doto db (.delete nil (to-database-entry key))))
(defn db-fetch-all [#^Database db]
"Lazily loads all content of the db. Returns a sequence of vectors [key value]"
(let [found-key (DatabaseEntry.) found-value (DatabaseEntry.) cursor (.openCursor db nil nil)]
(letfn [(get-next []
(if (not= OperationStatus/SUCCESS (.getNext cursor found-key found-value LockMode/DEFAULT))
(.close cursor)
(map #(.getData %) [found-key found-value])))
(lazy-fetch [] (lazy-seq (when-let [current (get-next)] (cons current (lazy-fetch)))))]
(lazy-fetch))
))