Skip to content

Commit 8f84bf9

Browse files
author
Daniel Neal
committed
Readme updates
1 parent fcb91e8 commit 8f84bf9

File tree

1 file changed

+153
-20
lines changed

1 file changed

+153
-20
lines changed

README.md

+153-20
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ It is the default index type for primary indexes.
205205

206206
### Index type: one-to-many
207207

208-
209208
Demonstrated above, the one-to-many index will maintain a hash-map of `key -> set` pairs, where the
210209
set contains all the items that share the key.
211210

@@ -226,25 +225,82 @@ Like a one-to-one index except that a nested hash-map of `path* -> item` is main
226225
- `id` - the id for the index in the compound
227226

228227
```clojure
229-
(-> (c/compound [{:index-type :one-to-one
230-
:id :delivery-date-product
231-
:kfn (juxt :delivery-date :product)}
232-
{:index-type :nested-to-one
233-
:path [:delivery-date :product]}])
234-
(c/add-items [{:delivery-date "2012-03-03" :product :bananas}
235-
{:delivery-date "2012-03-03" :product :apples}
236-
{:delivery-date "2012-03-04" :product :potatoes}
237-
{:delivery-date "2012-03-04" :product :bananas}
238-
{:delivery-date "2012-03-06" :product :potatoes}]))
239-
;; => {:delivery-date-product
240-
;; {["2012-03-03" :bananas] {:delivery-date "2012-03-03", :product :bananas},
241-
;; ... },
242-
;; [:delivery-date :product]
243-
;; {"2012-03-03" {:bananas {:delivery-date "2012-03-03", :product :bananas},
244-
;; :apples {:delivery-date "2012-03-03", :product :apples}},
245-
;; "2012-03-04" {:potatoes {:delivery-date "2012-03-04", :product :potatoes},
246-
;; :bananas {:delivery-date "2012-03-04", :product :bananas}},
247-
;; "2012-03-06" {:potatoes {:delivery-date "2012-03-06", :product :potatoes}}}}
228+
(-> (compound [{:index-type :one-to-one
229+
:id :delivery
230+
:kfn (juxt :customer :delivery-date :product)}
231+
{:index-type :nested-to-one
232+
:path [:customer :delivery-date :product]}])
233+
(add-items [{:customer 1 :delivery-date "2012-03-03" :product :bananas}
234+
{:customer 1 :delivery-date "2012-03-03" :product :apples}
235+
{:customer 1 :delivery-date "2012-03-10" :product :potatoes}
236+
{:customer 2 :delivery-date "2012-03-04" :product :bananas}
237+
{:customer 2 :delivery-date "2012-03-11" :product :potatoes}]))
238+
;; => {:delivery
239+
;; {[1 "2012-03-03" :bananas]
240+
;; {:customer 1, :delivery-date "2012-03-03", :product :bananas},
241+
;; [1 "2012-03-03" :apples]
242+
;; {:customer 1, :delivery-date "2012-03-03", :product :apples},
243+
;; [1 "2012-03-10" :potatoes]
244+
;; {:customer 1, :delivery-date "2012-03-10", :product :potatoes},
245+
;; [2 "2012-03-04" :bananas]
246+
;; {:customer 2, :delivery-date "2012-03-04", :product :bananas},
247+
;; [2 "2012-03-11" :potatoes]
248+
;; {:customer 2, :delivery-date "2012-03-11", :product :potatoes}},
249+
;; [:customer :delivery-date :product]
250+
;; {1
251+
;; {"2012-03-03"
252+
;; {:bananas
253+
;; {:customer 1, :delivery-date "2012-03-03", :product :bananas},
254+
;; :apples
255+
;; {:customer 1, :delivery-date "2012-03-03", :product :apples}},
256+
;; "2012-03-10"
257+
;; {:potatoes
258+
;; {:customer 1, :delivery-date "2012-03-10", :product :potatoes}}},
259+
;; 2
260+
;; {"2012-03-04"
261+
;; {:bananas
262+
;; {:customer 2, :delivery-date "2012-03-04", :product :bananas}},
263+
;; "2012-03-11"
264+
;; {:potatoes
265+
;; {:customer 2, :delivery-date "2012-03-11", :product :potatoes}}}}};; => {:delivery-date-product
266+
;; {[1 "2012-03-03" :bananas]
267+
;; {:customer 1, :delivery-date "2012-03-03", :product :bananas},
268+
;; [1 "2012-03-03" :apples]
269+
;; {:customer 1, :delivery-date "2012-03-03", :product :apples},
270+
;; [1 "2012-03-10" :potatoes]
271+
;; {:customer 1, :delivery-date "2012-03-10", :product :potatoes},
272+
;; [2 "2012-03-04" :bananas]
273+
;; {:customer 2, :delivery-date "2012-03-04", :product :bananas},
274+
;; [2 "2012-03-11" :potatoes]
275+
;; {:customer 2, :delivery-date "2012-03-11", :product :potatoes}},
276+
;; [:customer :delivery-date :product]
277+
;; {1
278+
;; {"2012-03-03"
279+
;; {:bananas
280+
;; {:customer 1, :delivery-date "2012-03-03", :product :bananas},
281+
;; :apples
282+
;; {:customer 1, :delivery-date "2012-03-03", :product :apples}},
283+
;; "2012-03-10"
284+
;; {:potatoes
285+
;; {:customer 1, :delivery-date "2012-03-10", :product :potatoes}}},
286+
;; 2
287+
;; {"2012-03-04"
288+
;; {:bananas
289+
;; {:customer 2, :delivery-date "2012-03-04", :product :bananas}},
290+
;; "2012-03-11"
291+
;; {:potatoes
292+
;; {:customer 2, :delivery-date "2012-03-11", :product :potatoes}}}}};; =>
293+
294+
(-> (compound [{:index-type :one-to-one
295+
:id :delivery
296+
:kfn (juxt :customer :delivery-date :product)}
297+
{:index-type :nested-to-many
298+
:path [:customer :delivery-date]}])
299+
(add-items [{:customer 1 :delivery-date "2012-03-03" :product :bananas}
300+
{:customer 1 :delivery-date "2012-03-03" :product :apples}
301+
{:customer 1 :delivery-date "2012-03-10" :product :potatoes}
302+
{:customer 2 :delivery-date "2012-03-04" :product :bananas}
303+
{:customer 2 :delivery-date "2012-03-11" :product :potatoes}]))
248304
```
249305

