forked from googleapis/google-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgc.go
249 lines (212 loc) · 6.75 KB
/
gc.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
Copyright 2015 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package bigtable
import (
"fmt"
"strings"
"time"
bttdpb "google.golang.org/genproto/googleapis/bigtable/admin/v2"
"google.golang.org/protobuf/types/known/durationpb"
)
// PolicyType represents the type of GCPolicy
type PolicyType int
const (
// PolicyUnspecified represents type of NoGCPolicy
PolicyUnspecified PolicyType = iota
// PolicyMaxAge represents type of MaxAgeGCPolicy
PolicyMaxAge
// PolicyMaxVersion represents type of MaxVersionGCPolicy
PolicyMaxVersion
// PolicyUnion represents type of UnionGCPolicy
PolicyUnion
// PolicyIntersection represents type of IntersectionGCPolicy
PolicyIntersection
)
// A GCPolicy represents a rule that determines which cells are eligible for garbage collection.
type GCPolicy interface {
String() string
proto() *bttdpb.GcRule
}
// GetPolicyType takes a gc policy and return appropriate PolicyType
func GetPolicyType(gc GCPolicy) PolicyType {
switch gc.proto().Rule.(type) {
case *bttdpb.GcRule_Intersection_:
return PolicyIntersection
case *bttdpb.GcRule_Union_:
return PolicyUnion
case *bttdpb.GcRule_MaxAge:
return PolicyMaxAge
case *bttdpb.GcRule_MaxNumVersions:
return PolicyMaxVersion
default:
return PolicyUnspecified
}
}
// IntersectionPolicy returns a GC policy that only applies when all its sub-policies apply.
func IntersectionPolicy(sub ...GCPolicy) GCPolicy { return IntersectionGCPolicy{sub} }
// IntersectionGCPolicy with list of children
type IntersectionGCPolicy struct {
// List of children policy in the intersection
Children []GCPolicy
}
func (ip IntersectionGCPolicy) String() string {
var ss []string
for _, sp := range ip.Children {
ss = append(ss, sp.String())
}
return "(" + strings.Join(ss, " && ") + ")"
}
func (ip IntersectionGCPolicy) proto() *bttdpb.GcRule {
inter := &bttdpb.GcRule_Intersection{}
for _, sp := range ip.Children {
inter.Rules = append(inter.Rules, sp.proto())
}
return &bttdpb.GcRule{
Rule: &bttdpb.GcRule_Intersection_{Intersection: inter},
}
}
// UnionPolicy returns a GC policy that applies when any of its sub-policies apply.
func UnionPolicy(sub ...GCPolicy) GCPolicy { return UnionGCPolicy{sub} }
// UnionGCPolicy with list of children
type UnionGCPolicy struct {
// List of children policy in the union
Children []GCPolicy
}
func (up UnionGCPolicy) String() string {
var ss []string
for _, sp := range up.Children {
ss = append(ss, sp.String())
}
return "(" + strings.Join(ss, " || ") + ")"
}
func (up UnionGCPolicy) proto() *bttdpb.GcRule {
union := &bttdpb.GcRule_Union{}
for _, sp := range up.Children {
union.Rules = append(union.Rules, sp.proto())
}
return &bttdpb.GcRule{
Rule: &bttdpb.GcRule_Union_{Union: union},
}
}
// MaxVersionsPolicy returns a GC policy that applies to all versions of a cell
// except for the most recent n.
func MaxVersionsPolicy(n int) GCPolicy { return MaxVersionsGCPolicy(n) }
// MaxVersionsGCPolicy type alias
type MaxVersionsGCPolicy int
func (mvp MaxVersionsGCPolicy) String() string {
return fmt.Sprintf("versions() > %d", int(mvp))
}
func (mvp MaxVersionsGCPolicy) proto() *bttdpb.GcRule {
return &bttdpb.GcRule{Rule: &bttdpb.GcRule_MaxNumVersions{MaxNumVersions: int32(mvp)}}
}
// MaxAgePolicy returns a GC policy that applies to all cells
// older than the given age.
func MaxAgePolicy(d time.Duration) GCPolicy { return MaxAgeGCPolicy(d) }
// MaxAgeGCPolicy type alias
type MaxAgeGCPolicy time.Duration
var units = []struct {
d time.Duration
suffix string
}{
{24 * time.Hour, "d"},
{time.Hour, "h"},
{time.Minute, "m"},
}
func (ma MaxAgeGCPolicy) String() string {
return fmt.Sprintf("age() > %s", ma.GetDurationString())
}
// GetDurationString returns the duration string of the MaxAgeGCPolicy
func (ma MaxAgeGCPolicy) GetDurationString() string {
d := time.Duration(ma)
for _, u := range units {
if d%u.d == 0 {
return fmt.Sprintf("%d%s", d/u.d, u.suffix)
}
}
return fmt.Sprintf("%d", d/time.Microsecond)
}
func (ma MaxAgeGCPolicy) proto() *bttdpb.GcRule {
// This doesn't handle overflows, etc.
// Fix this if people care about GC policies over 290 years.
ns := time.Duration(ma).Nanoseconds()
return &bttdpb.GcRule{
Rule: &bttdpb.GcRule_MaxAge{MaxAge: &durationpb.Duration{
Seconds: ns / 1e9,
Nanos: int32(ns % 1e9),
}},
}
}
type noGCPolicy struct{}
func (n noGCPolicy) String() string { return "" }
func (n noGCPolicy) proto() *bttdpb.GcRule { return &bttdpb.GcRule{Rule: nil} }
// NoGcPolicy applies to all cells setting maxage and maxversions to nil implies no gc policies
func NoGcPolicy() GCPolicy { return noGCPolicy{} }
// GCRuleToString converts the given GcRule proto to a user-visible string.
func GCRuleToString(rule *bttdpb.GcRule) string {
if rule == nil {
return "<never>"
}
switch r := rule.Rule.(type) {
case *bttdpb.GcRule_MaxNumVersions:
return MaxVersionsPolicy(int(r.MaxNumVersions)).String()
case *bttdpb.GcRule_MaxAge:
return MaxAgePolicy(time.Duration(r.MaxAge.Seconds) * time.Second).String()
case *bttdpb.GcRule_Intersection_:
return joinRules(r.Intersection.Rules, " && ")
case *bttdpb.GcRule_Union_:
return joinRules(r.Union.Rules, " || ")
default:
return ""
}
}
func gcRuleToPolicy(rule *bttdpb.GcRule) GCPolicy {
if rule == nil {
return NoGcPolicy()
}
switch r := rule.Rule.(type) {
case *bttdpb.GcRule_Intersection_:
return compoundRuleToPolicy(r.Intersection.Rules, PolicyIntersection)
case *bttdpb.GcRule_Union_:
return compoundRuleToPolicy(r.Union.Rules, PolicyUnion)
case *bttdpb.GcRule_MaxAge:
return MaxAgePolicy(time.Duration(r.MaxAge.Seconds) * time.Second)
case *bttdpb.GcRule_MaxNumVersions:
return MaxVersionsPolicy(int(r.MaxNumVersions))
default:
return NoGcPolicy()
}
}
func joinRules(rules []*bttdpb.GcRule, sep string) string {
var chunks []string
for _, r := range rules {
chunks = append(chunks, GCRuleToString(r))
}
return "(" + strings.Join(chunks, sep) + ")"
}
func compoundRuleToPolicy(rules []*bttdpb.GcRule, mode PolicyType) GCPolicy {
sub := []GCPolicy{}
for _, r := range rules {
p := gcRuleToPolicy(r)
if p.String() != "" {
sub = append(sub, gcRuleToPolicy(r))
}
}
switch mode {
case PolicyUnion:
return UnionGCPolicy{Children: sub}
case PolicyIntersection:
return IntersectionGCPolicy{Children: sub}
default:
return NoGcPolicy()
}
}