This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathcatalog_cache.cpp
177 lines (159 loc) · 5.79 KB
/
catalog_cache.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//===----------------------------------------------------------------------===//
//
// Peloton
//
// catalog_cache.cpp
//
// Identification: src/catalog/catalog_cache.cpp
//
// Copyright (c) 2015-17, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include <memory>
#include "catalog/catalog_cache.h"
#include "catalog/database_catalog.h"
#include "common/logger.h"
#include "concurrency/transaction_context.h"
namespace peloton {
namespace catalog {
/*@brief insert database catalog object into cache
* @param database_object
* @return false only if database_oid already exists in cache
*/
bool CatalogCache::InsertDatabaseObject(
std::shared_ptr<DatabaseCatalogObject> database_object) {
if (!database_object || database_object->GetDatabaseOid() == INVALID_OID) {
return false; // invalid object
}
// check if already in cache
if (database_objects_cache.find(database_object->GetDatabaseOid()) !=
database_objects_cache.end()) {
LOG_TRACE("Database %u already exists in cache!",
database_object->GetDatabaseOid());
return false;
}
if (database_name_cache.find(database_object->GetDatabaseName()) !=
database_name_cache.end()) {
LOG_TRACE("Database %s already exists in cache!",
database_object->GetDatabaseName().c_str());
return false;
}
database_objects_cache.insert(
std::make_pair(database_object->GetDatabaseOid(), database_object));
database_name_cache.insert(
std::make_pair(database_object->GetDatabaseName(), database_object));
return true;
}
/*@brief evict database catalog object from cache
* @param database_oid
* @return true if database_oid is found and evicted; false if not found
*/
bool CatalogCache::EvictDatabaseObject(oid_t database_oid) {
auto it = database_objects_cache.find(database_oid);
if (it == database_objects_cache.end()) {
return false; // database oid not found in cache
}
auto database_object = it->second;
PELOTON_ASSERT(database_object);
database_objects_cache.erase(it);
database_name_cache.erase(database_object->GetDatabaseName());
return true;
}
/*@brief evict database catalog object from cache
* @param database_name
* @return true if database_name is found and evicted; false if not found
*/
bool CatalogCache::EvictDatabaseObject(const std::string &database_name) {
auto it = database_name_cache.find(database_name);
if (it == database_name_cache.end()) {
return false; // database name not found in cache
}
auto database_object = it->second;
PELOTON_ASSERT(database_object);
database_name_cache.erase(it);
database_objects_cache.erase(database_object->GetDatabaseOid());
return true;
}
/*@brief get database catalog object from cache
* @param database_oid
* @return database catalog object; if not found return object with invalid oid
*/
std::shared_ptr<DatabaseCatalogObject> CatalogCache::GetDatabaseObject(
oid_t database_oid) {
auto it = database_objects_cache.find(database_oid);
if (it == database_objects_cache.end()) {
return nullptr;
}
return it->second;
}
/*@brief get database catalog object from cache
* @param database_name
* @return database catalog object; if not found return null
*/
std::shared_ptr<DatabaseCatalogObject> CatalogCache::GetDatabaseObject(
const std::string &database_name) {
auto it = database_name_cache.find(database_name);
if (it == database_name_cache.end()) {
return nullptr;
}
return it->second;
}
/* @brief Get database catalog object from cache,
or all the way from storage
* @param txn if nullptr, return nullptr on a cache miss
* @return Shared pointer to the requested database catalog object
*/
std::unordered_map<oid_t, std::shared_ptr<DatabaseCatalogObject>>
CatalogCache::GetDatabaseObjects(concurrency::TransactionContext *txn) {
if (!valid_database_objects && txn != nullptr) {
// cache miss get from pg_database
return DatabaseCatalog::GetInstance()->GetDatabaseObjects(txn);
}
// make sure to check IsValidTableObjects() before getting table objects
PELOTON_ASSERT(valid_database_objects);
return database_objects_cache;
}
/*@brief search table catalog object from all cached database objects
* @param table_oid
* @return table catalog object; if not found return null
*/
std::shared_ptr<TableCatalogObject> CatalogCache::GetCachedTableObject(
oid_t table_oid) {
for (auto it = database_objects_cache.begin();
it != database_objects_cache.end(); ++it) {
auto database_object = it->second;
auto table_object = database_object->GetTableObject(table_oid, true);
if (table_object) return table_object;
}
return nullptr;
}
/*@brief search index catalog object from all cached database objects
* @param index_oid
* @return index catalog object; if not found return null
*/
std::shared_ptr<IndexCatalogObject> CatalogCache::GetCachedIndexObject(
oid_t index_oid) {
for (auto it = database_objects_cache.begin();
it != database_objects_cache.end(); ++it) {
auto database_object = it->second;
auto index_object = database_object->GetCachedIndexObject(index_oid);
if (index_object) return index_object;
}
return nullptr;
}
/*@brief search index catalog object from all cached database objects
* @param index_name
* @return index catalog object; if not found return null
*/
std::shared_ptr<IndexCatalogObject> CatalogCache::GetCachedIndexObject(
const std::string &index_name) {
for (auto it = database_objects_cache.begin();
it != database_objects_cache.end(); ++it) {
auto database_object = it->second;
auto index_object = database_object->GetCachedIndexObject(index_name);
if (index_object) return index_object;
}
return nullptr;
}
} // namespace catalog
} // namespace peloton