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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Ari Selseng <ari@selseng.net>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Daniel Jagszent <daniel@jagszent.de>
* @author Joas Schilling <coding@schilljs.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Robin McCorkell <robin@mccorkell.me.uk>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Vincent Petry <pvince81@owncloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Files\Cache\Wrapper;
use OC\Files\Cache\Cache;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Search\ISearchQuery;
class CacheWrapper extends Cache {
/**
* @var \OCP\Files\Cache\ICache
*/
protected $cache;
/**
* @param \OCP\Files\Cache\ICache $cache
*/
public function __construct($cache) {
$this->cache = $cache;
}
protected function getCache() {
return $this->cache;
}
/**
* Make it easy for wrappers to modify every returned cache entry
*
* @param ICacheEntry $entry
* @return ICacheEntry
*/
protected function formatCacheEntry($entry) {
return $entry;
}
/**
* get the stored metadata of a file or folder
*
* @param string|int $file
* @return ICacheEntry|false
*/
public function get($file) {
$result = $this->getCache()->get($file);
if ($result) {
$result = $this->formatCacheEntry($result);
}
return $result;
}
/**
* get the metadata of all files stored in $folder
*
* @param string $folder
* @return ICacheEntry[]
*/
public function getFolderContents($folder) {
// can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this
// and not the wrapped cache
$fileId = $this->getId($folder);
return $this->getFolderContentsById($fileId);
}
/**
* get the metadata of all files stored in $folder
*
* @param int $fileId the file id of the folder
* @return array
*/
public function getFolderContentsById($fileId) {
$results = $this->getCache()->getFolderContentsById($fileId);
return array_map([$this, 'formatCacheEntry'], $results);
}
/**
* insert or update meta data for a file or folder
*
* @param string $file
* @param array $data
*
* @return int file id
* @throws \RuntimeException
*/
public function put($file, array $data) {
if (($id = $this->getId($file)) > -1) {
$this->update($id, $data);
return $id;
} else {
return $this->insert($file, $data);
}
}
/**
* insert meta data for a new file or folder
*
* @param string $file
* @param array $data
*
* @return int file id
* @throws \RuntimeException
*/
public function insert($file, array $data) {
return $this->getCache()->insert($file, $data);
}
/**
* update the metadata in the cache
*
* @param int $id
* @param array $data
*/
public function update($id, array $data) {
$this->getCache()->update($id, $data);
}
/**
* get the file id for a file
*
* @param string $file
* @return int
*/
public function getId($file) {
return $this->getCache()->getId($file);
}
/**
* get the id of the parent folder of a file
*
* @param string $file
* @return int
*/
public function getParentId($file) {
return $this->getCache()->getParentId($file);
}
/**
* check if a file is available in the cache
*
* @param string $file
* @return bool
*/
public function inCache($file) {
return $this->getCache()->inCache($file);
}
/**
* remove a file or folder from the cache
*
* @param string $file
*/
public function remove($file) {
$this->getCache()->remove($file);
}
/**
* Move a file or folder in the cache
*
* @param string $source
* @param string $target
*/
public function move($source, $target) {
$this->getCache()->move($source, $target);
}
protected function getMoveInfo($path) {
/** @var Cache $cache */
$cache = $this->getCache();
return $cache->getMoveInfo($path);
}
public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
$this->getCache()->moveFromCache($sourceCache, $sourcePath, $targetPath);
}
/**
* remove all entries for files that are stored on the storage from the cache
*/
public function clear() {
$this->getCache()->clear();
}
/**
* @param string $file
*
* @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
*/
public function getStatus($file) {
return $this->getCache()->getStatus($file);
}
/**
* search for files matching $pattern
*
* @param string $pattern
* @return ICacheEntry[] an array of file data
*/
public function search($pattern) {
$results = $this->getCache()->search($pattern);
return array_map([$this, 'formatCacheEntry'], $results);
}
/**
* search for files by mimetype
*
* @param string $mimetype
* @return ICacheEntry[]
*/
public function searchByMime($mimetype) {
$results = $this->getCache()->searchByMime($mimetype);
return array_map([$this, 'formatCacheEntry'], $results);
}
public function searchQuery(ISearchQuery $query) {
$results = $this->getCache()->searchQuery($query);
return array_map([$this, 'formatCacheEntry'], $results);
}
/**
* update the folder size and the size of all parent folders
*
* @param string|boolean $path
* @param array $data (optional) meta data of the folder
*/
public function correctFolderSize($path, $data = null, $isBackgroundScan = false) {
if ($this->getCache() instanceof Cache) {
$this->getCache()->correctFolderSize($path, $data, $isBackgroundScan);
}
}
/**
* get the size of a folder and set it in the cache
*
* @param string $path
* @param array $entry (optional) meta data of the folder
* @return int
*/
public function calculateFolderSize($path, $entry = null) {
if ($this->getCache() instanceof Cache) {
return $this->getCache()->calculateFolderSize($path, $entry);
} else {
return 0;
}
}
/**
* get all file ids on the files on the storage
*
* @return int[]
*/
public function getAll() {
return $this->getCache()->getAll();
}
/**
* find a folder in the cache which has not been fully scanned
*
* If multiple incomplete folders are in the cache, the one with the highest id will be returned,
* use the one with the highest id gives the best result with the background scanner, since that is most
* likely the folder where we stopped scanning previously
*
* @return string|bool the path of the folder or false when no folder matched
*/
public function getIncomplete() {
return $this->getCache()->getIncomplete();
}
/**
* get the path of a file on this storage by it's id
*
* @param int $id
* @return string|null
*/
public function getPathById($id) {
return $this->getCache()->getPathById($id);
}
/**
* Returns the numeric storage id
*
* @return int
*/
public function getNumericStorageId() {
return $this->getCache()->getNumericStorageId();
}
/**
* get the storage id of the storage for a file and the internal path of the file
* unlike getPathById this does not limit the search to files on this storage and
* instead does a global search in the cache table
*
* @param int $id
* @return array first element holding the storage id, second the path
*/
static public function getById($id) {
return parent::getById($id);
}
}
|