-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCache.php
More file actions
233 lines (199 loc) · 6.52 KB
/
Cache.php
File metadata and controls
233 lines (199 loc) · 6.52 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
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
<?php
namespace Utopia\Migration;
use Utopia\Migration\Resources\Database\Column;
use Utopia\Migration\Resources\Database\Index;
use Utopia\Migration\Resources\Database\Row;
use Utopia\Migration\Resources\Database\Table;
use Utopia\Migration\Resources\Functions\Deployment;
use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment;
use Utopia\Migration\Resources\Storage\File;
/**
* Cache stores a local version of all data copied over from the source, This can be used as reference point for
* previous transfers and also help the destination to determine what needs to be updated, modified,
* added or removed. It is also used for debugging and validation purposes.
*/
class Cache
{
/**
* @var array<string, array<string, Resource|string>> $cache
*/
protected array $cache = [];
public function __construct()
{
$this->cache = [];
}
/**
* Get cache key from resource
*
* @param Resource $resource
* @return string
*/
public function resolveResourceCacheKey(Resource $resource): string
{
if (! $resource->getSequence()) {
$resource->setSequence(uniqid());
}
$resourceName = $resource->getName();
$keys = [];
switch ($resourceName) {
case Resource::TYPE_TABLE:
case Resource::TYPE_COLLECTION:
/** @var Table $resource */
$keys[] = $resource->getDatabase()->getSequence();
break;
case Resource::TYPE_ROW:
case Resource::TYPE_DOCUMENT:
case Resource::TYPE_COLUMN:
case Resource::TYPE_ATTRIBUTE:
case Resource::TYPE_INDEX:
/** @var Row|Column|Index $resource */
$table = $resource->getTable();
$keys[] = $table->getDatabase()->getSequence();
$keys[] = $table->getSequence();
break;
case Resource::TYPE_FILE:
/** @var File $resource */
$keys[] = $resource->getBucket()->getSequence();
break;
case Resource::TYPE_DEPLOYMENT:
/** @var Deployment $resource */
$keys[] = $resource->getFunction()->getSequence();
break;
case Resource::TYPE_SITE_DEPLOYMENT:
/** @var SiteDeployment $resource */
$keys[] = $resource->getSite()->getSequence();
break;
default:
break;
}
$keys[] = $resource->getSequence();
return \implode('_', $keys);
}
/**
* Add Resource
*
* Places the resource in the cache, in the cache backend this also gets assigned a unique ID.
*
*/
public function add(Resource $resource): void
{
$key = $this->resolveResourceCacheKey($resource);
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
$status = $resource->getStatus();
$counter = $this->cache[$resource->getName()][$status] ?? 0;
$counter = intval($counter) + 1;
$this->cache[$resource->getName()][$status] = $counter . ''; // Transfer.php check is_string($resource)
return;
}
if ($resource->getName() == Resource::TYPE_FILE || $resource->getName() == Resource::TYPE_DEPLOYMENT || $resource->getName() == Resource::TYPE_SITE_DEPLOYMENT) {
/** @var File|Deployment|SiteDeployment $resource */
$resource->setData(''); // Prevent Memory Leak
}
$this->cache[$resource->getName()][$key] = $resource;
}
/**
* Add All Resources
*
* @param array<Resource> $resources
* @return void
*/
public function addAll(array $resources): void
{
foreach ($resources as $resource) {
$this->add($resource);
}
}
/**
* Update Resource
*
* Updates the resource in the cache, if the resource does not exist in the cache an exception is thrown.
* Use Add to add a new resource to the cache.
*
* @param Resource $resource
* @return void
*/
public function update(Resource $resource): void
{
/**
* todo: call directly $this->add($resource);
*/
$key = $this->resolveResourceCacheKey($resource);
/**
* if rows then updating the status counter only
*/
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
$this->add($resource);
return;
}
if (! in_array($resource->getName(), $this->cache)) {
$this->add($resource);
}
$this->cache[$resource->getName()][$key] = $resource;
}
/**
* @param array<Resource> $resources
* @return void
*/
public function updateAll(array $resources): void
{
foreach ($resources as $resource) {
$this->update($resource);
}
}
/**
* Remove Resource
*
* Removes the resource from the cache, if the resource does not exist in the cache an exception is thrown.
*
* @param Resource $resource
* @return void
* @throws \Exception
*/
public function remove(Resource $resource): void
{
$key = $this->resolveResourceCacheKey($resource);
if ($resource->getName() == Resource::TYPE_ROW || $resource->getName() == Resource::TYPE_DOCUMENT) {
if (! isset($this->cache[$resource->getName()][$key])) {
throw new \Exception('Resource does not exist in cache');
}
}
if (! in_array($resource, $this->cache[$resource->getName()])) {
throw new \Exception('Resource does not exist in cache');
}
unset($this->cache[$resource->getName()][$key]);
}
/**
* Get Resources
*
* @param string|Resource $resource
* @return array<Resource>
*/
public function get(string|Resource $resource): array
{
if (is_string($resource)) {
return $this->cache[$resource] ?? [];
} else {
return $this->cache[$resource->getName()] ?? [];
}
}
/**
* Get All Resources
*
* @return array<string, array<string, Resource|string>>
*/
public function getAll(): array
{
return $this->cache;
}
/**
* Wipe Cache
*
* Removes all resources from the cache.
*
* @return void
*/
public function wipe(): void
{
$this->cache = [];
}
}