-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.go
88 lines (62 loc) · 1.46 KB
/
check.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
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
package identitycheck
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"git.mills.io/prologic/bitcask"
"github.com/dotcypress/phonetics"
)
// IdentityCheck provides base checking
type IdentityCheck struct {
person *bitcask.Bitcask
index *bitcask.Bitcask
}
// NewIdentityCheck creates new pep and sanction check
func NewIdentityCheck() (*IdentityCheck, error) {
c := new(IdentityCheck)
pt := filepath.Join("source", "person")
it := filepath.Join("source", "person.idx")
pd, err := bitcask.Open(pt)
if err != nil {
return nil, err
}
c.person = pd
id, err := bitcask.Open(it)
if err != nil {
return nil, err
}
c.index = id
return c, nil
}
// Check finds given person from PEP and sanctions lists
func (i *IdentityCheck) Check(name string) (*[]Person, error) {
// create metaphone hash for given name
hash := phonetics.EncodeMetaphone(name)
var ps []Person
f := func(k []byte) error {
kp := strings.Split(string(k), ".")
ik := []byte("data." + kp[2])
// read person data from database
po, err := i.person.Get(ik)
if err != nil {
if err == bitcask.ErrKeyNotFound {
return fmt.Errorf("the key %s does not exist", string(ik))
}
return err
}
p := new(Person)
err = json.Unmarshal(po, p)
if err != nil {
return err
}
ps = append(ps, *p)
return nil
}
// iterate over records by given hash prefix
err := i.index.Scan([]byte("idx."+hash), f)
if err != nil {
return nil, err
}
return &ps, nil
}