forked from duckdb/duckdb
/
object_cache.hpp
64 lines (53 loc) · 1.54 KB
/
object_cache.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
//===----------------------------------------------------------------------===//
// DuckDB
//
// duckdb/storage/object_cache.hpp
//
//
//===----------------------------------------------------------------------===//
#pragma once
#include "duckdb/common/common.hpp"
#include "duckdb/common/string.hpp"
#include "duckdb/common/unordered_map.hpp"
#include "duckdb/common/mutex.hpp"
#include "duckdb/main/client_context.hpp"
#include "duckdb/main/database.hpp"
namespace duckdb {
class ClientContext;
//! ObjectCache is the base class for objects caches in DuckDB
class ObjectCacheEntry {
public:
virtual ~ObjectCacheEntry() {
}
virtual string GetObjectType() = 0;
};
class ObjectCache {
public:
shared_ptr<ObjectCacheEntry> GetObject(const string &key) {
lock_guard<mutex> glock(lock);
auto entry = cache.find(key);
if (entry == cache.end()) {
return nullptr;
}
return entry->second;
}
template <class T>
shared_ptr<T> Get(const string &key) {
shared_ptr<ObjectCacheEntry> object = GetObject(key);
if (!object || object->GetObjectType() != T::ObjectType()) {
return nullptr;
}
return std::static_pointer_cast<T, ObjectCacheEntry>(object);
}
void Put(string key, shared_ptr<ObjectCacheEntry> value) {
lock_guard<mutex> glock(lock);
cache[key] = move(value);
}
static ObjectCache &GetObjectCache(ClientContext &context);
static bool ObjectCacheEnabled(ClientContext &context);
private:
//! Object Cache
unordered_map<string, shared_ptr<ObjectCacheEntry>> cache;
mutex lock;
};
} // namespace duckdb