-
Notifications
You must be signed in to change notification settings - Fork 11
/
document.go
207 lines (180 loc) · 4.99 KB
/
document.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package be_indexer
import (
"encoding/json"
"fmt"
"strings"
)
type (
DocID int64
DocIDList []DocID
Conjunction struct { // 每个conjunction 内的field 逻辑为且, 参考DNF定义
Expressions map[BEField][]*BoolValues `json:"exprs"` // 同一个Conj内不允许重复的Field
}
Document struct {
ID DocID `json:"id"` // 只支持int32最大值个Doc
Cons []*Conjunction `json:"cons"` // conjunction之间的关系是或,具体描述可以看论文的表述
}
)
func NewDocument(id DocID) *Document {
return &Document{
ID: id,
Cons: make([]*Conjunction, 0),
}
}
func (s DocIDList) Contain(id DocID) bool {
for _, v := range s {
if v == id {
return true
}
}
return false
}
func (s DocIDList) Sub(other DocIDList) (r DocIDList) {
BASE:
for _, v := range s {
for _, c := range other {
if v == c {
continue BASE
}
}
r = append(r, v)
}
return
}
// Len sort API
func (s DocIDList) Len() int { return len(s) }
func (s DocIDList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s DocIDList) Less(i, j int) bool { return s[i] < s[j] }
// AddConjunction 一组完整的expression, 必须是完整一个描述文档的DNF Bool表达的条件组合*/
func (doc *Document) AddConjunction(cons ...*Conjunction) *Document {
for _, conj := range cons {
doc.Cons = append(doc.Cons, conj)
}
return doc
}
func (doc *Document) AddConjunctions(conj *Conjunction, others ...*Conjunction) *Document {
doc.Cons = append(doc.Cons, conj)
for _, conj := range others {
doc.Cons = append(doc.Cons, conj)
}
return doc
}
func (doc *Document) JSONString() string {
data, _ := json.Marshal(doc)
return string(data)
}
// String a more compacted string
func (doc *Document) String() string {
strBuilder := strings.Builder{}
strBuilder.WriteString(fmt.Sprintf("doc:%d, cons:[\n", doc.ID))
cnt := len(doc.Cons)
for i, conj := range doc.Cons {
strBuilder.WriteString(fmt.Sprintf("\t%d:%s", i, conj.String()))
cnt--
if cnt > 0 {
strBuilder.WriteString(",\n")
}
}
strBuilder.WriteString("\n]")
return strBuilder.String()
}
func NewConjunction() *Conjunction {
return &Conjunction{
Expressions: make(map[BEField][]*BoolValues),
}
}
// In any value in values is a **true** expression
func (conj *Conjunction) In(field BEField, values Values) *Conjunction {
conj.addExpression(field, NewBoolValue(ValueOptEQ, values, true))
return conj
}
// NotIn any value in values is a **false** expression
func (conj *Conjunction) NotIn(field BEField, values Values) *Conjunction {
conj.addExpression(field, NewBoolValue(ValueOptEQ, values, false))
return conj
}
func (conj *Conjunction) Include(field BEField, values Values) *Conjunction {
conj.addExpression(field, NewBoolValue(ValueOptEQ, values, true))
return conj
}
func (conj *Conjunction) Exclude(field BEField, values Values) *Conjunction {
conj.addExpression(field, NewBoolValue(ValueOptEQ, values, false))
return conj
}
func (conj *Conjunction) GreatThan(field BEField, value int64) *Conjunction {
conj.AddBoolExprs(&BooleanExpr{
Field: field,
BoolValues: NewGTBoolValue(value),
})
return conj
}
func (conj *Conjunction) LessThan(field BEField, value int64) *Conjunction {
conj.AddBoolExprs(&BooleanExpr{
Field: field,
BoolValues: NewLTBoolValue(value),
})
return conj
}
func (conj *Conjunction) Between(field BEField, l, h int64) *Conjunction {
conj.AddBoolExprs(&BooleanExpr{
Field: field,
BoolValues: NewBoolValue(ValueOptBetween, []int64{l, h}, true),
})
return conj
}
// AddBoolExprs append boolean expression,
// don't allow same field added twice in one conjunction
func (conj *Conjunction) AddBoolExprs(exprs ...*BooleanExpr) *Conjunction {
for _, expr := range exprs {
conj.addExpression(expr.Field, expr.BoolValues)
}
return conj
}
func (conj *Conjunction) AddExpression3(field string, include bool, values Values) *Conjunction {
conj.addExpression(BEField(field), NewBoolValue(ValueOptEQ, values, include))
return conj
}
func (conj *Conjunction) addExpression(field BEField, boolValues BoolValues) {
conj.Expressions[field] = append(conj.Expressions[field], &boolValues)
}
func (conj *Conjunction) JSONString() string {
data, _ := json.Marshal(conj)
return string(data)
}
func (conj *Conjunction) String() string {
strBuilder := strings.Builder{}
strBuilder.WriteString("{")
cnt := len(conj.Expressions)
for field, exprs := range conj.Expressions {
strBuilder.WriteString(fmt.Sprintf("%s (", field))
for i, expr := range exprs {
if i != 0 {
strBuilder.WriteString(",")
}
strBuilder.WriteString(expr.String())
}
cnt--
if cnt > 0 {
strBuilder.WriteString(") and ")
} else {
strBuilder.WriteString(")")
}
}
strBuilder.WriteString("}")
return strBuilder.String()
}
func (conj *Conjunction) CalcConjSize() (size int) {
for _, bvs := range conj.Expressions {
EXPR:
for _, expr := range bvs {
if expr.Incl {
size++
break EXPR
}
}
}
return size
}
func (conj *Conjunction) ExpressionCount() (size int) {
return len(conj.Expressions)
}