CacheWrapper.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 Stefan Weil <sw@weilnetz.de>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Files\Cache\Wrapper;
  29. use OC\Files\Cache\Cache;
  30. use OCP\Files\Cache\ICacheEntry;
  31. use OCP\Files\Cache\ICache;
  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. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
  173. $this->getCache()->moveFromCache($sourceCache, $sourcePath, $targetPath);
  174. }
  175. /**
  176. * remove all entries for files that are stored on the storage from the cache
  177. */
  178. public function clear() {
  179. $this->getCache()->clear();
  180. }
  181. /**
  182. * @param string $file
  183. *
  184. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  185. */
  186. public function getStatus($file) {
  187. return $this->getCache()->getStatus($file);
  188. }
  189. /**
  190. * search for files matching $pattern
  191. *
  192. * @param string $pattern
  193. * @return ICacheEntry[] an array of file data
  194. */
  195. public function search($pattern) {
  196. $results = $this->getCache()->search($pattern);
  197. return array_map(array($this, 'formatCacheEntry'), $results);
  198. }
  199. /**
  200. * search for files by mimetype
  201. *
  202. * @param string $mimetype
  203. * @return ICacheEntry[]
  204. */
  205. public function searchByMime($mimetype) {
  206. $results = $this->getCache()->searchByMime($mimetype);
  207. return array_map(array($this, 'formatCacheEntry'), $results);
  208. }
  209. /**
  210. * search for files by tag
  211. *
  212. * @param string|int $tag name or tag id
  213. * @param string $userId owner of the tags
  214. * @return ICacheEntry[] file data
  215. */
  216. public function searchByTag($tag, $userId) {
  217. $results = $this->getCache()->searchByTag($tag, $userId);
  218. return array_map(array($this, 'formatCacheEntry'), $results);
  219. }
  220. /**
  221. * update the folder size and the size of all parent folders
  222. *
  223. * @param string|boolean $path
  224. * @param array $data (optional) meta data of the folder
  225. */
  226. public function correctFolderSize($path, $data = null) {
  227. if ($this->getCache() instanceof Cache) {
  228. $this->getCache()->correctFolderSize($path, $data);
  229. }
  230. }
  231. /**
  232. * get the size of a folder and set it in the cache
  233. *
  234. * @param string $path
  235. * @param array $entry (optional) meta data of the folder
  236. * @return int
  237. */
  238. public function calculateFolderSize($path, $entry = null) {
  239. if ($this->getCache() instanceof Cache) {
  240. return $this->getCache()->calculateFolderSize($path, $entry);
  241. } else {
  242. return 0;
  243. }
  244. }
  245. /**
  246. * get all file ids on the files on the storage
  247. *
  248. * @return int[]
  249. */
  250. public function getAll() {
  251. return $this->getCache()->getAll();
  252. }
  253. /**
  254. * find a folder in the cache which has not been fully scanned
  255. *
  256. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  257. * use the one with the highest id gives the best result with the background scanner, since that is most
  258. * likely the folder where we stopped scanning previously
  259. *
  260. * @return string|bool the path of the folder or false when no folder matched
  261. */
  262. public function getIncomplete() {
  263. return $this->getCache()->getIncomplete();
  264. }
  265. /**
  266. * get the path of a file on this storage by it's id
  267. *
  268. * @param int $id
  269. * @return string|null
  270. */
  271. public function getPathById($id) {
  272. return $this->getCache()->getPathById($id);
  273. }
  274. /**
  275. * Returns the numeric storage id
  276. *
  277. * @return int
  278. */
  279. public function getNumericStorageId() {
  280. return $this->getCache()->getNumericStorageId();
  281. }
  282. /**
  283. * get the storage id of the storage for a file and the internal path of the file
  284. * unlike getPathById this does not limit the search to files on this storage and
  285. * instead does a global search in the cache table
  286. *
  287. * @param int $id
  288. * @return array first element holding the storage id, second the path
  289. */
  290. static public function getById($id) {
  291. return parent::getById($id);
  292. }
  293. }