Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 252 lines (197 sloc) 7.719 kb
7c720fe @cemerick dump of REPL interactions and other inline code snippets
cemerick authored
1
2 ;-----
3 (use '[com.ashafa.clutch :only (create-database with-db put-document
4 get-document delete-document)
5 :as clutch])
6
7 (def db (create-database "repl-crud"))
8
9 (put-document db {:_id "foo" :some-data "bar"})
10 ;= {:_rev "1-2bd2719826", :some-data "bar", :_id "foo"}
11 (put-document db (assoc *1 :other-data "quux"))
12 ;= {:other-data "quux", :_rev "2-9f29b39770", :some-data "bar", :_id "foo"}
13 (get-document db "foo")
14 ;= {:_id "foo", :_rev "2-9f29b39770", :other-data "quux", :some-data "bar"}
15 (delete-document db *1)
16 ;= {:ok true, :id "foo", :rev "3-3e98dd1028"}
17 (get-document db "foo")
18 ;= nil
19
20
21 ;-----
22 (clutch/create-document {:_id "foo"
23 :data ["bar" {:details ["bat" false 42]}]})
24 ;= {:_id "foo", :data ["bar" {:details ["bat" false 42]}],
25 ;= :_rev "1-6d7460947434b90bf88f033785f81cdd"}
26 (->> (get-document db "foo")
27 :data
28 second
29 :details
30 (filter number?))
31 ;= (42)
32
33
34 ;-----
35 (clutch/bulk-update (create-database "logging")
36 [{:evt-type "auth/new-user" :username "Chas"}
37 {:evt-type "auth/new-user" :username "Dave"}
38 {:evt-type "sales/purchase" :username "Chas" :products ["widget1"]}
39 {:evt-type "sales/purchase" :username "Robin" :products ["widget14"]}
40 {:evt-type "sales/RFQ" :username "Robin" :budget 20000}])
41
42
43
44 ;-----
45 (clutch/save-view "logging" "jsviews"
46 (clutch/view-server-fns :javascript
47 {:type-counts
48 {:map "function(doc) {
49 emit(doc['evt-type'], null);
50 }"
51 :reduce "function (keys, vals, rereduce) {
52 return vals.length;
53 }"}}))
54
55
56 ;-----
57 (clutch/get-view "logging" "jsviews" :type-counts {:group true})
58 ;= ({:key "auth/new-user", :value 2}
59 ;= {:key "sales/purchase", :value 2}
60 ;= {:key "sales/RFQ", :value 1})
61
62
63 ;-----
64 (->> (clutch/get-view "logging" "jsviews" :type-counts {:group true})
65 (map (juxt :key :value))
66 (into {}))
67 ;= {"auth/new-user" 2, "sales/purchase" 2, "sales/RFQ" 1}
68
69
70 ;-----
71 (use '[com.ashafa.clutch.view-server :only (view-server-exec-string)])
72
73 (clutch/configure-view-server "http://localhost:5984" (view-server-exec-string))
74 ;= ""
75
76
77 ;-----
78 (clutch/save-view "logging" "clj-views"
79 (clutch/view-server-fns :clojure
80 {:type-counts
81 {:map (fn [doc]
82 [[(:evt-type doc) nil]])
83 :reduce (fn [keys vals rereduce]
84 (count vals))}}))
85
86
87 ;-----
88 (->> (clutch/get-view "logging" "clj-views" :type-counts {:group true})
89 (map (juxt :key :value))
90 (into {}))
91 ;= {"auth/new-user" 2, "sales/purchase" 2, "sales/RFQ" 1}
92
93
94 ;-----
95 (ns eventing.types)
96
97 (derive 'sales/purchase 'sales/all)
98 (derive 'sales/purchase 'finance/accounts-receivable)
99 (derive 'finance/accounts-receivable 'finance/all)
100 (derive 'finance/all 'events/all)
101 (derive 'sales/all 'events/all)
102 (derive 'sales/RFQ 'sales/lead-generation)
103 (derive 'sales/lead-generation 'sales/all)
104 (derive 'auth/new-user 'sales/lead-generation)
105 (derive 'auth/new-user 'security/all)
106 (derive 'security/all 'events/all)
107
108
109 ;-----
110 (clutch/save-view "logging" "clj-views"
111 (clutch/view-server-fns :clojure
112 {:type-counts
113 {:map (do
114 (require 'eventing.types)
115 (fn [doc]
116 (let [concrete-type (-> doc :evt-type symbol)]
117 (for [evtsym (cons concrete-type
118 (ancestors concrete-type))]
119 [(str evtsym) nil]))))
120 :reduce (fn [keys vals rereduce]
121 (count vals))}}))
122
123 (->> (clutch/with-db "logging"
124 (clutch/get-view "clj-views" :type-counts {:group true}))
125 (map (juxt :key :value))
126 (into {}))
127 ;= {"events/all" 5,
128 ;= "sales/all" 5,
129 ;= "finance/all" 2,
130 ;= "finance/accounts-receivable" 2,
131 ;= "sales/lead-generation" 3,
132 ;= "sales/purchase" 2,
133 ;= "sales/RFQ" 1,
134 ;= "security/all" 2,
135 ;= "auth/new-user" 2}
136
137
138 ;-----
139 (clutch/create-database "changes")
140 (clutch/watch-changes "changes" :echo (partial println "changes:"))
141
142 (clutch/bulk-update "changes" [{:_id "doc1"} {:_id "doc2"}])
143 ;= [{:id "doc1", :rev "5-f36e792166"}
144 ;= {:id "doc2", :rev "3-5570e8bbb3"}]
145 ; change: {:seq 7, :id doc1, :changes [{:rev 5-f36e792166}]}
146 ; change: {:seq 8, :id doc2, :changes [{:rev 3-5570e8bbb3}]}
147 (clutch/delete-document "changes" (zipmap [:_id :_rev]
148 ((juxt :id :rev) (first *1))))
149 ;= {:ok true, :id "doc1", :rev "6-616e3df68"}
150 ; change: {:seq 9, :id doc1, :changes [{:rev 6-616e3df68}], :deleted true}
151 (clutch/stop-changes "changes" :echo)
152 ;= nil
153
154
155 ;-----
156 {:evt-type "auth/new-user" :username "Chas"}
157 {:evt-type "auth/new-user" :username "Dave"}
158 {:evt-type "sales/purchase" :username "Chas" :products ["widget1"]}
159 {:evt-type "sales/purchase" :username "Robin" :products ["widget14"]}
160 {:evt-type "sales/RFQ" :username "Robin" :budget 20000}
161
162
163 ;-----
164 (ns eventing.processing)
165
166 (derive 'sales/lead-generation 'processing/realtime)
167 (derive 'sales/purchase 'processing/realtime)
168
169 (derive 'security/all 'processing/archive)
170 (derive 'finance/all 'processing/archive)
171
172
173 ;-----
174 (clutch/save-filter "logging" "event-filters"
175 (clutch/view-server-fns :clojure
176 {:event-isa? (do
177 (require '[eventing types processing])
178 (fn [doc request]
179 (let [req-type (-> request :query :type)
180 evt-type (:evt-type doc)]
181 (and req-type evt-type
182 (isa? (symbol evt-type) (symbol req-type))))))}))
183
184
185 ;-----
186 (clutch/watch-changes "logging" :echo-leads (partial println "change:")
187 :filter "event-filters/event-isa?"
188 :type "sales/lead-generation"
189 :include_docs true)
190
191 (clutch/put-document "logging"
192 {:evt-type "sales/RFQ" :username "Lilly" :budget 20000})
193 ;= {:_id "8f264da359f887ec3e86c8d34801704b",
194 ;= :_rev "1-eb10044985c9dccb731bd5f31d0188c6",
195 ;= :budget 20000, :evt-type "sales/RFQ", :username "Lilly"}
196 ; change: {:seq 26, :id 8f264da359f887ec3e86c8d34801704b,
197 ; :changes [{:rev 1-eb10044985c9dccb731bd5f31d0188c6}],
198 ; :doc {:_id 8f264da359f887ec3e86c8d34801704b,
199 ; :_rev 1-eb10044985c9dccb731bd5f31d0188c6,
200 ; :budget 20000,
201 ; :evt-type sales/RFQ,
202 ; :username Lilly}}
203 (clutch/stop-changes "logging" :echo-leads)
204 ;= nil
205
206
207 ;-----
208 (ns eventing.processing)
209
210 (defmulti process-event :evt-type)
211
212
213 ;-----
214 (ns salesorg.event-handling
215 (use [eventing.processing :only (process-event)]))
216
217 (defmethod process-event 'sales/purchase
218 [evt]
219 (println (format "We made a sale of %s to %s!" (:products evt) (:username evt))))
220
221 (defmethod process-event 'sales/lead-generation
222 [evt]
223 (println "Add prospect to CRM system: " evt))
224
225
226 ;-----
227 (require 'eventing.processing 'salesorg.event-handling)
228
229 (clutch/watch-changes "logging" :process-events
230 #(-> %
231 :doc
232 (dissoc :_id :_rev)
233 (update-in [:evt-type] symbol)
234 eventing.processing/process-event)
235 :filter "event-filters/event-isa?"
236 :type "processing/realtime"
237 :include_docs true)
238
239 (clutch/bulk-update "logging"
240 [{:evt-type "auth/new-user" :username "Chas"}
241 {:evt-type "auth/new-user" :username "Dave"}
242 {:evt-type "sales/purchase" :username "Chas" :products ["widget1"]}
243 {:evt-type "sales/purchase" :username "Robin" :products ["widget14"]}
244 {:evt-type "sales/RFQ" :username "Robin" :budget 20000}])
245 ; Add prospect to CRM system: {:evt-type auth/new-user, :username Chas}
246 ; Add prospect to CRM system: {:evt-type auth/new-user, :username Dave}
247 ; We made a sale of ["widget1"] to Chas!
248 ; We made a sale of ["widget14"] to Robin!
249 ; Add prospect to CRM system: {:budget 20000, :evt-type sales/RFQ, :username Robin}
250
251
Something went wrong with that request. Please try again.