forked from activeloopai/deeplake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_version.cpp
More file actions
165 lines (130 loc) · 4.02 KB
/
Copy pathtable_version.cpp
File metadata and controls
165 lines (130 loc) · 4.02 KB
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
#ifdef __cplusplus
extern "C" {
#endif
#include <postgres.h>
#include <storage/ipc.h>
#include <storage/lwlock.h>
#include <storage/shmem.h>
#ifdef __cplusplus
}
#endif
#include "table_version.hpp"
namespace {
struct locker
{
LWLock* lwlock;
locker(LWLock* lock, LWLockMode m)
: lwlock(lock)
{
LWLockAcquire(lwlock, m);
}
~locker()
{
LWLockRelease(lwlock);
}
};
} // unnamed namespace
namespace pg {
extern bool use_shared_mem_for_refresh;
table_version_data* table_version_tracker::version_data_ = nullptr;
Size table_version_tracker::get_shmem_size()
{
Size size = MAXALIGN(sizeof(table_version_data));
size = add_size(size, mul_size(MAX_TRACKED_TABLES, sizeof(table_version_entry)));
return size;
}
void table_version_tracker::initialize()
{
bool found = false;
locker lock(AddinShmemInitLock, LW_EXCLUSIVE);
version_data_ = (table_version_data*)ShmemInitStruct(
"deeplake_table_versions",
get_shmem_size(),
&found
);
if (!found) {
// First time initialization
version_data_->lock = &(GetNamedLWLockTranche("deeplake_versions")->lock);
version_data_->max_tables = MAX_TRACKED_TABLES;
version_data_->num_tables = 0;
// Initialize all entries
for (int32_t i = 0; i < MAX_TRACKED_TABLES; ++i) {
version_data_->entries[i].table_oid = InvalidOid;
version_data_->entries[i].version = 0;
}
}
}
table_version_entry* table_version_tracker::find_entry(Oid table_oid, bool create)
{
if (version_data_ == nullptr || !pg::use_shared_mem_for_refresh) {
return nullptr;
}
// Linear search for the table
for (int32_t i = 0; i < version_data_->num_tables; ++i) {
if (version_data_->entries[i].table_oid == table_oid) {
return &version_data_->entries[i];
}
}
if (create && version_data_->num_tables < version_data_->max_tables) {
// Create new entry
int32_t idx = version_data_->num_tables++;
version_data_->entries[idx].table_oid = table_oid;
version_data_->entries[idx].version = 0;
return &version_data_->entries[idx];
}
return nullptr;
}
void table_version_tracker::increment_version(Oid table_oid)
{
if (version_data_ == nullptr || !pg::use_shared_mem_for_refresh) {
return;
}
locker lock(version_data_->lock, LW_EXCLUSIVE);
table_version_entry* entry = find_entry(table_oid, true);
if (entry != nullptr) {
++entry->version;
}
}
uint64_t table_version_tracker::get_version(Oid table_oid)
{
if (version_data_ == nullptr || !pg::use_shared_mem_for_refresh) {
return 0;
}
locker lock(version_data_->lock, LW_SHARED);
uint64_t version = 0;
table_version_entry* entry = find_entry(table_oid, false);
if (entry != nullptr) {
version = entry->version;
}
return version;
}
void table_version_tracker::drop_table(Oid table_oid)
{
if (version_data_ == nullptr || !pg::use_shared_mem_for_refresh) {
return;
}
locker lock(version_data_->lock, LW_EXCLUSIVE);
for (int32_t i = 0; i < version_data_->num_tables; ++i) {
if (version_data_->entries[i].table_oid == table_oid) {
// Move last entry to this position
version_data_->entries[i] = version_data_->entries[version_data_->num_tables - 1];
version_data_->entries[version_data_->num_tables - 1].table_oid = InvalidOid;
version_data_->entries[version_data_->num_tables - 1].version = 0;
--version_data_->num_tables;
break;
}
}
}
void table_version_tracker::clear_all_versions()
{
if (version_data_ == nullptr || !pg::use_shared_mem_for_refresh) {
return;
}
locker lock(version_data_->lock, LW_EXCLUSIVE);
version_data_->num_tables = 0;
for (int32_t i = 0; i < version_data_->max_tables; ++i) {
version_data_->entries[i].table_oid = InvalidOid;
version_data_->entries[i].version = 0;
}
}
} // namespace pg