1 /*
2 * Copyright 2021 TiKV Project Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18 package org.tikv.common.statistics;
19
20 import org.tikv.common.key.Key;
21
22 /**
23 * Bucket is histogram element.
24 *
25 * <p>Bucket bound is the smallest and greatest values stored in the bucket. The lower and upper
26 * bound are stored in bucket as lowerBound and upperBound.
27 *
28 * <p>A bucket count is the number of items stored in all previous buckets and the current bucket.
29 * Bucket counts are always in increasing order.
30 *
31 * <p>A bucket repeat is the number of repeats of the greatest bucket value, it can be used to find
32 * popular values.
33 *
34 * <p>Note that lowerBound and upperBound keys should be 'comparable objects', and these bounds are
35 * encoded as `binary` type in TiDB. Intuitively, you should also use Keys encoded as binary format
36 * to do comparison in row count estimation.
37 */
38 public class Bucket implements Comparable<Bucket> {
39 public long count;
40 private long repeats;
41 private Key lowerBound;
42 private Key upperBound;
43
44 public Bucket(long count, long repeats, Key lowerBound, Key upperBound) {
45 this.count = count;
46 this.repeats = repeats;
47 this.lowerBound = lowerBound;
48 this.upperBound = upperBound;
49 assert upperBound != null;
50 }
51
52 /** used for binary search only */
53 public Bucket(Key upperBound) {
54 this.upperBound = upperBound;
55 assert upperBound != null;
56 }
57
58 @Override
59 public int compareTo(Bucket b) {
60 return upperBound.compareTo(b.upperBound);
61 }
62
63 public long getCount() {
64 return count;
65 }
66
67 public void setCount(long count) {
68 this.count = count;
69 }
70
71 public long getRepeats() {
72 return repeats;
73 }
74
75 public void setRepeats(long repeats) {
76 this.repeats = repeats;
77 }
78
79 public Key getLowerBound() {
80 return lowerBound;
81 }
82
83 public void setLowerBound(Key lowerBound) {
84 this.lowerBound = lowerBound;
85 }
86
87 public Key getUpperBound() {
88 return upperBound;
89 }
90
91 public void setUpperBound(Key upperBound) {
92 this.upperBound = upperBound;
93 }
94
95 @Override
96 public String toString() {
97 return "{count="
98 + count
99 + ", repeats="
100 + repeats
101 + ", range=["
102 + lowerBound
103 + ", "
104 + upperBound
105 + "]}";
106 }
107 }