Skip to content

Commit

Permalink
Switched to Web Storage for now
Browse files Browse the repository at this point in the history
  • Loading branch information
greenyouse committed Feb 25, 2015
1 parent 90e616b commit 057b70d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 56 deletions.
9 changes: 3 additions & 6 deletions examples/star-wars.cljs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
(ns example.star-wars
(:require [pldb-cache.core :as pc]
[clodexeddb.core :as cldb]
[cljs.core.logic.pldb :as pldb])
(:require-macros [cljs.core.logic.macros :as lm]
[cljs.core.logic.pldb :as pm]))

;; NOTE: IndexedDB is disabled in private browsing mode

(pm/db-rel parent Parent Child)
(pm/db-rel female Person)
(pm/db-rel male Person)
Expand All @@ -16,7 +13,7 @@
;; This allows a program to store its info persistently!
;; For reference: http://www.chartgeek.com/star-wars-family-tree/
(def test-atom
(pc/db-atom "test" "woot" :facts
(pc/db-atom "woot" :facts
[[parent "Shmi Skywalker" "Anakin Skywalker"]
[parent "Ruwee Naberrie" "Padme Amidala"]
[parent "Jorbal Naberrie" "Padme Amidala"]
Expand Down Expand Up @@ -115,7 +112,7 @@


;; What if we only care about one relationship and want to delete
;; everything else?
;; everything else? Just reset the database:
(pc/reset-facts! test-atom
[parent "???" "Jar Jar Binks"]
[female "???"]
Expand All @@ -141,4 +138,4 @@
(pc/clear! test-atom)

;; and here's how to delete the entire database:
(pc/clear! test-atom :delete true)
(pc/rm-db)
5 changes: 2 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
(defproject com.greenyouse/pldb-cache "0.1.0"
(defproject com.greenyouse/pldb-cache "0.1.0-webstorage"
:description "Clientside caching for pldb"
:url "https://github.com/greenyouse/pldb-cache"
:license {:name "BSD 2-Clause"
:url "http://www.opensource.org/licenses/BSD-2-Clause"}
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-2816"]
[org.clojure/core.logic "0.8.9"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
[com.greenyouse/clodexeddb "0.1.0"]]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]]

:profiles {:dev {:dependencies [[weasel "0.6.0-SNAPSHOT"]]}}

Expand Down
80 changes: 33 additions & 47 deletions src/pldb_cache/core.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
(ns pldb-cache.core
(:require [clodexeddb.core :as cldb]
(:require [pldb-cache.storage :as s]
[cljs.reader :as reader]
[cljs.core.logic.pldb :as pldb]
[cljs.core.async :as async :refer [put! <! chan]]
ydn.db)
[cljs.core.async :as async :refer [put! <! chan]])
(:require-macros [cljs.core.async.macros :refer [go-loop]]))

(def ^:private trans-chan (chan))
Expand All @@ -12,10 +11,9 @@
"Transactor to cache the in-memory database asynchronously"
[]
(go-loop []
(let [[db name new-db] (<! trans-chan)
val {:name name :value (pr-str new-db)}]
(try (cldb/clear db "database" name)
(cldb/add db "database" val)
(let [[store val] (<! trans-chan)]
(try (s/clear-db store)
(s/add-db store val)
(catch js/Error e
(.log js/console "pldb-cache Error: improper message to transactor")))
(recur))))
Expand All @@ -24,47 +22,31 @@
;;; DB Atom

(defprotocol IDatabase
(-pldb-load [this facts]
"Defines a new clientside database.")
(-pldb-add! [this facts])
(-pldb-remove! [this facts])
(-pldb-reset! [this facts])
(-pldb-clear! [this delete]))
(-pldb-clear! [this]))

;; state is the pldb database state, db-state is the value of cldb/setup
;; FIXME: make names for state/db-state better to avoid confusion
(deftype DBAtom [^:mutable state meta validator watches
^string db ^string store ^:mutable db-state]
(deftype DBAtom [^:mutable state meta validator watches ^string store]

IDatabase
(-pldb-load [this facts]
(transactor)
(set! db-state (cldb/setup db))
(cldb/get-query db-state "database" store
(fn [e]
(if (= e js/undefined)
(-pldb-reset! this facts)
(let [new-db (-> (get e "value")
(reader/read-string))]
(-reset! this new-db))))))
(-pldb-add! [this facts]
(let [new-db (apply pldb/db-facts state facts)]
(put! trans-chan [db-state store new-db])
(put! trans-chan [store new-db])
(-reset! this new-db)))
(-pldb-remove! [this facts]
(let [new-db (apply pldb/db-retractions state facts)]
(put! trans-chan [db-state store new-db])
(put! trans-chan [store new-db])
(-reset! this new-db)))
(-pldb-reset! [this facts]
(let [new-db (apply pldb/db facts)]
(put! trans-chan [db-state store new-db])
(put! trans-chan [store new-db])
(-reset! this new-db)))
(-pldb-clear! [this delete]
(cldb/rm-db db)
(set! state nil) ;clear the atom
(set! db-state nil)
(if-not delete
(-pldb-load this nil))) ;reload the database (just clears stores)
(-pldb-clear! [this]
(s/clear-db store)
(put! trans-chan [store {}]) ;reset to an empty pldb database
(-reset! this {}))

IReset
(-reset! [this new-value]
Expand Down Expand Up @@ -108,15 +90,13 @@

(defn db-atom
"This is a variation of the standard Clojure atom that holds a pldb database
in memory and caches any pldb updates to clientside storage. The first
argument, db, is the name of a clientside database for your project. The second
argument, store, is the name of a pldb store. Generally, a project should only
use one db to hold all of its pldb stores.
in memory and caches any pldb updates to clientside storage. The first argument
is the name of a pldb store and is used as the key name for localStorage.
A new :facts keyword is used to define a template pldb database wrapped in a
sequential data structure (like a vector or list):
(db-atom \"db\" \"store\"
(db-atom \"store\"
:facts [[some db items] [some more items]]
:meta metadata-map
:validator validate-fn)
Expand All @@ -126,10 +106,14 @@
the pldb store will ignore these facts and use the previous database state
instead."
;; HACK: facts shouldn't have to be a key, hard to get around that though
([db store & {:keys [meta validator facts]}]
(let [a (DBAtom. nil meta validator nil db store nil)]
(-pldb-load a facts)
a)))
[store & {:keys [meta validator facts]}]
(transactor)
(let [state (if (s/get-db store)
(reader/read-string (s/get-db store))
(let [new-db (apply pldb/db facts)]
(put! trans-chan [store new-db])
new-db))]
(DBAtom. state meta validator nil store)))

(defn add-facts!
"Adds new facts and caches the new state."
Expand All @@ -147,9 +131,11 @@
(-pldb-reset! a facts))

(defn clear!
"Clears the atom and all associated data stores from clientside storage but
keeps the clientside db. With the option ':delete true', the clientside db
and all data will be permenantely deleted (making all connected db-atoms
unsuable)."
[a & {:keys [delete]}]
(-pldb-clear! a delete))
"Clears the atom and all associated clientside storage."
[a]
(-pldb-clear! a))

(defn rm-db
"Removes all clientside data (meant only for things like uninstalling a program)."
[]
(s/rm-db))
18 changes: 18 additions & 0 deletions src/pldb_cache/storage.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns pldb-cache.storage)

;; clear, getItem, key, removeItem, setItem
(defn add-db
[store value]
(js/window.localStorage.setItem store value))

(defn rm-db
[]
(js/window.localStorage.clear))

(defn clear-db
[store]
(js/window.localStorage.removeItem store))

(defn get-db
[store]
(js/window.localStorage.getItem store))

0 comments on commit 057b70d

Please sign in to comment.