ICache.php 8.0 KB

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