250306
### Index type: nested-to-many
@@ -257,6 +313,46 @@ Like a one-to-many index except that a nested hash-map is `path* -> set` is main
257313
#### Optional keys:
258314
- `id` - the id for the index in the compound
259315

316+
``` clojure
317+
(-> (compound [{:index-type :one-to-one
318+
:id :delivery
319+
:kfn (juxt :customer :delivery-date :product)}
320+
{:index-type :nested-to-many
321+
:path [:customer :delivery-date]}])
322+
(add-items [{:customer 1 :delivery-date "2012-03-03" :product :bananas}
323+
{:customer 1 :delivery-date "2012-03-03" :product :apples}
324+
{:customer 1 :delivery-date "2012-03-10" :product :potatoes}
325+
{:customer 2 :delivery-date "2012-03-04" :product :bananas}
326+
{:customer 2 :delivery-date "2012-03-11" :product :potatoes}]))
327+
;; => {:delivery
328+
;; {[1 "2012-03-03" :bananas]
329+
;; {:customer 1, :delivery-date "2012-03-03", :product :bananas},
330+
;; [1 "2012-03-03" :apples]
331+
;; {:customer 1, :delivery-date "2012-03-03", :product :apples},
332+
;; [1 "2012-03-10" :potatoes]
333+
;; {:customer 1, :delivery-date "2012-03-10", :product :potatoes},
334+
;; [2 "2012-03-04" :bananas]
335+
;; {:customer 2, :delivery-date "2012-03-04", :product :bananas},
336+
;; [2 "2012-03-11" :potatoes]
337+
;; {:customer 2, :delivery-date "2012-03-11", :product :potatoes}},
338+
;; [:customer :delivery-date]
339+
;; {1
340+
;; {"2012-03-03"
341+
;; #{{:customer 1, :delivery-date "2012-03-03", :product :bananas}
342+
;; {:customer 1, :delivery-date "2012-03-03", :product :apples}},
343+
;; "2012-03-10"
344+
;; #{{:customer 1,
345+
;; :delivery-date "2012-03-10",
346+
;; :product :potatoes}}},
347+
;; 2
348+
;; {"2012-03-04"
349+
;; #{{:customer 2, :delivery-date "2012-03-04", :product :bananas}},
350+
;; "2012-03-11"
351+
;; #{{:customer 2,
352+
;; :delivery-date "2012-03-11",
353+
;; :product :potatoes}}}}}
354+
```
355+
260356
### Index type: many-to-many
261357

