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
/
multitimeseries_table.go
99 lines (88 loc) · 2.95 KB
/
multitimeseries_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
package gocassa
import (
"time"
)
type multiTimeSeriesT struct {
t Table
indexField string
timeField string
idField string
bucketSize time.Duration
}
func (o *multiTimeSeriesT) Table() Table { return o.t }
func (o *multiTimeSeriesT) Create() error { return o.Table().Create() }
func (o *multiTimeSeriesT) CreateIfNotExist() error { return o.Table().CreateIfNotExist() }
func (o *multiTimeSeriesT) Name() string { return o.Table().Name() }
func (o *multiTimeSeriesT) Recreate() error { return o.Table().Recreate() }
func (o *multiTimeSeriesT) CreateStatement() (Statement, error) { return o.Table().CreateStatement() }
func (o *multiTimeSeriesT) CreateIfNotExistStatement() (Statement, error) {
return o.Table().CreateIfNotExistStatement()
}
func (o *multiTimeSeriesT) 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 *multiTimeSeriesT) Update(v interface{}, timeStamp time.Time, id interface{}, m map[string]interface{}) Op {
bucket := bucket(timeStamp, o.bucketSize)
return o.Table().
Where(Eq(o.indexField, v),
Eq(bucketFieldName, bucket),
Eq(o.timeField, timeStamp),
Eq(o.idField, id)).
Update(m)
}
func (o *multiTimeSeriesT) Delete(v interface{}, timeStamp time.Time, id interface{}) Op {
bucket := bucket(timeStamp, o.bucketSize)
return o.Table().
Where(Eq(o.indexField, v),
Eq(bucketFieldName, bucket),
Eq(o.timeField, timeStamp),
Eq(o.idField, id)).
Delete()
}
func (o *multiTimeSeriesT) Read(v interface{}, timeStamp time.Time, id, pointer interface{}) Op {
bucket := bucket(timeStamp, o.bucketSize)
return o.Table().
Where(Eq(o.indexField, v),
Eq(bucketFieldName, bucket),
Eq(o.timeField, timeStamp),
Eq(o.idField, id)).
ReadOne(pointer)
}
func (o *multiTimeSeriesT) List(v 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())
}
return o.Table().
Where(Eq(o.indexField, v),
In(bucketFieldName, buckets...),
GTE(o.timeField, startTime),
LTE(o.timeField, endTime)).
Read(pointerToASlice)
}
func (o *multiTimeSeriesT) 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 *multiTimeSeriesT) WithOptions(opt Options) MultiTimeSeriesTable {
return &multiTimeSeriesT{
t: o.Table().WithOptions(opt),
indexField: o.indexField,
timeField: o.timeField,
idField: o.idField,
bucketSize: o.bucketSize,
}
}