This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
forked from gocassa/gocassa
-
Notifications
You must be signed in to change notification settings - Fork 13
/
multiflakeseries_table.go
147 lines (128 loc) · 4.06 KB
/
multiflakeseries_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package gocassa
import (
"fmt"
"time"
)
type multiFlakeSeriesT struct {
t Table
indexField string
idField string
bucketSize time.Duration
}
func (o *multiFlakeSeriesT) Table() Table { return o.t }
func (o *multiFlakeSeriesT) Create() error { return o.Table().Create() }
func (o *multiFlakeSeriesT) CreateIfNotExist() error { return o.Table().CreateIfNotExist() }
func (o *multiFlakeSeriesT) Name() string { return o.Table().Name() }
func (o *multiFlakeSeriesT) Recreate() error { return o.Table().Recreate() }
func (o *multiFlakeSeriesT) CreateStatement() (Statement, error) { return o.Table().CreateStatement() }
func (o *multiFlakeSeriesT) CreateIfNotExistStatement() (Statement, error) {
return o.Table().CreateIfNotExistStatement()
}
func (o *multiFlakeSeriesT) Set(v interface{}) Op {
m, ok := toMap(v)
if !ok {
panic("Can't set: not able to convert")
}
id, ok := m[o.idField].(string)
if !ok {
panic(fmt.Sprintf("Id field (%s) is not present or is not a string", o.idField))
}
timestamp, err := flakeToTime(id)
if err != nil {
return errOp{err: err}
}
m[flakeTimestampFieldName] = timestamp
m[bucketFieldName] = bucket(timestamp, o.bucketSize)
return o.Table().
Set(m)
}
func (o *multiFlakeSeriesT) Update(v interface{}, id string, m map[string]interface{}) Op {
timestamp, err := flakeToTime(id)
if err != nil {
return errOp{err: err}
}
bucket := bucket(timestamp, o.bucketSize)
return o.Table().
Where(Eq(o.indexField, v),
Eq(bucketFieldName, bucket),
Eq(flakeTimestampFieldName, timestamp),
Eq(o.idField, id)).
Update(m)
}
func (o *multiFlakeSeriesT) Delete(v interface{}, id string) Op {
timestamp, err := flakeToTime(id)
if err != nil {
return errOp{err: err}
}
bucket := bucket(timestamp, o.bucketSize)
return o.Table().
Where(Eq(o.indexField, v),
Eq(bucketFieldName, bucket),
Eq(flakeTimestampFieldName, timestamp),
Eq(o.idField, id)).
Delete()
}
func (o *multiFlakeSeriesT) Read(v interface{}, id string, pointer interface{}) Op {
timestamp, err := flakeToTime(id)
if err != nil {
return errOp{err: err}
}
bucket := bucket(timestamp, o.bucketSize)
return o.Table().
Where(Eq(o.indexField, v),
Eq(bucketFieldName, bucket),
Eq(flakeTimestampFieldName, timestamp),
Eq(o.idField, id)).
ReadOne(pointer)
}
func (o *multiFlakeSeriesT) List(v interface{}, startTime, 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())
}
return o.Table().
Where(Eq(o.indexField, v),
In(bucketFieldName, buckets...),
GTE(flakeTimestampFieldName, startTime),
LT(flakeTimestampFieldName, endTime)).
Read(pointerToASlice)
}
func (o *multiFlakeSeriesT) Buckets(v interface{}, start time.Time) Buckets {
return bucketIter{
v: start,
step: o.bucketSize,
field: bucketFieldName,
invariant: o.Table().Where(Eq(o.indexField, v))}
}
func (o *multiFlakeSeriesT) ListSince(v interface{}, id string, window time.Duration, pointerToASlice interface{}) Op {
startTime, err := flakeToTime(id)
if err != nil {
return errOp{err: err}
}
var endTime time.Time
if window == 0 {
// no window set - so go up until 5 mins in the future
endTime = time.Now().Add(5 * time.Minute)
} else {
endTime = startTime.Add(window)
}
buckets := []interface{}{}
for bucket := o.Buckets(v, startTime); bucket.Bucket().Before(endTime); bucket = bucket.Next() {
buckets = append(buckets, bucket.Bucket())
}
return o.Table().
Where(Eq(o.indexField, v),
In(bucketFieldName, buckets),
GTE(flakeTimestampFieldName, startTime),
LT(flakeTimestampFieldName, endTime),
GT(o.idField, id)).
Read(pointerToASlice)
}
func (o *multiFlakeSeriesT) WithOptions(opt Options) MultiFlakeSeriesTable {
return &multiFlakeSeriesT{
t: o.Table().WithOptions(opt),
indexField: o.indexField,
idField: o.idField,
bucketSize: o.bucketSize,
}
}