-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (119 loc) · 3.06 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bufio"
"database/sql"
"fmt"
"github.com/bwells/trie"
//"github.com/bwells/uniq"
"github.com/deckarep/golang-set"
_ "github.com/go-sql-driver/mysql"
"os"
"sort"
"strings"
)
type Contact struct {
Id int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
func (c Contact) Key() int {
return c.Id
}
func (c *Contact) String() string {
return fmt.Sprintf("{%d %s %s}", c.Id, c.Name, c.Email)
}
// type ContactSlice []*Contact
// func (s ContactSlice) Len() int {
// return len(s)
// }
// func (s ContactSlice) Get(i int) uniq.Keyer {
// return uniq.Keyer(s[i])
// }
// func (s ContactSlice) Set(i int, item uniq.Keyer) {
// s[i] = item.(*Contact)
// }
// func (s ContactSlice) Slice(left, right int) uniq.Interface {
// return s[left:right]
// }
// func (s ContactSlice) New(size int) uniq.Interface {
// return make(ContactSlice, size)
// }
type byName []*Contact
func (a byName) Len() int { return len(a) }
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byName) Less(i, j int) bool {
iname := strings.ToLower(a[i].Name)
jname := strings.ToLower(a[j].Name)
if iname != jname {
return iname < jname
}
return strings.ToLower(a[i].Email) < strings.ToLower(a[j].Email)
}
func buildTrieFromDB() *trie.Trie {
con, _ := sql.Open("mysql", "developer:w3bd3v2008@tcp(localhost:3306)/erp5?charset=utf8")
defer con.Close()
query := `select contacts.id as Id,
concat_ws(' ', contacts.first_name, contacts.last_name) as Name,
users.username as Email
from users join contacts on users.contact_id = contacts.id;`
rows, _ := con.Query(query)
defer rows.Close()
t := trie.NewTrie()
for rows.Next() {
var id int
var name string
var email string
_ = rows.Scan(&id, &name, &email)
c := Contact{id, name, email}
name = strings.ToLower(c.Name)
email = strings.ToLower(c.Email)
for _, f := range strings.Fields(name) {
t.Add(f, &c)
}
for _, f := range strings.Split(email, "@") {
t.Add(f, &c)
}
}
return t
}
func multiMatch(t *trie.Trie, query string) []*Contact {
// strip whitespace
query = strings.ToLower(strings.TrimSpace(query))
// skip empty input
if len(query) == 0 {
return make([]*Contact, 0)
}
// pull matches on the first term and create a Set
terms := strings.Fields(query)
m, _ := t.MatchPartial(terms[0])
matches := mapset.NewSetFromSlice(m)
// pull matches for remaining terms and intersect them with the first Set
for _, term := range terms[1:] {
m, _ = t.MatchPartial(term)
matches = matches.Intersect(mapset.NewSetFromSlice(m))
}
// copy the Set out to a slice
results := make([]*Contact, matches.Cardinality())
i := 0
for key := range matches.Iter() {
results[i] = key.(*Contact)
i++
}
// sort them
sort.Sort(byName(results))
return results
}
func main() {
search_trie = buildTrieFromDB()
fmt.Println("Enter a search term:")
serve()
in := bufio.NewReader(os.Stdin)
for {
input, err := in.ReadString('\n')
if err != nil {
return
}
results := multiMatch(search_trie, input)
fmt.Println(results)
}
}