This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathorm.go
692 lines (617 loc) · 22.2 KB
/
orm.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// Copyright 2017 Pilosa Corp.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
package pilosa
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"time"
)
const timeFormat = "2006-01-02T15:04"
// Schema contains the index properties
type Schema struct {
indexes map[string]*Index
}
// NewSchema creates a new Schema
func NewSchema() *Schema {
return &Schema{
indexes: make(map[string]*Index),
}
}
// Index returns an index with a name and options.
// Pass nil for default options.
func (s *Schema) Index(name string, options *IndexOptions) (*Index, error) {
if index, ok := s.indexes[name]; ok {
return index, nil
}
index, err := NewIndex(name, options)
if err != nil {
return nil, err
}
s.indexes[name] = index
return index, nil
}
// Indexes return a copy of the indexes in this schema
func (s *Schema) Indexes() map[string]*Index {
result := make(map[string]*Index)
for k, v := range s.indexes {
result[k] = v.copy()
}
return result
}
func (s *Schema) diff(other *Schema) *Schema {
result := NewSchema()
for indexName, index := range s.indexes {
if otherIndex, ok := other.indexes[indexName]; !ok {
// if the index doesn't exist in the other schema, simply copy it
result.indexes[indexName] = index.copy()
} else {
// the index exists in the other schema; check the frames
resultIndex, _ := NewIndex(indexName, index.options)
for frameName, frame := range index.frames {
if _, ok := otherIndex.frames[frameName]; !ok {
// the frame doesn't exist in the other schema, copy it
resultIndex.frames[frameName] = frame.copy()
}
}
// check whether we modified result index
if len(resultIndex.frames) > 0 {
// if so, move it to the result
result.indexes[indexName] = resultIndex
}
}
}
return result
}
// PQLQuery is an interface for PQL queries.
type PQLQuery interface {
Index() *Index
serialize() string
Error() error
}
// PQLBaseQuery is the base implementation for PQLQuery.
type PQLBaseQuery struct {
index *Index
pql string
err error
}
// NewPQLBaseQuery creates a new PQLQuery with the given PQL and index.
func NewPQLBaseQuery(pql string, index *Index, err error) *PQLBaseQuery {
return &PQLBaseQuery{
index: index,
pql: pql,
err: err,
}
}
// Index returns the index for this query
func (q *PQLBaseQuery) Index() *Index {
return q.index
}
func (q *PQLBaseQuery) serialize() string {
return q.pql
}
// Error returns the error or nil for this query.
func (q PQLBaseQuery) Error() error {
return q.err
}
// PQLBitmapQuery is the return type for bitmap queries.
type PQLBitmapQuery struct {
index *Index
pql string
err error
}
// Index returns the index for this query/
func (q *PQLBitmapQuery) Index() *Index {
return q.index
}
func (q *PQLBitmapQuery) serialize() string {
return q.pql
}
// Error returns the error or nil for this query.
func (q PQLBitmapQuery) Error() error {
return q.err
}
// PQLBatchQuery contains a batch of PQL queries.
// Use Index.BatchQuery function to create an instance.
//
// Usage:
//
// index, err := NewIndex("repository", nil)
// stargazer, err := index.Frame("stargazer", nil)
// query := repo.BatchQuery(
// stargazer.Bitmap(5),
// stargazer.Bitmap(15),
// repo.Union(stargazer.Bitmap(20), stargazer.Bitmap(25)))
type PQLBatchQuery struct {
index *Index
queries []string
err error
}
// Index returns the index for this query.
func (q *PQLBatchQuery) Index() *Index {
return q.index
}
func (q *PQLBatchQuery) serialize() string {
return strings.Join(q.queries, "")
}
func (q *PQLBatchQuery) Error() error {
return q.err
}
// Add adds a query to the batch.
func (q *PQLBatchQuery) Add(query PQLQuery) {
err := query.Error()
if err != nil {
q.err = err
}
q.queries = append(q.queries, query.serialize())
}
// IndexOptions contains options to customize Index structs and column queries.
type IndexOptions struct {
ColumnLabel string
TimeQuantum TimeQuantum
}
func (options *IndexOptions) withDefaults() (updated *IndexOptions) {
// copy options so the original is not updated
updated = &IndexOptions{}
*updated = *options
// impose defaults
if updated.ColumnLabel == "" {
updated.ColumnLabel = "columnID"
}
return
}
func (options IndexOptions) String() string {
return fmt.Sprintf(`{"options": {"columnLabel": "%s"}}`, options.ColumnLabel)
}
// NewPQLBitmapQuery creates a new PqlBitmapQuery.
func NewPQLBitmapQuery(pql string, index *Index, err error) *PQLBitmapQuery {
return &PQLBitmapQuery{
index: index,
pql: pql,
err: err,
}
}
// Index is a Pilosa index. The purpose of the Index is to represent a data namespace.
// You cannot perform cross-index queries. Column-level attributes are global to the Index.
type Index struct {
name string
options *IndexOptions
frames map[string]*Frame
}
// NewIndex creates an index with a name and options.
// Pass nil for default options.
func NewIndex(name string, options *IndexOptions) (*Index, error) {
if err := validateIndexName(name); err != nil {
return nil, err
}
if options == nil {
options = &IndexOptions{}
}
options = options.withDefaults()
if err := validateLabel(options.ColumnLabel); err != nil {
return nil, err
}
return &Index{
name: name,
options: options,
frames: map[string]*Frame{},
}, nil
}
// Frames return a copy of the frames in this index
func (d *Index) Frames() map[string]*Frame {
result := make(map[string]*Frame)
for k, v := range d.frames {
result[k] = v.copy()
}
return result
}
func (d *Index) copy() *Index {
frames := make(map[string]*Frame)
for name, f := range d.frames {
frames[name] = f.copy()
}
index := &Index{
name: d.name,
frames: frames,
options: &IndexOptions{},
}
*index.options = *d.options
return index
}
// Name returns the name of this index.
func (d *Index) Name() string {
return d.name
}
// Frame creates a frame struct with the specified name and defaults.
func (d *Index) Frame(name string, options *FrameOptions) (*Frame, error) {
if frame, ok := d.frames[name]; ok {
return frame, nil
}
if options == nil {
options = &FrameOptions{}
}
if err := validateFrameName(name); err != nil {
return nil, err
}
options = options.withDefaults()
if err := validateLabel(options.RowLabel); err != nil {
return nil, err
}
frame := &Frame{
name: name,
index: d,
options: options,
}
d.frames[name] = frame
return frame, nil
}
// BatchQuery creates a batch query with the given queries.
func (d *Index) BatchQuery(queries ...PQLQuery) *PQLBatchQuery {
stringQueries := make([]string, 0, len(queries))
for _, query := range queries {
stringQueries = append(stringQueries, query.serialize())
}
return &PQLBatchQuery{
index: d,
queries: stringQueries,
}
}
// RawQuery creates a query with the given string.
// Note that the query is not validated before sending to the server.
func (d *Index) RawQuery(query string) *PQLBaseQuery {
return NewPQLBaseQuery(query, d, nil)
}
// Union creates a Union query.
// Union performs a logical OR on the results of each BITMAP_CALL query passed to it.
func (d *Index) Union(bitmaps ...*PQLBitmapQuery) *PQLBitmapQuery {
return d.bitmapOperation("Union", bitmaps...)
}
// Intersect creates an Intersect query.
// Intersect performs a logical AND on the results of each BITMAP_CALL query passed to it.
func (d *Index) Intersect(bitmaps ...*PQLBitmapQuery) *PQLBitmapQuery {
if len(bitmaps) < 1 {
return NewPQLBitmapQuery("", d, NewError("Intersect operation requires at least 1 bitmap"))
}
return d.bitmapOperation("Intersect", bitmaps...)
}
// Difference creates an Intersect query.
// Difference returns all of the bits from the first BITMAP_CALL argument passed to it, without the bits from each subsequent BITMAP_CALL.
func (d *Index) Difference(bitmaps ...*PQLBitmapQuery) *PQLBitmapQuery {
if len(bitmaps) < 1 {
return NewPQLBitmapQuery("", d, NewError("Difference operation requires at least 1 bitmap"))
}
return d.bitmapOperation("Difference", bitmaps...)
}
// Xor creates an Xor query.
func (d *Index) Xor(bitmaps ...*PQLBitmapQuery) *PQLBitmapQuery {
if len(bitmaps) < 2 {
return NewPQLBitmapQuery("", d, NewError("Xor operation requires at least 2 bitmaps"))
}
return d.bitmapOperation("Xor", bitmaps...)
}
// Count creates a Count query.
// Returns the number of set bits in the BITMAP_CALL passed in.
func (d *Index) Count(bitmap *PQLBitmapQuery) *PQLBaseQuery {
return NewPQLBaseQuery(fmt.Sprintf("Count(%s)", bitmap.serialize()), d, nil)
}
// SetColumnAttrs creates a SetColumnAttrs query.
// SetColumnAttrs associates arbitrary key/value pairs with a column in an index.
// Following types are accepted: integer, float, string and boolean types.
func (d *Index) SetColumnAttrs(columnID uint64, attrs map[string]interface{}) *PQLBaseQuery {
attrsString, err := createAttributesString(attrs)
if err != nil {
return NewPQLBaseQuery("", d, err)
}
return NewPQLBaseQuery(fmt.Sprintf("SetColumnAttrs(%s=%d, %s)",
d.options.ColumnLabel, columnID, attrsString), d, nil)
}
func (d *Index) bitmapOperation(name string, bitmaps ...*PQLBitmapQuery) *PQLBitmapQuery {
var err error
args := make([]string, 0, len(bitmaps))
for _, bitmap := range bitmaps {
if err = bitmap.Error(); err != nil {
return NewPQLBitmapQuery("", d, err)
}
args = append(args, bitmap.serialize())
}
return NewPQLBitmapQuery(fmt.Sprintf("%s(%s)", name, strings.Join(args, ", ")), d, nil)
}
// FrameInfo represents schema information for a frame.
type FrameInfo struct {
Name string `json:"name"`
}
// FrameOptions contains options to customize Frame objects and frame queries.
type FrameOptions struct {
RowLabel string
// If a Frame has a time quantum, then Views are generated for each of the defined time segments.
TimeQuantum TimeQuantum
// Enables inverted frames
InverseEnabled bool
CacheType CacheType
CacheSize uint
fields map[string]RangeField
}
func (options *FrameOptions) withDefaults() (updated *FrameOptions) {
// copy options so the original is not updated
updated = &FrameOptions{}
*updated = *options
// impose defaults
if updated.RowLabel == "" {
updated.RowLabel = "rowID"
}
if updated.fields == nil {
updated.fields = map[string]RangeField{}
}
return
}
func (options FrameOptions) String() string {
mopt := map[string]interface{}{
"rowLabel": options.RowLabel,
}
if options.InverseEnabled {
mopt["inverseEnabled"] = true
}
if options.TimeQuantum != TimeQuantumNone {
mopt["timeQuantum"] = string(options.TimeQuantum)
}
if options.CacheType != CacheTypeDefault {
mopt["cacheType"] = string(options.CacheType)
}
if options.CacheSize != 0 {
mopt["cacheSize"] = options.CacheSize
}
if len(options.fields) > 0 {
mopt["rangeEnabled"] = true
fields := make([]RangeField, 0, len(options.fields))
for _, field := range options.fields {
fields = append(fields, field)
}
mopt["fields"] = fields
}
return fmt.Sprintf(`{"options": %s}`, encodeMap(mopt))
}
// AddIntField adds an integer field to the frame options
func (options *FrameOptions) AddIntField(name string, min int, max int) error {
err := validateLabel(name)
if err != nil {
return err
}
if max <= min {
return errors.New("Max should be greater than min for int fields")
}
if options.fields == nil {
options.fields = map[string]RangeField{}
}
options.fields[name] = map[string]interface{}{
"name": name,
"type": "int",
"min": min,
"max": max,
}
return nil
}
// Frame structs are used to segment and define different functional characteristics within your entire index.
// You can think of a Frame as a table-like data partition within your Index.
// Row-level attributes are namespaced at the Frame level.
type Frame struct {
name string
index *Index
options *FrameOptions
}
// Name returns the name of the frame
func (f *Frame) Name() string {
return f.name
}
func (f *Frame) copy() *Frame {
frame := &Frame{
name: f.name,
index: f.index,
options: &FrameOptions{},
}
*frame.options = *f.options
return frame
}
// Bitmap creates a bitmap query using the row label.
// Bitmap retrieves the indices of all the set bits in a row or column based on whether the row label or column label is given in the query.
// It also retrieves any attributes set on that row or column.
func (f *Frame) Bitmap(rowID uint64) *PQLBitmapQuery {
return NewPQLBitmapQuery(fmt.Sprintf("Bitmap(%s=%d, frame='%s')",
f.options.RowLabel, rowID, f.name), f.index, nil)
}
// InverseBitmap creates a bitmap query using the column label.
// Bitmap retrieves the indices of all the set bits in a row or column based on whether the row label or column label is given in the query.
// It also retrieves any attributes set on that row or column.
func (f *Frame) InverseBitmap(columnID uint64) *PQLBaseQuery {
return NewPQLBaseQuery(fmt.Sprintf("Bitmap(%s=%d, frame='%s')",
f.index.options.ColumnLabel, columnID, f.name), f.index, nil)
}
// SetBit creates a SetBit query.
// SetBit, assigns a value of 1 to a bit in the binary matrix, thus associating the given row in the given frame with the given column.
func (f *Frame) SetBit(rowID uint64, columnID uint64) *PQLBaseQuery {
return NewPQLBaseQuery(fmt.Sprintf("SetBit(%s=%d, frame='%s', %s=%d)",
f.options.RowLabel, rowID, f.name, f.index.options.ColumnLabel, columnID), f.index, nil)
}
// SetBitTimestamp creates a SetBit query with timestamp.
// SetBit, assigns a value of 1 to a bit in the binary matrix,
// thus associating the given row in the given frame with the given column.
func (f *Frame) SetBitTimestamp(rowID uint64, columnID uint64, timestamp time.Time) *PQLBaseQuery {
return NewPQLBaseQuery(fmt.Sprintf("SetBit(%s=%d, frame='%s', %s=%d, timestamp='%s')",
f.options.RowLabel, rowID, f.name, f.index.options.ColumnLabel, columnID, timestamp.Format(timeFormat)),
f.index, nil)
}
// ClearBit creates a ClearBit query.
// ClearBit, assigns a value of 0 to a bit in the binary matrix, thus disassociating the given row in the given frame from the given column.
func (f *Frame) ClearBit(rowID uint64, columnID uint64) *PQLBaseQuery {
return NewPQLBaseQuery(fmt.Sprintf("ClearBit(%s=%d, frame='%s', %s=%d)",
f.options.RowLabel, rowID, f.name, f.index.options.ColumnLabel, columnID), f.index, nil)
}
// TopN creates a TopN query with the given item count.
// Returns the id and count of the top n bitmaps (by count of bits) in the frame.
func (f *Frame) TopN(n uint64) *PQLBitmapQuery {
return NewPQLBitmapQuery(fmt.Sprintf("TopN(frame='%s', n=%d, inverse=false)", f.name, n), f.index, nil)
}
// InverseTopN creates a TopN query with the given item count.
// Returns the id and count of the top n bitmaps (by count of bits) in the frame.
// This variant sets inverse=true
func (f *Frame) InverseTopN(n uint64) *PQLBitmapQuery {
return NewPQLBitmapQuery(fmt.Sprintf("TopN(frame='%s', n=%d, inverse=true)", f.name, n), f.index, nil)
}
// BitmapTopN creates a TopN query with the given item count and bitmap.
// This variant supports customizing the bitmap query.
func (f *Frame) BitmapTopN(n uint64, bitmap *PQLBitmapQuery) *PQLBitmapQuery {
return NewPQLBitmapQuery(fmt.Sprintf("TopN(%s, frame='%s', n=%d, inverse=false)",
bitmap.serialize(), f.name, n), f.index, nil)
}
// InverseBitmapTopN creates a TopN query with the given item count and bitmap.
// This variant supports customizing the bitmap query and sets inverse=true.
func (f *Frame) InverseBitmapTopN(n uint64, bitmap *PQLBitmapQuery) *PQLBitmapQuery {
return NewPQLBitmapQuery(fmt.Sprintf("TopN(%s, frame='%s', n=%d, inverse=true)",
bitmap.serialize(), f.name, n), f.index, nil)
}
// FilterFieldTopN creates a TopN query with the given item count, bitmap, field and the filter for that field
// The field and filters arguments work together to only return Bitmaps which have the attribute specified by field with one of the values specified in filters.
func (f *Frame) FilterFieldTopN(n uint64, bitmap *PQLBitmapQuery, field string, values ...interface{}) *PQLBitmapQuery {
return f.filterFieldTopN(n, bitmap, false, field, values...)
}
// InverseFilterFieldTopN creates a TopN query with the given item count, bitmap, field and the filter for that field
// The field and filters arguments work together to only return Bitmaps which have the attribute specified by field with one of the values specified in filters.
// This variant sets inverse=true.
func (f *Frame) InverseFilterFieldTopN(n uint64, bitmap *PQLBitmapQuery, field string, values ...interface{}) *PQLBitmapQuery {
return f.filterFieldTopN(n, bitmap, true, field, values...)
}
func (f *Frame) filterFieldTopN(n uint64, bitmap *PQLBitmapQuery, inverse bool, field string, values ...interface{}) *PQLBitmapQuery {
if err := validateLabel(field); err != nil {
return NewPQLBitmapQuery("", f.index, err)
}
b, err := json.Marshal(values)
if err != nil {
return NewPQLBitmapQuery("", f.index, err)
}
inverseStr := "true"
if !inverse {
inverseStr = "false"
}
if bitmap == nil {
return NewPQLBitmapQuery(fmt.Sprintf("TopN(frame='%s', n=%d, inverse=%s, field='%s', filters=%s)",
f.name, n, inverseStr, field, string(b)), f.index, nil)
}
return NewPQLBitmapQuery(fmt.Sprintf("TopN(%s, frame='%s', n=%d, inverse=%s, field='%s', filters=%s)",
bitmap.serialize(), f.name, n, inverseStr, field, string(b)), f.index, nil)
}
// Range creates a Range query.
// Similar to Bitmap, but only returns bits which were set with timestamps between the given start and end timestamps.
func (f *Frame) Range(rowID uint64, start time.Time, end time.Time) *PQLBitmapQuery {
return NewPQLBitmapQuery(fmt.Sprintf("Range(%s=%d, frame='%s', start='%s', end='%s')",
f.options.RowLabel, rowID, f.name, start.Format(timeFormat), end.Format(timeFormat)), f.index, nil)
}
// InverseRange creates a Range query.
// Similar to Bitmap, but only returns bits which were set with timestamps between the given start and end timestamps.
func (f *Frame) InverseRange(columnID uint64, start time.Time, end time.Time) *PQLBitmapQuery {
return NewPQLBitmapQuery(fmt.Sprintf("Range(%s=%d, frame='%s', start='%s', end='%s')",
f.index.options.ColumnLabel, columnID, f.name, start.Format(timeFormat), end.Format(timeFormat)), f.index, nil)
}
// SetRowAttrs creates a SetRowAttrs query.
// SetRowAttrs associates arbitrary key/value pairs with a row in a frame.
// Following types are accepted: integer, float, string and boolean types.
func (f *Frame) SetRowAttrs(rowID uint64, attrs map[string]interface{}) *PQLBaseQuery {
attrsString, err := createAttributesString(attrs)
if err != nil {
return NewPQLBaseQuery("", f.index, err)
}
return NewPQLBaseQuery(fmt.Sprintf("SetRowAttrs(%s=%d, frame='%s', %s)",
f.options.RowLabel, rowID, f.name, attrsString), f.index, nil)
}
// SumReduce creates a SumReduce query.
// The corresponding frame should include the field in its options.
func (f *Frame) SumReduce(bitmap *PQLBitmapQuery, field string) *PQLBaseQuery {
return f.rangeQuery("SumReduce", bitmap, field)
}
// SetIntFieldValue creates a SetFieldValue query.
func (f *Frame) SetIntFieldValue(columnID uint64, field string, value int) *PQLBaseQuery {
qry := fmt.Sprintf("SetFieldValue(frame='%s', %s=%d, %s=%d)", f.name, f.index.options.ColumnLabel, columnID, field, value)
return NewPQLBaseQuery(qry, f.index, nil)
}
func (f *Frame) rangeQuery(call string, bitmap *PQLBitmapQuery, field string) *PQLBaseQuery {
qry := fmt.Sprintf("%s(%s, frame='%s', field='%s')", call, bitmap.serialize(), f.name, field)
return NewPQLBaseQuery(qry, f.index, nil)
}
func createAttributesString(attrs map[string]interface{}) (string, error) {
attrsList := make([]string, 0, len(attrs))
for k, v := range attrs {
// TODO: validate the type of v is one of string, int64, float64, bool
if err := validateLabel(k); err != nil {
return "", err
}
if vs, ok := v.(string); ok {
attrsList = append(attrsList, fmt.Sprintf("%s=\"%s\"", k, strings.Replace(vs, "\"", "\\\"", -1)))
} else {
attrsList = append(attrsList, fmt.Sprintf("%s=%v", k, v))
}
}
sort.Strings(attrsList)
return strings.Join(attrsList, ", "), nil
}
// TimeQuantum type represents valid time quantum values for frames having support for that.
type TimeQuantum string
// TimeQuantum constants
const (
TimeQuantumNone TimeQuantum = ""
TimeQuantumYear TimeQuantum = "Y"
TimeQuantumMonth TimeQuantum = "M"
TimeQuantumDay TimeQuantum = "D"
TimeQuantumHour TimeQuantum = "H"
TimeQuantumYearMonth TimeQuantum = "YM"
TimeQuantumMonthDay TimeQuantum = "MD"
TimeQuantumDayHour TimeQuantum = "DH"
TimeQuantumYearMonthDay TimeQuantum = "YMD"
TimeQuantumMonthDayHour TimeQuantum = "MDH"
TimeQuantumYearMonthDayHour TimeQuantum = "YMDH"
)
// CacheType represents cache type for a frame
type CacheType string
// CacheType constants
const (
CacheTypeDefault CacheType = ""
CacheTypeLRU CacheType = "lru"
CacheTypeRanked CacheType = "ranked"
)
// RangeField represents a single field
type RangeField map[string]interface{}
func encodeMap(m map[string]interface{}) string {
result, err := json.Marshal(m)
if err != nil {
panic(err)
}
return string(result)
}