-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex_test.go
236 lines (213 loc) · 5.27 KB
/
index_test.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package indexer
import (
"fmt"
"testing"
datastructures "github.com/deepfabric/go-datastructures"
"github.com/deepfabric/indexer/cql"
"github.com/stretchr/testify/require"
)
const (
NumDocs = 10000
)
func newDocProt() *cql.DocumentWithIdx {
return &cql.DocumentWithIdx{
Doc: cql.Document{
DocID: 0,
UintProps: []*cql.UintProp{
&cql.UintProp{
Name: "object",
ValLen: 8,
Val: 0,
},
&cql.UintProp{
Name: "price",
ValLen: 4,
Val: 0,
},
&cql.UintProp{
Name: "priceF64",
IsFloat: true,
ValLen: 8,
Val: 0,
},
&cql.UintProp{
Name: "number",
ValLen: 4,
Val: 0,
},
&cql.UintProp{
Name: "date",
ValLen: 8,
Val: 0,
},
},
StrProps: []*cql.StrProp{
&cql.StrProp{
Name: "description",
Val: "",
},
&cql.StrProp{
Name: "note",
Val: "",
},
},
},
Index: "orders",
}
}
//TESTCASE: normal operation sequence: create, insert, del, destroy
func TestIndexNormal(t *testing.T) {
var err error
var ind *Index
var found bool
var bits map[uint64][]uint64
docProt := newDocProt()
ind, err = NewIndex(docProt, "/tmp/index_test")
require.NoError(t, err)
require.Equal(t, docProt, ind.DocProt)
for i := 0; i < NumDocs; i++ {
doc := newDocProt()
doc.Doc.DocID = uint64(i)
for j := 0; j < len(doc.Doc.UintProps); j++ {
val := uint64(i * (j + 1))
val, err = cql.ParseUintProp(doc.Doc.UintProps[j], fmt.Sprintf("%v", val))
require.NoError(t, err)
doc.Doc.UintProps[j].Val = val
}
for j := 0; j < len(doc.Doc.StrProps); j++ {
doc.Doc.StrProps[j].Val = fmt.Sprintf("%03d%03d and some random text", i, j)
}
err = ind.Insert(doc)
require.NoError(t, err)
}
// query numerical(integer) range
var qr *QueryResult
var items []datastructures.Comparable
low := uint64(30)
high := uint64(600)
cs := &cql.CqlSelect{
Index: docProt.Index,
UintPreds: map[string]cql.UintPred{
"price": cql.UintPred{
Name: "price",
Low: low,
High: high,
},
},
}
qr, err = ind.Select(cs)
require.NoError(t, err)
fmt.Printf("query result: %v\n", qr.Bm.Bits())
// low <= 2*i <= high, (low+1)/2 <= i <= high/2
want := uint64(high/2 - (low+1)/2 + 1)
require.Equalf(t, want, qr.Bm.Count(), "incorrect number of matches")
// query numerical range + order by + text
cs.OrderBy = "price"
cs.Limit = 20
qr, err = ind.Select(cs)
require.NoError(t, err)
items = qr.Oa.Finalize()
fmt.Printf("query result: %v\n", items)
require.Equalf(t, cs.Limit, len(items), "incorrect number of matches")
// dump bits
for name, frame := range ind.txtFrames {
var termID uint64
if termID, found = frame.td.GetTermID("017001"); !found {
continue
}
bits, err = frame.Bits()
require.NoError(t, err)
//fmt.Printf("frmae %v bits: %v\n", name, bits)
fmt.Printf("frame %v bits[%v]: %v\n", name, termID, bits[termID])
}
// query numerical range + text
cs.StrPreds = map[string]cql.StrPred{
"note": cql.StrPred{
Name: "note",
ContWord: "017001",
// ContWord: "random",
},
}
cs.Limit = 20
qr, err = ind.Select(cs)
require.NoError(t, err)
items = qr.Oa.Finalize()
fmt.Printf("query result: %v\n", items)
require.Equalf(t, 1, len(items), "incorrect number of matches")
// query numerical(float) range
valSs := []string{"30", "600"}
vals := make([]uint64, len(valSs))
for i, valS := range valSs {
var val uint64
val, err = cql.Float64ToSortableUint64(valS)
require.NoError(t, err)
vals[i] = val
fmt.Printf("FLOAT64 %v\t%v\n", valS, val)
}
low, high = vals[0], vals[1]
cs = &cql.CqlSelect{
Index: docProt.Index,
UintPreds: map[string]cql.UintPred{
"priceF64": cql.UintPred{
Name: "priceF64",
Low: low,
High: high,
},
},
}
qr, err = ind.Select(cs)
require.NoError(t, err)
fmt.Printf("query result: %v\n", qr.Bm.Bits())
// low <= 3*i <= high, (low+2)/3 <= i <= high/3
want = uint64(600/3 - (30+2)/3 + 1)
require.Equalf(t, want, qr.Bm.Count(), "incorrect number of matches")
//delete docs
for i := 0; i < NumDocs; i++ {
doc := newDocProt()
doc.Doc.DocID = uint64(i)
for j := 0; j < len(doc.Doc.UintProps); j++ {
doc.Doc.UintProps[j].Val = uint64(i * (j + 1))
}
found, err = ind.Del(doc.Doc.DocID)
require.NoError(t, err)
require.Equalf(t, true, found, "document %v not found", doc)
}
}
func TestIndexOpenClose(t *testing.T) {
var err error
var ind, ind2 *Index
//create index
docProt := newDocProt()
ind, err = NewIndex(docProt, "/tmp/index_test")
require.NoError(t, err)
//insert documents
for i := 0; i < NumDocs; i++ {
doc := newDocProt()
doc.Doc.DocID = uint64(i)
for j := 0; j < len(doc.Doc.UintProps); j++ {
doc.Doc.UintProps[j].Val = uint64(i * (j + 1))
}
err = ind.Insert(doc)
require.NoError(t, err)
}
//close index
err = ind.Close()
require.NoError(t, err)
//open index
err = ind.Open()
require.NoError(t, err)
//close index
err = ind.Close()
require.NoError(t, err)
//open index with another Index object. This occurs when program restart.
ind2, err = NewIndexExt("/tmp/index_test", "orders")
require.NoError(t, err)
//verify DocProt keeps unchanged
require.Equal(t, ind.DocProt, ind2.DocProt)
//close index
err = ind2.Close()
require.NoError(t, err)
//destroy index
err = ind.Destroy()
require.NoError(t, err)
}