262358
Like a one-to-many index, except the kfn should return a seq of values, and the item will be indexed under each of these.
@@ -267,6 +363,43 @@ Like a one-to-many index, except the kfn should return a seq of values, and the
267363
#### Optional keys:
268364
- `id` - the id for the index in the compound
269365

366+
```clojure
367+
(-> (c/compound [{:kfn :id}
368+
{:kfn :tags
369+
:index-type :many-to-many}])
370+
(c/add-items [{:id 1
371+
:name "Peanuts"
372+
:tags ["Nut" "New" "Yellow"]}
373+
{:id 2
374+
:name "Bananas"
375+
:tags ["Fruit" "Yellow"]}
376+
{:id 3
377+
:name "Plums"
378+
:tags ["Purple" "Fruit" "New"]}
379+
{:id 4
380+
:name "Kiwi"
381+
:tags ["Green" "Fruit"]}]))
382+
;; => {:id
383+
;; {1 {:id 1, :name "Peanuts", :tags ["Nut" "New" "Yellow"]},
384+
;; 2 {:id 2, :name "Bananas", :tags ["Fruit" "Yellow"]},
385+
;; 3 {:id 3, :name "Plums", :tags ["Purple" "Fruit" "New"]},
386+
;; 4 {:id 4, :name "Kiwi", :tags ["Green" "Fruit"]}},
387+
;; :tags
388+
;; {"Nut" #{{:id 1, :name "Peanuts", :tags ["Nut" "New" "Yellow"]}},
389+
;; "New"
390+
;; #{{:id 3, :name "Plums", :tags ["Purple" "Fruit" "New"]}
391+
;; {:id 1, :name "Peanuts", :tags ["Nut" "New" "Yellow"]}},
392+
;; "Yellow"
393+
;; #{{:id 1, :name "Peanuts", :tags ["Nut" "New" "Yellow"]}
394+
;; {:id 2, :name "Bananas", :tags ["Fruit" "Yellow"]}},
395+
;; "Fruit"
396+
;; #{{:id 3, :name "Plums", :tags ["Purple" "Fruit" "New"]}
397+
;; {:id 4, :name "Kiwi", :tags ["Green" "Fruit"]}
398+
;; {:id 2, :name "Bananas", :tags ["Fruit" "Yellow"]}},
399+
;; "Purple" #{{:id 3, :name "Plums", :tags ["Purple" "Fruit" "New"]}},
400+
;; "Green" #{{:id 4, :name "Kiwi", :tags ["Green" "Fruit"]}}}}
401+
```
402+
270403
## Macros vs function implementation
271404

272405
The default implementation for compound is now a macro. This gives about a 10% speedup over the function implementation (by splicing all the indexes into the loop variables of a single loop/recur).

0 commit comments

Comments
 (0)