mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-26 17:41:59 +00:00
Commit missed files
This commit is contained in:
parent
cdc18aa801
commit
d6293d432c
120
dbms/src/Storages/MergeTree/MergeTreeIndexGranularity.cpp
Normal file
120
dbms/src/Storages/MergeTree/MergeTreeIndexGranularity.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
#include <Storages/MergeTree/MergeTreeIndexGranularity.h>
|
||||
#include <Common/Exception.h>
|
||||
#include <IO/WriteHelpers.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
namespace ErrorCodes
|
||||
{
|
||||
extern const int LOGICAL_ERROR;
|
||||
}
|
||||
|
||||
MergeTreeIndexGranularity::MergeTreeIndexGranularity(const std::vector<size_t> & marks_rows_partial_sums_)
|
||||
: marks_rows_partial_sums(marks_rows_partial_sums_)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
MergeTreeIndexGranularity::MergeTreeIndexGranularity(size_t marks_count, size_t fixed_granularity)
|
||||
: marks_rows_partial_sums(marks_count, fixed_granularity)
|
||||
{
|
||||
}
|
||||
|
||||
size_t MergeTreeIndexGranularity::getAvgGranularity() const
|
||||
{
|
||||
if (marks_rows_partial_sums.empty())
|
||||
throw Exception("Trying to compute average index granularity with zero marks", ErrorCodes::LOGICAL_ERROR);
|
||||
|
||||
return marks_rows_partial_sums.back() / marks_rows_partial_sums.size();
|
||||
}
|
||||
|
||||
|
||||
size_t MergeTreeIndexGranularity::getMarkStartingRow(size_t mark_index) const
|
||||
{
|
||||
if (mark_index == 0)
|
||||
return 0;
|
||||
return marks_rows_partial_sums[mark_index - 1];
|
||||
}
|
||||
|
||||
size_t MergeTreeIndexGranularity::getMarksCount() const
|
||||
{
|
||||
return marks_rows_partial_sums.size();
|
||||
}
|
||||
|
||||
size_t MergeTreeIndexGranularity::getTotalRows() const
|
||||
{
|
||||
if (marks_rows_partial_sums.empty())
|
||||
return 0;
|
||||
return marks_rows_partial_sums.back();
|
||||
}
|
||||
|
||||
void MergeTreeIndexGranularity::appendMark(size_t rows_count)
|
||||
{
|
||||
if (marks_rows_partial_sums.empty())
|
||||
marks_rows_partial_sums.push_back(rows_count);
|
||||
else
|
||||
marks_rows_partial_sums.push_back(marks_rows_partial_sums.back() + rows_count);
|
||||
}
|
||||
|
||||
|
||||
size_t MergeTreeIndexGranularity::getMarkPositionInRows(const size_t mark_index) const
|
||||
{
|
||||
if (mark_index == 0)
|
||||
return 0;
|
||||
return marks_rows_partial_sums[mark_index - 1];
|
||||
}
|
||||
|
||||
|
||||
size_t MergeTreeIndexGranularity::getRowsCountInRange(size_t begin, size_t end) const
|
||||
{
|
||||
size_t subtrahend = 0;
|
||||
if (begin != 0)
|
||||
subtrahend = marks_rows_partial_sums[begin - 1];
|
||||
return marks_rows_partial_sums[end - 1] - subtrahend;
|
||||
}
|
||||
|
||||
size_t MergeTreeIndexGranularity::getRowsCountInRange(const MarkRange & range) const
|
||||
{
|
||||
return getRowsCountInRange(range.begin, range.end);
|
||||
}
|
||||
|
||||
size_t MergeTreeIndexGranularity::getRowsCountInRanges(const MarkRanges & ranges) const
|
||||
{
|
||||
size_t total = 0;
|
||||
for (const auto & range : ranges)
|
||||
total += getRowsCountInRange(range);
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
size_t MergeTreeIndexGranularity::countMarksForRows(size_t from_mark, size_t number_of_rows, size_t offset_in_rows) const
|
||||
{
|
||||
size_t rows_before_mark = getMarkStartingRow(from_mark);
|
||||
size_t last_row_pos = rows_before_mark + offset_in_rows + number_of_rows;
|
||||
auto position = std::upper_bound(marks_rows_partial_sums.begin(), marks_rows_partial_sums.end(), last_row_pos);
|
||||
size_t to_mark;
|
||||
if (position == marks_rows_partial_sums.end())
|
||||
to_mark = marks_rows_partial_sums.size();
|
||||
else
|
||||
to_mark = position - marks_rows_partial_sums.begin();
|
||||
|
||||
return getRowsCountInRange(from_mark, std::max(1UL, to_mark)) - offset_in_rows;
|
||||
|
||||
}
|
||||
|
||||
void MergeTreeIndexGranularity::resizeWithFixedGranularity(size_t size, size_t fixed_granularity)
|
||||
{
|
||||
marks_rows_partial_sums.resize(size);
|
||||
|
||||
size_t prev = 0;
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
{
|
||||
marks_rows_partial_sums[i] = fixed_granularity + prev;
|
||||
prev = marks_rows_partial_sums[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
64
dbms/src/Storages/MergeTree/MergeTreeIndexGranularity.h
Normal file
64
dbms/src/Storages/MergeTree/MergeTreeIndexGranularity.h
Normal file
@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <Storages/MergeTree/MarkRange.h>
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
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);
|
||||
|
||||
|
||||
size_t getRowsCountInRange(const MarkRange & range) const;
|
||||
size_t getRowsCountInRange(size_t begin, size_t end) const;
|
||||
size_t getRowsCountInRanges(const MarkRanges & ranges) const;
|
||||
size_t getMarkPositionInRows(const size_t mark_index) const;
|
||||
|
||||
size_t countMarksForRows(size_t from_mark, size_t number_of_rows, size_t offset_in_rows=0) const;
|
||||
|
||||
size_t getAvgGranularity() const;
|
||||
size_t getMarksCount() const;
|
||||
size_t getTotalRows() const;
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
size_t getMarkStartingRow(size_t mark_index) const;
|
||||
|
||||
size_t getLastMarkRows() const
|
||||
{
|
||||
size_t last = marks_rows_partial_sums.size() - 1;
|
||||
return getMarkRows(last);
|
||||
}
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
return marks_rows_partial_sums.empty();
|
||||
}
|
||||
bool isInitialized() const
|
||||
{
|
||||
return initialized;
|
||||
}
|
||||
|
||||
void setInitialized()
|
||||
{
|
||||
initialized = true;
|
||||
}
|
||||
void appendMark(size_t rows_count);
|
||||
void resizeWithFixedGranularity(size_t size, size_t fixed_granularity);
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user