You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
7.7 KiB

3 years ago
  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. * 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. * find a folder in the cache which has not been fully scanned
  209. *
  210. * If multiple incomplete folders are in the cache, the one with the highest id will be returned,
  211. * use the one with the highest id gives the best result with the background scanner, since that is most
  212. * likely the folder where we stopped scanning previously
  213. *
  214. * @return string|bool the path of the folder or false when no folder matched
  215. * @since 9.0.0
  216. */
  217. public function getIncomplete();
  218. /**
  219. * get the path of a file on this storage by it's file id
  220. *
  221. * @param int $id the file id of the file or folder to search
  222. * @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
  223. * @since 9.0.0
  224. */
  225. public function getPathById($id);
  226. /**
  227. * normalize the given path for usage in the cache
  228. *
  229. * @param string $path
  230. * @return string
  231. * @since 9.0.0
  232. */
  233. public function normalize($path);
  234. }