1
0

CacheWrapper.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Jagszent <daniel@jagszent.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Files\Cache\Wrapper;
  28. use OC\Files\Cache\Cache;
  29. use OCP\Files\Cache\ICacheEntry;
  30. use OCP\Files\Cache\ICache;
  31. use OCP\Files\Search\ISearchQuery;
  32. class CacheWrapper extends Cache {
  33. /**
  34. * @var \OCP\Files\Cache\ICache
  35. */
  36. protected $cache;
  37. /**
  38. * @param \OCP\Files\Cache\ICache $cache
  39. */
  40. public function __construct($cache) {
  41. $this->cache = $cache;
  42. }
  43. protected function getCache() {
  44. return $this->cache;
  45. }
  46. /**
  47. * Make it easy for wrappers to modify every returned cache entry
  48. *
  49. * @param ICacheEntry $entry
  50. * @return ICacheEntry
  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) {
  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(array($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. /**
  195. * search for files matching $pattern
  196. *
  197. * @param string $pattern
  198. * @return ICacheEntry[] an array of file data
  199. */
  200. public function search($pattern) {
  201. $results = $this->getCache()->search($pattern);
  202. return array_map(array($this, 'formatCacheEntry'), $results);
  203. }
  204. /**
  205. * search for files by mimetype
  206. *
  207. * @param string $mimetype
  208. * @return ICacheEntry[]
  209. */
  210. public function searchByMime($mimetype) {
  211. $results = $this->getCache()->searchByMime($mimetype);
  212. return array_map(array($this, 'formatCacheEntry'), $results);
  213. }
  214. public function searchQuery(ISearchQuery $query) {
  215. $results = $this->getCache()->searchQuery($query);
  216. return array_map(array($this, 'formatCacheEntry'), $results);
  217. }
  218. /**
  219. * search for files by tag
  220. *
  221. * @param string|int $tag name or tag id
  222. * @param string $userId owner of the tags
  223. * @return ICacheEntry[] file data
  224. */
  225. public function searchByTag($tag, $userId) {
  226. $results = $this->getCache()->searchByTag($tag, $userId);
  227. return array_map(array($this, 'formatCacheEntry'), $results);
  228. }
  229. /**
  230. * update the folder size and the size of all parent folders
  231. *
  232. * @param string|boolean $path
  233. * @param array $data (optional) meta data of the folder
  234. */
  235. public function correctFolderSize($path, $data = null) {
  236. if ($this->getCache() instanceof Cache) {
  237. $this->getCache()->correctFolderSize($path, $data);
  238. }
  239. }
  240. /**
  241. * get the size of a folder and set it in the cache
  242. *
  243. * @param string $path
  244. * @param array $entry (optional) meta data of the folder
  245. * @return int
  246. */
  247. public function calculateFolderSize($path, $entry = null) {
  248. if ($this->getCache() instanceof Cache) {
  249. return $this->getCache()->calculateFolderSize($path, $entry);
  250. } else {
  251. return 0;
  252. }
  253. }
  254. /**
  255. * get all file ids on the files on the storage
  256. *
  257. * @return int[]
  258. */
  259. public function getAll() {
  260. return $this->getCache()->getAll();
  261. }
  262. /**
  263. * find a folder in the cache which has not been fully scanned
  264. *
  265. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  266. * use the one with the highest id gives the best result with the background scanner, since that is most
  267. * likely the folder where we stopped scanning previously
  268. *
  269. * @return string|bool the path of the folder or false when no folder matched
  270. */
  271. public function getIncomplete() {
  272. return $this->getCache()->getIncomplete();
  273. }
  274. /**
  275. * get the path of a file on this storage by it's id
  276. *
  277. * @param int $id
  278. * @return string|null
  279. */
  280. public function getPathById($id) {
  281. return $this->getCache()->getPathById($id);
  282. }
  283. /**
  284. * Returns the numeric storage id
  285. *
  286. * @return int
  287. */
  288. public function getNumericStorageId() {
  289. return $this->getCache()->getNumericStorageId();
  290. }
  291. /**
  292. * get the storage id of the storage for a file and the internal path of the file
  293. * unlike getPathById this does not limit the search to files on this storage and
  294. * instead does a global search in the cache table
  295. *
  296. * @param int $id
  297. * @return array first element holding the storage id, second the path
  298. */
  299. static public function getById($id) {
  300. return parent::getById($id);
  301. }
  302. }