-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_test.go
290 lines (256 loc) · 8.11 KB
/
table_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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package pixidb
import (
"math"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/owlpinetech/flatsphere"
"github.com/owlpinetech/healpix"
"golang.org/x/exp/maps"
)
func TestTableOpen(t *testing.T) {
dir, err := os.MkdirTemp(".", "pixidb_table_basic_open")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
testCases := []struct {
name string
indexer LocationIndexer
metadata map[string]string
proj flatsphere.Projection
}{
{"mercatortagless", NewMercatorCutoffIndexer(math.Pi/4, -math.Pi/4, 10, 10, true), map[string]string{}, flatsphere.NewMercator()},
{"cyleqtags", NewCylindricalEquirectangularIndexer(0, 10, 10, true), map[string]string{"one": "fish", "two": "fish"}, flatsphere.NewCylindricalEqualArea(0)},
{"healpixtagged", NewFlatHealpixIndexer(2, healpix.NestScheme), map[string]string{"hello": "there"}, flatsphere.NewHEALPixStandard()},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
orig, err := NewTable(filepath.Join(dir, tc.name), tc.indexer, Column{Name: "dummy", Type: ColumnTypeFloat32, Default: []byte{1, 2, 3, 4}})
if err != nil {
t.Fatal(err)
}
for k, v := range tc.metadata {
orig.SetMetadata(k, v)
}
tbl, err := OpenTable(filepath.Join(dir, tc.name))
if err != nil {
t.Fatal(err)
}
if !maps.Equal(tbl.Metadata, orig.Metadata) {
t.Errorf("expected table metadata %v, got %v", orig.Metadata, tbl.Metadata)
}
if tbl.IndexerName != orig.IndexerName {
t.Errorf("expected table indexer name %s, got %s", orig.IndexerName, tbl.IndexerName)
}
if tbl.Indexer.Size() != orig.Indexer.Size() {
t.Errorf("expected table indexer size %d, got %d", orig.Indexer.Size(), tbl.Indexer.Size())
}
if tbl.Indexer.Projection() == nil {
t.Errorf("projection not present for deserialize table")
}
if reflect.TypeOf(orig.Indexer) != reflect.TypeOf(tbl.Indexer) {
t.Errorf("expected indexer type %T, got %T", orig.Indexer, tbl.Indexer)
}
if reflect.TypeOf(orig.Indexer.Projection()) != reflect.TypeOf(tbl.Indexer.Projection()) {
t.Errorf("expected indexer type %T, got %T", orig.Indexer.Projection(), tbl.Indexer.Projection())
}
})
}
}
func TestTableQuery(t *testing.T) {
dir, err := os.MkdirTemp(".", "pixidb_table_basic_query")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
tbl, err := NewTable(filepath.Join(dir, "querytbl"), NewFlatHealpixIndexer(2, healpix.NestScheme),
Column{Name: "col1", Type: ColumnTypeInt32, Default: []byte{0, 0, 0, 3}},
Column{Name: "col2", Type: ColumnTypeInt16, Default: []byte{0, 6}})
if err != nil {
t.Fatal(err)
}
res, err := tbl.GetRows([]string{"col1"}, IndexLocation(0), IndexLocation(1), IndexLocation(2))
if err != nil {
t.Fatal(err)
}
for _, c := range res.Columns {
if c.Name != "col1" {
t.Errorf("expected column name to be col1, got %s", c.Name)
}
}
if len(res.Rows) != 3 {
t.Errorf("expected to get 3 result rows, got %d", len(res.Rows))
}
for _, r := range res.Rows {
if r[0].AsInt32() != 3 {
t.Errorf("expected row to equal 3, got %d", r[0].AsInt32())
}
}
res, err = tbl.GetRows([]string{"col2"}, IndexLocation(3), IndexLocation(4), IndexLocation(5))
if err != nil {
t.Fatal(err)
}
for _, c := range res.Columns {
if c.Name != "col2" {
t.Errorf("expected column name to be col2, got %s", c.Name)
}
}
if len(res.Rows) != 3 {
t.Errorf("expected to get 3 result rows, got %d", len(res.Rows))
}
for _, r := range res.Rows {
if r[0].AsInt16() != 6 {
t.Errorf("expected row to equal 3, got %d", r[0].AsInt16())
}
}
}
func TestTableSetGet(t *testing.T) {
dir, err := os.MkdirTemp(".", "pixidb_table_set_get")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
tbl, err := NewTable(filepath.Join(dir, "querytbl"), NewCylindricalEquirectangularIndexer(0, 10, 10, true),
Column{Name: "col1", Type: ColumnTypeInt32, Default: []byte{0, 0, 0, 3}})
if err != nil {
t.Fatal(err)
}
res, err := tbl.GetRows([]string{"col1"}, GridLocation{X: 0, Y: 0})
if err != nil {
t.Fatal(err)
}
if res.Rows[0][0].AsInt32() != 3 {
t.Errorf("expected value to equal 3, got %d", res.Rows[0][0].AsInt32())
}
n, err := tbl.SetRows([]string{"col1"}, []Location{GridLocation{X: 0, Y: 0}}, [][]Value{{NewInt32Value(5)}})
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Errorf("expected to only update one row, got %d", n)
}
// verify we see the updated value
res, err = tbl.GetRows([]string{"col1"}, GridLocation{X: 0, Y: 0})
if err != nil {
t.Fatal(err)
}
if res.Rows[0][0].AsInt32() != 5 {
t.Errorf("expected value to equal 5, got %d", res.Rows[0][0].AsInt32())
}
// verify that further gets on different pixels don't have an updated value
res, err = tbl.GetRows([]string{"col1"}, GridLocation{X: 1, Y: 0})
if err != nil {
t.Fatal(err)
}
if res.Rows[0][0].AsInt32() != 3 {
t.Errorf("expected unchanged value to equal 3, got %d", res.Rows[0][0].AsInt32())
}
res, err = tbl.GetRows([]string{"col1"}, GridLocation{X: 0, Y: 1})
if err != nil {
t.Fatal(err)
}
if res.Rows[0][0].AsInt32() != 3 {
t.Errorf("expected unchanged value to equal 3, got %d", res.Rows[0][0].AsInt32())
}
// verify again that we see the updated value
res, err = tbl.GetRows([]string{"col1"}, GridLocation{X: 0, Y: 0})
if err != nil {
t.Fatal(err)
}
if res.Rows[0][0].AsInt32() != 5 {
t.Errorf("expected value to equal 5, got %d", res.Rows[0][0].AsInt32())
}
// set the middle pixel
n, err = tbl.SetRows([]string{"col1"}, []Location{GridLocation{X: 5, Y: 5}}, [][]Value{{NewInt32Value(8)}})
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Errorf("expected to only update one row, got %d", n)
}
// verify again that we see the updated value
res, err = tbl.GetRows([]string{"col1"}, GridLocation{X: 5, Y: 5})
if err != nil {
t.Fatal(err)
}
if res.Rows[0][0].AsInt32() != 8 {
t.Errorf("expected value to equal 8, got %d", res.Rows[0][0].AsInt32())
}
}
func TestSmallIterateGetSetGet(t *testing.T) {
dir, err := os.MkdirTemp(".", "pixidb_table_set_get")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
tbl, err := NewTable(filepath.Join(dir, "querytbl"), NewCylindricalEquirectangularIndexer(0, 10, 10, true),
Column{Name: "col1", Type: ColumnTypeInt16, Default: NewInt16Value(math.MaxInt16)})
if err != nil {
t.Fatal(err)
}
for i := 0; i < tbl.store.Rows; i++ {
loc := GridLocation{X: i % 10, Y: i / 10}
results, err := tbl.GetRows([]string{"col1"}, loc)
if err != nil {
t.Fatal(err)
}
if results.Rows[0][0].AsInt16() != math.MaxInt16 {
t.Errorf("expected anti-set value to max-int, got %d", results.Rows[0][0].AsInt16())
}
n, err := tbl.SetRows([]string{"col1"}, []Location{loc}, [][]Value{{NewInt16Value(int16(i))}})
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Errorf("expected to only set 1 row, but set %d", n)
}
results, err = tbl.GetRows([]string{"col1"}, loc)
if err != nil {
t.Fatal(err)
}
if results.Rows[0][0].AsInt16() != int16(i) {
t.Errorf("expected post-set value to max-int, got %d", results.Rows[0][0].AsInt16())
}
}
}
func TestTableSetAllPersist(t *testing.T) {
dir, err := os.MkdirTemp(".", "pixidb_table_set_get")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
tbl, err := NewTable(filepath.Join(dir, "querytbl"), NewCylindricalEquirectangularIndexer(0, 10, 10, true),
Column{Name: "col1", Type: ColumnTypeInt16, Default: NewInt16Value(math.MaxInt16)})
if err != nil {
t.Fatal(err)
}
for i := 0; i < tbl.store.Rows; i++ {
loc := GridLocation{X: i % 10, Y: i / 10}
n, err := tbl.SetRows([]string{"col1"}, []Location{loc}, [][]Value{{NewInt16Value(int16(i))}})
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Errorf("expected to only set 1 row, but set %d", n)
}
}
if err = tbl.Checkpoint(); err != nil {
t.Fatal(err)
}
opened, err := OpenTable(filepath.Join(dir, "querytbl"))
if err != nil {
t.Fatal(err)
}
for i := 0; i < opened.store.Rows; i++ {
loc := GridLocation{X: i % 10, Y: i / 10}
rs, err := opened.GetRows([]string{"col1"}, loc)
if err != nil {
t.Fatal(err)
}
if rs.Rows[0][0].AsInt16() != int16(i) {
t.Errorf("expected to get %d at index %d, but got %d", i, i, rs.Rows[0][0].AsInt16())
}
}
}