-
Notifications
You must be signed in to change notification settings - Fork 10
/
PartitionScheme.cpp
128 lines (104 loc) · 3.95 KB
/
PartitionScheme.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#include "catalog/PartitionScheme.hpp"
#include <cstddef>
#include <limits>
#include <sstream>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "catalog/Catalog.pb.h"
#include "catalog/PartitionSchemeHeader.hpp"
#include "storage/StorageBlockInfo.hpp"
#include "glog/logging.h"
using std::move;
using std::unordered_set;
using std::vector;
namespace quickstep {
bool PartitionScheme::ProtoIsValid(
const serialization::PartitionScheme &proto) {
// Check that proto is fully initialized.
if (!proto.IsInitialized()) {
return false;
}
if (!PartitionSchemeHeader::ProtoIsValid(proto.header())) {
return false;
}
if (static_cast<std::size_t>(proto.partitions_size()) != proto.header().num_partitions()) {
return false;
}
return true;
}
PartitionScheme* PartitionScheme::ReconstructFromProto(const serialization::PartitionScheme &proto) {
DCHECK(ProtoIsValid(proto))
<< "Attempted to create PartitionScheme from an invalid proto description:\n"
<< proto.DebugString();
vector<unordered_set<block_id>> blocks_in_partition;
for (int i = 0; i < proto.partitions_size(); ++i) {
unordered_set<block_id> blocks;
const serialization::Partition &proto_blocks = proto.partitions(i);
for (int j = 0; j < proto_blocks.blocks_size(); ++j) {
blocks.insert(proto_blocks.blocks(j));
}
blocks_in_partition.push_back(move(blocks));
}
return new PartitionScheme(
PartitionSchemeHeader::ReconstructFromProto(proto.header()),
move(blocks_in_partition));
}
serialization::PartitionScheme PartitionScheme::getProto() const {
serialization::PartitionScheme proto;
proto.mutable_header()->MergeFrom(header_->getProto());
// Add blocks to the corresponding partitions.
for (std::size_t i = 0; i < blocks_in_partition_.size(); ++i) {
serialization::Partition *proto_blocks = proto.add_partitions();
SpinSharedMutexSharedLock<false> lock(blocks_in_partition_mutexes_[i]);
const std::unordered_set<block_id> &partition = blocks_in_partition_[i];
for (const block_id block : partition) {
proto_blocks->add_blocks(block);
}
}
return proto;
}
partition_id PartitionScheme::getPartitionForBlock(const block_id block) const {
// Check if the block is present in the available partitions.
// If so, return the partition id for the block.
for (partition_id part_id = 0; part_id < header_->getNumPartitions(); ++part_id) {
SpinSharedMutexSharedLock<false> lock(
blocks_in_partition_mutexes_[part_id]);
if (blocks_in_partition_[part_id].find(block) !=
blocks_in_partition_[part_id].end()) {
return part_id;
}
}
// Block was not found in any partitions.
return std::numeric_limits<std::size_t>::max();
}
std::string PartitionScheme::toString(const CatalogRelationSchema &relation_schema) const {
std::ostringstream oss;
oss << " |";
for (std::size_t i = 0; i < blocks_in_partition_.size(); ++i) {
SpinSharedMutexSharedLock<false> lock(blocks_in_partition_mutexes_[i]);
oss << ' ' << blocks_in_partition_[i].size() << " |";
}
oss << '\n';
return header_->toString(relation_schema) + oss.str();
}
} // namespace quickstep