FSTLevelDBKey.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #import "Firestore/Source/Core/FSTTypes.h"
  18. #import "Firestore/Source/Local/StringView.h"
  19. @class FSTDocumentKey;
  20. @class FSTResourcePath;
  21. NS_ASSUME_NONNULL_BEGIN
  22. // All leveldb logical tables should have their keys structures described in this file.
  23. //
  24. // mutations:
  25. // - tableName: string = "mutation"
  26. // - userID: string
  27. // - batchID: FSTBatchID
  28. //
  29. // document_mutations:
  30. // - tableName: string = "document_mutation"
  31. // - userID: string
  32. // - path: FSTResourcePath
  33. // - batchID: FSTBatchID
  34. //
  35. // mutation_queues:
  36. // - tableName: string = "mutation_queue"
  37. // - userID: string
  38. //
  39. // targets:
  40. // - tableName: string = "target"
  41. // - targetId: FSTTargetID
  42. //
  43. // target_globals:
  44. // - tableName: string = "target_global"
  45. //
  46. // query_targets:
  47. // - tableName: string = "query_target"
  48. // - canonicalID: string
  49. // - targetId: FSTTargetID
  50. //
  51. // target_documents:
  52. // - tableName: string = "target_document"
  53. // - targetID: FSTTargetID
  54. // - path: FSTResourcePath
  55. //
  56. // document_targets:
  57. // - tableName: string = "document_target"
  58. // - path: FSTResourcePath
  59. // - targetID: FSTTargetID
  60. //
  61. // remote_documents:
  62. // - tableName: string = "remote_document"
  63. // - path: FSTResourcePath
  64. /** Helpers for any LevelDB key. */
  65. @interface FSTLevelDBKey : NSObject
  66. /**
  67. * Parses the given key and returns a human readable description of its contents, suitable for
  68. * error messages and logging.
  69. */
  70. + (NSString *)descriptionForKey:(Firestore::StringView)key;
  71. @end
  72. /** A key to a singleton row storing the version of the schema. */
  73. @interface FSTLevelDBVersionKey : NSObject
  74. /** Returns the key pointing to the singleton row storing the schema version. */
  75. + (std::string)key;
  76. @end
  77. /** A key in the mutations table. */
  78. @interface FSTLevelDBMutationKey : NSObject
  79. /** Creates a key prefix that points just before the first key in the table. */
  80. + (std::string)keyPrefix;
  81. /** Creates a key prefix that points just before the first key for the given userID. */
  82. + (std::string)keyPrefixWithUserID:(Firestore::StringView)userID;
  83. /** Creates a complete key that points to a specific userID and batchID. */
  84. + (std::string)keyWithUserID:(Firestore::StringView)userID batchID:(FSTBatchID)batchID;
  85. /**
  86. * Decodes the given complete key, storing the decoded values as properties of the receiver.
  87. *
  88. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  89. * the receiver are in an undefined state until the next call to -decodeKey:.
  90. */
  91. - (BOOL)decodeKey:(Firestore::StringView)key;
  92. /** The user that owns the mutation batches. */
  93. @property(nonatomic, assign, readonly) const std::string &userID;
  94. /** The batchID of the batch. */
  95. @property(nonatomic, assign, readonly) FSTBatchID batchID;
  96. @end
  97. /**
  98. * A key in the document mutations index, which stores the batches in which documents are mutated.
  99. */
  100. @interface FSTLevelDBDocumentMutationKey : NSObject
  101. /** Creates a key prefix that points just before the first key in the table. */
  102. + (std::string)keyPrefix;
  103. /** Creates a key prefix that points just before the first key for the given userID. */
  104. + (std::string)keyPrefixWithUserID:(Firestore::StringView)userID;
  105. /**
  106. * Creates a key prefix that points just before the first key for the userID and resource path.
  107. *
  108. * Note that this uses an FSTResourcePath rather than an FSTDocumentKey in order to allow prefix
  109. * scans over a collection. However a naive scan over those results isn't useful since it would
  110. * match both immediate children of the collection and any subcollections.
  111. */
  112. + (std::string)keyPrefixWithUserID:(Firestore::StringView)userID
  113. resourcePath:(FSTResourcePath *)resourcePath;
  114. /** Creates a complete key that points to a specific userID, document key, and batchID. */
  115. + (std::string)keyWithUserID:(Firestore::StringView)userID
  116. documentKey:(FSTDocumentKey *)documentKey
  117. batchID:(FSTBatchID)batchID;
  118. /**
  119. * Decodes the given complete key, storing the decoded values as properties of the receiver.
  120. *
  121. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  122. * the receiver are in an undefined state until the next call to -decodeKey:.
  123. */
  124. - (BOOL)decodeKey:(Firestore::StringView)key;
  125. /** The user that owns the mutation batches. */
  126. @property(nonatomic, assign, readonly) const std::string &userID;
  127. /** The path to the document, as encoded in the key. */
  128. @property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;
  129. /** The batchID in which the document participates. */
  130. @property(nonatomic, assign, readonly) FSTBatchID batchID;
  131. @end
  132. /**
  133. * A key in the mutation_queues table.
  134. *
  135. * Note that where mutation_queues contains one row about each queue, mutations contains the actual
  136. * mutation batches themselves.
  137. */
  138. @interface FSTLevelDBMutationQueueKey : NSObject
  139. /** Creates a key prefix that points just before the first key in the table. */
  140. + (std::string)keyPrefix;
  141. /** Creates a complete key that points to a specific mutation queue entry for the given userID. */
  142. + (std::string)keyWithUserID:(Firestore::StringView)userID;
  143. /**
  144. * Decodes the given complete key, storing the decoded values as properties of the receiver.
  145. *
  146. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  147. * the receiver are in an undefined state until the next call to -decodeKey:.
  148. */
  149. - (BOOL)decodeKey:(Firestore::StringView)key;
  150. @property(nonatomic, assign, readonly) const std::string &userID;
  151. @end
  152. /** A key in the target globals table, a record of global values across all targets. */
  153. @interface FSTLevelDBTargetGlobalKey : NSObject
  154. /** Creates a key that points to the single target global row. */
  155. + (std::string)key;
  156. /**
  157. * Decodes the contents of a target global key, essentially just verifying that the key has the
  158. * correct table name.
  159. */
  160. - (BOOL)decodeKey:(Firestore::StringView)key;
  161. @end
  162. /** A key in the targets table. */
  163. @interface FSTLevelDBTargetKey : NSObject
  164. /** Creates a key prefix that points just before the first key in the table. */
  165. + (std::string)keyPrefix;
  166. /** Creates a complete key that points to a specific target, by targetID. */
  167. + (std::string)keyWithTargetID:(FSTTargetID)targetID;
  168. /**
  169. * Decodes the contents of a target key into properties on this instance.
  170. *
  171. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  172. * the receiver are in an undefined state until the next call to -decodeKey:.
  173. */
  174. - (BOOL)decodeKey:(Firestore::StringView)key;
  175. /** The targetID identifying a target. */
  176. @property(nonatomic, assign, readonly) FSTTargetID targetID;
  177. @end
  178. /**
  179. * A key in the query targets table, an index of canonicalIDs to the targets they may match. This
  180. * is not a unique mapping because canonicalID does not promise a unique name for all possible
  181. * queries.
  182. */
  183. @interface FSTLevelDBQueryTargetKey : NSObject
  184. /**
  185. * Creates a key that contains just the query targets table prefix and points just before the
  186. * first key.
  187. */
  188. + (std::string)keyPrefix;
  189. /** Creates a key that points to the first query-target association for a canonicalID. */
  190. + (std::string)keyPrefixWithCanonicalID:(Firestore::StringView)canonicalID;
  191. /** Creates a key that points to a specific query-target entry. */
  192. + (std::string)keyWithCanonicalID:(Firestore::StringView)canonicalID targetID:(FSTTargetID)targetID;
  193. /** Decodes the contents of a query target key into properties on this instance. */
  194. - (BOOL)decodeKey:(Firestore::StringView)key;
  195. /** The canonicalID derived from the query. */
  196. @property(nonatomic, assign, readonly) const std::string &canonicalID;
  197. /** The targetID identifying a target. */
  198. @property(nonatomic, assign, readonly) FSTTargetID targetID;
  199. @end
  200. /**
  201. * A key in the target documents table, an index of targetIDs to the documents they contain.
  202. */
  203. @interface FSTLevelDBTargetDocumentKey : NSObject
  204. /**
  205. * Creates a key that contains just the target documents table prefix and points just before the
  206. * first key.
  207. */
  208. + (std::string)keyPrefix;
  209. /** Creates a key that points to the first target-document association for a targetID. */
  210. + (std::string)keyPrefixWithTargetID:(FSTTargetID)targetID;
  211. /** Creates a key that points to a specific target-document entry. */
  212. + (std::string)keyWithTargetID:(FSTTargetID)targetID documentKey:(FSTDocumentKey *)documentKey;
  213. /** Decodes the contents of a target document key into properties on this instance. */
  214. - (BOOL)decodeKey:(Firestore::StringView)key;
  215. /** The targetID identifying a target. */
  216. @property(nonatomic, assign, readonly) FSTTargetID targetID;
  217. /** The path to the document, as encoded in the key. */
  218. @property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;
  219. @end
  220. /**
  221. * A key in the document targets table, an index from documents to the targets that contain them.
  222. */
  223. @interface FSTLevelDBDocumentTargetKey : NSObject
  224. /**
  225. * Creates a key that contains just the document targets table prefix and points just before the
  226. * first key.
  227. */
  228. + (std::string)keyPrefix;
  229. /** Creates a key that points to the first document-target association for document. */
  230. + (std::string)keyPrefixWithResourcePath:(FSTResourcePath *)resourcePath;
  231. /** Creates a key that points to a specific document-target entry. */
  232. + (std::string)keyWithDocumentKey:(FSTDocumentKey *)documentKey targetID:(FSTTargetID)targetID;
  233. /** Decodes the contents of a document target key into properties on this instance. */
  234. - (BOOL)decodeKey:(Firestore::StringView)key;
  235. /** The targetID identifying a target. */
  236. @property(nonatomic, assign, readonly) FSTTargetID targetID;
  237. /** The path to the document, as encoded in the key. */
  238. @property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;
  239. @end
  240. /** A key in the remote documents table. */
  241. @interface FSTLevelDBRemoteDocumentKey : NSObject
  242. /**
  243. * Creates a key that contains just the remote documents table prefix and points just before the
  244. * first remote document key.
  245. */
  246. + (std::string)keyPrefix;
  247. /**
  248. * Creates a complete key that points to a specific document. The documentKey must have an even
  249. * number of path segments.
  250. */
  251. + (std::string)keyWithDocumentKey:(FSTDocumentKey *)key;
  252. /**
  253. * Creates a key prefix that contains a part of a document path. Odd numbers of segments create a
  254. * collection key prefix, while an even number of segments create a document key prefix. Note that
  255. * a document key prefix will match the document itself and any documents that exist in its
  256. * subcollections.
  257. */
  258. + (std::string)keyPrefixWithResourcePath:(FSTResourcePath *)resourcePath;
  259. /**
  260. * Decodes the contents of a remote document key into properties on this instance. This can only
  261. * decode complete document paths (i.e. the result of +keyWithDocumentKey:).
  262. *
  263. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  264. * the receiver are in an undefined state until the next call to -decodeKey:.
  265. */
  266. - (BOOL)decodeKey:(Firestore::StringView)key;
  267. /** The path to the document, as encoded in the key. */
  268. @property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;
  269. @end
  270. NS_ASSUME_NONNULL_END