forked from monzo/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multikey_timeseries_table.go
129 lines (109 loc) · 4.19 KB
/
multikey_timeseries_table.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
package gocassa
import (
"time"
)
type multiKeyTimeSeriesT struct {
t Table
indexFields []string
timeField string
idFields []string
bucketSize time.Duration
}
func (o *multiKeyTimeSeriesT) Table() Table { return o.t }
func (o *multiKeyTimeSeriesT) Create() error { return o.Table().Create() }
func (o *multiKeyTimeSeriesT) CreateIfNotExist() error { return o.Table().CreateIfNotExist() }
func (o *multiKeyTimeSeriesT) Name() string { return o.Table().Name() }
func (o *multiKeyTimeSeriesT) Recreate() error { return o.Table().Recreate() }
func (o *multiKeyTimeSeriesT) CreateStatement() (Statement, error) { return o.Table().CreateStatement() }
func (o *multiKeyTimeSeriesT) CreateIfNotExistStatement() (Statement, error) {
return o.Table().CreateIfNotExistStatement()
}
func (o *multiKeyTimeSeriesT) Set(v interface{}) Op {
m, ok := toMap(v)
if !ok {
panic("Can't set: not able to convert")
}
if tim, ok := m[o.timeField].(time.Time); !ok {
panic("timeField is not actually a time.Time")
} else {
m[bucketFieldName] = bucket(tim, o.bucketSize)
}
return o.Table().
Set(m)
}
func (o *multiKeyTimeSeriesT) Update(v map[string]interface{}, timeStamp time.Time, id map[string]interface{}, m map[string]interface{}) Op {
bucket := bucket(timeStamp, o.bucketSize)
relations := make([]Relation, 0)
relations = append(relations, o.ListOfEqualRelations(v, id)...)
relations = append(relations, Eq(bucketFieldName, bucket))
relations = append(relations, Eq(o.timeField, timeStamp))
return o.Table().
Where(relations...).
Update(m)
}
func (o *multiKeyTimeSeriesT) Delete(v map[string]interface{}, timeStamp time.Time, id map[string]interface{}) Op {
bucket := bucket(timeStamp, o.bucketSize)
relations := make([]Relation, 0)
relations = append(relations, o.ListOfEqualRelations(v, id)...)
relations = append(relations, Eq(bucketFieldName, bucket))
relations = append(relations, Eq(o.timeField, timeStamp))
return o.Table().
Where(relations...).
Delete()
}
func (o *multiKeyTimeSeriesT) Read(v map[string]interface{}, timeStamp time.Time, id map[string]interface{}, pointer interface{}) Op {
bucket := bucket(timeStamp, o.bucketSize)
relations := make([]Relation, 0)
relations = append(relations, o.ListOfEqualRelations(v, id)...)
relations = append(relations, Eq(bucketFieldName, bucket))
relations = append(relations, Eq(o.timeField, timeStamp))
return o.Table().
Where(relations...).
ReadOne(pointer)
}
func (o *multiKeyTimeSeriesT) List(v map[string]interface{}, startTime time.Time, endTime time.Time, pointerToASlice interface{}) Op {
buckets := []interface{}{}
for bucket := o.Buckets(v, startTime); bucket.Bucket().Before(endTime); bucket = bucket.Next() {
buckets = append(buckets, bucket.Bucket())
}
relations := make([]Relation, 0)
relations = append(relations, o.ListOfEqualRelations(v, nil)...)
relations = append(relations, In(bucketFieldName, buckets...))
relations = append(relations, GTE(o.timeField, startTime))
relations = append(relations, LTE(o.timeField, endTime))
return o.Table().
Where(relations...).
Read(pointerToASlice)
}
func (o *multiKeyTimeSeriesT) Buckets(v map[string]interface{}, start time.Time) Buckets {
return bucketIter{
v: start,
step: o.bucketSize,
field: bucketFieldName,
invariant: o.Table().Where(o.ListOfEqualRelations(v, nil)...)}
}
func (o *multiKeyTimeSeriesT) WithOptions(opt Options) MultiKeyTimeSeriesTable {
return &multiKeyTimeSeriesT{
t: o.Table().WithOptions(opt),
indexFields: o.indexFields,
timeField: o.timeField,
idFields: o.idFields,
bucketSize: o.bucketSize,
}
}
func (o *multiKeyTimeSeriesT) ListOfEqualRelations(fieldsToIndex, ids map[string]interface{}) []Relation {
relations := make([]Relation, 0)
for _, field := range o.indexFields {
if value := fieldsToIndex[field]; value != nil && value != "" {
relation := Eq(field, value)
relations = append(relations, relation)
}
}
for _, field := range o.idFields {
if value := ids[field]; value != nil && value != "" {
relation := Eq(field, value)
relations = append(relations, relation)
}
}
return relations
}