1
0

CacheWrapper.php 8.4 KB

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