Skip to content

Commit

Permalink
- replay the transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
le.yul committed Jun 29, 2015
1 parent c7e4f68 commit db190b1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/imdb/boot.clj
Expand Up @@ -23,7 +23,18 @@
(swap! before-fs conj fstart)
(swap! after-fs conj fstop))

(defn- clear

(defn clear-state
[]
(alter-var-root #'state (constantly (atom {}))))

(defn clear-index
[]
(doseq [c @state]
(when (.endsWith (str (first c)) "idx")
(swap! state dissoc (first c)))))

(defn clear
[]
(alter-var-root #'state (constantly (atom {})))
(alter-var-root #'after-fs (constantly (atom [])))
Expand All @@ -49,7 +60,7 @@
(swap! after-fs conj f))

(defn stop! []
(if-not (empty? @state)
(when-not (empty? @state)
(reduce (fn [r f]
(f state))
state
Expand Down
28 changes: 28 additions & 0 deletions src/imdb/core.clj
Expand Up @@ -6,6 +6,7 @@
[imdb.boot :as b]
[imdb.schema :as sc]
[imdb.query :as query]
[clj-leveldb :as cl]
)
(:use [clojure.test]))

Expand All @@ -31,6 +32,11 @@
(if-let [pieces (cmd/cmd-to-pieces cmd)]
(tx/run-tx pieces handle-pieces) ))

(defn replay!
[]
(doseq [i (cl/iterator (b/get-state :log-db))]
(handle-pieces (second i))))

(def schemas
{:user {:name [:string :index :uniqure]
:event [:string :index]
Expand All @@ -46,12 +52,16 @@
(start! [this]
(b/attach :schemas schemas)
(b/attach :piece-name-ids (sc/store-piece-name-ids schemas))
(replay!)
(println "starting imdb"))
(stop! [this]
(println "stopping imdb")))






(def query-sample
{:entity :user
:query [:and
Expand Down Expand Up @@ -113,3 +123,21 @@
(is (= '(111111) (:idx (q imdb query-sample1))))
(is (= '(111119 111112) (:idx (q imdb query-end-with))))
(stop! imdb))))


(deftest test-replay
(testing ""
(let [cmds test-data
imdb (SimpleImdb. schemas)]
(start! imdb)
(doseq [cmd cmds]
(pub imdb cmd))
(is (not (nil? (b/get-state ":user-:name-vidx"))))
(b/clear-index)
(is (nil? (b/get-state ":user-:name-vidx")))
(stop! imdb)
(start! imdb)
(is (= '(111113) (:idx (q imdb query-sample))))
(is (= '(111111) (:idx (q imdb query-sample1))))
(is (= '(111119 111112) (:idx (q imdb query-end-with))))
(stop! imdb))))
4 changes: 2 additions & 2 deletions src/imdb/warmup.clj
Expand Up @@ -6,7 +6,7 @@
(defn start-tx-db
[state]
(b/attach :log-db (leveldb/create-db
(b/get-state :tx-db-path "/tmp/tx-db4")
(b/get-state :tx-db-path "/tmp/tx-db10")
{:key-encoder cvt/->bytes
:val-encoder cvt/->bytes
:key-decoder cvt/->long
Expand All @@ -15,7 +15,7 @@
(defn start-schema-db
[state]
(b/attach :schema-db (leveldb/create-db
(b/get-state :schema-db-path "/tmp/schema-db4")
(b/get-state :schema-db-path "/tmp/schema-db10")
{:key-encoder cvt/->bytes
:val-encoder cvt/->bytes
:key-decoder cvt/->long
Expand Down

0 comments on commit db190b1

Please sign in to comment.