CacheWrapper.php 7.9 KB

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