ICache.php 8.5 KB

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