| 1 | #pragma once |
| 2 | |
| 3 | #include <Storages/MergeTree/MergeTreePartInfo.h> |
| 4 | #include <Core/Types.h> |
| 5 | #include <map> |
| 6 | |
| 7 | |
| 8 | namespace DB |
| 9 | { |
| 10 | |
| 11 | /** Supports multiple names of active parts of data. |
| 12 | * Repeats part of the MergeTreeData functionality. |
| 13 | * TODO: generalize with MergeTreeData |
| 14 | */ |
| 15 | class ActiveDataPartSet |
| 16 | { |
| 17 | public: |
| 18 | ActiveDataPartSet(MergeTreeDataFormatVersion format_version_) : format_version(format_version_) {} |
| 19 | ActiveDataPartSet(MergeTreeDataFormatVersion format_version_, const Strings & names); |
| 20 | |
| 21 | ActiveDataPartSet(const ActiveDataPartSet & other) |
| 22 | : format_version(other.format_version) |
| 23 | , part_info_to_name(other.part_info_to_name) |
| 24 | {} |
| 25 | |
| 26 | ActiveDataPartSet(ActiveDataPartSet && other) noexcept { swap(other); } |
| 27 | |
| 28 | void swap(ActiveDataPartSet & other) noexcept |
| 29 | { |
| 30 | std::swap(format_version, other.format_version); |
| 31 | std::swap(part_info_to_name, other.part_info_to_name); |
| 32 | } |
| 33 | |
| 34 | ActiveDataPartSet & operator=(const ActiveDataPartSet & other) |
| 35 | { |
| 36 | if (&other != this) |
| 37 | { |
| 38 | ActiveDataPartSet tmp(other); |
| 39 | swap(tmp); |
| 40 | } |
| 41 | return *this; |
| 42 | } |
| 43 | |
| 44 | /// Returns true if the part was actually added. If out_replaced_parts != nullptr, it will contain |
| 45 | /// parts that were replaced from the set by the newly added part. |
| 46 | bool add(const String & name, Strings * out_replaced_parts = nullptr); |
| 47 | |
| 48 | bool remove(const MergeTreePartInfo & part_info) |
| 49 | { |
| 50 | return part_info_to_name.erase(part_info) > 0; |
| 51 | } |
| 52 | |
| 53 | bool remove(const String & part_name) |
| 54 | { |
| 55 | return remove(MergeTreePartInfo::fromPartName(part_name, format_version)); |
| 56 | } |
| 57 | |
| 58 | /// If not found, return an empty string. |
| 59 | String getContainingPart(const MergeTreePartInfo & part_info) const; |
| 60 | String getContainingPart(const String & name) const; |
| 61 | |
| 62 | Strings getPartsCoveredBy(const MergeTreePartInfo & part_info) const; |
| 63 | |
| 64 | /// Returns parts in ascending order of the partition_id and block number. |
| 65 | Strings getParts() const; |
| 66 | |
| 67 | size_t size() const; |
| 68 | |
| 69 | MergeTreeDataFormatVersion getFormatVersion() const { return format_version; } |
| 70 | |
| 71 | private: |
| 72 | MergeTreeDataFormatVersion format_version; |
| 73 | std::map<MergeTreePartInfo, String> part_info_to_name; |
| 74 | |
| 75 | std::map<MergeTreePartInfo, String>::const_iterator getContainingPartImpl(const MergeTreePartInfo & part_info) const; |
| 76 | }; |
| 77 | |
| 78 | } |
| 79 | |