Skip to content

Commit 5c1c0f0

Browse files
committed
Tweaks.
1 parent 2d16813 commit 5c1c0f0

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

ext/bloom/bloom.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type bloom struct {
3030
schema string
3131
storage string
3232
prob float64
33-
nfilter int64
33+
bytes int64
3434
hashes int
3535
}
3636

@@ -41,15 +41,17 @@ func create(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom,
4141
storage: table + "_storage",
4242
}
4343

44-
nelem := 100
44+
var nelem int64
4545
if len(arg) > 0 {
46-
nelem, err = strconv.Atoi(arg[0])
46+
nelem, err = strconv.ParseInt(arg[0], 10, 64)
4747
if err != nil {
4848
return nil, err
4949
}
5050
if nelem <= 0 {
5151
return nil, errors.New("bloom: number of elements in filter must be positive")
5252
}
53+
} else {
54+
nelem = 100
5355
}
5456

5557
if len(arg) > 1 {
@@ -73,10 +75,10 @@ func create(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom,
7375
return nil, errors.New("bloom: number of hash functions must be positive")
7476
}
7577
} else {
76-
t.hashes = int(math.Round(-math.Log2(t.prob)))
78+
t.hashes = max(1, numHashes(t.prob))
7779
}
7880

79-
t.nfilter = computeLength(nelem, t.prob)
81+
t.bytes = numBytes(nelem, t.prob)
8082

8183
err = db.Exec(fmt.Sprintf(
8284
`CREATE TABLE %s.%s (data BLOB, p REAL, n INTEGER, m INTEGER, k INTEGER)`,
@@ -89,7 +91,7 @@ func create(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom,
8991
`INSERT INTO %s.%s (rowid, data, p, n, m, k)
9092
VALUES (1, zeroblob(%d), %f, %d, %d, %d)`,
9193
sqlite3.QuoteIdentifier(t.schema), sqlite3.QuoteIdentifier(t.storage),
92-
t.nfilter, t.prob, nelem, t.nfilter*8, t.hashes))
94+
t.bytes, t.prob, nelem, 8*t.bytes, t.hashes))
9395
if err != nil {
9496
return nil, err
9597
}
@@ -131,7 +133,7 @@ func connect(db *sqlite3.Conn, _, schema, table string, arg ...string) (_ *bloom
131133
return nil, err
132134
}
133135

134-
t.nfilter = load.ColumnInt64(0)
136+
t.bytes = load.ColumnInt64(0)
135137
t.prob = load.ColumnFloat(1)
136138
t.hashes = load.ColumnInt(2)
137139
return &t, nil
@@ -188,7 +190,7 @@ func (b *bloom) Update(arg ...sqlite3.Value) (rowid int64, err error) {
188190

189191
for n := 0; n < b.hashes; n++ {
190192
hash := calcHash(n, blob)
191-
hash %= uint64(b.nfilter * 8)
193+
hash %= uint64(b.bytes * 8)
192194
bitpos := byte(hash % 8)
193195
bytepos := int64(hash / 8)
194196

@@ -202,7 +204,7 @@ func (b *bloom) Update(arg ...sqlite3.Value) (rowid int64, err error) {
202204
return 0, err
203205
}
204206

205-
buf[0] |= (1 << bitpos)
207+
buf[0] |= 1 << bitpos
206208

207209
_, err = f.Seek(bytepos, io.SeekStart)
208210
if err != nil {
@@ -241,9 +243,9 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
241243
}
242244
defer f.Close()
243245

244-
for n := 0; n < c.hashes; n++ {
246+
for n := 0; n < c.hashes && !c.eof; n++ {
245247
hash := calcHash(n, blob)
246-
hash %= uint64(c.nfilter * 8)
248+
hash %= uint64(c.bytes * 8)
247249
bitpos := byte(hash % 8)
248250
bytepos := int64(hash / 8)
249251

@@ -257,10 +259,7 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error {
257259
return err
258260
}
259261

260-
c.eof = (buf[0] & (1 << bitpos)) == 0
261-
if c.eof {
262-
break
263-
}
262+
c.eof = buf[0]&(1<<bitpos) == 0
264263
}
265264
return nil
266265
}
@@ -294,7 +293,12 @@ func calcHash(k int, b []byte) uint64 {
294293
return siphash.Hash(^uint64(k), uint64(k), b)
295294
}
296295

297-
func computeLength(n int, p float64) int64 {
298-
bits := math.Ceil(-((float64(n) * math.Log(p)) / (math.Ln2 * math.Ln2)))
299-
return (int64(bits) + 7) / 8
296+
func numHashes(p float64) int {
297+
k := math.Round(-math.Log2(p))
298+
return max(1, int(k))
299+
}
300+
301+
func numBytes(n int64, p float64) int64 {
302+
m := math.Ceil(float64(n) * math.Log(p) / -(math.Ln2 * math.Ln2))
303+
return (int64(m) + 7) / 8
300304
}

0 commit comments

Comments
 (0)