Passed
Push — master ( 4c60ff...437d93 )
by Julius
15:25 queued 12s
created

CacheWrapper::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Ari Selseng <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Daniel Jagszent <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Vincent Petry <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
namespace OC\Files\Cache\Wrapper;
31
32
use OC\Files\Cache\Cache;
33
use OC\Files\Cache\QuerySearchHelper;
34
use OCP\Files\Cache\ICache;
35
use OCP\Files\Cache\ICacheEntry;
36
use OCP\Files\Search\ISearchOperator;
37
use OCP\Files\Search\ISearchQuery;
38
39
class CacheWrapper extends Cache {
40
	/**
41
	 * @var \OCP\Files\Cache\ICache
42
	 */
43
	protected $cache;
44
45
	/**
46
	 * @param \OCP\Files\Cache\ICache $cache
47
	 */
48
	public function __construct($cache) {
49
		$this->cache = $cache;
50
		$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
51
		$this->connection = \OC::$server->getDatabaseConnection();
52
		$this->querySearchHelper = \OC::$server->get(QuerySearchHelper::class);
53
	}
54
55
	protected function getCache() {
56
		return $this->cache;
57
	}
58
59
	/**
60
	 * Make it easy for wrappers to modify every returned cache entry
61
	 *
62
	 * @param ICacheEntry $entry
63
	 * @return ICacheEntry
64
	 */
65
	protected function formatCacheEntry($entry) {
66
		return $entry;
67
	}
68
69
	/**
70
	 * get the stored metadata of a file or folder
71
	 *
72
	 * @param string|int $file
73
	 * @return ICacheEntry|false
74
	 */
75
	public function get($file) {
76
		$result = $this->getCache()->get($file);
77
		if ($result) {
78
			$result = $this->formatCacheEntry($result);
79
		}
80
		return $result;
81
	}
82
83
	/**
84
	 * get the metadata of all files stored in $folder
85
	 *
86
	 * @param string $folder
87
	 * @return ICacheEntry[]
88
	 */
89
	public function getFolderContents($folder) {
90
		// can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this
91
		// and not the wrapped cache
92
		$fileId = $this->getId($folder);
93
		return $this->getFolderContentsById($fileId);
94
	}
95
96
	/**
97
	 * get the metadata of all files stored in $folder
98
	 *
99
	 * @param int $fileId the file id of the folder
100
	 * @return array
101
	 */
102
	public function getFolderContentsById($fileId) {
103
		$results = $this->getCache()->getFolderContentsById($fileId);
104
		return array_map([$this, 'formatCacheEntry'], $results);
105
	}
106
107
	/**
108
	 * insert or update meta data for a file or folder
109
	 *
110
	 * @param string $file
111
	 * @param array $data
112
	 *
113
	 * @return int file id
114
	 * @throws \RuntimeException
115
	 */
116
	public function put($file, array $data) {
117
		if (($id = $this->getId($file)) > -1) {
118
			$this->update($id, $data);
119
			return $id;
120
		} else {
121
			return $this->insert($file, $data);
122
		}
123
	}
124
125
	/**
126
	 * insert meta data for a new file or folder
127
	 *
128
	 * @param string $file
129
	 * @param array $data
130
	 *
131
	 * @return int file id
132
	 * @throws \RuntimeException
133
	 */
134
	public function insert($file, array $data) {
135
		return $this->getCache()->insert($file, $data);
136
	}
137
138
	/**
139
	 * update the metadata in the cache
140
	 *
141
	 * @param int $id
142
	 * @param array $data
143
	 */
144
	public function update($id, array $data) {
145
		$this->getCache()->update($id, $data);
146
	}
147
148
	/**
149
	 * get the file id for a file
150
	 *
151
	 * @param string $file
152
	 * @return int
153
	 */
154
	public function getId($file) {
155
		return $this->getCache()->getId($file);
156
	}
157
158
	/**
159
	 * get the id of the parent folder of a file
160
	 *
161
	 * @param string $file
162
	 * @return int
163
	 */
164
	public function getParentId($file) {
165
		return $this->getCache()->getParentId($file);
166
	}
167
168
	/**
169
	 * check if a file is available in the cache
170
	 *
171
	 * @param string $file
172
	 * @return bool
173
	 */
174
	public function inCache($file) {
175
		return $this->getCache()->inCache($file);
176
	}
177
178
	/**
179
	 * remove a file or folder from the cache
180
	 *
181
	 * @param string $file
182
	 */
183
	public function remove($file) {
184
		$this->getCache()->remove($file);
185
	}
186
187
	/**
188
	 * Move a file or folder in the cache
189
	 *
190
	 * @param string $source
191
	 * @param string $target
192
	 */
193
	public function move($source, $target) {
194
		$this->getCache()->move($source, $target);
195
	}
196
197
	protected function getMoveInfo($path) {
198
		/** @var Cache $cache */
199
		$cache = $this->getCache();
200
		return $cache->getMoveInfo($path);
201
	}
202
203
	public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
204
		$this->getCache()->moveFromCache($sourceCache, $sourcePath, $targetPath);
205
	}
