1
0

CacheWrapper.php 8.1 KB

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