-
Notifications
You must be signed in to change notification settings - Fork 10
/
Catalog.hpp
299 lines (256 loc) · 7.75 KB
/
Catalog.hpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* 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.
**/
#ifndef QUICKSTEP_CATALOG_CATALOG_HPP_
#define QUICKSTEP_CATALOG_CATALOG_HPP_
#include <exception>
#include <string>
#include <unordered_map>
#include "catalog/Catalog.pb.h"
#include "catalog/CatalogDatabase.hpp"
#include "catalog/CatalogTypedefs.hpp"
#include "utility/Macros.hpp"
#include "utility/PtrVector.hpp"
namespace quickstep {
/** \addtogroup Catalog
* @{
*/
/**
* @brief Exception thrown for a database name collision.
**/
class DatabaseNameCollision : public std::exception {
public:
/**
* @brief Constructor.
*
* @param db_name The name of the database for which there was a collision.
**/
explicit DatabaseNameCollision(const std::string &db_name)
: message_("DatabaseNameCollision: catalog already has a database ") {
message_.append(db_name);
}
~DatabaseNameCollision() throw() {
}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief Exception thrown when a database with the specified name can't be
* found.
**/
class DatabaseNameNotFound : public std::exception {
public:
/**
* @brief Constructor.
*
* @param dbName The database name which could not be found.
**/
explicit DatabaseNameNotFound(const std::string &db_name)
: message_("DatabaseNameNotFound: catalog has no database named ") {
message_.append(db_name);
}
~DatabaseNameNotFound() throw() {
}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief Exception thrown when a database with the specified ID can't be
* found.
**/
class DatabaseIdNotFound : public std::exception {
public:
/**
* @brief Constructor.
*
* @param id The id which could not be found.
**/
explicit DatabaseIdNotFound(const database_id id)
: message_("DatabaseIdNotFound: catalog has no database with ID ") {
message_.append(std::to_string(id));
}
~DatabaseIdNotFound() throw() {
}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief The entire database catalog.
**/
class Catalog {
public:
typedef std::unordered_map<std::string, CatalogDatabase*>::size_type size_type;
typedef PtrVector<CatalogDatabase, true>::const_skip_iterator const_iterator;
/**
* @brief Construct an empty catalog.
**/
Catalog() {
}
/**
* @brief Reconstruct a catalog from its serialized Protocol Buffer form.
*
* @param proto The Protocol Buffer serialization of a catalog,
* previously produced by getProto().
**/
explicit Catalog(const serialization::Catalog &proto);
/**
* @brief Destructor which recursively destroys children.
**/
~Catalog() {
}
/**
* @brief Check whether a database with the given name exists.
*
* @param db_name The name to check for.
* @return Whether the database exists.
**/
bool hasDatabaseWithName(const std::string &db_name) const {
return (db_map_.find(db_name) != db_map_.end());
}
/**
* @brief Check whether a database with the given id exists.
*
* @param id The id to check for.
* @return Whether the database exists.
**/
bool hasDatabaseWithId(const database_id id) const {
return (idInRange(id) && !db_vec_.elementIsNull(id));
}
/**
* @brief Get a database by name.
*
* @param db_name The name to search for.
* @exception DatabaseNameNotFound No database with the given name exists.
* @return The database with the given name.
**/
const CatalogDatabase& getDatabaseByName(const std::string &db_name) const;
/**
* @brief Get a mutable pointer to a database by name.
*
* @param db_name The name to search for.
* @exception DatabaseNameNotFound No database with the given name exists.
* @return The database with the given name.
**/
CatalogDatabase* getDatabaseByNameMutable(const std::string &db_name);
/**
* @brief Get a database by ID.
*
* @param id The id to search for.
* @exception DatabaseIdNotFound No database with the given ID exists.
* @return The database with the given ID.
**/
const CatalogDatabase& getDatabaseById(const database_id id) const {
if (hasDatabaseWithId(id)) {
return db_vec_[id];
} else {
throw DatabaseIdNotFound(id);
}
}
/**
* @brief Get a mutable pointer to a database by ID.
*
* @param id The id to search for.
* @exception DatabaseIdNotFound No database with the given ID exists.
* @return The database with the given ID.
**/
CatalogDatabase* getDatabaseByIdMutable(const database_id id) {
if (hasDatabaseWithId(id)) {
return &(db_vec_[id]);
} else {
throw DatabaseIdNotFound(id);
}
}
/**
* @brief Add a new database to the catalog. If the database already has an
* ID and/or parent, it will be overwritten.
*
* @param new_db The database to be added.
* @exception DatabaseNameCollision A database with the same name as new_db
* is already present in the catalog.
* @return The id assigned to the database.
**/
database_id addDatabase(CatalogDatabase *new_db);
/**
* @brief Serialize the catalog as Protocol Buffer.
*
* @return The Protocol Buffer representation of the catalog.
**/
serialization::Catalog getProto() const;
/**
* @brief Get the number of child databases.
*
* @return The number of child databases.
**/
size_type size() const {
return db_map_.size();
}
/**
* @brief Get an iterator at the beginning of the child databases.
*
* @return An iterator on the first child database.
**/
const_iterator begin() const {
return db_vec_.begin_skip();
}
/**
* @brief Get an iterator at one-past-the-end of the child databases.
*
* @return An iterator one-past-the-end of the child databases.
**/
const_iterator end() const {
return db_vec_.end_skip();
}
private:
/**
* @brief Check whether a database_id is within the range of IDs contained
* in this Catalog.
*
* @param id The id to check.
* @return true if id is in range, false otherwise.
**/
bool idInRange(const database_id id) const {
return ((id >= 0)
&& (static_cast<PtrVector<CatalogDatabase>::size_type>(id) < db_vec_.size()));
}
/**
* @brief Check whether a serialization::Catalog is fully-formed and
* all parts are valid.
*
* @param proto A serialized Protocol Buffer representation of a Catalog,
* originally generated by getProto().
* @return Whether proto is fully-formed and valid.
**/
static bool ProtoIsValid(const serialization::Catalog &proto);
// A vector of databases, and NULL if it has dropped from the catalog.
PtrVector<CatalogDatabase, true> db_vec_;
// A map from a database name to a pointer to the database.
std::unordered_map<std::string, CatalogDatabase*> db_map_;
DISALLOW_COPY_AND_ASSIGN(Catalog);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_CATALOG_CATALOG_HPP_