FSTLevelDBKey.h 12 KB

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