Skip to content

Commit

Permalink
Switch to pluggable storage system
Browse files Browse the repository at this point in the history
  • Loading branch information
pashields committed Jan 16, 2014
1 parent c3a7385 commit b54c8d0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
4 changes: 3 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
[org.clojure/core.typed "0.2.19"]
[org.clojure/algo.generic "0.1.1"]]
:dev-dependencies [[reiddraper/simple-check "0.5.3"]]
:core.typed {:check [graf-znak.core]})
:core.typed {:check [graf-znak.core
graf-znak.hook-storage
graf-znak.atom-storage]})
25 changes: 25 additions & 0 deletions src/graf_znak/atom_storage.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns graf-znak.atom-storage
(:require [clojure.core.typed :refer :all]
[graf-znak.hook-storage :refer :all]))

(defn> safe-update
:- (Map (Coll Any) Number)
[coll :- (Map (Coll Any) Number) k :- (Coll Any)]
(let [new-val (inc (get coll k 0))]
(assoc coll k new-val)))

(ann-datatype AtomStorage [storage :- (Atom1 (Map (Coll Any) Number))])
(deftype AtomStorage [storage]
HookStorage
(inc-hook
[this group]
(swap! storage safe-update group))
(get-groups
[this]
@storage))

(defn> factory
:- AtomStorage
[]
(let [base {}]
(AtomStorage. (atom (ann-form base (Map (Coll Any) Number))))))
37 changes: 14 additions & 23 deletions src/graf_znak/core.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
(ns graf-znak.core
(:require [clojure.core.typed :refer :all]
[clojure.algo.generic.functor :refer :all]
[clojure.core.reducers :as r])
[clojure.core.reducers :as r]
[graf-znak.hook-storage :refer :all])
(:import [java.util.concurrent.atomic AtomicInteger]))

;; Type aliases
(def-alias hook-type (Coll (U Keyword String)))
(def-alias hooks-type (Seq hook-type))
(def-alias state-type (Map hook-type
(Atom1 (Map (Coll Any) Int))))
HookStorage))
(def-alias input-type (Map (U Keyword String) Any))

;; Annotations
Expand All @@ -30,11 +31,11 @@
"Processes a single input for a single hook"
:- Any
[state :- state-type hook :- hook-type val :- input-type]
(let [groups (get state hook)
(let [storage (get state hook)
group (map #(get val %) hook)]
(assert (not (nil? groups)))
(assert (not (nil? storage)))
(when (not-any? nil? group)
(swap! groups (fn> [coll :- (Map (Coll Any) Int) k :- (Coll Any)] (assoc coll k (inc (get coll k 0)))) group))))
(inc-hook storage group))))

(defn> process
"Processes a single input for n hooks"
Expand All @@ -48,38 +49,28 @@
[hook :- hook-type]
(process-hook state hook val)) hooks)))))

(defn> int-value
:- Int
[^AtomicInteger value :- AtomicInteger]
(.intValue value))

(defn> check-hook
"Returns all groups and their respective counts for a given hook."
:- (Map (Coll Any) Int)
:- (Map (Coll Any) Number)
[state :- state-type hook :- hook-type]
(let [categories (get state hook)]
(assert (not (nil? categories)))
@categories))

(defn> hook-state-factory
:- (Map (Coll Any) Int)
[]
{})
(let [storage (get state hook)]
(assert (not (nil? storage)))
(get-groups storage)))

(def-alias send-type (Fn [input-type -> Number]))
(def-alias check-type (Fn [hook-type -> (Map (Coll Any) Int)]))
(def-alias check-type (Fn [hook-type -> (Map (Coll Any) Number)]))
(def-alias net-type (HMap :mandatory {:send send-type :check check-type}))

(defn> create-net
"Generates a new net."
:- net-type
[hooks :- hooks-type]
(let [state (zipmap hooks (repeatedly #(atom (hook-state-factory))))]
[hooks :- hooks-type storage-factory :- (Fn [-> HookStorage])]
(let [state (zipmap hooks (repeatedly storage-factory))]
{:send (partial process hooks state)
:check (partial check-hook state)}))

(defn> check-net
:- (Map (Coll Any) Int)
:- (Map (Coll Any) Number)
[net :- net-type hook :- hook-type]
((:check net) hook))

Expand Down
13 changes: 13 additions & 0 deletions src/graf_znak/hook_storage.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns graf-znak.hook-storage
(:require [clojure.core.typed :refer :all]))

(ann-protocol HookStorage
inc-hook [HookStorage (Coll Any) -> Any]
get-groups [HookStorage -> (Map (Coll Any) Number)])
(defprotocol> HookStorage
(inc-hook
[storage group]
"Increment or initialize the count for a given group")
(get-groups
[storage]
"Retrieves all known groups and their counts"))
3 changes: 2 additions & 1 deletion test/graf_znak/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns graf-znak.core-test
(:require [clojure.test :refer :all]
[graf-znak.core :refer :all]
[graf-znak.atom-storage :as atom-storage]
[simple-check.core :as sc]
[simple-check.generators :as gen]
[simple-check.properties :as prop]
Expand Down Expand Up @@ -35,7 +36,7 @@

(defn graf-groups
[hook-colls inputs]
(let [net (create-net [hook-colls])]
(let [net (create-net [hook-colls] atom-storage/factory)]
(doseq [i inputs] (send-net net i))
(check-net net hook-colls)))

Expand Down

0 comments on commit b54c8d0

Please sign in to comment.