-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsdb.go
92 lines (72 loc) · 1.7 KB
/
tsdb.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
package tsdb
import (
"time"
)
type TSDB[T any] struct {
retentionPolicy time.Duration
stop chan struct{}
isClosed bool
idx *index
store *shard[T]
}
func New[T any](retentionPolicy time.Duration) *TSDB[T] {
store := newShard[T]()
idx := newIndex()
stop := make(chan struct{})
db := &TSDB[T]{retentionPolicy: retentionPolicy, store: store, idx: idx, stop: stop}
go db.gc()
return db
}
// WritePoints 写入数据点
func (db *TSDB[T]) WritePoints(points []Point[T]) error {
if db.isClosed {
return ErrDBClosed
}
seriesTags := make(map[string][]Tag, len(points))
values := make(map[string][]Value[T], len(points))
for _, point := range points {
s := point.Series()
if len(s) == 0 {
return ErrPointMissingTag
}
v := Value[T]{UnixNano: point.time.UnixNano(), V: point.field}
values[s] = append(values[s], v)
if _, ok := seriesTags[s]; ok {
continue
}
seriesTags[s] = point.tags
}
db.idx.createSeriesIfNotExists(seriesTags)
db.store.writeMulti(values)
return nil
}
// QueryPoints 查询数据点
func (db *TSDB[T]) QueryPoints(tags []Tag, min, max time.Time) map[string][]Value[T] {
series := db.idx.findSeries(tags)
values := make(map[string][]Value[T])
for _, s := range series {
p := db.store.getPartitions(s)
vs := p.valuesBetween(s, min.UnixNano(), max.UnixNano())
if len(vs) != 0 {
values[s] = vs
}
}
return values
}
func (db *TSDB[T]) Stop() {
db.stop <- struct{}{}
db.isClosed = true
}
func (db *TSDB[T]) gc() {
ticker := time.NewTicker(db.retentionPolicy)
defer ticker.Stop()
for {
select {
case <-db.stop:
return
case <-ticker.C:
remove := time.Now().Add(-db.retentionPolicy).UnixNano()
db.store.removeBefore(remove)
}
}
}