1 | #include <Storages/MergeTree/MergeTreeIndexGranularity.h> |
2 | #include <Common/Exception.h> |
3 | #include <IO/WriteHelpers.h> |
4 | |
5 | |
6 | namespace DB |
7 | { |
8 | namespace ErrorCodes |
9 | { |
10 | extern const int LOGICAL_ERROR; |
11 | } |
12 | |
13 | MergeTreeIndexGranularity::MergeTreeIndexGranularity(const std::vector<size_t> & marks_rows_partial_sums_) |
14 | : marks_rows_partial_sums(marks_rows_partial_sums_) |
15 | { |
16 | } |
17 | |
18 | |
19 | MergeTreeIndexGranularity::MergeTreeIndexGranularity(size_t marks_count, size_t fixed_granularity) |
20 | : marks_rows_partial_sums(marks_count, fixed_granularity) |
21 | { |
22 | } |
23 | |
24 | size_t MergeTreeIndexGranularity::getMarkStartingRow(size_t mark_index) const |
25 | { |
26 | if (mark_index == 0) |
27 | return 0; |
28 | return marks_rows_partial_sums[mark_index - 1]; |
29 | } |
30 | |
31 | size_t MergeTreeIndexGranularity::getMarksCount() const |
32 | { |
33 | return marks_rows_partial_sums.size(); |
34 | } |
35 | |
36 | size_t MergeTreeIndexGranularity::getTotalRows() const |
37 | { |
38 | if (marks_rows_partial_sums.empty()) |
39 | return 0; |
40 | return marks_rows_partial_sums.back(); |
41 | } |
42 | |
43 | void MergeTreeIndexGranularity::appendMark(size_t rows_count) |
44 | { |
45 | if (marks_rows_partial_sums.empty()) |
46 | marks_rows_partial_sums.push_back(rows_count); |
47 | else |
48 | marks_rows_partial_sums.push_back(marks_rows_partial_sums.back() + rows_count); |
49 | } |
50 | |
51 | size_t MergeTreeIndexGranularity::getRowsCountInRange(size_t begin, size_t end) const |
52 | { |
53 | size_t subtrahend = 0; |
54 | if (begin != 0) |
55 | subtrahend = marks_rows_partial_sums[begin - 1]; |
56 | return marks_rows_partial_sums[end - 1] - subtrahend; |
57 | } |
58 | |
59 | size_t MergeTreeIndexGranularity::getRowsCountInRange(const MarkRange & range) const |
60 | { |
61 | return getRowsCountInRange(range.begin, range.end); |
62 | } |
63 | |
64 | size_t MergeTreeIndexGranularity::getRowsCountInRanges(const MarkRanges & ranges) const |
65 | { |
66 | size_t total = 0; |
67 | for (const auto & range : ranges) |
68 | total += getRowsCountInRange(range); |
69 | |
70 | return total; |
71 | } |
72 | |
73 | |
74 | size_t MergeTreeIndexGranularity::countMarksForRows(size_t from_mark, size_t number_of_rows, size_t offset_in_rows) const |
75 | { |
76 | size_t rows_before_mark = getMarkStartingRow(from_mark); |
77 | size_t last_row_pos = rows_before_mark + offset_in_rows + number_of_rows; |
78 | auto position = std::upper_bound(marks_rows_partial_sums.begin(), marks_rows_partial_sums.end(), last_row_pos); |
79 | size_t to_mark; |
80 | if (position == marks_rows_partial_sums.end()) |
81 | to_mark = marks_rows_partial_sums.size(); |
82 | else |
83 | to_mark = position - marks_rows_partial_sums.begin(); |
84 | |
85 | return getRowsCountInRange(from_mark, std::max(1UL, to_mark)) - offset_in_rows; |
86 | |
87 | } |
88 | |
89 | void MergeTreeIndexGranularity::resizeWithFixedGranularity(size_t size, size_t fixed_granularity) |
90 | { |
91 | marks_rows_partial_sums.resize(size); |
92 | |
93 | size_t prev = 0; |
94 | for (size_t i = 0; i < size; ++i) |
95 | { |
96 | marks_rows_partial_sums[i] = fixed_granularity + prev; |
97 | prev = marks_rows_partial_sums[i]; |
98 | } |
99 | } |
100 | |
101 | |
102 | } |
103 | |