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.
 
 
 

331 lines
11 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. btpb "google.golang.org/genproto/googleapis/bigtable/v2"
  19. )
  20. // A Filter represents a row filter.
  21. type Filter interface {
  22. String() string
  23. proto() *btpb.RowFilter
  24. }
  25. // ChainFilters returns a filter that applies a sequence of filters.
  26. func ChainFilters(sub ...Filter) Filter { return chainFilter{sub} }
  27. type chainFilter struct {
  28. sub []Filter
  29. }
  30. func (cf chainFilter) String() string {
  31. var ss []string
  32. for _, sf := range cf.sub {
  33. ss = append(ss, sf.String())
  34. }
  35. return "(" + strings.Join(ss, " | ") + ")"
  36. }
  37. func (cf chainFilter) proto() *btpb.RowFilter {
  38. chain := &btpb.RowFilter_Chain{}
  39. for _, sf := range cf.sub {
  40. chain.Filters = append(chain.Filters, sf.proto())
  41. }
  42. return &btpb.RowFilter{
  43. Filter: &btpb.RowFilter_Chain_{Chain: chain},
  44. }
  45. }
  46. // InterleaveFilters returns a filter that applies a set of filters in parallel
  47. // and interleaves the results.
  48. func InterleaveFilters(sub ...Filter) Filter { return interleaveFilter{sub} }
  49. type interleaveFilter struct {
  50. sub []Filter
  51. }
  52. func (ilf interleaveFilter) String() string {
  53. var ss []string
  54. for _, sf := range ilf.sub {
  55. ss = append(ss, sf.String())
  56. }
  57. return "(" + strings.Join(ss, " + ") + ")"
  58. }
  59. func (ilf interleaveFilter) proto() *btpb.RowFilter {
  60. inter := &btpb.RowFilter_Interleave{}
  61. for _, sf := range ilf.sub {
  62. inter.Filters = append(inter.Filters, sf.proto())
  63. }
  64. return &btpb.RowFilter{
  65. Filter: &btpb.RowFilter_Interleave_{Interleave: inter},
  66. }
  67. }
  68. // RowKeyFilter returns a filter that matches cells from rows whose
  69. // key matches the provided RE2 pattern.
  70. // See https://github.com/google/re2/wiki/Syntax for the accepted syntax.
  71. func RowKeyFilter(pattern string) Filter { return rowKeyFilter(pattern) }
  72. type rowKeyFilter string
  73. func (rkf rowKeyFilter) String() string { return fmt.Sprintf("row(%s)", string(rkf)) }
  74. func (rkf rowKeyFilter) proto() *btpb.RowFilter {
  75. return &btpb.RowFilter{Filter: &btpb.RowFilter_RowKeyRegexFilter{RowKeyRegexFilter: []byte(rkf)}}
  76. }
  77. // FamilyFilter returns a filter that matches cells whose family name
  78. // matches the provided RE2 pattern.
  79. // See https://github.com/google/re2/wiki/Syntax for the accepted syntax.
  80. func FamilyFilter(pattern string) Filter { return familyFilter(pattern) }
  81. type familyFilter string
  82. func (ff familyFilter) String() string { return fmt.Sprintf("col(%s:)", string(ff)) }
  83. func (ff familyFilter) proto() *btpb.RowFilter {
  84. return &btpb.RowFilter{Filter: &btpb.RowFilter_FamilyNameRegexFilter{FamilyNameRegexFilter: string(ff)}}
  85. }
  86. // ColumnFilter returns a filter that matches cells whose column name
  87. // matches the provided RE2 pattern.
  88. // See https://github.com/google/re2/wiki/Syntax for the accepted syntax.
  89. func ColumnFilter(pattern string) Filter { return columnFilter(pattern) }
  90. type columnFilter string
  91. func (cf columnFilter) String() string { return fmt.Sprintf("col(.*:%s)", string(cf)) }
  92. func (cf columnFilter) proto() *btpb.RowFilter {
  93. return &btpb.RowFilter{Filter: &btpb.RowFilter_ColumnQualifierRegexFilter{ColumnQualifierRegexFilter: []byte(cf)}}
  94. }
  95. // ValueFilter returns a filter that matches cells whose value
  96. // matches the provided RE2 pattern.
  97. // See https://github.com/google/re2/wiki/Syntax for the accepted syntax.
  98. func ValueFilter(pattern string) Filter { return valueFilter(pattern) }
  99. type valueFilter string
  100. func (vf valueFilter) String() string { return fmt.Sprintf("value_match(%s)", string(vf)) }
  101. func (vf valueFilter) proto() *btpb.RowFilter {
  102. return &btpb.RowFilter{Filter: &btpb.RowFilter_ValueRegexFilter{ValueRegexFilter: []byte(vf)}}
  103. }
  104. // LatestNFilter returns a filter that matches the most recent N cells in each column.
  105. func LatestNFilter(n int) Filter { return latestNFilter(n) }
  106. type latestNFilter int32
  107. func (lnf latestNFilter) String() string { return fmt.Sprintf("col(*,%d)", lnf) }
  108. func (lnf latestNFilter) proto() *btpb.RowFilter {
  109. return &btpb.RowFilter{Filter: &btpb.RowFilter_CellsPerColumnLimitFilter{CellsPerColumnLimitFilter: int32(lnf)}}
  110. }
  111. // StripValueFilter returns a filter that replaces each value with the empty string.
  112. func StripValueFilter() Filter { return stripValueFilter{} }
  113. type stripValueFilter struct{}
  114. func (stripValueFilter) String() string { return "strip_value()" }
  115. func (stripValueFilter) proto() *btpb.RowFilter {
  116. return &btpb.RowFilter{Filter: &btpb.RowFilter_StripValueTransformer{StripValueTransformer: true}}
  117. }
  118. // TimestampRangeFilter returns a filter that matches any cells whose timestamp is within the given time bounds. A zero
  119. // time means no bound.
  120. // The timestamp will be truncated to millisecond granularity.
  121. func TimestampRangeFilter(startTime time.Time, endTime time.Time) Filter {
  122. trf := timestampRangeFilter{}
  123. if !startTime.IsZero() {
  124. trf.startTime = Time(startTime)
  125. }
  126. if !endTime.IsZero() {
  127. trf.endTime = Time(endTime)
  128. }
  129. return trf
  130. }
  131. // TimestampRangeFilterMicros returns a filter that matches any cells whose timestamp is within the given time bounds,
  132. // specified in units of microseconds since 1 January 1970. A zero value for the end time is interpreted as no bound.
  133. // The timestamp will be truncated to millisecond granularity.
  134. func TimestampRangeFilterMicros(startTime Timestamp, endTime Timestamp) Filter {
  135. return timestampRangeFilter{startTime, endTime}
  136. }
  137. type timestampRangeFilter struct {
  138. startTime Timestamp
  139. endTime Timestamp
  140. }
  141. func (trf timestampRangeFilter) String() string {
  142. return fmt.Sprintf("timestamp_range(%v,%v)", trf.startTime, trf.endTime)
  143. }
  144. func (trf timestampRangeFilter) proto() *btpb.RowFilter {
  145. return &btpb.RowFilter{
  146. Filter: &btpb.RowFilter_TimestampRangeFilter{TimestampRangeFilter: &btpb.TimestampRange{
  147. StartTimestampMicros: int64(trf.startTime.TruncateToMilliseconds()),
  148. EndTimestampMicros: int64(trf.endTime.TruncateToMilliseconds()),
  149. },
  150. }}
  151. }
  152. // ColumnRangeFilter returns a filter that matches a contiguous range of columns within a single
  153. // family, as specified by an inclusive start qualifier and exclusive end qualifier.
  154. func ColumnRangeFilter(family, start, end string) Filter {
  155. return columnRangeFilter{family, start, end}
  156. }
  157. type columnRangeFilter struct {
  158. family string
  159. start string
  160. end string
  161. }
  162. func (crf columnRangeFilter) String() string {
  163. return fmt.Sprintf("columnRangeFilter(%s,%s,%s)", crf.family, crf.start, crf.end)
  164. }
  165. func (crf columnRangeFilter) proto() *btpb.RowFilter {
  166. r := &btpb.ColumnRange{FamilyName: crf.family}
  167. if crf.start != "" {
  168. r.StartQualifier = &btpb.ColumnRange_StartQualifierClosed{StartQualifierClosed: []byte(crf.start)}
  169. }
  170. if crf.end != "" {
  171. r.EndQualifier = &btpb.ColumnRange_EndQualifierOpen{EndQualifierOpen: []byte(crf.end)}
  172. }
  173. return &btpb.RowFilter{Filter: &btpb.RowFilter_ColumnRangeFilter{ColumnRangeFilter: r}}
  174. }
  175. // ValueRangeFilter returns a filter that matches cells with values that fall within
  176. // the given range, as specified by an inclusive start value and exclusive end value.
  177. func ValueRangeFilter(start, end []byte) Filter {
  178. return valueRangeFilter{start, end}
  179. }
  180. type valueRangeFilter struct {
  181. start []byte
  182. end []byte
  183. }
  184. func (vrf valueRangeFilter) String() string {
  185. return fmt.Sprintf("valueRangeFilter(%s,%s)", vrf.start, vrf.end)
  186. }
  187. func (vrf valueRangeFilter) proto() *btpb.RowFilter {
  188. r := &btpb.ValueRange{}
  189. if vrf.start != nil {
  190. r.StartValue = &btpb.ValueRange_StartValueClosed{StartValueClosed: vrf.start}
  191. }
  192. if vrf.end != nil {
  193. r.EndValue = &btpb.ValueRange_EndValueOpen{EndValueOpen: vrf.end}
  194. }
  195. return &btpb.RowFilter{Filter: &btpb.RowFilter_ValueRangeFilter{ValueRangeFilter: r}}
  196. }
  197. // ConditionFilter returns a filter that evaluates to one of two possible filters depending
  198. // on whether or not the given predicate filter matches at least one cell.
  199. // If the matched filter is nil then no results will be returned.
  200. // IMPORTANT NOTE: The predicate filter does not execute atomically with the
  201. // true and false filters, which may lead to inconsistent or unexpected
  202. // results. Additionally, condition filters have poor performance, especially
  203. // when filters are set for the false condition.
  204. func ConditionFilter(predicateFilter, trueFilter, falseFilter Filter) Filter {
  205. return conditionFilter{predicateFilter, trueFilter, falseFilter}
  206. }
  207. type conditionFilter struct {
  208. predicateFilter Filter
  209. trueFilter Filter
  210. falseFilter Filter
  211. }
  212. func (cf conditionFilter) String() string {
  213. return fmt.Sprintf("conditionFilter(%s,%s,%s)", cf.predicateFilter, cf.trueFilter, cf.falseFilter)
  214. }
  215. func (cf conditionFilter) proto() *btpb.RowFilter {
  216. var tf *btpb.RowFilter
  217. var ff *btpb.RowFilter
  218. if cf.trueFilter != nil {
  219. tf = cf.trueFilter.proto()
  220. }
  221. if cf.falseFilter != nil {
  222. ff = cf.falseFilter.proto()
  223. }
  224. return &btpb.RowFilter{
  225. Filter: &btpb.RowFilter_Condition_{Condition: &btpb.RowFilter_Condition{
  226. PredicateFilter: cf.predicateFilter.proto(),
  227. TrueFilter: tf,
  228. FalseFilter: ff,
  229. }}}
  230. }
  231. // CellsPerRowOffsetFilter returns a filter that skips the first N cells of each row, matching all subsequent cells.
  232. func CellsPerRowOffsetFilter(n int) Filter {
  233. return cellsPerRowOffsetFilter(n)
  234. }
  235. type cellsPerRowOffsetFilter int32
  236. func (cof cellsPerRowOffsetFilter) String() string {
  237. return fmt.Sprintf("cells_per_row_offset(%d)", cof)
  238. }
  239. func (cof cellsPerRowOffsetFilter) proto() *btpb.RowFilter {
  240. return &btpb.RowFilter{Filter: &btpb.RowFilter_CellsPerRowOffsetFilter{CellsPerRowOffsetFilter: int32(cof)}}
  241. }
  242. // CellsPerRowLimitFilter returns a filter that matches only the first N cells of each row.
  243. func CellsPerRowLimitFilter(n int) Filter {
  244. return cellsPerRowLimitFilter(n)
  245. }
  246. type cellsPerRowLimitFilter int32
  247. func (clf cellsPerRowLimitFilter) String() string {
  248. return fmt.Sprintf("cells_per_row_limit(%d)", clf)
  249. }
  250. func (clf cellsPerRowLimitFilter) proto() *btpb.RowFilter {
  251. return &btpb.RowFilter{Filter: &btpb.RowFilter_CellsPerRowLimitFilter{CellsPerRowLimitFilter: int32(clf)}}
  252. }
  253. // RowSampleFilter returns a filter that matches a row with a probability of p (must be in the interval (0, 1)).
  254. func RowSampleFilter(p float64) Filter {
  255. return rowSampleFilter(p)
  256. }
  257. type rowSampleFilter float64
  258. func (rsf rowSampleFilter) String() string {
  259. return fmt.Sprintf("filter(%f)", rsf)
  260. }
  261. func (rsf rowSampleFilter) proto() *btpb.RowFilter {
  262. return &btpb.RowFilter{Filter: &btpb.RowFilter_RowSampleFilter{RowSampleFilter: float64(rsf)}}
  263. }