1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#pragma once
#include <Storages/MergeTree/MergeTreePartInfo.h>
#include <base/types.h>
#include <map>
#include <vector>
namespace Poco
{
class Logger;
}
namespace DB
{
using Strings = std::vector<String>;
/** Supports multiple names of active parts of data.
* Repeats part of the MergeTreeData functionality.
* TODO: generalize with MergeTreeData
*/
class ActiveDataPartSet
{
public:
explicit ActiveDataPartSet(MergeTreeDataFormatVersion format_version_) : format_version(format_version_) {}
ActiveDataPartSet(MergeTreeDataFormatVersion format_version_, const Strings & names);
ActiveDataPartSet(const ActiveDataPartSet & other) = default;
ActiveDataPartSet & operator=(const ActiveDataPartSet & other) = default;
ActiveDataPartSet(ActiveDataPartSet && other) noexcept = default;
void swap(ActiveDataPartSet & other) noexcept
{
std::swap(format_version, other.format_version);
std::swap(part_info_to_name, other.part_info_to_name);
}
/// Returns true if the part was actually added. If out_replaced_parts != nullptr, it will contain
/// parts that were replaced from the set by the newly added part.
bool add(const String & name, Strings * out_replaced_parts = nullptr);
bool add(const MergeTreePartInfo & part_info, const String & name, Strings * out_replaced_parts = nullptr);
bool add(const MergeTreePartInfo & part_info, Strings * out_replaced_parts = nullptr);
bool remove(const MergeTreePartInfo & part_info)
{
return part_info_to_name.erase(part_info) > 0;
}
bool remove(const String & part_name)
{
return remove(MergeTreePartInfo::fromPartName(part_name, format_version));
}
/// Remove part and all covered parts from active set
bool removePartAndCoveredParts(const String & part_name)
{
Strings parts_covered_by = getPartsCoveredBy(MergeTreePartInfo::fromPartName(part_name, format_version));
bool result = true;
result &= remove(part_name);
for (const auto & part : parts_covered_by)
result &= remove(part);
return result;
}
/// Remove only covered parts from active set
bool removePartsCoveredBy(const String & part_name)
{
Strings parts_covered_by = getPartsCoveredBy(MergeTreePartInfo::fromPartName(part_name, format_version));
bool result = true;
for (const auto & part : parts_covered_by)
if (part != part_name)
result &= remove(part);
return result;
}
/// If not found, return an empty string.
String getContainingPart(const MergeTreePartInfo & part_info) const;
String getContainingPart(const String & name) const;
Strings getPartsCoveredBy(const MergeTreePartInfo & part_info) const;
/// Returns parts in ascending order of the partition_id and block number.
Strings getParts() const;
std::vector<MergeTreePartInfo> getPartInfos() const;
size_t size() const;
void clear()
{
part_info_to_name.clear();
}
MergeTreeDataFormatVersion getFormatVersion() const { return format_version; }
private:
MergeTreeDataFormatVersion format_version;
std::map<MergeTreePartInfo, String> part_info_to_name;
std::map<MergeTreePartInfo, String>::const_iterator getContainingPartImpl(const MergeTreePartInfo & part_info) const;
};
}
|