You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

168 lines
4.5 KiB

  1. /*
  2. Copyright 2015 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package bigtable
  14. import (
  15. "fmt"
  16. "strings"
  17. "time"
  18. durpb "github.com/golang/protobuf/ptypes/duration"
  19. bttdpb "google.golang.org/genproto/googleapis/bigtable/admin/v2"
  20. )
  21. // A GCPolicy represents a rule that determines which cells are eligible for garbage collection.
  22. type GCPolicy interface {
  23. String() string
  24. proto() *bttdpb.GcRule
  25. }
  26. // IntersectionPolicy returns a GC policy that only applies when all its sub-policies apply.
  27. func IntersectionPolicy(sub ...GCPolicy) GCPolicy { return intersectionPolicy{sub} }
  28. type intersectionPolicy struct {
  29. sub []GCPolicy
  30. }
  31. func (ip intersectionPolicy) String() string {
  32. var ss []string
  33. for _, sp := range ip.sub {
  34. ss = append(ss, sp.String())
  35. }
  36. return "(" + strings.Join(ss, " && ") + ")"
  37. }
  38. func (ip intersectionPolicy) proto() *bttdpb.GcRule {
  39. inter := &bttdpb.GcRule_Intersection{}
  40. for _, sp := range ip.sub {
  41. inter.Rules = append(inter.Rules, sp.proto())
  42. }
  43. return &bttdpb.GcRule{
  44. Rule: &bttdpb.GcRule_Intersection_{Intersection: inter},
  45. }
  46. }
  47. // UnionPolicy returns a GC policy that applies when any of its sub-policies apply.
  48. func UnionPolicy(sub ...GCPolicy) GCPolicy { return unionPolicy{sub} }
  49. type unionPolicy struct {
  50. sub []GCPolicy
  51. }
  52. func (up unionPolicy) String() string {
  53. var ss []string
  54. for _, sp := range up.sub {
  55. ss = append(ss, sp.String())
  56. }
  57. return "(" + strings.Join(ss, " || ") + ")"
  58. }
  59. func (up unionPolicy) proto() *bttdpb.GcRule {
  60. union := &bttdpb.GcRule_Union{}
  61. for _, sp := range up.sub {
  62. union.Rules = append(union.Rules, sp.proto())
  63. }
  64. return &bttdpb.GcRule{
  65. Rule: &bttdpb.GcRule_Union_{Union: union},
  66. }
  67. }
  68. // MaxVersionsPolicy returns a GC policy that applies to all versions of a cell
  69. // except for the most recent n.
  70. func MaxVersionsPolicy(n int) GCPolicy { return maxVersionsPolicy(n) }
  71. type maxVersionsPolicy int
  72. func (mvp maxVersionsPolicy) String() string { return fmt.Sprintf("versions() > %d", int(mvp)) }
  73. func (mvp maxVersionsPolicy) proto() *bttdpb.GcRule {
  74. return &bttdpb.GcRule{Rule: &bttdpb.GcRule_MaxNumVersions{MaxNumVersions: int32(mvp)}}
  75. }
  76. // MaxAgePolicy returns a GC policy that applies to all cells
  77. // older than the given age.
  78. func MaxAgePolicy(d time.Duration) GCPolicy { return maxAgePolicy(d) }
  79. type maxAgePolicy time.Duration
  80. var units = []struct {
  81. d time.Duration
  82. suffix string
  83. }{
  84. {24 * time.Hour, "d"},
  85. {time.Hour, "h"},
  86. {time.Minute, "m"},
  87. }
  88. func (ma maxAgePolicy) String() string {
  89. d := time.Duration(ma)
  90. for _, u := range units {
  91. if d%u.d == 0 {
  92. return fmt.Sprintf("age() > %d%s", d/u.d, u.suffix)
  93. }
  94. }
  95. return fmt.Sprintf("age() > %d", d/time.Microsecond)
  96. }
  97. func (ma maxAgePolicy) proto() *bttdpb.GcRule {
  98. // This doesn't handle overflows, etc.
  99. // Fix this if people care about GC policies over 290 years.
  100. ns := time.Duration(ma).Nanoseconds()
  101. return &bttdpb.GcRule{
  102. Rule: &bttdpb.GcRule_MaxAge{MaxAge: &durpb.Duration{
  103. Seconds: ns / 1e9,
  104. Nanos: int32(ns % 1e9),
  105. }},
  106. }
  107. }
  108. type noGCPolicy struct{}
  109. func (n noGCPolicy) String() string { return "" }
  110. func (n noGCPolicy) proto() *bttdpb.GcRule { return &bttdpb.GcRule{Rule: nil} }
  111. // NoGcPolicy applies to all cells setting maxage and maxversions to nil implies no gc policies
  112. func NoGcPolicy() GCPolicy { return noGCPolicy{} }
  113. // GCRuleToString converts the given GcRule proto to a user-visible string.
  114. func GCRuleToString(rule *bttdpb.GcRule) string {
  115. if rule == nil {
  116. return "<never>"
  117. }
  118. switch r := rule.Rule.(type) {
  119. case *bttdpb.GcRule_MaxNumVersions:
  120. return MaxVersionsPolicy(int(r.MaxNumVersions)).String()
  121. case *bttdpb.GcRule_MaxAge:
  122. return MaxAgePolicy(time.Duration(r.MaxAge.Seconds) * time.Second).String()
  123. case *bttdpb.GcRule_Intersection_:
  124. return joinRules(r.Intersection.Rules, " && ")
  125. case *bttdpb.GcRule_Union_:
  126. return joinRules(r.Union.Rules, " || ")
  127. default:
  128. return ""
  129. }
  130. }
  131. func joinRules(rules []*bttdpb.GcRule, sep string) string {
  132. var chunks []string
  133. for _, r := range rules {
  134. chunks = append(chunks, GCRuleToString(r))
  135. }
  136. return "(" + strings.Join(chunks, sep) + ")"
  137. }