FSTLevelDBKey.h 11 KB

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