Completed
Push — master ( 04a216...ef2e71 )
by
unknown
42:59
created
lib/private/Files/Cache/Wrapper/CacheWrapper.php 1 patch
Indentation   +315 added lines, -315 removed lines patch added patch discarded remove patch
@@ -16,319 +16,319 @@
 block discarded – undo
16 16
 use OCP\Server;
17 17
 
18 18
 class CacheWrapper extends Cache {
19
-	/**
20
-	 * @var ?ICache
21
-	 */
22
-	protected $cache;
23
-
24
-	public function __construct(?ICache $cache, ?CacheDependencies $dependencies = null) {
25
-		$this->cache = $cache;
26
-		if (!$dependencies && $cache instanceof Cache) {
27
-			$this->mimetypeLoader = $cache->mimetypeLoader;
28
-			$this->connection = $cache->connection;
29
-			$this->querySearchHelper = $cache->querySearchHelper;
30
-		} else {
31
-			if (!$dependencies) {
32
-				$dependencies = Server::get(CacheDependencies::class);
33
-			}
34
-			$this->mimetypeLoader = $dependencies->getMimeTypeLoader();
35
-			$this->connection = $dependencies->getConnection();
36
-			$this->querySearchHelper = $dependencies->getQuerySearchHelper();
37
-		}
38
-	}
39
-
40
-	public function getCache(): ICache {
41
-		if (!$this->cache) {
42
-			throw new \Exception('Source cache not initialized');
43
-		}
44
-		return $this->cache;
45
-	}
46
-
47
-	protected function hasEncryptionWrapper(): bool {
48
-		$cache = $this->getCache();
49
-		if ($cache instanceof Cache) {
50
-			return $cache->hasEncryptionWrapper();
51
-		} else {
52
-			return false;
53
-		}
54
-	}
55
-
56
-	protected function shouldEncrypt(string $targetPath): bool {
57
-		$cache = $this->getCache();
58
-		if ($cache instanceof Cache) {
59
-			return $cache->shouldEncrypt($targetPath);
60
-		} else {
61
-			return false;
62
-		}
63
-	}
64
-
65
-	/**
66
-	 * Make it easy for wrappers to modify every returned cache entry
67
-	 *
68
-	 * @param ICacheEntry $entry
69
-	 * @return ICacheEntry|false
70
-	 */
71
-	protected function formatCacheEntry($entry) {
72
-		return $entry;
73
-	}
74
-
75
-	/**
76
-	 * get the stored metadata of a file or folder
77
-	 *
78
-	 * @param string|int $file
79
-	 * @return ICacheEntry|false
80
-	 */
81
-	public function get($file) {
82
-		$result = $this->getCache()->get($file);
83
-		if ($result instanceof ICacheEntry) {
84
-			$result = $this->formatCacheEntry($result);
85
-		}
86
-		return $result;
87
-	}
88
-
89
-	/**
90
-	 * get the metadata of all files stored in $folder
91
-	 *
92
-	 * @param string $folder
93
-	 * @return ICacheEntry[]
94
-	 */
95
-	public function getFolderContents($folder) {
96
-		// can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this
97
-		// and not the wrapped cache
98
-		$fileId = $this->getId($folder);
99
-		return $this->getFolderContentsById($fileId);
100
-	}
101
-
102
-	/**
103
-	 * get the metadata of all files stored in $folder
104
-	 *
105
-	 * @param int $fileId the file id of the folder
106
-	 * @return array
107
-	 */
108
-	public function getFolderContentsById($fileId) {
109
-		$results = $this->getCache()->getFolderContentsById($fileId);
110
-		return array_map([$this, 'formatCacheEntry'], $results);
111
-	}
112
-
113
-	/**
114
-	 * insert or update meta data for a file or folder
115
-	 *
116
-	 * @param string $file
117
-	 * @param array $data
118
-	 *
119
-	 * @return int file id
120
-	 * @throws \RuntimeException
121
-	 */
122
-	public function put($file, array $data) {
123
-		if (($id = $this->getId($file)) > -1) {
124
-			$this->update($id, $data);
125
-			return $id;
126
-		} else {
127
-			return $this->insert($file, $data);
128
-		}
129
-	}
130
-
131
-	/**
132
-	 * insert meta data for a new file or folder
133
-	 *
134
-	 * @param string $file
135
-	 * @param array $data
136
-	 *
137
-	 * @return int file id
138
-	 * @throws \RuntimeException
139
-	 */
140
-	public function insert($file, array $data) {
141
-		return $this->getCache()->insert($file, $data);
142
-	}
143
-
144
-	/**
145
-	 * update the metadata in the cache
146
-	 *
147
-	 * @param int $id
148
-	 * @param array $data
149
-	 */
150
-	public function update($id, array $data) {
151
-		$this->getCache()->update($id, $data);
152
-	}
153
-
154
-	/**
155
-	 * get the file id for a file
156
-	 *
157
-	 * @param string $file
158
-	 * @return int
159
-	 */
160
-	public function getId($file) {
161
-		return $this->getCache()->getId($file);
162
-	}
163
-
164
-	/**
165
-	 * get the id of the parent folder of a file
166
-	 *
167
-	 * @param string $file
168
-	 * @return int
169
-	 */
170
-	public function getParentId($file) {
171
-		return $this->getCache()->getParentId($file);
172
-	}
173
-
174
-	/**
175
-	 * check if a file is available in the cache
176
-	 *
177
-	 * @param string $file
178
-	 * @return bool
179
-	 */
180
-	public function inCache($file) {
181
-		return $this->getCache()->inCache($file);
182
-	}
183
-
184
-	/**
185
-	 * remove a file or folder from the cache
186
-	 *
187
-	 * @param string $file
188
-	 */
189
-	public function remove($file) {
190
-		$this->getCache()->remove($file);
191
-	}
192
-
193
-	/**
194
-	 * Move a file or folder in the cache
195
-	 *
196
-	 * @param string $source
197
-	 * @param string $target
198
-	 */
199
-	public function move($source, $target) {
200
-		$this->getCache()->move($source, $target);
201
-	}
202
-
203
-	protected function getMoveInfo($path) {
204
-		/** @var Cache $cache */
205
-		$cache = $this->getCache();
206
-		return $cache->getMoveInfo($path);
207
-	}
208
-
209
-	public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
210
-		$this->getCache()->moveFromCache($sourceCache, $sourcePath, $targetPath);
211
-	}
212
-
213
-	/**
214
-	 * remove all entries for files that are stored on the storage from the cache
215
-	 */
216
-	public function clear() {
217
-		$cache = $this->getCache();
218
-		if ($cache instanceof Cache) {
219
-			$cache->clear();
220
-		} else {
221
-			$cache->remove('');
222
-		}
223
-	}
224
-
225
-	/**
226
-	 * @param string $file
227
-	 *
228
-	 * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
229
-	 */
230
-	public function getStatus($file) {
231
-		return $this->getCache()->getStatus($file);
232
-	}
233
-
234
-	public function searchQuery(ISearchQuery $query) {
235
-		return current($this->querySearchHelper->searchInCaches($query, [$this]));
236
-	}
237
-
238
-	/**
239
-	 * update the folder size and the size of all parent folders
240
-	 *
241
-	 * @param array|ICacheEntry|null $data (optional) meta data of the folder
242
-	 */
243
-	public function correctFolderSize(string $path, $data = null, bool $isBackgroundScan = false): void {
244
-		$cache = $this->getCache();
245
-		if ($cache instanceof Cache) {
246
-			$cache->correctFolderSize($path, $data, $isBackgroundScan);
247
-		}
248
-	}
249
-
250
-	/**
251
-	 * get the size of a folder and set it in the cache
252
-	 *
253
-	 * @param string $path
254
-	 * @param array|null|ICacheEntry $entry (optional) meta data of the folder
255
-	 * @return int|float
256
-	 */
257
-	public function calculateFolderSize($path, $entry = null) {
258
-		$cache = $this->getCache();
259
-		if ($cache instanceof Cache) {
260
-			return $cache->calculateFolderSize($path, $entry);
261
-		} else {
262
-			return 0;
263
-		}
264
-	}
265
-
266
-	/**
267
-	 * get all file ids on the files on the storage
268
-	 *
269
-	 * @return int[]
270
-	 */
271
-	public function getAll() {
272
-		/** @var Cache $cache */
273
-		$cache = $this->getCache();
274
-		return $cache->getAll();
275
-	}
276
-
277
-	/**
278
-	 * find a folder in the cache which has not been fully scanned
279
-	 *
280
-	 * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
281
-	 * use the one with the highest id gives the best result with the background scanner, since that is most
282
-	 * likely the folder where we stopped scanning previously
283
-	 *
284
-	 * @return string|false the path of the folder or false when no folder matched
285
-	 */
286
-	public function getIncomplete() {
287
-		return $this->getCache()->getIncomplete();
288
-	}
289
-
290
-	/**
291
-	 * get the path of a file on this storage by it's id
292
-	 *
293
-	 * @param int $id
294
-	 * @return string|null
295
-	 */
296
-	public function getPathById($id) {
297
-		return $this->getCache()->getPathById($id);
298
-	}
299
-
300
-	/**
301
-	 * Returns the numeric storage id
302
-	 *
303
-	 * @return int
304
-	 */
305
-	public function getNumericStorageId() {
306
-		return $this->getCache()->getNumericStorageId();
307
-	}
308
-
309
-	/**
310
-	 * get the storage id of the storage for a file and the internal path of the file
311
-	 * unlike getPathById this does not limit the search to files on this storage and
312
-	 * instead does a global search in the cache table
313
-	 *
314
-	 * @param int $id
315
-	 * @return array first element holding the storage id, second the path
316
-	 */
317
-	public static function getById($id) {
318
-		return parent::getById($id);
319
-	}
320
-
321
-	public function getQueryFilterForStorage(): ISearchOperator {
322
-		return $this->getCache()->getQueryFilterForStorage();
323
-	}
324
-
325
-	public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
326
-		$rawEntry = $this->getCache()->getCacheEntryFromSearchResult($rawEntry);
327
-		if ($rawEntry) {
328
-			$entry = $this->formatCacheEntry(clone $rawEntry);
329
-			return $entry ?: null;
330
-		}
331
-
332
-		return null;
333
-	}
19
+    /**
20
+     * @var ?ICache
21
+     */
22
+    protected $cache;
23
+
24
+    public function __construct(?ICache $cache, ?CacheDependencies $dependencies = null) {
25
+        $this->cache = $cache;
26
+        if (!$dependencies && $cache instanceof Cache) {
27
+            $this->mimetypeLoader = $cache->mimetypeLoader;
28
+            $this->connection = $cache->connection;
29
+            $this->querySearchHelper = $cache->querySearchHelper;
30
+        } else {
31
+            if (!$dependencies) {
32
+                $dependencies = Server::get(CacheDependencies::class);
33
+            }
34
+            $this->mimetypeLoader = $dependencies->getMimeTypeLoader();
35
+            $this->connection = $dependencies->getConnection();
36
+            $this->querySearchHelper = $dependencies->getQuerySearchHelper();
37
+        }
38
+    }
39
+
40
+    public function getCache(): ICache {
41
+        if (!$this->cache) {
42
+            throw new \Exception('Source cache not initialized');
43
+        }
44
+        return $this->cache;
45
+    }
46
+
47
+    protected function hasEncryptionWrapper(): bool {
48
+        $cache = $this->getCache();
49
+        if ($cache instanceof Cache) {
50
+            return $cache->hasEncryptionWrapper();
51
+        } else {
52
+            return false;
53
+        }
54
+    }
55
+
56
+    protected function shouldEncrypt(string $targetPath): bool {
57
+        $cache = $this->getCache();
58
+        if ($cache instanceof Cache) {
59
+            return $cache->shouldEncrypt($targetPath);
60
+        } else {
61
+            return false;
62
+        }
63
+    }
64
+
65
+    /**
66
+     * Make it easy for wrappers to modify every returned cache entry
67
+     *
68
+     * @param ICacheEntry $entry
69
+     * @return ICacheEntry|false
70
+     */
71
+    protected function formatCacheEntry($entry) {
72
+        return $entry;
73
+    }
74
+
75
+    /**
76
+     * get the stored metadata of a file or folder
77
+     *
78
+     * @param string|int $file
79
+     * @return ICacheEntry|false
80
+     */
81
+    public function get($file) {
82
+        $result = $this->getCache()->get($file);
83
+        if ($result instanceof ICacheEntry) {
84
+            $result = $this->formatCacheEntry($result);
85
+        }
86
+        return $result;
87
+    }
88
+
89
+    /**
90
+     * get the metadata of all files stored in $folder
91
+     *
92
+     * @param string $folder
93
+     * @return ICacheEntry[]
94
+     */
95
+    public function getFolderContents($folder) {
96
+        // can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this
97
+        // and not the wrapped cache
98
+        $fileId = $this->getId($folder);
99
+        return $this->getFolderContentsById($fileId);
100
+    }
101
+
102
+    /**
103
+     * get the metadata of all files stored in $folder
104
+     *
105
+     * @param int $fileId the file id of the folder
106
+     * @return array
107
+     */
108
+    public function getFolderContentsById($fileId) {
109
+        $results = $this->getCache()->getFolderContentsById($fileId);
110
+        return array_map([$this, 'formatCacheEntry'], $results);
111
+    }
112
+
113
+    /**
114
+     * insert or update meta data for a file or folder
115
+     *
116
+     * @param string $file
117
+     * @param array $data
118
+     *
119
+     * @return int file id
120
+     * @throws \RuntimeException
121
+     */
122
+    public function put($file, array $data) {
123
+        if (($id = $this->getId($file)) > -1) {
124
+            $this->update($id, $data);
125
+            return $id;
126
+        } else {
127
+            return $this->insert($file, $data);
128
+        }
129
+    }
130
+
131
+    /**
132
+     * insert meta data for a new file or folder
133
+     *
134
+     * @param string $file
135
+     * @param array $data
136
+     *
137
+     * @return int file id
138
+     * @throws \RuntimeException
139
+     */
140
+    public function insert($file, array $data) {
141
+        return $this->getCache()->insert($file, $data);
142
+    }
143
+
144
+    /**
145
+     * update the metadata in the cache
146
+     *
147
+     * @param int $id
148
+     * @param array $data
149
+     */
150
+    public function update($id, array $data) {
151
+        $this->getCache()->update($id, $data);
152
+    }
153
+
154
+    /**
155
+     * get the file id for a file
156
+     *
157
+     * @param string $file
158
+     * @return int
159
+     */
160
+    public function getId($file) {
161
+        return $this->getCache()->getId($file);
162
+    }
163
+
164
+    /**
165
+     * get the id of the parent folder of a file
166
+     *
167
+     * @param string $file
168
+     * @return int
169
+     */
170
+    public function getParentId($file) {
171
+        return $this->getCache()->getParentId($file);
172
+    }
173
+
174
+    /**
175
+     * check if a file is available in the cache
176
+     *
177
+     * @param string $file
178
+     * @return bool
179
+     */
180
+    public function inCache($file) {
181
+        return $this->getCache()->inCache($file);
182
+    }
183
+
184
+    /**
185
+     * remove a file or folder from the cache
186
+     *
187
+     * @param string $file
188
+     */
189
+    public function remove($file) {
190
+        $this->getCache()->remove($file);
191
+    }
192
+
193
+    /**
194
+     * Move a file or folder in the cache
195
+     *
196
+     * @param string $source
197
+     * @param string $target
198
+     */
199
+    public function move($source, $target) {
200
+        $this->getCache()->move($source, $target);
201
+    }
202
+
203
+    protected function getMoveInfo($path) {
204
+        /** @var Cache $cache */
205
+        $cache = $this->getCache();
206
+        return $cache->getMoveInfo($path);
207
+    }
208
+
209
+    public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
210
+        $this->getCache()->moveFromCache($sourceCache, $sourcePath, $targetPath);
211
+    }
212
+
213
+    /**
214
+     * remove all entries for files that are stored on the storage from the cache
215
+     */
216
+    public function clear() {
217
+        $cache = $this->getCache();
218
+        if ($cache instanceof Cache) {
219
+            $cache->clear();
220
+        } else {
221
+            $cache->remove('');
222
+        }
223
+    }
224
+
225
+    /**
226
+     * @param string $file
227
+     *
228
+     * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
229
+     */
230
+    public function getStatus($file) {
231
+        return $this->getCache()->getStatus($file);
232
+    }
233
+
234
+    public function searchQuery(ISearchQuery $query) {
235
+        return current($this->querySearchHelper->searchInCaches($query, [$this]));
236
+    }
237
+
238
+    /**
239
+     * update the folder size and the size of all parent folders
240
+     *
241
+     * @param array|ICacheEntry|null $data (optional) meta data of the folder
242
+     */
243
+    public function correctFolderSize(string $path, $data = null, bool $isBackgroundScan = false): void {
244
+        $cache = $this->getCache();
245
+        if ($cache instanceof Cache) {
246
+            $cache->correctFolderSize($path, $data, $isBackgroundScan);
247
+        }
248
+    }
249
+
250
+    /**
251
+     * get the size of a folder and set it in the cache
252
+     *
253
+     * @param string $path
254
+     * @param array|null|ICacheEntry $entry (optional) meta data of the folder
255
+     * @return int|float
256
+     */
257
+    public function calculateFolderSize($path, $entry = null) {
258
+        $cache = $this->getCache();
259
+        if ($cache instanceof Cache) {
260
+            return $cache->calculateFolderSize($path, $entry);
261
+        } else {
262
+            return 0;
263
+        }
264
+    }
265
+
266
+    /**
267
+     * get all file ids on the files on the storage
268
+     *
269
+     * @return int[]
270
+     */
271
+    public function getAll() {
272
+        /** @var Cache $cache */
273
+        $cache = $this->getCache();
274
+        return $cache->getAll();
275
+    }
276
+
277
+    /**
278
+     * find a folder in the cache which has not been fully scanned
279
+     *
280
+     * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
281
+     * use the one with the highest id gives the best result with the background scanner, since that is most
282
+     * likely the folder where we stopped scanning previously
283
+     *
284
+     * @return string|false the path of the folder or false when no folder matched
285
+     */
286
+    public function getIncomplete() {
287
+        return $this->getCache()->getIncomplete();
288
+    }
289
+
290
+    /**
291
+     * get the path of a file on this storage by it's id
292
+     *
293
+     * @param int $id
294
+     * @return string|null
295
+     */
296
+    public function getPathById($id) {
297
+        return $this->getCache()->getPathById($id);
298
+    }
299
+
300
+    /**
301
+     * Returns the numeric storage id
302
+     *
303
+     * @return int
304
+     */
305
+    public function getNumericStorageId() {
306
+        return $this->getCache()->getNumericStorageId();
307
+    }
308
+
309
+    /**
310
+     * get the storage id of the storage for a file and the internal path of the file
311
+     * unlike getPathById this does not limit the search to files on this storage and
312
+     * instead does a global search in the cache table
313
+     *
314
+     * @param int $id
315
+     * @return array first element holding the storage id, second the path
316
+     */
317
+    public static function getById($id) {
318
+        return parent::getById($id);
319
+    }
320
+
321
+    public function getQueryFilterForStorage(): ISearchOperator {
322
+        return $this->getCache()->getQueryFilterForStorage();
323
+    }
324
+
325
+    public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
326
+        $rawEntry = $this->getCache()->getCacheEntryFromSearchResult($rawEntry);
327
+        if ($rawEntry) {
328
+            $entry = $this->formatCacheEntry(clone $rawEntry);
329
+            return $entry ?: null;
330
+        }
331
+
332
+        return null;
333
+    }
334 334
 }
Please login to merge, or discard this patch.