             [com.stuartsierra.component :as component]
             [onyx.peer.window-state :as ws]
             [onyx.state.protocol.db :as db]
+            [onyx.peer.grouping :as g]
             [taoensso.timbre :refer [fatal info]])
   (:import [java.util.concurrent.locks LockSupport]))
 


(defmulti process-store 
  (fn [[cmd] _ _]
    ;(println "CMD" cmd)
    cmd)) 

; (defn drop-prior-db [st event]

    (assoc-in st
              (state-key replica-version event)
              {:state-indices (ws/state-indices event)
               :grouped? (g/grouped-task? (:onyx.core/task-map event))
               :db (db/open-db-reader peer-config exported serializers)})))

(defmethod process-store :created-db
  [[_ replica-version event exported] state peer-config]
  (swap! state add-new-db event replica-version peer-config exported))

(defmethod process-store :drop-db 
  [[_ replica-version event] state peer-config]
  (let [serializers (onyx.state.serializers.utils/event->state-serializers event)
        k (state-key replica-version event)
        store (get @state k)]
    (dissoc-db state event replica-version)
