ICache.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCP\Files\Cache;
  23. use OCP\Files\Search\ISearchOperator;
  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. const NOT_FOUND = 0;
  39. const PARTIAL = 1; //only partial data available, file not cached in the database
  40. const SHALLOW = 2; //folder in cache, but not all child files are completely scanned
  41. 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. * Get the scan status of a file
  167. *
  168. * - ICache::NOT_FOUND: File is not in the cache
  169. * - ICache::PARTIAL: File is not stored in the cache but some incomplete data is known
  170. * - ICache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned
  171. * - ICache::COMPLETE: The file or folder, with all it's children) are fully scanned
  172. *
  173. * @param string $file
  174. *
  175. * @return int ICache::NOT_FOUND, ICache::PARTIAL, ICache::SHALLOW or ICache::COMPLETE
  176. * @since 9.0.0
  177. */
  178. public function getStatus($file);
  179. /**
  180. * search for files matching $pattern, files are matched if their filename matches the search pattern
  181. *
  182. * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%')
  183. * @return ICacheEntry[] an array of cache entries where the name matches the search pattern
  184. * @since 9.0.0
  185. * @deprecated 9.0.0 due to lack of pagination, not all backends might implement this
  186. */
  187. public function search($pattern);
  188. /**
  189. * search for files by mimetype
  190. *
  191. * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image')
  192. * where it will search for all mimetypes in the group ('image/*')
  193. * @return ICacheEntry[] an array of cache entries where the mimetype matches the search
  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 searchByMime($mimetype);
  198. /**
  199. * Search for files with a flexible query
  200. *
  201. * @param ISearchQuery $query
  202. * @return ICacheEntry[]
  203. * @throw \InvalidArgumentException if the cache is unable to perform the query
  204. * @since 12.0.0
  205. */
  206. public function searchQuery(ISearchQuery $query);
  207. /**
  208. * Search for files by tag of a given users.
  209. *
  210. * Note that every user can tag files differently.
  211. *
  212. * @param string|int $tag name or tag id
  213. * @param string $userId owner of the tags
  214. * @return ICacheEntry[] file data
  215. * @since 9.0.0
  216. * @deprecated 9.0.0 due to lack of pagination, not all backends might implement this
  217. */
  218. public function searchByTag($tag, $userId);
  219. /**
  220. * find a folder in the cache which has not been fully scanned
  221. *
  222. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  223. * use the one with the highest id gives the best result with the background scanner, since that is most
  224. * likely the folder where we stopped scanning previously
  225. *
  226. * @return string|bool the path of the folder or false when no folder matched
  227. * @since 9.0.0
  228. */
  229. public function getIncomplete();
  230. /**
  231. * get the path of a file on this storage by it's file id
  232. *
  233. * @param int $id the file id of the file or folder to search
  234. * @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
  235. * @since 9.0.0
  236. */
  237. public function getPathById($id);
  238. /**
  239. * normalize the given path for usage in the cache
  240. *
  241. * @param string $path
  242. * @return string
  243. * @since 9.0.0
  244. */
  245. public function normalize($path);
  246. }