Permalink
Browse files

Fix bug in checking of empty logs.

  • Loading branch information...
1 parent 8b3cc99 commit a464adbeb2873fe1e53519758d1c69c669b775cc @mmcgrana committed Jul 6, 2010
Showing with 20 additions and 14 deletions.
  1. +10 −12 src/clj/fleetdb/embedded.clj
  2. +4 −1 src/clj/fleetdb/file.clj
  3. +6 −1 test/fleetdb/embedded_test.clj
View
22 src/clj/fleetdb/embedded.clj
@@ -15,18 +15,16 @@
(first (core/query* db q)))
(defn- check-log [read-path]
- (let [f (File. #^String read-path)
- raf (RandomAccessFile. f "rw")
- last-i (dec (.length f))]
- (.seek raf last-i)
- (when-not (= 10 (.read raf))
- (loop [i last-i]
- (.seek raf i)
- (if (= 10 (.read raf))
- (let [truncate-to (inc i)]
- (.setLength raf truncate-to)
- truncate-to)
- (recur (dec i)))))))
+ (let [f (File. #^String read-path)
+ raf (RandomAccessFile. f "rw")]
+ (loop [l (.length f)]
+ (if (= 0 l)
+ (.setLength raf 0)
+ (do
+ (.seek raf (dec l))
+ (if (= 10 (.read raf))
+ (.setLength raf l)
+ (recur (dec l))))))))
(defn- read-db [read-path]
(check-log read-path)
View
5 src/clj/fleetdb/file.clj
@@ -16,4 +16,7 @@
(assert (.renameTo (File. from) (File. to))))
(defn rm [#^String path]
- (.delete (File. path)))
+ (.delete (File. path)))
+
+(defn touch [#^String path]
+ (.createNewFile (File. path)))
View
7 test/fleetdb/embedded_test.clj
@@ -91,11 +91,16 @@
(with-dba [dba-1 (embedded/load-persistent log-path)]
(assert= 5001 (embedded/query dba-1 ["count" "elems"])))))
-(deftest "persistent: check"
+(deftest "persistent: check nonempty"
(file/rm log-path)
(with-dba [dba-0 (embedded/init-persistent log-path)]
(dotimes [i 3]
(embedded/query dba-0 ["insert" "elems" {"id" (+ 3 i)}])))
(.setLength (RandomAccessFile. (File. #^String log-path) "rw") 70)
(with-dba [dba-1 (embedded/load-persistent log-path)]
(assert= 2 (embedded/query dba-1 ["count" "elems"]))))
+
+(deftest "persistent: check empty"
+ (file/rm log-path)
+ (file/touch log-path)
+ (embedded/load-persistent log-path))

0 comments on commit a464adb

Please sign in to comment.