forked from Hacker0912/quickstep-datalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCatalogDatabase.cpp
154 lines (135 loc) · 4.82 KB
/
CatalogDatabase.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* 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/CatalogDatabase.hpp"
#include <cstddef>
#include <cstring>
#include <string>
#include <unordered_map>
#include <utility>
#include "catalog/Catalog.pb.h"
#include "catalog/CatalogErrors.hpp"
#include "catalog/CatalogRelation.hpp"
#include "threading/Mutex.hpp"
#include "threading/SpinSharedMutex.hpp"
#include "utility/PtrVector.hpp"
#include "utility/StringUtil.hpp"
#include "glog/logging.h"
using std::size_t;
using std::strcmp;
using std::string;
namespace quickstep {
bool CatalogDatabase::ProtoIsValid(const serialization::CatalogDatabase &proto) {
// Check that proto is fully initialized.
if (!proto.IsInitialized()) {
return false;
}
for (int i = 0; i < proto.relations_size(); ++i) {
if (!CatalogRelation::ProtoIsValid(proto.relations(i))) {
return false;
}
}
return true;
}
CatalogDatabase::CatalogDatabase(const serialization::CatalogDatabase &proto)
: parent_(nullptr),
name_(proto.name()),
status_(Status::kConsistent) {
DCHECK(ProtoIsValid(proto))
<< "Attempted to create CatalogDatabase from an invalid proto description:\n"
<< proto.DebugString();
for (int index_relations = 0, index_null_relations = 0;
index_relations < proto.null_relations_size() + proto.relations_size();
++index_relations) {
if (index_null_relations < proto.null_relations_size() &&
index_relations == proto.null_relations(index_null_relations)) {
rel_vec_.push_back(NULL);
++index_null_relations;
} else {
addRelation(new CatalogRelation(proto.relations(index_relations - index_null_relations)));
}
}
}
const CatalogRelation* CatalogDatabase::getRelationByName(const string &rel_name) const {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
std::unordered_map<string, CatalogRelation*>::const_iterator it = rel_map_.find(ToLower(rel_name));
if (it == rel_map_.end()) {
return nullptr;
} else {
return it->second;
}
}
CatalogRelation* CatalogDatabase::getRelationByNameMutable(const string &rel_name) {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
std::unordered_map<string, CatalogRelation*>::iterator it = rel_map_.find(ToLower(rel_name));
if (it == rel_map_.end()) {
return nullptr;
} else {
return it->second;
}
}
relation_id CatalogDatabase::addRelation(CatalogRelation *new_rel) {
const string lower_rel_name = ToLower(new_rel->getName());
{
SpinSharedMutexExclusiveLock<false> lock(relations_mutex_);
if (hasRelationWithNameUnsafe(lower_rel_name)) {
throw RelationNameCollision(name_, new_rel->getName());
} else if (rel_vec_.size() > static_cast<size_t>(kCatalogMaxID)) {
throw CatalogIDOverflow("relation");
} else {
rel_map_[lower_rel_name] = new_rel;
rel_vec_.push_back(new_rel);
new_rel->setParent(this);
new_rel->setID(static_cast<relation_id>(rel_vec_.size() - 1));
return (new_rel->getID());
}
}
}
void CatalogDatabase::dropRelationByName(const std::string &rel_name) {
SpinSharedMutexExclusiveLock<false> lock(relations_mutex_);
std::unordered_map<string, CatalogRelation*>::iterator it = rel_map_.find(ToLower(rel_name));
if (it == rel_map_.end()) {
throw RelationNameNotFound(name_, rel_name);
} else {
rel_vec_.deleteElement(it->second->getID());
rel_map_.erase(it);
}
}
void CatalogDatabase::dropRelationById(const relation_id id) {
SpinSharedMutexExclusiveLock<false> lock(relations_mutex_);
if (hasRelationWithIdUnsafe(id)) {
rel_map_.erase(ToLower(rel_vec_[id].getName()));
rel_vec_.deleteElement(id);
} else {
throw RelationIdNotFound(name_, id);
}
}
serialization::CatalogDatabase CatalogDatabase::getProto() const {
serialization::CatalogDatabase proto;
proto.set_name(name_);
int i = 0;
for (PtrVector<CatalogRelation, true>::const_iterator it = rel_vec_.begin(); it != rel_vec_.end(); ++it, ++i) {
if (it.isNull()) {
proto.add_null_relations(i);
} else {
proto.add_relations()->MergeFrom(it->getProto());
}
}
return proto;
}
} // namespace quickstep