-
Notifications
You must be signed in to change notification settings - Fork 10
/
CatalogDatabaseCache.hpp
139 lines (115 loc) · 4.26 KB
/
CatalogDatabaseCache.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
/**
* 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_CACHE_HPP_
#define QUICKSTEP_CATALOG_CATALOG_DATABASE_CACHE_HPP_
#include <cstddef>
#include <memory>
#include <unordered_map>
#include <utility>
#include "catalog/CatalogDatabaseLite.hpp"
#include "catalog/CatalogTypedefs.hpp"
#include "storage/StorageConstants.hpp"
#include "threading/SharedMutex.hpp"
#include "threading/SpinSharedMutex.hpp"
#include "utility/Macros.hpp"
#include "glog/logging.h"
namespace quickstep {
class CatalogRelationSchema;
namespace serialization { class CatalogDatabase; }
/** \addtogroup Catalog
* @{
*/
/**
* @brief A database cache managed by Shiftboss in the distributed version.
* During the runtime, it contains all the referenced relation schemas
* to execute a query. For a SELECT query, the temporary query
* result relation will be dropped after the CLI finishes processing the
* result and notifies Shiftboss.
*
* @note A CatalogRelationSchema should be kept unless all associated blocks
* have been deleted.
**/
class CatalogDatabaseCache final : public CatalogDatabaseLite {
public:
/**
* @brief Constructor.
**/
CatalogDatabaseCache() {}
/**
* @brief Constructor. Reconstruct a database cache from its serialized
* Protocol Buffer form.
*
* @param proto The Protocol Buffer serialization of a database, previously
* produced by the optimizer.
**/
explicit CatalogDatabaseCache(const serialization::CatalogDatabase &proto);
/**
* @brief Destructor which recursively destroys children.
**/
~CatalogDatabaseCache() override {
}
bool hasRelationWithId(const relation_id id) const override {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
return rel_map_.find(id) != rel_map_.end();
}
const CatalogRelationSchema& getRelationSchemaById(const relation_id id) const override {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
const auto cit = rel_map_.find(id);
DCHECK(cit != rel_map_.end());
return *(cit->second);
}
void dropRelationById(const relation_id id) override;
/**
* @brief Get the number of child relations.
*
* @return The number of child relations.
**/
std::size_t size() const {
SpinSharedMutexSharedLock<false> lock(relations_mutex_);
return rel_map_.size();
}
/**
* @brief Update the cache from its serialized Protocol Buffer form. If the
* relation schema exists, it will be ignored.
*
* @param proto The Protocol Buffer serialization of a catalog cache,
* previously produced in optimizer.
**/
void update(const serialization::CatalogDatabase &proto);
private:
/**
* @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 the optimizer.
* @return Whether proto is fully-formed and valid.
**/
static bool ProtoIsValid(const serialization::CatalogDatabase &proto);
/**
* @brief Map from relation id to the pointer to the corresponding relation schema.
*/
std::unordered_map<relation_id, std::unique_ptr<const CatalogRelationSchema>> rel_map_;
// Concurrency protection for 'rel_map_'.
alignas(kCacheLineBytes) mutable SpinSharedMutex<false> relations_mutex_;
DISALLOW_COPY_AND_ASSIGN(CatalogDatabaseCache);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_CATALOG_CATALOG_DATABASE_CACHE_HPP_