kv store implementation over realm

Former-commit-id: 95d0f566d7
This commit is contained in:
michaelr 2016-03-17 20:15:24 +02:00
parent 62642eadc1
commit 85f7a08380
7 changed files with 47 additions and 28 deletions

View File

@ -17,7 +17,8 @@
[messenger.comm.intercom :as intercom :refer [load-user-phone-number [messenger.comm.intercom :as intercom :refer [load-user-phone-number
load-user-whisper-identity]] load-user-whisper-identity]]
[messenger.protocol.protocol-handler :refer [make-handler]] [messenger.protocol.protocol-handler :refer [make-handler]]
[syng-im.protocol.api :refer [init-protocol]])) [syng-im.protocol.api :refer [init-protocol]]
[messenger.init :refer [init-simple-store]]))
(def app-registry (.-AppRegistry js/React)) (def app-registry (.-AppRegistry js/React))
@ -62,6 +63,7 @@
(defonce app-root (om/factory RootNode)) (defonce app-root (om/factory RootNode))
(defn init [] (defn init []
(init-simple-store)
(pubsub/setup-pub-sub) (pubsub/setup-pub-sub)
(init-protocol (make-handler)) (init-protocol (make-handler))
(load-user-phone-number) (load-user-phone-number)

6
src/messenger/init.cljs Normal file
View File

@ -0,0 +1,6 @@
(ns messenger.init
(:require [messenger.persistence.simple-kv-store :as kv]
[messenger.state :as state]))
(defn init-simple-store []
(swap! state/app-state assoc-in state/simple-store-path (kv/->SimpleKvStore)))

View File

@ -1,6 +1,6 @@
(ns messenger.models.protocol (ns messenger.models.protocol
(:require [messenger.state :as state] (:require [messenger.state :as state]
[messenger.persistence.realm :as r])) [syng-im.protocol.state.storage :as s]))
(defn set-initialized [initialized?] (defn set-initialized [initialized?]
(swap! state/app-state assoc-in state/protocol-initialized-path initialized?)) (swap! state/app-state assoc-in state/protocol-initialized-path initialized?))
@ -8,12 +8,7 @@
;; TODO at least the private key has to be encrypted with user's password ;; TODO at least the private key has to be encrypted with user's password
(defn update-identity [identity] (defn update-identity [identity]
(r/write (s/put (state/kv-store) :identity identity))
(fn []
(r/create :kv-store {:key :identity
:value (str identity)} true))))
(defn current-identity [] (defn current-identity []
(-> (r/get-by-field :kv-store :key :identity) (s/get (state/kv-store) :identity))
(r/single-cljs)
(r/decode-value)))

View File

@ -61,23 +61,9 @@
(defn decode-value [{:keys [key value]}] (defn decode-value [{:keys [key value]}]
(read-string value)) (read-string value))
(comment (defn delete [obj]
(use 'figwheel-sidecar.repl-api)
(cljs-repl)
(def x (-> (get-by-field :kv-store :key :identity)
(single)))
(aget x 1)
(write (fn [] (write (fn []
(.delete realm (-> (get-by-field :kv-store :key :identity) (.delete realm obj))))
(single)))))
(log/info (.keys js/Object realm))
(log/info (clj->js opts))
(clj->js (clj->js {:a [{:b 123}]}))
(comment
) )

View File

@ -0,0 +1,26 @@
(ns messenger.persistence.simple-kv-store
(:require [syng-im.protocol.state.storage :as st]
[messenger.persistence.realm :as r]))
(defrecord SimpleKvStore []
st/Storage
(put [_ key value]
(r/write
(fn []
(r/create :kv-store {:key key
:value (str value)} true))))
(get [_ key]
(-> (r/get-by-field :kv-store :key key)
(r/single-cljs)
(r/decode-value)))
(contains-key? [_ key]
(= 0
(.-length (r/get-by-field :kv-store :key key))))
(delete [_ key]
(-> (r/get-by-field :kv-store :key key)
(r/single)
(r/delete))))
(comment
)

View File

@ -2,12 +2,13 @@
(:require [syng-im.utils.logging :as log] (:require [syng-im.utils.logging :as log]
[messenger.constants :refer [ethereum-rpc-url]] [messenger.constants :refer [ethereum-rpc-url]]
[messenger.comm.intercom :refer [protocol-initialized]] [messenger.comm.intercom :refer [protocol-initialized]]
[messenger.models.protocol :refer [current-identity]])) [messenger.models.protocol :refer [current-identity]]
[messenger.state :refer [kv-store]]))
(defn make-handler [] (defn make-handler []
{:ethereum-rpc-url ethereum-rpc-url {:ethereum-rpc-url ethereum-rpc-url
:identity (current-identity) :identity (current-identity)
:storage nil :storage (kv-store)
:handler (fn [{:keys [event-type] :as event}] :handler (fn [{:keys [event-type] :as event}]
(log/info "Event:" (clj->js event)) (log/info "Event:" (clj->js event))
(case event-type (case event-type

View File

@ -38,5 +38,8 @@
(def pub-sub-path [:channels :pub-sub-publication]) (def pub-sub-path [:channels :pub-sub-publication])
(def user-notification-path [:user-notification]) (def user-notification-path [:user-notification])
(def protocol-initialized-path [:protocol-initialized]) (def protocol-initialized-path [:protocol-initialized])
(def simple-store-path [:simple-store])
(defn pub-sub-publisher [app] (get-in app pub-sub-bus-path)) (defn pub-sub-publisher [app] (get-in app pub-sub-bus-path))
(defn kv-store []
(get-in @app-state simple-store-path))