ClickHouse/src/Storages/MergeTree/MergeTreeIndexGranularity.h

109 lines
3.3 KiB
C++
Raw Normal View History

2019-03-30 13:49:29 +00:00
#pragma once
#include <vector>
#include <Storages/MergeTree/MarkRange.h>
namespace DB
{
2019-12-19 13:10:57 +00:00
/// Class contains information about index granularity in rows of IMergeTreeDataPart
/// Inside it contains vector of partial sums of rows after mark:
/// |-----|---|----|----|
/// | 5 | 8 | 12 | 16 |
/// If user doesn't specify setting adaptive_index_granularity_bytes for MergeTree* table
/// all values in inner vector would have constant stride (default 8192).
2019-03-30 13:49:29 +00:00
class MergeTreeIndexGranularity
{
private:
std::vector<size_t> marks_rows_partial_sums;
bool initialized = false;
public:
MergeTreeIndexGranularity() = default;
explicit MergeTreeIndexGranularity(const std::vector<size_t> & marks_rows_partial_sums_);
MergeTreeIndexGranularity(size_t marks_count, size_t fixed_granularity);
/// Return count of rows between marks
2019-03-30 13:49:29 +00:00
size_t getRowsCountInRange(const MarkRange & range) const;
/// Return count of rows between marks
2019-03-30 13:49:29 +00:00
size_t getRowsCountInRange(size_t begin, size_t end) const;
/// Return sum of rows between all ranges
2019-03-30 13:49:29 +00:00
size_t getRowsCountInRanges(const MarkRanges & ranges) const;
/// Return amount of marks that contains amount of `number_of_rows` starting from
/// `from_mark` and possible some offset_in_rows from `from_mark`
/// 1 2 <- answer
/// |-----|---------------------------|----|----|
/// ^------------------------^-----------^
//// from_mark offset_in_rows number_of_rows
2019-03-30 13:49:29 +00:00
size_t countMarksForRows(size_t from_mark, size_t number_of_rows, size_t offset_in_rows=0) const;
/// Total marks
2019-03-30 13:49:29 +00:00
size_t getMarksCount() const;
/// Total rows
2019-03-30 13:49:29 +00:00
size_t getTotalRows() const;
/// Total number marks without final mark if it exists
size_t getMarksCountWithoutFinal() const { return getMarksCount() - hasFinalMark(); }
/// Rows after mark to next mark
2019-03-30 13:49:29 +00:00
inline size_t getMarkRows(size_t mark_index) const
{
if (mark_index == 0)
return marks_rows_partial_sums[0];
else
return marks_rows_partial_sums[mark_index] - marks_rows_partial_sums[mark_index - 1];
}
/// Return amount of rows before mark
2019-03-30 13:49:29 +00:00
size_t getMarkStartingRow(size_t mark_index) const;
/// Amount of rows after last mark
2019-03-30 13:49:29 +00:00
size_t getLastMarkRows() const
{
size_t last = marks_rows_partial_sums.size() - 1;
return getMarkRows(last);
}
2019-06-19 12:30:56 +00:00
size_t getLastNonFinalMarkRows() const
{
size_t last_mark_rows = getLastMarkRows();
if (last_mark_rows != 0)
return last_mark_rows;
return getMarkRows(marks_rows_partial_sums.size() - 2);
}
bool hasFinalMark() const
{
return getLastMarkRows() == 0;
}
2019-03-30 13:49:29 +00:00
bool empty() const
{
return marks_rows_partial_sums.empty();
}
2019-03-30 13:49:29 +00:00
bool isInitialized() const
{
return initialized;
}
void setInitialized()
{
initialized = true;
}
/// Add new mark with rows_count
2019-03-30 13:49:29 +00:00
void appendMark(size_t rows_count);
2020-02-03 12:08:40 +00:00
/// Extends last mark by rows_count.
2019-11-27 11:35:27 +00:00
void addRowsToLastMark(size_t rows_count);
2020-02-03 12:08:40 +00:00
/// Drops last mark if any exists.
2019-11-26 09:48:22 +00:00
void popMark();
/// Add `size` of marks with `fixed_granularity` rows
2019-03-30 13:49:29 +00:00
void resizeWithFixedGranularity(size_t size, size_t fixed_granularity);
};
}