206
207
	/**
208
	 * remove all entries for files that are stored on the storage from the cache
209
	 */
210
	public function clear() {
211
		$this->getCache()->clear();
0 ignored issues
show
Bug introduced by
The method clear() does not exist on OCP\Files\Cache\ICache. It seems like you code against a sub-type of said class. However, the method does not exist in OC\Lockdown\Filesystem\NullCache. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
		$this->getCache()->/** @scrutinizer ignore-call */ clear();
Loading history...
212
	}
213
214
	/**
215
	 * @param string $file
216
	 *
217
	 * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
218
	 */
219
	public function getStatus($file) {
220
		return $this->getCache()->getStatus($file);
221
	}
222
223
	public function searchQuery(ISearchQuery $searchQuery) {
224
		return current($this->querySearchHelper->searchInCaches($searchQuery, [$this]));
225
	}
226
227
	/**
228
	 * update the folder size and the size of all parent folders
229
	 *
230
	 * @param string|boolean $path
231
	 * @param array $data (optional) meta data of the folder
232
	 */
233
	public function correctFolderSize($path, $data = null, $isBackgroundScan = false) {
234
		if ($this->getCache() instanceof Cache) {
235
			$this->getCache()->correctFolderSize($path, $data, $isBackgroundScan);
236
		}
237
	}
238
239
	/**
240
	 * get the size of a folder and set it in the cache
241
	 *
242
	 * @param string $path
243
	 * @param array $entry (optional) meta data of the folder
244
	 * @return int
245
	 */
246
	public function calculateFolderSize($path, $entry = null) {
247
		if ($this->getCache() instanceof Cache) {
248
			return $this->getCache()->calculateFolderSize($path, $entry);
249
		} else {
250
			return 0;
251
		}
252
	}
253
254
	/**
255
	 * get all file ids on the files on the storage
256
	 *
257
	 * @return int[]
258
	 */
259
	public function getAll() {
260
		return $this->getCache()->getAll();
0 ignored issues
show
Bug introduced by
The method getAll() does not exist on OCP\Files\Cache\ICache. It seems like you code against a sub-type of said class. However, the method does not exist in OC\Lockdown\Filesystem\NullCache. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

260
		return $this->getCache()->/** @scrutinizer ignore-call */ getAll();
Loading history...
261
	}
262
263
	/**
264
	 * find a folder in the cache which has not been fully scanned
265
	 *
266
	 * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
267
	 * use the one with the highest id gives the best result with the background scanner, since that is most
268
	 * likely the folder where we stopped scanning previously
269
	 *
270
	 * @return string|bool the path of the folder or false when no folder matched
271
	 */
272
	public function getIncomplete() {
273
		return $this->getCache()->getIncomplete();
274
	}
275
276
	/**
277
	 * get the path of a file on this storage by it's id
278
	 *
279
	 * @param int $id
280
	 * @return string|null
281
	 */
282
	public function getPathById($id) {
283
		return $this->getCache()->getPathById($id);
284
	}
285
286
	/**
287
	 * Returns the numeric storage id
288
	 *
289
	 * @return int
290
	 */
291
	public function getNumericStorageId() {
292
		return $this->getCache()->getNumericStorageId();
293
	}
294
295
	/**
296
	 * get the storage id of the storage for a file and the internal path of the file
297
	 * unlike getPathById this does not limit the search to files on this storage and
298
	 * instead does a global search in the cache table
299
	 *
300
	 * @param int $id
301
	 * @return array first element holding the storage id, second the path
302
	 */
303
	public static function getById($id) {
304
		return parent::getById($id);
305
	}
306
307
	public function getQueryFilterForStorage(): ISearchOperator {
308
		return $this->getCache()->getQueryFilterForStorage();
309
	}
310
311
	public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
312
		$rawEntry = $this->getCache()->getCacheEntryFromSearchResult($rawEntry);
313
		if ($rawEntry) {
314
			return $this->formatCacheEntry(clone $rawEntry);
315
		}
316
317
		return null;
318
	}
319
}
320