-
Notifications
You must be signed in to change notification settings - Fork 4
/
bloom.clj
165 lines (120 loc) · 3.65 KB
/
bloom.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(ns merkle-db.bloom
"Bloom filters provide a probablistic way to test for membership in a set
using a fixed amount of space. This is useful for reducing work when the test
definitively rules out the existence of a record."
(:refer-clojure :exclude [contains? into merge])
(:require
[bigml.sketchy.bits :as bits]
[bigml.sketchy.bloom :as bloom]
[bigml.sketchy.murmur :as murmur]))
;; The filter type wraps the bigml.sketchy bloom-filter implementation and
;; provides some extra features:
;;
;; - Strict typing to discriminate from regular maps.
;; - Better printing than the map representation.
;; - Filters can be invoked on elements to test membership, similar to regular
;; set types.
(deftype BloomFilter
[bins bits k _meta]
java.lang.Object
(toString
[this]
(format "#{2^%d bits, %d hashes}" bits k))
(equals
[this that]
(boolean
(or (identical? this that)
(when (identical? (class this) (class that))
(let [that ^BloomFilter that]
(and (= bins (.bins that))
(= bits (.bits that))
(= k (.k that))))))))
(hashCode
[this]
(hash [(class this) bins bits k]))
clojure.lang.IObj
(meta [this] _meta)
(withMeta
[this meta-map]
(BloomFilter. bins bits k meta-map))
clojure.lang.ILookup
(valAt
[this key]
(.valAt this key nil))
(valAt
[this key not-found]
(case key
:bins bins
:bits bits
:k k
not-found))
clojure.lang.IFn
(invoke
[this x]
(bloom/contains? this x)))
(alter-meta! #'->BloomFilter assoc :private true)
(defmethod print-method BloomFilter
[bf w]
(print-method
(tagged-literal 'merkle-db/bloom-filter [(:bits bf) (:k bf)])
w))
(defn filter?
"Predicate which tests whether the value is a BloomFilter."
[x]
(instance? BloomFilter x))
;; ## Constructors
(defn create
"Constructs a new bloom filter with the given expected population size and
desired error (false positive) rate."
([expected-size]
(create expected-size 0.01))
([expected-size error-rate]
(let [{:keys [bins bits k]} (bloom/create expected-size error-rate)]
(->BloomFilter bins bits k nil))))
(defn filter->form
"Build a serializable form for the bloom filter."
[^BloomFilter bf]
(with-meta [(.k bf) (.bits bf) (.bins bf)] (._meta bf)))
(defn form->filter
"Build a bloom filter from a serialized form."
[form]
(let [[k bits bins] form]
(->BloomFilter bins bits k (meta form))))
;; ## Membership
(defn- insert*
"Insert a single element into a bloom filter. Returns the updated filter."
[bf x]
(let [bits (:bits bf)
k (:k bf)]
(->BloomFilter
(apply bits/set (:bins bf) (take k (murmur/hash-seq x bits)))
bits k (meta bf))))
(defn insert
"Insert the given elements into a bloom filter. Returns the updated filter."
[bf & xs]
(reduce insert* bf xs))
(defn into
"Insert the collection of elements into a bloom filter. Returns the updated
filter."
[bf coll]
(reduce insert* bf coll))
(defn contains?
"True if the filter probably contains the given value."
[bf x]
(bloom/contains? bf x))
(defn mergeable?
"Returns true if the two bloom filters can be merged together."
[a b]
(and (= (:bits a) (:bits b))
(= (:k a) (:k b))))
(defn merge
"Merge the bloom filters together. Only works if `bits` and `k` match."
[a b]
(when-not (mergeable? a b)
(throw (IllegalArgumentException.
(format "Bloom filters with different sizes cannot be merged: %s != %s" a b))))
(->BloomFilter
(bits/or (:bins a) (:bins b))
(:bits a)
(:k a)
(clojure.core/merge (meta a) (meta b))))