This repository was archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathholder.go
252 lines (216 loc) · 6.64 KB
/
holder.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package test
import (
"math"
"testing"
"time"
pilosa "github.com/featurebasedb/featurebase/v3"
"github.com/featurebasedb/featurebase/v3/pql"
"github.com/featurebasedb/featurebase/v3/testhook"
)
// Holder is a test wrapper for pilosa.Holder.
type Holder struct {
*pilosa.Holder
tb testing.TB
closed bool
}
// NewHolder returns a new instance of Holder with a temporary path.
func NewHolder(tb testing.TB) *Holder {
path, err := testhook.TempDir(tb, "pilosa-holder-")
if err != nil {
panic(err)
}
cfg := pilosa.TestHolderConfig()
h := &Holder{Holder: pilosa.NewHolder(path, cfg), tb: tb}
return h
}
// MustOpenHolder creates and opens a holder at a temporary path. Panic on error.
func MustOpenHolder(tb testing.TB) *Holder {
h := NewHolder(tb)
if err := h.Open(); err != nil {
tb.Fatalf("opening holder: %v", err)
}
tb.Cleanup(func() {
err := h.Close()
if err != nil {
tb.Fatalf("closing holder after test: %v", err)
}
})
return h
}
// Close closes the holder. The data should be removed by the cleanup
// registered when we created the initial tempdir.
func (h *Holder) Close() error {
if h.closed {
h.tb.Fatal("double-closed holder")
}
h.closed = true
return h.Holder.Close()
}
// Reopen instantiates and opens a new holder.
// Note that the holder must be Closed first.
func (h *Holder) Reopen() error {
return h.Holder.Open()
}
// MustCreateIndexIfNotExists returns a given index. Panic on error.
func (h *Holder) MustCreateIndexIfNotExists(index string, opt pilosa.IndexOptions) *Index {
idx, err := h.Holder.CreateIndexIfNotExists(index, "", opt)
if err != nil {
h.tb.Fatalf("creating index: %v", err)
}
return &Index{Index: idx}
}
// Row returns a Row for a given field.
func (h *Holder) Row(index, field string, rowID uint64) *pilosa.Row {
idx := h.MustCreateIndexIfNotExists(index, pilosa.IndexOptions{})
f, err := idx.CreateFieldIfNotExists(field, "", pilosa.OptFieldTypeDefault())
if err != nil {
panic(err)
}
qcx := h.Txf().NewQcx()
defer qcx.Abort()
row, err := f.Row(qcx, rowID)
if err != nil {
h.tb.Fatalf("retrieving row: %v", err)
}
// clone it so that mmapped storage doesn't disappear from under it
// once the qcx goes away.
return row
}
// ReadRow returns a Row for a given field. If the field does not exist,
// it fails the holder's test rather than creating the field.
func (h *Holder) ReadRow(index, field string, rowID uint64) *pilosa.Row {
idx := h.Holder.Index(index)
if idx == nil {
h.tb.Fatalf("read row from index %q: index not found", index)
}
f := idx.Field(field)
if f == nil {
h.tb.Fatalf("read row from field %q/%q: field not found", index, field)
}
qcx := h.Txf().NewQcx()
defer qcx.Abort()
row, err := f.Row(qcx, rowID)
if err != nil {
h.tb.Fatalf("retrieving row: %v", err)
}
// clone it so that mmapped storage doesn't disappear from under it
// once the qcx goes away.
return row.Clone()
}
func (h *Holder) RowTime(index, field string, rowID uint64, t time.Time, quantum string) *pilosa.Row {
idx := h.MustCreateIndexIfNotExists(index, pilosa.IndexOptions{})
f, err := idx.CreateFieldIfNotExists(field, "", pilosa.OptFieldTypeDefault())
if err != nil {
panic(err)
}
qcx := h.Txf().NewQcx()
defer qcx.Abort()
row, err := f.RowTime(qcx, rowID, t, quantum)
if err != nil {
panic(err)
}
// clone it so that mmapped storage doesn't disappear from under it
// once the qcx goes away.
return row.Clone()
}
// SetBit sets a bit on the given field.
func (h *Holder) SetBit(index, field string, rowID, columnID uint64) {
h.SetBitTime(index, field, rowID, columnID, nil)
}
// SetBitTime sets a bit with timestamp on the given field.
func (h *Holder) SetBitTime(index, field string, rowID, columnID uint64, t *time.Time) {
idx := h.MustCreateIndexIfNotExists(index, pilosa.IndexOptions{})
f, err := idx.CreateFieldIfNotExists(field, "", pilosa.OptFieldTypeDefault())
if err != nil {
panic(err)
}
qcx := h.Txf().NewWritableQcx()
defer qcx.Abort()
_, err = f.SetBit(qcx, rowID, columnID, t)
if err != nil {
h.tb.Fatalf("setting bit: %v", err)
}
err = qcx.Finish()
if err != nil {
h.tb.Fatalf("finishing qcx: %v", err)
}
}
// ClearBit clears a bit on the given field.
func (h *Holder) ClearBit(index, field string, rowID, columnID uint64) {
idx := h.MustCreateIndexIfNotExists(index, pilosa.IndexOptions{})
f, err := idx.CreateFieldIfNotExists(field, "", pilosa.OptFieldTypeDefault())
if err != nil {
panic(err)
}
qcx := h.Txf().NewWritableQcx()
defer qcx.Abort()
_, err = f.ClearBit(qcx, rowID, columnID)
if err != nil {
h.tb.Fatalf("clearing bit: %v", err)
}
err = qcx.Finish()
if err != nil {
h.tb.Fatalf("finishing qcx: %v", err)
}
}
// MustSetBits sets columns on a row. Panic on error.
// This function does not accept a timestamp or quantum.
func (h *Holder) MustSetBits(index, field string, rowID uint64, columnIDs ...uint64) {
for _, columnID := range columnIDs {
h.SetBit(index, field, rowID, columnID)
}
}
// SetValue sets an integer value on the given field.
func (h *Holder) SetValue(index, field string, columnID uint64, value int64) *Index {
idx := h.MustCreateIndexIfNotExists(index, pilosa.IndexOptions{})
f, err := idx.CreateFieldIfNotExists(field, "", pilosa.OptFieldTypeInt(math.MinInt64, math.MaxInt64))
if err != nil {
panic(err)
}
qcx := h.Txf().NewWritableQcx()
defer qcx.Abort()
_, err = f.SetValue(qcx, columnID, value)
if err != nil {
h.tb.Fatalf("setting value: %v", err)
}
err = qcx.Finish()
if err != nil {
h.tb.Fatalf("finishing qcx: %v", err)
}
return idx
}
// Value returns the integer value for a given column.
func (h *Holder) Value(index, field string, columnID uint64) (int64, bool) {
idx := h.MustCreateIndexIfNotExists(index, pilosa.IndexOptions{})
f, err := idx.CreateFieldIfNotExists(field, "", pilosa.OptFieldTypeInt(math.MinInt64, math.MaxInt64))
if err != nil {
panic(err)
}
qcx := h.Txf().NewQcx()
defer qcx.Abort()
val, exists, err := f.Value(qcx, columnID)
if err != nil {
panic(err)
}
return val, exists
}
// Range returns a Row (of column IDs) for a field based
// on the given range.
func (h *Holder) Range(index, field string, op pql.Token, predicate int64) *pilosa.Row {
idx := h.MustCreateIndexIfNotExists(index, pilosa.IndexOptions{})
f, err := idx.CreateFieldIfNotExists(field, "", pilosa.OptFieldTypeInt(math.MinInt64, math.MaxInt64))
if err != nil {
panic(err)
}
qcx := h.Txf().NewQcx()
defer qcx.Abort()
row, err := f.Range(qcx, field, op, predicate)
if err != nil {
panic(err)
}
// clone it so that mmapped storage doesn't disappear from under it
// once the qcx goes away.
return row.Clone()
}