/
bloom.go
50 lines (43 loc) · 1.32 KB
/
bloom.go
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
// package bloom creates and serves bloom filters for canonical names, and names of viruses. The
// filters are persistent throughout the life of the program. The filters are
// used to find exact matches to the database data fast.
package bloom
import (
"github.com/gnames/gnmatcher/config"
"github.com/gnames/gnmatcher/ent/exact"
"github.com/gnames/gnsys"
log "github.com/sirupsen/logrus"
)
type exactMatcher struct {
cfg config.Config
filters *bloomFilters
}
// NewENewExactMatcher takes configuration object and returns ExactMatcher.
func NewExactMatcher(cfg config.Config) exact.ExactMatcher {
return &exactMatcher{cfg: cfg}
}
func (em *exactMatcher) Init() {
em.prepareDir()
log.Println("Initializing bloom filters.")
em.getFilters()
}
func (em *exactMatcher) MatchCanonicalID(uuid string) bool {
em.filters.mux.Lock()
isIn := em.filters.canonical.Check([]byte(uuid))
em.filters.mux.Unlock()
return isIn
}
func (em *exactMatcher) MatchNameStringID(uuid string) bool {
em.filters.mux.Lock()
isIn := em.filters.virus.Check([]byte(uuid))
em.filters.mux.Unlock()
return isIn
}
func (em exactMatcher) prepareDir() {
log.Println("Preparing dir for bloom filters.")
bloomDir := em.cfg.FiltersDir()
err := gnsys.MakeDir(em.cfg.FiltersDir())
if err != nil {
log.Fatalf("Cannot create directory %s: %s.", bloomDir, err)
}
}