ICache.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. public const NOT_FOUND = 0;
  40. public const PARTIAL = 1; //only partial data available, file not cached in the database
  41. public const SHALLOW = 2; //folder in cache, but not all child files are completely scanned
  42. public const COMPLETE = 3;
  43. /**
  44. * Get the numeric storage id for this cache's storage
  45. *
  46. * @return int
  47. * @since 9.0.0
  48. */
  49. public function getNumericStorageId();
  50. /**
  51. * get the stored metadata of a file or folder
  52. *
  53. * @param string | int $file either the path of a file or folder or the file id for a file or folder
  54. * @return ICacheEntry|false the cache entry or false if the file is not found in the cache
  55. * @since 9.0.0
  56. */
  57. public function get($file);
  58. /**
  59. * get the metadata of all files stored in $folder
  60. *
  61. * Only returns files one level deep, no recursion
  62. *
  63. * @param string $folder
  64. * @return ICacheEntry[]
  65. * @since 9.0.0
  66. */
  67. public function getFolderContents($folder);
  68. /**
  69. * get the metadata of all files stored in $folder
  70. *
  71. * Only returns files one level deep, no recursion
  72. *
  73. * @param int $fileId the file id of the folder
  74. * @return ICacheEntry[]
  75. * @since 9.0.0
  76. */
  77. public function getFolderContentsById($fileId);
  78. /**
  79. * store meta data for a file or folder
  80. * This will automatically call either insert or update depending on if the file exists
  81. *
  82. * @param string $file
  83. * @param array $data
  84. *
  85. * @return int file id
  86. * @throws \RuntimeException
  87. * @since 9.0.0
  88. */
  89. public function put($file, array $data);
  90. /**
  91. * insert meta data for a new file or folder
  92. *
  93. * @param string $file
  94. * @param array $data
  95. *
  96. * @return int file id
  97. * @throws \RuntimeException
  98. * @since 9.0.0
  99. */
  100. public function insert($file, array $data);
  101. /**
  102. * update the metadata of an existing file or folder in the cache
  103. *
  104. * @param int $id the fileid of the existing file or folder
  105. * @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
  106. * @since 9.0.0
  107. */
  108. public function update($id, array $data);
  109. /**
  110. * get the file id for a file
  111. *
  112. * 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
  113. *
  114. * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing
  115. *
  116. * @param string $file
  117. * @return int
  118. * @since 9.0.0
  119. */
  120. public function getId($file);
  121. /**
  122. * get the id of the parent folder of a file
  123. *
  124. * @param string $file
  125. * @return int
  126. * @since 9.0.0
  127. */
  128. public function getParentId($file);
  129. /**
  130. * check if a file is available in the cache
  131. *
  132. * @param string $file
  133. * @return bool
  134. * @since 9.0.0
  135. */
  136. public function inCache($file);
  137. /**
  138. * remove a file or folder from the cache
  139. *
  140. * when removing a folder from the cache all files and folders inside the folder will be removed as well
  141. *
  142. * @param string $file
  143. * @since 9.0.0
  144. */
  145. public function remove($file);
  146. /**
  147. * Move a file or folder in the cache
  148. *
  149. * @param string $source
  150. * @param string $target
  151. * @since 9.0.0
  152. */
  153. public function move($source, $target);
  154. /**
  155. * Move a file or folder in the cache
  156. *
  157. * Note that this should make sure the entries are removed from the source cache
  158. *
  159. * @param \OCP\Files\Cache\ICache $sourceCache
  160. * @param string $sourcePath
  161. * @param string $targetPath
  162. * @throws \OC\DatabaseException
  163. * @since 9.0.0
  164. */
  165. public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath);
  166. /**
  167. * Copy a file or folder in the cache
  168. *
  169. * @param ICache $sourceCache
  170. * @param ICacheEntry $sourceEntry
  171. * @param string $targetPath
  172. * @return int fileid of copied entry
  173. * @since 22.0.0
  174. */
  175. public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int;
  176. /**
  177. * Get the scan status of a file
  178. *
  179. * - ICache::NOT_FOUND: File is not in the cache
  180. * - ICache::PARTIAL: File is not stored in the cache but some incomplete data is known
  181. * - ICache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned
  182. * - ICache::COMPLETE: The file or folder, with all it's children) are fully scanned
  183. *
  184. * @param string $file
  185. *
  186. * @return int ICache::NOT_FOUND, ICache::PARTIAL, ICache::SHALLOW or ICache::COMPLETE
  187. * @since 9.0.0
  188. */
  189. public function getStatus($file);
  190. /**
  191. * search for files matching $pattern, files are matched if their filename matches the search pattern
  192. *
  193. * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
  194. * @return ICacheEntry[] an array of cache entries where the name matches the search pattern
  195. * @since 9.0.0
  196. * @deprecated 9.0.0 due to lack of pagination, not all backends might implement this
  197. */
  198. public function search($pattern);
  199. /**
  200. * search for files by mimetype
  201. *
  202. * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
  203. * where it will search for all mimetypes in the group ('image/*')
  204. * @return ICacheEntry[] an array of cache entries where the mimetype matches the search
  205. * @since 9.0.0
  206. * @deprecated 9.0.0 due to lack of pagination, not all backends might implement this
  207. */
  208. public function searchByMime($mimetype);
  209. /**
  210. * Search for files with a flexible query
  211. *
  212. * @param ISearchQuery $query
  213. * @return ICacheEntry[]
  214. * @throw \InvalidArgumentException if the cache is unable to perform the query
  215. * @since 12.0.0
  216. */
  217. public function searchQuery(ISearchQuery $query);
  218. /**
  219. * find a folder in the cache which has not been fully scanned
  220. *
  221. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  222. * use the one with the highest id gives the best result with the background scanner, since that is most
  223. * likely the folder where we stopped scanning previously
  224. *
  225. * @return string|false the path of the folder or false when no folder matched
  226. * @since 9.0.0
  227. */
  228. public function getIncomplete();
  229. /**
  230. * get the path of a file on this storage by it's file id
  231. *
  232. * @param int $id the file id of the file or folder to search
  233. * @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
  234. * @since 9.0.0
  235. */
  236. public function getPathById($id);
  237. /**
  238. * normalize the given path for usage in the cache
  239. *
  240. * @param string $path
  241. * @return string
  242. * @since 9.0.0
  243. */
  244. public function normalize($path);
  245. /**
  246. * Get the query expression required to filter files within this storage.
  247. *
  248. * In the most basic case this is just comparing the storage id
  249. * but storage wrappers can add additional expressions to filter down things further
  250. *
  251. * @return ISearchOperator
  252. * @since 22.0.0
  253. */
  254. public function getQueryFilterForStorage(): ISearchOperator;
  255. /**
  256. * Construct a cache entry from a search result row *if* the entry belongs to this storage.
  257. *
  258. * This method will be called for every item in the search results, including results from different storages.
  259. * It's the responsibility of this method to return `null` for all results that don't belong to this storage.
  260. *
  261. * Additionally some implementations might need to further process the resulting entry such as modifying the path
  262. * or permissions of the result.
  263. *
  264. * @param ICacheEntry $rawEntry
  265. * @return ICacheEntry|null
  266. * @since 22.0.0
  267. */
  268. public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry;
  269. }