cachewrapper.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Daniel Jagszent <daniel@jagszent.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Files\Cache\Wrapper;
  27. use OC\Files\Cache\Cache;
  28. use OCP\Files\Cache\ICacheEntry;
  29. use OCP\Files\Cache\ICache;
  30. class CacheWrapper extends Cache {
  31. /**
  32. * @var \OCP\Files\Cache\ICache
  33. */
  34. protected $cache;
  35. /**
  36. * @param \OCP\Files\Cache\ICache $cache
  37. */
  38. public function __construct($cache) {
  39. $this->cache = $cache;
  40. }
  41. /**
  42. * Make it easy for wrappers to modify every returned cache entry
  43. *
  44. * @param ICacheEntry $entry
  45. * @return ICacheEntry
  46. */
  47. protected function formatCacheEntry($entry) {
  48. return $entry;
  49. }
  50. /**
  51. * get the stored metadata of a file or folder
  52. *
  53. * @param string /int $file
  54. * @return ICacheEntry|false
  55. */
  56. public function get($file) {
  57. $result = $this->cache->get($file);
  58. if ($result) {
  59. $result = $this->formatCacheEntry($result);
  60. }
  61. return $result;
  62. }
  63. /**
  64. * get the metadata of all files stored in $folder
  65. *
  66. * @param string $folder
  67. * @return ICacheEntry[]
  68. */
  69. public function getFolderContents($folder) {
  70. // cant do a simple $this->cache->.... call here since getFolderContentsById needs to be called on this
  71. // and not the wrapped cache
  72. $fileId = $this->getId($folder);
  73. return $this->getFolderContentsById($fileId);
  74. }
  75. /**
  76. * get the metadata of all files stored in $folder
  77. *
  78. * @param int $fileId the file id of the folder
  79. * @return array
  80. */
  81. public function getFolderContentsById($fileId) {
  82. $results = $this->cache->getFolderContentsById($fileId);
  83. return array_map(array($this, 'formatCacheEntry'), $results);
  84. }
  85. /**
  86. * insert or update meta data for a file or folder
  87. *
  88. * @param string $file
  89. * @param array $data
  90. *
  91. * @return int file id
  92. * @throws \RuntimeException
  93. */
  94. public function put($file, array $data) {
  95. if (($id = $this->getId($file)) > -1) {
  96. $this->update($id, $data);
  97. return $id;
  98. } else {
  99. return $this->insert($file, $data);
  100. }
  101. }
  102. /**
  103. * insert meta data for a new file or folder
  104. *
  105. * @param string $file
  106. * @param array $data
  107. *
  108. * @return int file id
  109. * @throws \RuntimeException
  110. */
  111. public function insert($file, array $data) {
  112. return $this->cache->insert($file, $data);
  113. }
  114. /**
  115. * update the metadata in the cache
  116. *
  117. * @param int $id
  118. * @param array $data
  119. */
  120. public function update($id, array $data) {
  121. $this->cache->update($id, $data);
  122. }
  123. /**
  124. * get the file id for a file
  125. *
  126. * @param string $file
  127. * @return int
  128. */
  129. public function getId($file) {
  130. return $this->cache->getId($file);
  131. }
  132. /**
  133. * get the id of the parent folder of a file
  134. *
  135. * @param string $file
  136. * @return int
  137. */
  138. public function getParentId($file) {
  139. return $this->cache->getParentId($file);
  140. }
  141. /**
  142. * check if a file is available in the cache
  143. *
  144. * @param string $file
  145. * @return bool
  146. */
  147. public function inCache($file) {
  148. return $this->cache->inCache($file);
  149. }
  150. /**
  151. * remove a file or folder from the cache
  152. *
  153. * @param string $file
  154. */
  155. public function remove($file) {
  156. $this->cache->remove($file);
  157. }
  158. /**
  159. * Move a file or folder in the cache
  160. *
  161. * @param string $source
  162. * @param string $target
  163. */
  164. public function move($source, $target) {
  165. $this->cache->move($source, $target);
  166. }
  167. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
  168. $this->cache->moveFromCache($sourceCache, $sourcePath, $targetPath);
  169. }
  170. /**
  171. * remove all entries for files that are stored on the storage from the cache
  172. */
  173. public function clear() {
  174. $this->cache->clear();
  175. }
  176. /**
  177. * @param string $file
  178. *
  179. * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  180. */
  181. public function getStatus($file) {
  182. return $this->cache->getStatus($file);
  183. }
  184. /**
  185. * search for files matching $pattern
  186. *
  187. * @param string $pattern
  188. * @return ICacheEntry[] an array of file data
  189. */
  190. public function search($pattern) {
  191. $results = $this->cache->search($pattern);
  192. return array_map(array($this, 'formatCacheEntry'), $results);
  193. }
  194. /**
  195. * search for files by mimetype
  196. *
  197. * @param string $mimetype
  198. * @return ICacheEntry[]
  199. */
  200. public function searchByMime($mimetype) {
  201. $results = $this->cache->searchByMime($mimetype);
  202. return array_map(array($this, 'formatCacheEntry'), $results);
  203. }
  204. /**
  205. * search for files by tag
  206. *
  207. * @param string|int $tag name or tag id
  208. * @param string $userId owner of the tags
  209. * @return ICacheEntry[] file data
  210. */
  211. public function searchByTag($tag, $userId) {
  212. $results = $this->cache->searchByTag($tag, $userId);
  213. return array_map(array($this, 'formatCacheEntry'), $results);
  214. }
  215. /**
  216. * update the folder size and the size of all parent folders
  217. *
  218. * @param string|boolean $path
  219. * @param array $data (optional) meta data of the folder
  220. */
  221. public function correctFolderSize($path, $data = null) {
  222. if ($this->cache instanceof Cache) {
  223. $this->cache->correctFolderSize($path, $data);
  224. }
  225. }
  226. /**
  227. * get the size of a folder and set it in the cache
  228. *
  229. * @param string $path
  230. * @param array $entry (optional) meta data of the folder
  231. * @return int
  232. */
  233. public function calculateFolderSize($path, $entry = null) {
  234. if ($this->cache instanceof Cache) {
  235. return $this->cache->calculateFolderSize($path, $entry);
  236. } else {
  237. return 0;
  238. }
  239. }
  240. /**
  241. * get all file ids on the files on the storage
  242. *
  243. * @return int[]
  244. */
  245. public function getAll() {
  246. return $this->cache->getAll();
  247. }
  248. /**
  249. * find a folder in the cache which has not been fully scanned
  250. *
  251. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  252. * use the one with the highest id gives the best result with the background scanner, since that is most
  253. * likely the folder where we stopped scanning previously
  254. *
  255. * @return string|bool the path of the folder or false when no folder matched
  256. */
  257. public function getIncomplete() {
  258. return $this->cache->getIncomplete();
  259. }
  260. /**
  261. * get the path of a file on this storage by it's id
  262. *
  263. * @param int $id
  264. * @return string|null
  265. */
  266. public function getPathById($id) {
  267. return $this->cache->getPathById($id);
  268. }
  269. /**
  270. * Returns the numeric storage id
  271. *
  272. * @return int
  273. */
  274. public function getNumericStorageId() {
  275. return $this->cache->getNumericStorageId();
  276. }
  277. /**
  278. * get the storage id of the storage for a file and the internal path of the file
  279. * unlike getPathById this does not limit the search to files on this storage and
  280. * instead does a global search in the cache table
  281. *
  282. * @param int $id
  283. * @return array first element holding the storage id, second the path
  284. */
  285. static public function getById($id) {
  286. return parent::getById($id);
  287. }
  288. }