-
Notifications
You must be signed in to change notification settings - Fork 10
/
CatalogDatabase.hpp
478 lines (416 loc) · 13.2 KB
/
CatalogDatabase.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* 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_DATABASE_HPP_
#define QUICKSTEP_CATALOG_CATALOG_DATABASE_HPP_
#include <exception>
#include <string>
#include <unordered_map>
#include "catalog/Catalog.pb.h"
#include "catalog/CatalogDatabaseLite.hpp"
#include "catalog/CatalogRelation.hpp"
#include "catalog/CatalogTypedefs.hpp"
#include "storage/StorageConstants.hpp"
#include "threading/Mutex.hpp"
#include "threading/SharedMutex.hpp"
#include "threading/SpinSharedMutex.hpp"
#include "utility/Macros.hpp"
#include "utility/PtrVector.hpp"
#include "utility/StringUtil.hpp"
#include "glog/logging.h"
namespace quickstep {
class Catalog;
class CatalogRelationSchema;
/** \addtogroup Catalog
* @{
*/
/**
* @brief Exception thrown for a relation name collision.
**/
class RelationNameCollision : public std::exception {
public:
/**
* @brief Constructor.
*
* @param db_name Name of the database in which the collision occurred.
* @param rel_name Name of the relation for which there was a collision.
**/
RelationNameCollision(const std::string &db_name, const std::string &rel_name)
: message_("RelationNameCollision: database ") {
message_.append(db_name);
message_.append(" already has a relation ");
message_.append(rel_name);
}
~RelationNameCollision() throw() {
}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief Exception thrown when a relation with the specified name can't be
* found.
**/
class RelationNameNotFound : public std::exception {
public:
/**
* @brief Constructor.
*
* @param db_name Name of the database in which the exception occurred.
* @param rel_name The relation name which could not be found.
**/
RelationNameNotFound(const std::string &db_name, const std::string &rel_name)
: message_("RelationNameNotFound: database ") {
message_.append(db_name);
message_.append(" has no relation named ");
message_.append(rel_name);
}
~RelationNameNotFound() throw() {
}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief Exception thrown when a relation with the specified ID can't be found.
**/
class RelationIdNotFound : public std::exception {
public:
/**
* @brief Constructor
*
* @param db_name Name of the database in which the exception occurred.
* @param id The relation ID which could not be found.
**/
RelationIdNotFound(const std::string &db_name, const relation_id id)
: message_("RelationIdNotFound: database ") {
message_.append(db_name);
message_.append(" has no relation with ID ");
message_.append(std::to_string(id));
}
~RelationIdNotFound() throw() {
}
virtual const char* what() const throw() {
return message_.c_str();
}
private:
std::string message_;
};
/**
* @brief A single database in the catalog.
**/
class CatalogDatabase final : public CatalogDatabaseLite {
public:
typedef std::unordered_map<std::string, CatalogRelation*>::size_type size_type;
typedef PtrVector<CatalogRelation, true>::const_skip_iterator const_iterator;
enum class Status {
kConsistent = 0,
kPendingBlockDeletions
};
/**
* @brief Create a new database.
*
* @param parent The catalog this database belongs to.
* @param name This database's name.
* @param id This database's ID (defaults to -1, which means invalid/unset).
**/
CatalogDatabase(Catalog *parent, const std::string &name, const database_id id = -1)
: CatalogDatabaseLite(id),
parent_(parent),
name_(name),
status_(Status::kConsistent) {
}
/**
* @brief Reconstruct a database from its serialized Protocol Buffer form.
*
* @param proto The Protocol Buffer serialization of a database, previously
* produced by getProto().
**/
explicit CatalogDatabase(const serialization::CatalogDatabase &proto);
/**
* @brief Check whether a serialization::CatalogDatabase is fully-formed and
* all parts are valid.
*
* @param proto A serialized Protocol Buffer representation of a CatalogDatabase,
* originally generated by getProto().
* @return Whether proto is fully-formed and valid.
**/
static bool ProtoIsValid(const serialization::CatalogDatabase &proto);
/**
* @brief Destructor which recursively destroys children.
**/
~CatalogDatabase() override {
}
bool hasRelationWithId(const relation_id id) const override {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
return hasRelationWithIdUnsafe(id);
}
const CatalogRelationSchema& getRelationSchemaById(const relation_id id) const override {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
DCHECK(hasRelationWithIdUnsafe(id));
return rel_vec_[id];
}
/**
* @exception RelationIdNotFound No relation with the given ID exists.
**/
void dropRelationById(const relation_id id) override;
/**
* @brief Get the parent catalog.
*
* @return Parent catalog.
**/
const Catalog& getParent() const {
return *DCHECK_NOTNULL(parent_);
}
/**
* @brief Get a mutable pointer to the parent catalog.
*
* @return Parent catalog.
**/
Catalog* getParentMutable() {
return parent_;
}
/**
* @brief Get this database's name.
*
* @return This database's name.
**/
const std::string& getName() const {
return name_;
}
/**
* @brief Get this database's status.
*
* @return This database's status.
**/
Status status() const {
SpinSharedMutexSharedLock<false> lock(status_mutex_);
return status_;
}
/**
* @brief Whether this database is consistent.
*
* @return True if it is consistent. Otherwise, false.
**/
bool isStatusConsistent() const {
SpinSharedMutexSharedLock<false> lock(status_mutex_);
return status_ == Status::kConsistent;
}
/**
* @brief Set this database's status.
*
* @param status The status to set.
**/
void setStatus(const Status status) {
SpinSharedMutexExclusiveLock<false> lock(status_mutex_);
status_ = status;
}
/**
* @brief Check whether a relation with the given name exists. The search is case-insensitive.
*
* @param rel_name The name to check for.
* @return Whether the relation exists.
**/
bool hasRelationWithName(const std::string &rel_name) const {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
return hasRelationWithNameUnsafe(rel_name);
}
/**
* @brief Get a relation by name. The search is case-insensitive.
*
* @param rel_name The name to search for.
* @return The relation with the given name. NULL if the relation is not found.
**/
const CatalogRelation* getRelationByName(const std::string &rel_name) const;
/**
* @brief Get a mutable pointer to a relation by name. The search is case-insensitive.
*
* @param rel_name The name to search for.
* @return The relation with the given name. NULL if the relation is not found.
**/
CatalogRelation* getRelationByNameMutable(const std::string &rel_name);
/**
* @brief Get a relation by ID.
*
* @param id The id to search for.
* @return The relation with the given ID.
**/
const CatalogRelation* getRelationById(const relation_id id) const {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
if (hasRelationWithIdUnsafe(id)) {
return &rel_vec_[id];
} else {
return nullptr;
}
}
/**
* @brief Get a mutable pointer to a relation by ID.
*
* @param id The id to search for.
* @return The relation with the given ID.
**/
CatalogRelation* getRelationByIdMutable(const relation_id id) {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
if (hasRelationWithIdUnsafe(id)) {
return &(rel_vec_[id]);
} else {
return nullptr;
}
}
/**
* @brief Add a new relation to the database. If the relation already has an
* ID and/or parent, it will be overwritten.
*
* @param new_rel The relation to be added.
* @exception RelationNameCollision A relation with the same name as new_rel
* is already present in the database.
* @return The id assigned to the relation.
**/
relation_id addRelation(CatalogRelation *new_rel);
/**
* @brief Drop (delete) a relation by name.
*
* @param rel_name The name of the relation to drop.
* @exception RelationNameNotFound No relation with the given name exists.
**/
void dropRelationByName(const std::string &rel_name);
/**
* @brief Serialize the database as Protocol Buffer.
*
* @return The Protocol Buffer representation of the database.
**/
serialization::CatalogDatabase getProto() const;
/**
* @brief Check whether this CatalogDatabase is empty.
*
* @return true if empty, false otherwise.
**/
bool empty() const {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
return rel_map_.empty();
}
/**
* @brief Get the number of child relations.
*
* @return The number of child relations.
**/
size_type size() const {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
return rel_map_.size();
}
/**
* @brief Get an iterator at the beginning of the child relations.
*
* @return An iterator on the first child relation.
**/
const_iterator begin() const {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
return rel_vec_.begin_skip();
}
/**
* @brief Get an iterator at one-past-the-end of the child relations.
*
* @return An iterator one-past-the-end of the child relations.
**/
const_iterator end() const {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
return rel_vec_.end_skip();
}
private:
/**
* @brief Set the parent Catalog of this database. Used by Catalog (a friend
* of this class) when adding a new database.
*
* @param parent The new parent for this CatalogDatabase.
**/
void setParent(Catalog *parent) {
parent_ = parent;
}
/**
* @brief Set the ID of this database. Used by Catalog (a friend of this
* class) when adding a new database.
*
* @param id The new ID for this CatalogDatabase.
**/
void setID(const database_id id) {
id_ = id;
}
/**
* @brief Check whether a relation_id is within the range of IDs contained
* in this CatalogDatabase.
*
* @param id The id to check.
* @return true if id is in range, false otherwise.
*
* @note The caller of this function should make it sure that it holds at
* least a shared lock before calling this function.
*
**/
bool idInRange(const relation_id id) const {
return ((id >= 0)
&& (static_cast<PtrVector<CatalogRelation>::size_type>(id) < rel_vec_.size()));
}
/**
* @brief Check whether a relation with the given name exists. The search is case-insensitive.
*
* @note This method assumes that the caller has already acquired the
* necessary locks before invoking it.
*
* @param rel_name The name to check for.
* @return Whether the relation exists.
**/
inline bool hasRelationWithNameUnsafe(const std::string &rel_name) const {
return (rel_map_.find(ToLower(rel_name)) != rel_map_.end());
}
/**
* @brief Check whether a relation with the given id exists.
*
* @note This method assumes that the caller has already acquired the
* necessary locks before invoking it.
*
* @param id The id to check for.
* @return Whether the relation exists.
**/
inline bool hasRelationWithIdUnsafe(const relation_id id) const {
return (idInRange(id) && !rel_vec_.elementIsNull(id));
}
Catalog *parent_;
// The database name.
const std::string name_;
// Indicate the status of this database (i.e., consistent or not).
Status status_;
alignas(kCacheLineBytes) mutable SpinSharedMutex<false> status_mutex_;
// A vector of relations. NULL if the relation has dropped from the database.
PtrVector<CatalogRelation, true> rel_vec_;
/**
* @brief Map from relation name to the pointer to the corresponding relation.
* The relation name stored in the map is in lowercase characters.
*/
std::unordered_map<std::string, CatalogRelation*> rel_map_;
// Concurrency protection for 'rel_vec_' and 'rel_map_'.
alignas(kCacheLineBytes) mutable SpinSharedMutex<false> relations_mutex_;
friend class Catalog;
DISALLOW_COPY_AND_ASSIGN(CatalogDatabase);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_CATALOG_CATALOG_DATABASE_HPP_