Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make it safe to call open or close multiple times

  • Loading branch information...
commit f13f86efaf33bdb5ae201b6c4c37f2e6d27fef3a 1 parent 0e3d617
Alan Malloy authored September 26, 2012
33  src/masai/tokyo.clj
@@ -49,6 +49,10 @@
49 49
      (cons key (key-seq* hdb))
50 50
      nil)))
51 51
 
  52
+;; tokyocabinet makes it an error to open an open db, or close a closed one.
  53
+;; we'd prefer that it be a no-op, so we just ignore the request.
  54
+(def ^:private open-paths (atom #{}))
  55
+
52 56
 (defrecord DB [^HDB hdb opts key-format]
53 57
   Object
54 58
   (toString [this]
@@ -56,19 +60,24 @@
56 60
 
57 61
   masai.db/DB
58 62
   (open [this]
59  
-    (let [path (:path opts)
60  
-          bnum (or (:bnum opts)  0)
61  
-          apow (or (:apow opts) -1)
62  
-          fpow (or (:fpow opts) -1)]
63  
-      (.mkdirs (.getParentFile (java.io.File. ^String path)))
64  
-      (check (.tune hdb bnum apow fpow (tflags opts)))
65  
-      (when-let [rcnum (:cache opts)]
66  
-        (check (.setcache hdb rcnum)))
67  
-      (when-let [xmsiz (:xmsiz opts)]
68  
-        (check (.setxmsiz hdb xmsiz)))
69  
-      (check (.open hdb path (oflags opts)))))
  63
+    (let [path (:path opts)]
  64
+      (when-not (@open-paths path)
  65
+        (let [bnum (or (:bnum opts)  0)
  66
+              apow (or (:apow opts) -1)
  67
+              fpow (or (:fpow opts) -1)]
  68
+          (.mkdirs (.getParentFile (java.io.File. ^String path)))
  69
+          (check (.tune hdb bnum apow fpow (tflags opts)))
  70
+          (when-let [rcnum (:cache opts)]
  71
+            (check (.setcache hdb rcnum)))
  72
+          (when-let [xmsiz (:xmsiz opts)]
  73
+            (check (.setxmsiz hdb xmsiz)))
  74
+          (check (.open hdb path (oflags opts)))
  75
+          (swap! open-paths conj path)))))
70 76
   (close [this]
71  
-    (.close hdb))
  77
+    (let [path (:path opts)]
  78
+      (when (@open-paths path)
  79
+        (.close hdb)
  80
+        (swap! open-paths disj path))))
72 81
   (sync! [this]
73 82
     (.sync  hdb))
74 83
   (optimize! [this]
37  src/masai/tokyo_sorted.clj
@@ -55,24 +55,33 @@
55 55
   `(fn [^BDBCUR cur#]
56 56
      (. cur# ~method ~@args)))
57 57
 
  58
+;; tokyocabinet makes it an error to open an open db, or close a closed one.
  59
+;; we'd prefer that it be a no-op, so we just ignore the request.
  60
+(def ^:private open-paths (atom #{}))
  61
+
58 62
 (defrecord DB [^BDB bdb opts key-format]
59 63
   masai.db/DB
60 64
   (open [db]
61  
-    (let [path  (:path opts)
62  
-          bnum  (or (:bnum opts)  0)
63  
-          apow  (or (:apow opts) -1)
64  
-          fpow  (or (:fpow opts) -1)
65  
-          lmemb (or (:lmemb opts) 0)
66  
-          nmemb (or (:nmemb opts) 0)]
67  
-      (.mkdirs (.getParentFile (java.io.File. ^String path)))
68  
-      (check (.tune bdb lmemb nmemb bnum apow fpow (tflags opts)))
69  
-      (when-let [[lcnum rcnum] (:cache opts)]
70  
-        (check (.setcache bdb lcnum rcnum)))
71  
-      (when-let [xmsiz (:xmsiz opts)]
72  
-        (check (.setxmsiz bdb xmsiz)))
73  
-      (check (.open bdb path (oflags opts)))))
  65
+    (let [path  (:path opts)]
  66
+      (when-not (@open-paths path)
  67
+        (let [bnum  (or (:bnum opts)  0)
  68
+              apow  (or (:apow opts) -1)
  69
+              fpow  (or (:fpow opts) -1)
  70
+              lmemb (or (:lmemb opts) 0)
  71
+              nmemb (or (:nmemb opts) 0)]
  72
+          (.mkdirs (.getParentFile (java.io.File. ^String path)))
  73
+          (check (.tune bdb lmemb nmemb bnum apow fpow (tflags opts)))
  74
+          (when-let [[lcnum rcnum] (:cache opts)]
  75
+            (check (.setcache bdb lcnum rcnum)))
  76
+          (when-let [xmsiz (:xmsiz opts)]
  77
+            (check (.setxmsiz bdb xmsiz)))
  78
+          (check (.open bdb path (oflags opts)))
  79
+          (swap! open-paths conj path)))))
74 80
   (close [db]
75  
-    (.close bdb))
  81
+    (let [path (:path opts)]
  82
+      (when (@open-paths path)
  83
+        (.close bdb)
  84
+        (swap! open-paths disj path))))
76 85
   (sync! [db]
77 86
     (.sync  bdb))
78 87
   (optimize! [db]

0 notes on commit f13f86e

Please sign in to comment.
Something went wrong with that request. Please try again.