-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (62 loc) · 1.96 KB
/
Copy pathmain.go
File metadata and controls
69 lines (62 loc) · 1.96 KB
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
// ext-bloom: probabilistic set-membership with the typed bloom.Filter
// API. Build a filter, then test membership without an exact lookup —
// "present" means probably-present (Bloom filters have a tunable
// false-positive rate), "absent" is definitive. Filter mirrors
// vec.Table / fts.Index / spellfix1.Vocab.
//
// Run with:
//
// just example bloom
package main
import (
"context"
"database/sql"
"fmt"
"log"
_ "gosqlite.org"
"gosqlite.org/ext/bloom"
_ "gosqlite.org/ext/bloom/auto" // registers the vtab module on every conn
)
func main() {
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
log.Fatalf("open: %v", err)
}
defer db.Close()
// One connection keeps the in-memory filter alive across calls.
db.SetMaxOpenConns(1)
ctx := context.Background()
// Typed CREATE VIRTUAL TABLE … USING bloom(size=…, p=…) — the arg
// string and its quoting are hidden behind options.
seen, err := bloom.Create(ctx, db, "seen",
bloom.WithSize(10000), bloom.WithFalsePositiveRate(0.01), bloom.WithIfNotExists())
if err != nil {
log.Fatalf("create: %v", err)
}
// Add a known set in a single transaction.
if err := seen.AddMany(ctx, []string{
"alice@example.com", "bob@example.com", "carol@example.com",
}); err != nil {
log.Fatalf("addMany: %v", err)
}
fmt.Println("membership (present = probably-present, absent = definitely-not):")
for _, addr := range []string{
"alice@example.com", // added
"carol@example.com", // added
"mallory@example.com", // never added
"trent@example.com", // never added
} {
ok, err := seen.Contains(ctx, addr)
if err != nil {
log.Fatalf("contains %q: %v", addr, err)
}
verdict := "absent"
if ok {
verdict = "present"
}
fmt.Printf(" %-22s -> %s\n", addr, verdict)
}
fmt.Println("\nA Bloom filter never reports a false negative: every added key")
fmt.Println("tests present. A 'present' for an un-added key is a false positive,")
fmt.Println("bounded by the configured p (1% here).")
}