FSTLevelDBKey.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. /** Helpers for any LevelDB key. */
  66. @interface FSTLevelDBKey : NSObject
  67. /**
  68. * Parses the given key and returns a human readable description of its contents, suitable for
  69. * error messages and logging.
  70. */
  71. + (NSString *)descriptionForKey:(Firestore::StringView)key;
  72. @end
  73. /** A key to a singleton row storing the version of the schema. */
  74. @interface FSTLevelDBVersionKey : NSObject
  75. /** Returns the key pointing to the singleton row storing the schema version. */
  76. + (std::string)key;
  77. @end
  78. /** A key in the mutations table. */
  79. @interface FSTLevelDBMutationKey : NSObject
  80. /** Creates a key prefix that points just before the first key in the table. */
  81. + (std::string)keyPrefix;
  82. /** Creates a key prefix that points just before the first key for the given userID. */
  83. + (std::string)keyPrefixWithUserID:(Firestore::StringView)userID;
  84. /** Creates a complete key that points to a specific userID and batchID. */
  85. + (std::string)keyWithUserID:(Firestore::StringView)userID batchID:(FSTBatchID)batchID;
  86. /**
  87. * Decodes the given complete key, storing the decoded values as properties of the receiver.
  88. *
  89. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  90. * the receiver are in an undefined state until the next call to -decodeKey:.
  91. */
  92. - (BOOL)decodeKey:(Firestore::StringView)key;
  93. /** The user that owns the mutation batches. */
  94. @property(nonatomic, assign, readonly) const std::string &userID;
  95. /** The batchID of the batch. */
  96. @property(nonatomic, assign, readonly) FSTBatchID batchID;
  97. @end
  98. /**
  99. * A key in the document mutations index, which stores the batches in which documents are mutated.
  100. */
  101. @interface FSTLevelDBDocumentMutationKey : NSObject
  102. /** Creates a key prefix that points just before the first key in the table. */
  103. + (std::string)keyPrefix;
  104. /** Creates a key prefix that points just before the first key for the given userID. */
  105. + (std::string)keyPrefixWithUserID:(Firestore::StringView)userID;
  106. /**
  107. * Creates a key prefix that points just before the first key for the userID and resource path.
  108. *
  109. * Note that this uses a ResourcePath rather than an FSTDocumentKey in order to allow prefix
  110. * scans over a collection. However a naive scan over those results isn't useful since it would
  111. * match both immediate children of the collection and any subcollections.
  112. */
  113. + (std::string)keyPrefixWithUserID:(Firestore::StringView)userID
  114. resourcePath:(const firebase::firestore::model::ResourcePath &)resourcePath;
  115. /** Creates a complete key that points to a specific userID, document key, and batchID. */
  116. + (std::string)keyWithUserID:(Firestore::StringView)userID
  117. documentKey:(FSTDocumentKey *)documentKey
  118. batchID:(FSTBatchID)batchID;
  119. /**
  120. * Decodes the given complete key, storing the decoded values as properties of the receiver.
  121. *
  122. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  123. * the receiver are in an undefined state until the next call to -decodeKey:.
  124. */
  125. - (BOOL)decodeKey:(Firestore::StringView)key;
  126. /** The user that owns the mutation batches. */
  127. @property(nonatomic, assign, readonly) const std::string &userID;
  128. /** The path to the document, as encoded in the key. */
  129. @property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;
  130. /** The batchID in which the document participates. */
  131. @property(nonatomic, assign, readonly) FSTBatchID batchID;
  132. @end
  133. /**
  134. * A key in the mutation_queues table.
  135. *
  136. * Note that where mutation_queues contains one row about each queue, mutations contains the actual
  137. * mutation batches themselves.
  138. */
  139. @interface FSTLevelDBMutationQueueKey : NSObject
  140. /** Creates a key prefix that points just before the first key in the table. */
  141. + (std::string)keyPrefix;
  142. /** Creates a complete key that points to a specific mutation queue entry for the given userID. */
  143. + (std::string)keyWithUserID:(Firestore::StringView)userID;
  144. /**
  145. * Decodes the given complete key, storing the decoded values as properties of the receiver.
  146. *
  147. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  148. * the receiver are in an undefined state until the next call to -decodeKey:.
  149. */
  150. - (BOOL)decodeKey:(Firestore::StringView)key;
  151. @property(nonatomic, assign, readonly) const std::string &userID;
  152. @end
  153. /** A key in the target globals table, a record of global values across all targets. */
  154. @interface FSTLevelDBTargetGlobalKey : NSObject
  155. /** Creates a key that points to the single target global row. */
  156. + (std::string)key;
  157. /**
  158. * Decodes the contents of a target global key, essentially just verifying that the key has the
  159. * correct table name.
  160. */
  161. - (BOOL)decodeKey:(Firestore::StringView)key;
  162. @end
  163. /** A key in the targets table. */
  164. @interface FSTLevelDBTargetKey : NSObject
  165. /** Creates a key prefix that points just before the first key in the table. */
  166. + (std::string)keyPrefix;
  167. /** Creates a complete key that points to a specific target, by targetID. */
  168. + (std::string)keyWithTargetID:(FSTTargetID)targetID;
  169. /**
  170. * Decodes the contents of a target key into properties on this instance.
  171. *
  172. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  173. * the receiver are in an undefined state until the next call to -decodeKey:.
  174. */
  175. - (BOOL)decodeKey:(Firestore::StringView)key;
  176. /** The targetID identifying a target. */
  177. @property(nonatomic, assign, readonly) FSTTargetID targetID;
  178. @end
  179. /**
  180. * A key in the query targets table, an index of canonicalIDs to the targets they may match. This
  181. * is not a unique mapping because canonicalID does not promise a unique name for all possible
  182. * queries.
  183. */
  184. @interface FSTLevelDBQueryTargetKey : NSObject
  185. /**
  186. * Creates a key that contains just the query targets table prefix and points just before the
  187. * first key.
  188. */
  189. + (std::string)keyPrefix;
  190. /** Creates a key that points to the first query-target association for a canonicalID. */
  191. + (std::string)keyPrefixWithCanonicalID:(Firestore::StringView)canonicalID;
  192. /** Creates a key that points to a specific query-target entry. */
  193. + (std::string)keyWithCanonicalID:(Firestore::StringView)canonicalID targetID:(FSTTargetID)targetID;
  194. /** Decodes the contents of a query target key into properties on this instance. */
  195. - (BOOL)decodeKey:(Firestore::StringView)key;
  196. /** The canonicalID derived from the query. */
  197. @property(nonatomic, assign, readonly) const std::string &canonicalID;
  198. /** The targetID identifying a target. */
  199. @property(nonatomic, assign, readonly) FSTTargetID targetID;
  200. @end
  201. /**
  202. * A key in the target documents table, an index of targetIDs to the documents they contain.
  203. */
  204. @interface FSTLevelDBTargetDocumentKey : NSObject
  205. /**
  206. * Creates a key that contains just the target documents table prefix and points just before the
  207. * first key.
  208. */
  209. + (std::string)keyPrefix;
  210. /** Creates a key that points to the first target-document association for a targetID. */
  211. + (std::string)keyPrefixWithTargetID:(FSTTargetID)targetID;
  212. /** Creates a key that points to a specific target-document entry. */
  213. + (std::string)keyWithTargetID:(FSTTargetID)targetID documentKey:(FSTDocumentKey *)documentKey;
  214. /** Decodes the contents of a target document key into properties on this instance. */
  215. - (BOOL)decodeKey:(Firestore::StringView)key;
  216. /** The targetID identifying a target. */
  217. @property(nonatomic, assign, readonly) FSTTargetID targetID;
  218. /** The path to the document, as encoded in the key. */
  219. @property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;
  220. @end
  221. /**
  222. * A key in the document targets table, an index from documents to the targets that contain them.
  223. */
  224. @interface FSTLevelDBDocumentTargetKey : NSObject
  225. /**
  226. * Creates a key that contains just the document targets table prefix and points just before the
  227. * first key.
  228. */
  229. + (std::string)keyPrefix;
  230. /** Creates a key that points to the first document-target association for document. */
  231. + (std::string)keyPrefixWithResourcePath:
  232. (const firebase::firestore::model::ResourcePath &)resourcePath;
  233. /** Creates a key that points to a specific document-target entry. */
  234. + (std::string)keyWithDocumentKey:(FSTDocumentKey *)documentKey targetID:(FSTTargetID)targetID;
  235. /** Decodes the contents of a document target key into properties on this instance. */
  236. - (BOOL)decodeKey:(Firestore::StringView)key;
  237. /** The targetID identifying a target. */
  238. @property(nonatomic, assign, readonly) FSTTargetID targetID;
  239. /** The path to the document, as encoded in the key. */
  240. @property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;
  241. @end
  242. /** A key in the remote documents table. */
  243. @interface FSTLevelDBRemoteDocumentKey : NSObject
  244. /**
  245. * Creates a key that contains just the remote documents table prefix and points just before the
  246. * first remote document key.
  247. */
  248. + (std::string)keyPrefix;
  249. /**
  250. * Creates a complete key that points to a specific document. The documentKey must have an even
  251. * number of path segments.
  252. */
  253. + (std::string)keyWithDocumentKey:(FSTDocumentKey *)key;
  254. /**
  255. * Creates a key prefix that contains a part of a document path. Odd numbers of segments create a
  256. * collection key prefix, while an even number of segments create a document key prefix. Note that
  257. * a document key prefix will match the document itself and any documents that exist in its
  258. * subcollections.
  259. */
  260. + (std::string)keyPrefixWithResourcePath:
  261. (const firebase::firestore::model::ResourcePath &)resourcePath;
  262. /**
  263. * Decodes the contents of a remote document key into properties on this instance. This can only
  264. * decode complete document paths (i.e. the result of +keyWithDocumentKey:).
  265. *
  266. * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
  267. * the receiver are in an undefined state until the next call to -decodeKey:.
  268. */
  269. - (BOOL)decodeKey:(Firestore::StringView)key;
  270. /** The path to the document, as encoded in the key. */
  271. @property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;
  272. @end
  273. NS_ASSUME_NONNULL_END