CacheWrapper.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Cache\Wrapper;
  8. use OC\Files\Cache\Cache;
  9. use OC\Files\Cache\CacheDependencies;
  10. use OCP\Files\Cache\ICache;
  11. use OCP\Files\Cache\ICacheEntry;
  12. use OCP\Files\Search\ISearchOperator;
  13. use OCP\Files\Search\ISearchQuery;
  14. use OCP\Server;
  15. class CacheWrapper extends Cache {
  16. /**
  17. * @var ?ICache
  18. */
  19. protected $cache;
  20. public function __construct(?ICache $cache, ?CacheDependencies $dependencies = null) {
  21. $this->cache = $cache;
  22. if (!$dependencies && $cache instanceof Cache) {
  23. $this->mimetypeLoader = $cache->mimetypeLoader;
  24. $this->connection = $cache->connection;
  25. $this->querySearchHelper = $cache->querySearchHelper;
  26. } else {
  27. if (!$dependencies) {
  28. $dependencies = Server::get(CacheDependencies::class);
  29. }
  30. $this->mimetypeLoader = $dependencies->getMimeTypeLoader();
  31. $this->connection = $dependencies->getConnection();
  32. $this->querySearchHelper = $dependencies->getQuerySearchHelper();
  33. }
  34. }
  35. protected function getCache() {
  36. return $this->cache;
  37. }
  38. protected function hasEncryptionWrapper(): bool {
  39. $cache = $this->getCache();
  40. if ($cache instanceof Cache) {
  41. return $cache->hasEncryptionWrapper();
  42. } else {
  43. return false;
  44. }
  45. }
  46. /**
  47. * Make it easy for wrappers to modify every returned cache entry
  48. *
  49. * @param ICacheEntry $entry
  50. * @return ICacheEntry|false
  51. */
  52. protected function formatCacheEntry($entry) {
  53. return $entry;
  54. }
  55. /**
  56. * get the stored metadata of a file or folder
  57. *
  58. * @param string|int $file
  59. * @return ICacheEntry|false
  60. */
  61. public function get($file) {
  62. $result = $this->getCache()->get($file);
  63. if ($result instanceof ICacheEntry) {
  64. $result = $this->formatCacheEntry($result);
  65. }
  66. return $result;
  67. }
  68. /**
  69. * get the metadata of all files stored in $folder
  70. *
  71. * @param string $folder
  72. * @return ICacheEntry[]
  73. */
  74. public function getFolderContents($folder) {
  75. // can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this
  76. // and not the wrapped cache
  77. $fileId = $this->getId($folder);
  78. return $this->getFolderContentsById($fileId);
  79. }
  80. /**
  81. * get the metadata of all files stored in $folder
  82. *
  83. * @param int $fileId the file id of the folder
  84. * @return array
  85. */
  86. public function getFolderContentsById($fileId) {
  87. $results = $this->getCache()->getFolderContentsById($fileId);
  88. return array_map([$this, 'formatCacheEntry'], $results);
  89. }
  90. /**
  91. * insert or update meta data for a file or folder
  92. *
  93. * @param string $file
  94. * @param array $data
  95. *
  96. * @return int file id
  97. * @throws \RuntimeException
  98. */
  99. public function put($file, array $data) {
  100. if (($id = $this->getId($file)) > -1) {
  101. $this->update($id, $data);
  102. return $id;
  103. } else {
  104. return $this->insert($file, $data);
  105. }
  106. }
  107. /**
  108. * insert meta data for a new 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 insert($file, array $data) {
  117. return $this->getCache()->insert($file, $data);
  118. }
  119. /**
  120. * update the metadata in the cache
  121. *
  122. * @param int $id
  123. * @param array $data
  124. */
  125. public function update($id, array $data) {
  126. $this->getCache()->update($id, $data);
  127. }
  128. /**
  129. * get the file id for a file
  130. *
  131. * @param string $file
  132. * @return int
  133. */
  134. public function getId($file) {
  135. return $this->getCache()->getId($file);
  136. }
  137. /**
  138. * get the id of the parent folder of a file
  139. *
  140. * @param string $file
  141. * @return int
  142. */
  143. public function getParentId($file) {
  144. return $this->getCache()->getParentId($file);
  145. }
  146. /**
  147. * check if a file is available in the cache
  148. *
  149. * @param string $file
  150. * @return bool
  151. */
  152. public function inCache($file) {
  153. return $this->getCache()->inCache($file);
  154. }
  155. /**
  156. * remove a file or folder from the cache
  157. *
  158. * @param string $file
  159. */
  160. public function remove($file) {
  161. $this->getCache()->remove($file);
  162. }
  163. /**
  164. * Move a file or folder in the cache
  165. *
  166. * @param string $source
  167. * @param string $target
  168. */
  169. public function move($source, $target) {
  170. $this->getCache()->move($source, $target);
  171. }
  172. protected function getMoveInfo($path) {
  173. /** @var Cache $cache */
  174. $cache = $this->getCache();
  175. return $cache->getMoveInfo($path);
  176. }
  177. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
  178. $this->getCache()->moveFromCache($sourceCache, $sourcePath, $targetPath);
  179. }
  180. /**
  181. * remove all entries for files that are stored on the storage from the cache
  182. */
  183. public function clear() {
  184. $this->getCache()->clear();
  185. }
  186. /**
  187. * @param string $file
  188. *
  189. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  190. */
  191. public function getStatus($file) {
  192. return $this->getCache()->getStatus($file);
  193. }
  194. public function searchQuery(ISearchQuery $query) {
  195. return current($this->querySearchHelper->searchInCaches($query, [$this]));
  196. }
  197. /**
  198. * update the folder size and the size of all parent folders
  199. *
  200. * @param string|boolean $path
  201. * @param array $data (optional) meta data of the folder
  202. */
  203. public function correctFolderSize($path, $data = null, $isBackgroundScan = false) {
  204. if ($this->getCache() instanceof Cache) {
  205. $this->getCache()->correctFolderSize($path, $data, $isBackgroundScan);
  206. }
  207. }
  208. /**
  209. * get the size of a folder and set it in the cache
  210. *
  211. * @param string $path
  212. * @param array|null|ICacheEntry $entry (optional) meta data of the folder
  213. * @return int|float
  214. */
  215. public function calculateFolderSize($path, $entry = null) {
  216. if ($this->getCache() instanceof Cache) {
  217. return $this->getCache()->calculateFolderSize($path, $entry);
  218. } else {
  219. return 0;
  220. }
  221. }
  222. /**
  223. * get all file ids on the files on the storage
  224. *
  225. * @return int[]
  226. */
  227. public function getAll() {
  228. return $this->getCache()->getAll();
  229. }
  230. /**
  231. * find a folder in the cache which has not been fully scanned
  232. *
  233. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  234. * use the one with the highest id gives the best result with the background scanner, since that is most
  235. * likely the folder where we stopped scanning previously
  236. *
  237. * @return string|false the path of the folder or false when no folder matched
  238. */
  239. public function getIncomplete() {
  240. return $this->getCache()->getIncomplete();
  241. }
  242. /**
  243. * get the path of a file on this storage by it's id
  244. *
  245. * @param int $id
  246. * @return string|null
  247. */
  248. public function getPathById($id) {
  249. return $this->getCache()->getPathById($id);
  250. }
  251. /**
  252. * Returns the numeric storage id
  253. *
  254. * @return int
  255. */
  256. public function getNumericStorageId() {
  257. return $this->getCache()->getNumericStorageId();
  258. }
  259. /**
  260. * get the storage id of the storage for a file and the internal path of the file
  261. * unlike getPathById this does not limit the search to files on this storage and
  262. * instead does a global search in the cache table
  263. *
  264. * @param int $id
  265. * @return array first element holding the storage id, second the path
  266. */
  267. public static function getById($id) {
  268. return parent::getById($id);
  269. }
  270. public function getQueryFilterForStorage(): ISearchOperator {
  271. return $this->getCache()->getQueryFilterForStorage();
  272. }
  273. public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
  274. $rawEntry = $this->getCache()->getCacheEntryFromSearchResult($rawEntry);
  275. if ($rawEntry) {
  276. $entry = $this->formatCacheEntry(clone $rawEntry);
  277. return $entry ?: null;
  278. }
  279. return null;
  280. }
  281. }