4801
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
4800
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2019 by Claus Gittinger
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
|
14 |
"{ Package: 'stx:libbasic2' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
Collection subclass:#BloomFilter
|
|
19 |
instanceVariableNames:'bits hashingBlocks tally'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-Unordered'
|
|
23 |
!
|
|
24 |
|
|
25 |
!BloomFilter class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2019 by Claus Gittinger
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
documentation
|
|
42 |
"
|
4974
|
43 |
a BloomFilter (see eg. https://de.wikipedia.org/wiki/Bloomfilter)
|
|
44 |
is a Set-like data structure which can quickly answer whether
|
4800
|
45 |
an element is definitely NOT in the set, but might generate false positives
|
|
46 |
(i.e. it might answer true, although the element is not actually present).
|
|
47 |
|
|
48 |
Bloom filters are used for tuning, when accesses (eg. to the disk or database)
|
|
49 |
are expensive, and can be avoided in many cases (in the NOT present case),
|
|
50 |
and false positives only lead to additional useless queries.
|
|
51 |
|
|
52 |
Bloom filters cannot remove or enumerate elements.
|
|
53 |
The only useful operations are adding and inclusion testing (includes).
|
|
54 |
|
|
55 |
For example, a database query (eg. 'is something present'),
|
|
56 |
might make use of a bloom filter, to avoid the query in many cases.
|
|
57 |
|
|
58 |
Bloom filters false positive rate degrades as the number of elements is added,
|
|
59 |
thus a good choice of its initial size is mandatory.
|
|
60 |
"
|
|
61 |
!
|
|
62 |
|
|
63 |
examples
|
|
64 |
"<<END
|
|
65 |
[exBegin]
|
|
66 |
|filter set nFalsePositive|
|
|
67 |
|
|
68 |
filter := BloomFilter new:10000.
|
|
69 |
set := Set new:10000.
|
|
70 |
|
|
71 |
"/ add all words from the Collection class's source code
|
|
72 |
Collection sourceStream
|
|
73 |
linesDo:[:eachLine |
|
|
74 |
eachLine
|
|
75 |
splitOn:[:ch | ch isSeparator]
|
|
76 |
do:[:word |
|
|
77 |
filter add:word.
|
|
78 |
set add:word.
|
|
79 |
].
|
|
80 |
];
|
|
81 |
close.
|
|
82 |
|
|
83 |
"/ check some which are known to be present
|
|
84 |
set do:[:w |
|
|
85 |
self assert:(filter includes:w)
|
|
86 |
].
|
|
87 |
"/ check for false positives
|
|
88 |
nFalsePositive :=
|
|
89 |
(1 to:1000) count:[:n |
|
|
90 |
|randomWord isFalsePositive|
|
|
91 |
|
|
92 |
isFalsePositive := false.
|
|
93 |
randomWord := (1 to:6) collect:[:i | ($a to: $z) atRandom] as:String.
|
|
94 |
(set includes:randomWord) ifFalse:[
|
|
95 |
(filter includes:randomWord) ifTrue:[
|
|
96 |
Transcript showCR:'false positive: ',randomWord.
|
|
97 |
isFalsePositive := true.
|
|
98 |
].
|
|
99 |
].
|
|
100 |
isFalsePositive
|
|
101 |
].
|
|
102 |
Transcript show:nFalsePositive; showCR:' false positives'.
|
|
103 |
filter inspect.
|
|
104 |
[exEnd]
|
|
105 |
END>>"
|
|
106 |
! !
|
|
107 |
|
|
108 |
!BloomFilter class methodsFor:'instance creation'!
|
|
109 |
|
|
110 |
new:initialNumberOfBits
|
|
111 |
^ self
|
|
112 |
new:initialNumberOfBits
|
|
113 |
hashes:{
|
|
114 |
[:s | s hash_fnv1a] .
|
|
115 |
[:s | s hash_sdbm] .
|
|
116 |
}
|
|
117 |
|
|
118 |
"Created: / 10-02-2019 / 14:14:23 / Claus Gittinger"
|
|
119 |
!
|
|
120 |
|
|
121 |
new:initialNumberOfBits hashes:hashActionBlocks
|
|
122 |
^ super new
|
|
123 |
numberOfBits:initialNumberOfBits
|
|
124 |
hashes:hashActionBlocks
|
|
125 |
|
|
126 |
"Created: / 10-02-2019 / 14:13:05 / Claus Gittinger"
|
|
127 |
! !
|
|
128 |
|
4801
|
129 |
!BloomFilter methodsFor:'adding'!
|
4800
|
130 |
|
|
131 |
add:aString
|
|
132 |
"add the arguemnt, aString to the set"
|
|
133 |
|
|
134 |
|mod isNew|
|
|
135 |
|
|
136 |
mod := bits size.
|
|
137 |
isNew := false.
|
|
138 |
hashingBlocks do:[:each |
|
|
139 |
|h idx|
|
|
140 |
|
|
141 |
h := each value:aString.
|
|
142 |
idx := (h \\ mod)+1.
|
|
143 |
(bits at:idx) == 0 ifTrue:[ isNew := true ].
|
|
144 |
bits at:idx put:1.
|
|
145 |
].
|
|
146 |
isNew ifTrue:[
|
|
147 |
tally := tally + 1
|
|
148 |
].
|
|
149 |
|
|
150 |
"Created: / 10-02-2019 / 14:16:29 / Claus Gittinger"
|
4801
|
151 |
! !
|
4800
|
152 |
|
4801
|
153 |
!BloomFilter methodsFor:'blocked'!
|
4800
|
154 |
|
4801
|
155 |
do:aBlock
|
|
156 |
self shouldNotImplement
|
4800
|
157 |
|
4801
|
158 |
"Created: / 10-02-2019 / 14:40:17 / Claus Gittinger"
|
4800
|
159 |
!
|
|
160 |
|
|
161 |
remove:aString
|
|
162 |
"raise an error - elements cannot be removed"
|
|
163 |
|
|
164 |
self shouldNotImplement
|
|
165 |
|
|
166 |
"Created: / 10-02-2019 / 14:18:14 / Claus Gittinger"
|
|
167 |
! !
|
|
168 |
|
|
169 |
!BloomFilter methodsFor:'initialization'!
|
|
170 |
|
|
171 |
numberOfBits:initialNumberOfBits hashes:hashingBlocksArg
|
|
172 |
bits := BitArray new:(initialNumberOfBits nextPrime).
|
|
173 |
hashingBlocks := hashingBlocksArg.
|
|
174 |
tally := 0.
|
|
175 |
|
|
176 |
"Created: / 10-02-2019 / 14:15:10 / Claus Gittinger"
|
|
177 |
! !
|
|
178 |
|
4801
|
179 |
!BloomFilter methodsFor:'queries'!
|
|
180 |
|
|
181 |
includes:aString
|
|
182 |
"returns false, if the arguemnt, aString is definitly NOT in the set.
|
|
183 |
However, it may return true as a false positive"
|
|
184 |
|
|
185 |
|mod|
|
|
186 |
|
|
187 |
mod := bits size.
|
|
188 |
hashingBlocks do:[:each |
|
|
189 |
|h|
|
|
190 |
|
|
191 |
h := each value:aString.
|
|
192 |
(bits at:(h \\ mod)+1) == 0 ifTrue:[^ false].
|
|
193 |
].
|
|
194 |
^ true
|
|
195 |
|
|
196 |
"Created: / 10-02-2019 / 14:16:59 / Claus Gittinger"
|
|
197 |
! !
|
|
198 |
|
4800
|
199 |
!BloomFilter class methodsFor:'documentation'!
|
|
200 |
|
|
201 |
version_CVS
|
|
202 |
^ '$Header$'
|
|
203 |
! !
|
|
204 |
|