-
Notifications
You must be signed in to change notification settings - Fork 608
/
retention.go
81 lines (67 loc) · 1.72 KB
/
retention.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
package segment
import (
"time"
)
type RetentionPolicy struct {
now time.Time
AbsoluteTime time.Time
Levels map[int]time.Time
ExemplarsRetentionTime time.Time
}
func NewRetentionPolicy() *RetentionPolicy {
return &RetentionPolicy{now: time.Now()}
}
func (r RetentionPolicy) LowerTimeBoundary() time.Time {
if len(r.Levels) == 0 {
return r.AbsoluteTime
}
return r.Levels[0]
}
func (r *RetentionPolicy) SetAbsolutePeriod(period time.Duration) *RetentionPolicy {
r.AbsoluteTime = r.periodToTime(period)
return r
}
func (r *RetentionPolicy) SetExemplarsRetentionPeriod(period time.Duration) *RetentionPolicy {
r.ExemplarsRetentionTime = r.periodToTime(period)
return r
}
func (r *RetentionPolicy) SetLevelPeriod(level int, period time.Duration) *RetentionPolicy {
if r.Levels == nil {
r.Levels = make(map[int]time.Time)
}
r.Levels[level] = r.periodToTime(period)
return r
}
func (r *RetentionPolicy) SetLevels(levels ...time.Duration) *RetentionPolicy {
if r.Levels == nil {
r.Levels = make(map[int]time.Time)
}
for level, period := range levels {
if period != 0 {
r.Levels[level] = r.periodToTime(period)
}
}
return r
}
func (r RetentionPolicy) isToBeDeleted(sn *streeNode) bool {
return sn.isBefore(r.AbsoluteTime) || sn.isBefore(r.levelMaxTime(sn.depth))
}
func (r RetentionPolicy) periodToTime(age time.Duration) time.Time {
if age == 0 {
return time.Time{}
}
return r.now.Add(-1 * age)
}
func (r *RetentionPolicy) normalize() *RetentionPolicy {
r.AbsoluteTime = normalizeTime(r.AbsoluteTime)
for k, v := range r.Levels {
r.Levels[k] = normalizeTime(v)
}
return r
}
func (r RetentionPolicy) levelMaxTime(depth int) time.Time {
if r.Levels == nil {
return time.Time{}
}
return r.Levels[depth]
}