Commits

Anonymous committed c01b7ea

Initial commit

Comments (0)

Files changed (4)

+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
+(defproject jdbam "0.0.1-SNAPSHOT"
+  :description "Clojure JDBM binding"
+  :dependencies [[org.clojure/clojure "1.4.0"]
+                 [org.apache.jdbm/jdbm "3.0-alpha4"]])

src/jdbam/core.clj

+(ns jdbam.core
+  (:import [org.apache.jdbm DBMaker]))
+
+(defn open-db [file & {:keys [close-on-exit] :or {:close-on-exit true}}]
+  (let [maker (DBMaker/openFile file)
+        maker (if close-on-exit
+                (.closeOnExit maker)
+                maker)]
+    (.make maker)))
+
+(defn get-collection [db coll]
+  (let [coll (name coll)]
+    {:db db
+     :map (or (.getTreeMap db coll)
+              (.createTreeMap db coll))}))
+
+(defmacro with-transaction [db & body]
+  `(let [db# ~db]
+     (try
+       (when (do ~@body)
+         (.commit db#)
+         true)
+       (catch Throwable e#
+         (.rollback db#)
+         (throw e#)))))
+
+(defn put! [coll key value]
+  (with-transaction (:db coll)
+    (.put (:map coll) (name key) (pr-str value))
+    true))
+
+(defn fetch [coll key & [not-found]]
+  (if-let [value (.get (:map coll) (name key))]
+    (read-string value)
+    not-found))
+
+(defn update! [coll key update & args]
+  (let [key (name key)
+        coll*  (:map coll)
+        update (fn [value]
+                 (pr-str (apply update value args)))
+        value  (or (.get coll* key)
+                   (.putIfAbsent coll* key (update nil)))]
+    (when value
+      (loop [value (read-string value)]
+        (or (with-transaction (:db coll)
+              (.replace coll* key (pr-str value) (update value)))
+            (recur (fetch coll key)))))))

test/jdbam/test/core.clj

+(ns jdbam.test.core
+  (:use [jdbam.core])
+  (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+  (is false "No tests have been written."))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.