-
Notifications
You must be signed in to change notification settings - Fork 9
/
search.go
164 lines (141 loc) · 3.32 KB
/
search.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
159
160
161
162
163
164
package godnf
import (
"errors"
"fmt"
"sort"
"github.com/brg-liuwei/godnf/set"
)
// Cond: retrieval condition
//
// A dnf like ({Country in {CN, RU, US}) can be retrievaled by Cond as follows:
//
// Cond{
// Key: "Country",
// Val: "CN",
// }
//
// or
//
// Cond{
// Key: "Country",
// Val: "RU",
// }
//
// or
//
// Cond{
// Key: "Country",
// Val: "US",
// }
type Cond struct {
Key string
Val string
}
func (c *Cond) ToString() string {
return fmt.Sprintf("(%s: %s)", c.Key, c.Val)
}
func searchCondCheck(conds []Cond) error {
if conds == nil || len(conds) == 0 {
return errors.New("no conds to search")
}
m := make(map[string]bool)
for _, cond := range conds {
if _, ok := m[cond.Key]; ok {
return errors.New("duplicate keys: " + cond.Key)
}
m[cond.Key] = true
}
return nil
}
// Get doc size of current dnf
func (h *Handler) GetDocSize() int {
h.docs.RLock()
defer h.docs.RUnlock()
return len(h.docs.docs)
}
// Search docs which match conds and passed by attrFilter
func (h *Handler) Search(conds []Cond, attrFilter func(DocAttr) bool) (docs []int, err error) {
if err := searchCondCheck(conds); err != nil {
return nil, err
}
termids := make([]int, 0)
h.termMapLock.RLock()
for i := 0; i < len(conds); i++ {
if id, ok := h.termMap[conds[i].Key+"%"+conds[i].Val]; ok {
termids = append(termids, id)
}
}
h.termMapLock.RUnlock()
return h.doSearch(termids, attrFilter), nil
}
// SearchAll searches all docs which match conds
func (h *Handler) SearchAll(conds []Cond) (docs []int, err error) {
return h.Search(conds, func(DocAttr) bool { return true })
}
func (h *Handler) doSearch(terms []int, attrFilter func(DocAttr) bool) (docs []int) {
conjs := h.getConjs(terms)
if len(conjs) == 0 {
return nil
}
return h.getDocs(conjs, attrFilter)
}
func (h *Handler) getDocs(conjs []int, attrFilter func(DocAttr) bool) (docs []int) {
h.conjRvsLock.RLock()
defer h.conjRvsLock.RUnlock()
set := set.NewIntSet()
for _, conj := range conjs {
ASSERT(conj < len(h.conjRvs))
doclist := h.conjRvs[conj]
if doclist == nil {
continue
}
for _, doc := range doclist {
h.docs.RLock()
ok := h.docs.docs[doc].active && attrFilter(h.docs.docs[doc].attr)
h.docs.RUnlock()
if !ok {
continue
}
set.Add(doc, false)
}
}
return set.ToSlice(false)
}
func (h *Handler) getConjs(terms []int) (conjs []int) {
h.conjSzRvsLock.RLock()
defer h.conjSzRvsLock.RUnlock()
n := len(terms)
ASSERT(len(h.conjSzRvs) > 0)
if n >= len(h.conjSzRvs) {
n = len(h.conjSzRvs) - 1
}
ASSERT(n <= 255) // max(uint8) == 255
conjSet := set.NewIntSet()
for i := 0; i <= n; i++ {
termlist := h.conjSzRvs[i]
if termlist == nil || len(termlist) == 0 {
continue
}
countSet := set.NewCountSet(uint8(i), h.conjs.size())
for _, tid := range terms {
idx := sort.Search(len(termlist), func(i int) bool {
return termlist[i].termId >= tid
})
if idx < len(termlist) && termlist[idx].termId == tid &&
termlist[idx].cList != nil {
for _, pair := range termlist[idx].cList {
countSet.Add(pair.conjId, pair.belong, false)
}
}
}
// 处理∅
if i == 0 {
for _, pair := range termlist[0].cList {
ASSERT(pair.belong == true)
countSet.Add(pair.conjId, pair.belong, false)
}
}
conjSet.AddSlice(countSet.ToSlice(false), false)
}
return conjSet.ToSlice(false)
}