ICache.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCP\Files\Cache;
  24. use OCP\Files\Search\ISearchOperator;
  25. use OCP\Files\Search\ISearchQuery;
  26. /**
  27. * Metadata cache for a storage
  28. *
  29. * The cache stores the metadata for all files and folders in a storage and is kept up to date through the following mechanisms:
  30. *
  31. * - Scanner: scans the storage and updates the cache where needed
  32. * - Watcher: checks for changes made to the filesystem outside of the Nextcloud instance and rescans files and folder when a change is detected
  33. * - Updater: listens to changes made to the filesystem inside of the Nextcloud instance and updates the cache where needed
  34. * - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater
  35. *
  36. * @since 9.0.0
  37. */
  38. interface ICache {
  39. /**
  40. * @since 9.0.0
  41. */
  42. public const NOT_FOUND = 0;
  43. /**
  44. * @since 9.0.0
  45. */
  46. public const PARTIAL = 1; //only partial data available, file not cached in the database
  47. /**
  48. * @since 9.0.0
  49. */
  50. public const SHALLOW = 2; //folder in cache, but not all child files are completely scanned
  51. /**
  52. * @since 9.0.0
  53. */
  54. public const COMPLETE = 3;
  55. /**
  56. * Get the numeric storage id for this cache's storage
  57. *
  58. * @return int
  59. * @since 9.0.0
  60. */
  61. public function getNumericStorageId();
  62. /**
  63. * get the stored metadata of a file or folder
  64. *
  65. * @param string | int $file either the path of a file or folder or the file id for a file or folder
  66. * @return ICacheEntry|false the cache entry or false if the file is not found in the cache
  67. * @since 9.0.0
  68. */
  69. public function get($file);
  70. /**
  71. * get the metadata of all files stored in $folder
  72. *
  73. * Only returns files one level deep, no recursion
  74. *
  75. * @param string $folder
  76. * @return ICacheEntry[]
  77. * @since 9.0.0
  78. */
  79. public function getFolderContents($folder);
  80. /**
  81. * get the metadata of all files stored in $folder
  82. *
  83. * Only returns files one level deep, no recursion
  84. *
  85. * @param int $fileId the file id of the folder
  86. * @return ICacheEntry[]
  87. * @since 9.0.0
  88. */
  89. public function getFolderContentsById($fileId);
  90. /**
  91. * store meta data for a file or folder
  92. * This will automatically call either insert or update depending on if the file exists
  93. *
  94. * @param string $file
  95. * @param array $data
  96. *
  97. * @return int file id
  98. * @throws \RuntimeException
  99. * @since 9.0.0
  100. */
  101. public function put($file, array $data);
  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. * @since 9.0.0
  111. */
  112. public function insert($file, array $data);
  113. /**
  114. * update the metadata of an existing file or folder in the cache
  115. *
  116. * @param int $id the fileid of the existing file or folder
  117. * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged
  118. * @since 9.0.0
  119. */
  120. public function update($id, array $data);
  121. /**
  122. * get the file id for a file
  123. *
  124. * A file id is a numeric id for a file or folder that's unique within an Nextcloud instance which stays the same for the lifetime of a file
  125. *
  126. * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing
  127. *
  128. * @param string $file
  129. * @return int
  130. * @since 9.0.0
  131. */
  132. public function getId($file);
  133. /**
  134. * get the id of the parent folder of a file
  135. *
  136. * @param string $file
  137. * @return int
  138. * @since 9.0.0
  139. */
  140. public function getParentId($file);
  141. /**
  142. * check if a file is available in the cache
  143. *
  144. * @param string $file
  145. * @return bool
  146. * @since 9.0.0
  147. */
  148. public function inCache($file);
  149. /**
  150. * remove a file or folder from the cache
  151. *
  152. * when removing a folder from the cache all files and folders inside the folder will be removed as well
  153. *
  154. * @param string $file
  155. * @since 9.0.0
  156. */
  157. public function remove($file);
  158. /**
  159. * Move a file or folder in the cache
  160. *
  161. * @param string $source
  162. * @param string $target
  163. * @since 9.0.0
  164. */
  165. public function move($source, $target);
  166. /**
  167. * Move a file or folder in the cache
  168. *
  169. * Note that this should make sure the entries are removed from the source cache
  170. *
  171. * @param \OCP\Files\Cache\ICache $sourceCache
  172. * @param string $sourcePath
  173. * @param string $targetPath
  174. * @throws \OC\DatabaseException
  175. * @since 9.0.0
  176. */
  177. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath);
  178. /**
  179. * Copy a file or folder in the cache
  180. *
  181. * @param ICache $sourceCache
  182. * @param ICacheEntry $sourceEntry
  183. * @param string $targetPath
  184. * @return int fileid of copied entry
  185. * @since 22.0.0
  186. */
  187. public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int;
  188. /**
  189. * Get the scan status of a file
  190. *
  191. * - ICache::NOT_FOUND: File is not in the cache
  192. * - ICache::PARTIAL: File is not stored in the cache but some incomplete data is known
  193. * - ICache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned
  194. * - ICache::COMPLETE: The file or folder, with all it's children) are fully scanned
  195. *
  196. * @param string $file
  197. *
  198. * @return int ICache::NOT_FOUND, ICache::PARTIAL, ICache::SHALLOW or ICache::COMPLETE
  199. * @since 9.0.0
  200. */
  201. public function getStatus($file);
  202. /**
  203. * search for files matching $pattern, files are matched if their filename matches the search pattern
  204. *
  205. * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
  206. * @return ICacheEntry[] an array of cache entries where the name matches the search pattern
  207. * @since 9.0.0
  208. * @deprecated 9.0.0 due to lack of pagination, not all backends might implement this
  209. */
  210. public function search($pattern);
  211. /**
  212. * search for files by mimetype
  213. *
  214. * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
  215. * where it will search for all mimetypes in the group ('image/*')
  216. * @return ICacheEntry[] an array of cache entries where the mimetype matches the search
  217. * @since 9.0.0
  218. * @deprecated 9.0.0 due to lack of pagination, not all backends might implement this
  219. */
  220. public function searchByMime($mimetype);
  221. /**
  222. * Search for files with a flexible query
  223. *
  224. * @param ISearchQuery $query
  225. * @return ICacheEntry[]
  226. * @throw \InvalidArgumentException if the cache is unable to perform the query
  227. * @since 12.0.0
  228. */
  229. public function searchQuery(ISearchQuery $query);
  230. /**
  231. * find a folder in the cache which has not been fully scanned
  232. *
  233. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  234. * use the one with the highest id gives the best result with the background scanner, since that is most
  235. * likely the folder where we stopped scanning previously
  236. *
  237. * @return string|false the path of the folder or false when no folder matched
  238. * @since 9.0.0
  239. */
  240. public function getIncomplete();
  241. /**
  242. * get the path of a file on this storage by it's file id
  243. *
  244. * @param int $id the file id of the file or folder to search
  245. * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache
  246. * @since 9.0.0
  247. */
  248. public function getPathById($id);
  249. /**
  250. * normalize the given path for usage in the cache
  251. *
  252. * @param string $path
  253. * @return string
  254. * @since 9.0.0
  255. */
  256. public function normalize($path);
  257. /**
  258. * Get the query expression required to filter files within this storage.
  259. *
  260. * In the most basic case this is just comparing the storage id
  261. * but storage wrappers can add additional expressions to filter down things further
  262. *
  263. * @return ISearchOperator
  264. * @since 22.0.0
  265. */
  266. public function getQueryFilterForStorage(): ISearchOperator;
  267. /**
  268. * Construct a cache entry from a search result row *if* the entry belongs to this storage.
  269. *
  270. * This method will be called for every item in the search results, including results from different storages.
  271. * It's the responsibility of this method to return `null` for all results that don't belong to this storage.
  272. *
  273. * Additionally some implementations might need to further process the resulting entry such as modifying the path
  274. * or permissions of the result.
  275. *
  276. * @param ICacheEntry $rawEntry
  277. * @return ICacheEntry|null
  278. * @since 22.0.0
  279. */
  280. public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry;
  281. }