/
query_match.go
346 lines (302 loc) · 13.8 KB
/
query_match.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package leveldb_core
import (
"encoding/binary"
"fmt"
"math"
"reflect"
"regexp"
"time"
"github.com/Gophercraft/phylactery/database/query"
"github.com/Gophercraft/phylactery/database/storage"
"github.com/Gophercraft/phylactery/database/storage/record"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
)
var time_type = reflect.TypeFor[time.Time]()
func query_get_column_and_condition(col any, condition *query.Condition) (reflect_col reflect.Value, reflect_condition_value reflect.Value, err error) {
reflect_col = reflect.ValueOf(col)
reflect_condition_value = reflect.ValueOf(condition.Parameter)
if reflect_col.Kind() != reflect_condition_value.Kind() {
err = fmt.Errorf("cannot compare different kinds of fields (%s in column(%s), queried(%s))", condition.ColumnName, reflect_col.Type(), reflect_condition_value.Type())
}
return
}
func query_column_matches_condition_equals(col any, schema *storage.TableSchemaColumn, condition *query.Condition) (bool, error) {
// Get reflection values of queried value & the value of the current row
reflect_col, reflect_condition_value, err := query_get_column_and_condition(col, condition)
if err != nil {
return false, err
}
if reflect_col.Type() == time_type {
reflect_col_time := reflect_col.Interface().(time.Time)
condition_time := reflect_condition_value.Interface().(time.Time)
return reflect_col_time.Equal(condition_time), nil
}
switch reflect_col.Kind() {
case reflect.String:
return reflect_col.String() == reflect_condition_value.String(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return reflect_col.Uint() == reflect_condition_value.Uint(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return reflect_col.Int() == reflect_condition_value.Int(), nil
case reflect.Float32:
return reflect_col.Interface().(float32) == reflect_condition_value.Interface().(float32), nil
case reflect.Float64:
return reflect_col.Interface().(float64) == reflect_condition_value.Interface().(float64), nil
case reflect.Bool:
return reflect_col.Bool() == reflect_condition_value.Bool(), nil
default:
panic(fmt.Errorf("cannot == compare this kind of data %s", reflect_col))
}
}
func query_column_matches_condition_greater_than(col any, schema *storage.TableSchemaColumn, condition *query.Condition) (bool, error) {
// Get reflection values of queried value & the value of the current row
reflect_col, reflect_condition_value, err := query_get_column_and_condition(col, condition)
if err != nil {
return false, err
}
if reflect_col.Type() == time_type {
reflect_col_time := reflect_col.Interface().(time.Time)
condition_time := reflect_condition_value.Interface().(time.Time)
return reflect_col_time.After(condition_time), nil
}
switch reflect_col.Kind() {
case reflect.String:
return reflect_col.String() > reflect_condition_value.String(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return reflect_col.Uint() > reflect_condition_value.Uint(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return reflect_col.Int() > reflect_condition_value.Int(), nil
case reflect.Float32:
return reflect_col.Interface().(float32) > reflect_condition_value.Interface().(float32), nil
case reflect.Float64:
return reflect_col.Interface().(float64) > reflect_condition_value.Interface().(float64), nil
default:
panic(fmt.Errorf("cannot compare this kind of data %s", reflect_col))
}
}
func query_column_matches_condition_less_than(col any, schema *storage.TableSchemaColumn, condition *query.Condition) (bool, error) {
// Get reflection values of queried value & the value of the current row
reflect_col, reflect_condition_value, err := query_get_column_and_condition(col, condition)
if err != nil {
return false, err
}
if reflect_col.Type() == time_type {
reflect_col_time := reflect_col.Interface().(time.Time)
condition_time := reflect_condition_value.Interface().(time.Time)
return reflect_col_time.Before(condition_time), nil
}
switch reflect_col.Kind() {
case reflect.String:
return reflect_col.String() < reflect_condition_value.String(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return reflect_col.Uint() < reflect_condition_value.Uint(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return reflect_col.Int() < reflect_condition_value.Int(), nil
case reflect.Float32:
return reflect_col.Interface().(float32) < reflect_condition_value.Interface().(float32), nil
case reflect.Float64:
return reflect_col.Interface().(float64) < reflect_condition_value.Interface().(float64), nil
default:
panic(fmt.Errorf("cannot compare this kind of data %s", reflect_col))
}
}
func query_column_matches_condition_greater_than_or_equal(col any, schema *storage.TableSchemaColumn, condition *query.Condition) (bool, error) {
// Get reflection values of queried value & the value of the current row
reflect_col, reflect_condition_value, err := query_get_column_and_condition(col, condition)
if err != nil {
return false, err
}
if reflect_col.Type() == time_type {
reflect_col_time := reflect_col.Interface().(time.Time)
condition_time := reflect_condition_value.Interface().(time.Time)
return reflect_col_time.After(condition_time) || reflect_col_time.Equal(condition_time), nil
}
switch reflect_col.Kind() {
case reflect.String:
return reflect_col.String() >= reflect_condition_value.String(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return reflect_col.Uint() >= reflect_condition_value.Uint(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return reflect_col.Int() >= reflect_condition_value.Int(), nil
case reflect.Float32:
return reflect_col.Interface().(float32) >= reflect_condition_value.Interface().(float32), nil
case reflect.Float64:
return reflect_col.Interface().(float64) >= reflect_condition_value.Interface().(float64), nil
default:
panic(fmt.Errorf("cannot compare this kind of data %s", reflect_col))
}
}
func query_column_matches_condition_less_than_or_equal(col any, schema *storage.TableSchemaColumn, condition *query.Condition) (bool, error) {
// Get reflection values of queried value & the value of the current row
reflect_col, reflect_condition_value, err := query_get_column_and_condition(col, condition)
if err != nil {
return false, err
}
if reflect_col.Type() == time_type {
reflect_col_time := reflect_col.Interface().(time.Time)
condition_time := reflect_condition_value.Interface().(time.Time)
return reflect_col_time.Before(condition_time) || reflect_col_time.Equal(condition_time), nil
}
switch reflect_col.Kind() {
case reflect.String:
return reflect_col.String() <= reflect_condition_value.String(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return reflect_col.Uint() <= reflect_condition_value.Uint(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return reflect_col.Int() <= reflect_condition_value.Int(), nil
case reflect.Float32:
return reflect_col.Interface().(float32) <= reflect_condition_value.Interface().(float32), nil
case reflect.Float64:
return reflect_col.Interface().(float64) <= reflect_condition_value.Interface().(float64), nil
default:
panic(fmt.Errorf("cannot compare this kind of data %s", reflect_col))
}
}
func query_column_matches_condition_regular_expression(col any, schema *storage.TableSchemaColumn, condition *query.Condition) (bool, error) {
str, ok := col.(string)
if !ok {
return false, fmt.Errorf("field %s is not a string, cannot perform regex query on it", schema.Name)
}
regex := condition.Parameter.(*regexp.Regexp)
return regex.MatchString(str), nil
}
func query_column_matches_condition_bitwise_and(col any, schema *storage.TableSchemaColumn, condition *query.Condition) (bool, error) {
// Get reflection values of queried value & the value of the current row
reflect_col, reflect_condition_value, err := query_get_column_and_condition(col, condition)
if err != nil {
return false, err
}
switch reflect_col.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return (reflect_col.Uint() & reflect_condition_value.Uint()) != 0, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return (reflect_col.Int() & reflect_condition_value.Int()) != 0, nil
default:
panic(fmt.Errorf("cannot compare this kind of data %s", reflect_col))
}
}
func query_match_condition(schema *storage.TableSchemaStructure, value_record storage.Record, condition *query.Condition) (bool, error) {
switch condition.Type {
case query.Condition_Not,
query.Condition_Or:
return query_match_embedded_condition(schema, value_record, condition)
default:
column := value_record[condition.Column]
matched, err := query_column_matches_condition(column, &schema.Columns[condition.Column], condition)
if err != nil {
return false, err
}
return matched, nil
}
}
func query_match_embedded_condition(schema *storage.TableSchemaStructure, value_record storage.Record, condition *query.Condition) (bool, error) {
switch condition.Type {
case query.Condition_Not:
embedded_condition := condition.Parameter.(*query.Condition)
result, err := query_match_condition(schema, value_record, embedded_condition)
return !result, err
case query.Condition_Or:
embedded_conditions := condition.Parameter.([]query.Condition)
for i := range embedded_conditions {
embedded_condition := &embedded_conditions[i]
matched, err := query_match_condition(schema, value_record, embedded_condition)
if err != nil {
return false, err
}
if matched {
return true, nil
}
}
return false, nil
default:
return false, fmt.Errorf("not an embedded condition")
}
}
func query_column_matches_condition(col any, schema *storage.TableSchemaColumn, condition *query.Condition) (bool, error) {
switch condition.Type {
case query.Condition_Equals:
return query_column_matches_condition_equals(col, schema, condition)
case query.Condition_GreaterThan:
return query_column_matches_condition_greater_than(col, schema, condition)
case query.Condition_LessThan:
return query_column_matches_condition_less_than(col, schema, condition)
case query.Condition_GreaterThanOrEqual:
return query_column_matches_condition_greater_than_or_equal(col, schema, condition)
case query.Condition_LessThanOrEqual:
return query_column_matches_condition_less_than_or_equal(col, schema, condition)
case query.Condition_RegularExpression:
return query_column_matches_condition_regular_expression(col, schema, condition)
case query.Condition_Not:
base_condition := condition.Parameter.(*query.Condition)
matched, err := query_column_matches_condition(col, schema, base_condition)
if err != nil {
return false, err
}
return !matched, nil
case query.Condition_BitwiseAND:
return query_column_matches_condition_bitwise_and(col, schema, condition)
default:
panic(condition.Type)
}
}
type snapshot interface {
Get(b []byte, readopt *opt.ReadOptions) ([]byte, error)
NewIterator(ur *util.Range, readopt *opt.ReadOptions) iterator.Iterator
}
func (engine *engine) query_match_all_records(table_id int32, snap snapshot, schema *storage.TableSchemaStructure, query_expression *query.Expression) (records []storage.Record, ids []uint64, err error) {
var range_iteration util.Range
var read_options opt.ReadOptions
range_iteration.Start = make_record_sector_key(table_id, 0)
range_iteration.Limit = make_record_sector_key(table_id, math.MaxUint64)
iter := snap.NewIterator(&range_iteration, &read_options)
records, ids, err = query_match_iterator_all_records(table_id, iter, schema, query_expression)
return
}
// (SLOW!!!) iterate through all records
func query_match_iterator_all_records(table_id int32, iter iterator.Iterator, schema *storage.TableSchemaStructure, query_expression *query.Expression) (records []storage.Record, record_IDs []uint64, err error) {
limited := query_expression.Limit > 0
for iter.Next() {
// Stop iteration once limit is reached.
if limited && len(records) >= int(query_expression.Limit) {
break
}
// Get key & value from LevelDB instance
key := iter.Key()
value := iter.Value()
// Sanity check
// TODO disable once we are certain that the iterator is ordered correctly
key_table_id := int32(binary.LittleEndian.Uint32(key[0:4]))
key_type := key_type(key[4])
if !(key_type == key_type_table_record && key_table_id == table_id) {
panic(fmt.Errorf("invalid record in iterator, there must be a key sorting failure (key type %d, table id %d)", key_type, key_table_id))
}
key_record_ID := binary.LittleEndian.Uint64(key[5:13])
// Unmarshal value from LevelDB into a Record
var value_record storage.Record
value_record, err = record.Unmarshal(schema, value)
if err != nil {
return
}
// Matched = should we add this to the list?
var matched bool
// Range through all of the query conditions, rejecting the record if a condition isn't met
for c := range query_expression.Conditions {
condition := &query_expression.Conditions[c]
matched, err = query_match_condition(schema, value_record, condition)
if err != nil {
return
}
if !matched {
break
}
}
if matched {
records = append(records, value_record)
record_IDs = append(record_IDs, key_record_ID)
}
}
iter.Release()
return
}