1
0

CacheWrapper.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Ari Selseng <ari@selseng.net>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Jagszent <daniel@jagszent.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  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. use OC\Files\Cache\Cache;
  32. use OC\Files\Cache\CacheDependencies;
  33. use OCP\Files\Cache\ICache;
  34. use OCP\Files\Cache\ICacheEntry;
  35. use OCP\Files\Search\ISearchOperator;
  36. use OCP\Files\Search\ISearchQuery;
  37. use OCP\Server;
  38. class CacheWrapper extends Cache {
  39. /**
  40. * @var ?ICache
  41. */
  42. protected $cache;
  43. public function __construct(?ICache $cache, ?CacheDependencies $dependencies = null) {
  44. $this->cache = $cache;
  45. if (!$dependencies && $cache instanceof Cache) {
  46. $this->mimetypeLoader = $cache->mimetypeLoader;
  47. $this->connection = $cache->connection;
  48. $this->querySearchHelper = $cache->querySearchHelper;
  49. } else {
  50. if (!$dependencies) {
  51. $dependencies = Server::get(CacheDependencies::class);
  52. }
  53. $this->mimetypeLoader = $dependencies->getMimeTypeLoader();
  54. $this->connection = $dependencies->getConnection();
  55. $this->querySearchHelper = $dependencies->getQuerySearchHelper();
  56. }
  57. }
  58. protected function getCache() {
  59. return $this->cache;
  60. }
  61. protected function hasEncryptionWrapper(): bool {
  62. $cache = $this->getCache();
  63. if ($cache instanceof Cache) {
  64. return $cache->hasEncryptionWrapper();
  65. } else {
  66. return false;
  67. }
  68. }
  69. /**
  70. * Make it easy for wrappers to modify every returned cache entry
  71. *
  72. * @param ICacheEntry $entry
  73. * @return ICacheEntry|false
  74. */
  75. protected function formatCacheEntry($entry) {
  76. return $entry;
  77. }
  78. /**
  79. * get the stored metadata of a file or folder
  80. *
  81. * @param string|int $file
  82. * @return ICacheEntry|false
  83. */
  84. public function get($file) {
  85. $result = $this->getCache()->get($file);
  86. if ($result instanceof ICacheEntry) {
  87. $result = $this->formatCacheEntry($result);
  88. }
  89. return $result;
  90. }
  91. /**
  92. * get the metadata of all files stored in $folder
  93. *
  94. * @param string $folder
  95. * @return ICacheEntry[]
  96. */
  97. public function getFolderContents($folder) {
  98. // can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this
  99. // and not the wrapped cache
  100. $fileId = $this->getId($folder);
  101. return $this->getFolderContentsById($fileId);
  102. }
  103. /**
  104. * get the metadata of all files stored in $folder
  105. *
  106. * @param int $fileId the file id of the folder
  107. * @return array
  108. */
  109. public function getFolderContentsById($fileId) {
  110. $results = $this->getCache()->getFolderContentsById($fileId);
  111. return array_map([$this, 'formatCacheEntry'], $results);
  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. * insert meta data for a new file or folder
  132. *
  133. * @param string $file
  134. * @param array $data
  135. *
  136. * @return int file id
  137. * @throws \RuntimeException
  138. */
  139. public function insert($file, array $data) {
  140. return $this->getCache()->insert($file, $data);
  141. }
  142. /**
  143. * update the metadata in the cache
  144. *
  145. * @param int $id
  146. * @param array $data
  147. */
  148. public function update($id, array $data) {
  149. $this->getCache()->update($id, $data);
  150. }
  151. /**
  152. * get the file id for a file
  153. *
  154. * @param string $file
  155. * @return int
  156. */
  157. public function getId($file) {
  158. return $this->getCache()->getId($file);
  159. }
  160. /**
  161. * get the id of the parent folder of a file
  162. *
  163. * @param string $file
  164. * @return int
  165. */
  166. public function getParentId($file) {
  167. return $this->getCache()->getParentId($file);
  168. }
  169. /**
  170. * check if a file is available in the cache
  171. *
  172. * @param string $file
  173. * @return bool
  174. */
  175. public function inCache($file) {
  176. return $this->getCache()->inCache($file);
  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. * Move a file or folder in the cache
  188. *
  189. * @param string $source
  190. * @param string $target
  191. */
  192. public function move($source, $target) {
  193. $this->getCache()->move($source, $target);
  194. }
  195. protected function getMoveInfo($path) {
  196. /** @var Cache $cache */
  197. $cache = $this->getCache();
  198. return $cache->getMoveInfo($path);
  199. }
  200. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
  201. $this->getCache()->moveFromCache($sourceCache, $sourcePath, $targetPath);
  202. }
  203. /**
  204. * remove all entries for files that are stored on the storage from the cache
  205. */
  206. public function clear() {
  207. $this->getCache()->clear();
  208. }
  209. /**
  210. * @param string $file
  211. *
  212. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  213. */
  214. public function getStatus($file) {
  215. return $this->getCache()->getStatus($file);
  216. }
  217. public function searchQuery(ISearchQuery $searchQuery) {
  218. return current($this->querySearchHelper->searchInCaches($searchQuery, [$this]));
  219. }
  220. /**
  221. * update the folder size and the size of all parent folders
  222. *
  223. * @param string|boolean $path
  224. * @param array $data (optional) meta data of the folder
  225. */
  226. public function correctFolderSize($path, $data = null, $isBackgroundScan = false) {
  227. if ($this->getCache() instanceof Cache) {
  228. $this->getCache()->correctFolderSize($path, $data, $isBackgroundScan);
  229. }
  230. }
  231. /**
  232. * get the size of a folder and set it in the cache
  233. *
  234. * @param string $path
  235. * @param array|null|ICacheEntry $entry (optional) meta data of the folder
  236. * @return int|float
  237. */
  238. public function calculateFolderSize($path, $entry = null) {
  239. if ($this->getCache() instanceof Cache) {
  240. return $this->getCache()->calculateFolderSize($path, $entry);
  241. } else {
  242. return 0;
  243. }
  244. }
  245. /**
  246. * get all file ids on the files on the storage
  247. *
  248. * @return int[]
  249. */
  250. public function getAll() {
  251. return $this->getCache()->getAll();
  252. }
  253. /**
  254. * find a folder in the cache which has not been fully scanned
  255. *
  256. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  257. * use the one with the highest id gives the best result with the background scanner, since that is most
  258. * likely the folder where we stopped scanning previously
  259. *
  260. * @return string|false the path of the folder or false when no folder matched
  261. */
  262. public function getIncomplete() {
  263. return $this->getCache()->getIncomplete();
  264. }
  265. /**
  266. * get the path of a file on this storage by it's id
  267. *
  268. * @param int $id
  269. * @return string|null
  270. */
  271. public function getPathById($id) {
  272. return $this->getCache()->getPathById($id);
  273. }
  274. /**
  275. * Returns the numeric storage id
  276. *
  277. * @return int
  278. */
  279. public function getNumericStorageId() {
  280. return $this->getCache()->getNumericStorageId();
  281. }
  282. /**
  283. * get the storage id of the storage for a file and the internal path of the file
  284. * unlike getPathById this does not limit the search to files on this storage and
  285. * instead does a global search in the cache table
  286. *
  287. * @param int $id
  288. * @return array first element holding the storage id, second the path
  289. */
  290. public static function getById($id) {
  291. return parent::getById($id);
  292. }
  293. public function getQueryFilterForStorage(): ISearchOperator {
  294. return $this->getCache()->getQueryFilterForStorage();
  295. }
  296. public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
  297. $rawEntry = $this->getCache()->getCacheEntryFromSearchResult($rawEntry);
  298. if ($rawEntry) {
  299. $entry = $this->formatCacheEntry(clone $rawEntry);
  300. return $entry ?: null;
  301. }
  302. return null;
  303. }
  304. }