aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/Local/FSTLevelDBKey.h
blob: 5b234ecd9d17a17648f328e5e0c80936893f6d0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <Foundation/Foundation.h>

#include <string>

#import "Firestore/Source/Core/FSTTypes.h"
#import "Firestore/Source/Local/StringView.h"

#include "Firestore/core/src/firebase/firestore/model/resource_path.h"

@class FSTDocumentKey;

NS_ASSUME_NONNULL_BEGIN

// All leveldb logical tables should have their keys structures described in this file.
//
// mutations:
//   - tableName: string = "mutation"
//   - userID: string
//   - batchID: FSTBatchID
//
// document_mutations:
//   - tableName: string = "document_mutation"
//   - userID: string
//   - path: ResourcePath
//   - batchID: FSTBatchID
//
// mutation_queues:
//   - tableName: string = "mutation_queue"
//   - userID: string
//
// targets:
//   - tableName: string = "target"
//   - targetId: FSTTargetID
//
// target_globals:
//   - tableName: string = "target_global"
//
// query_targets:
//   - tableName: string = "query_target"
//   - canonicalID: string
//   - targetId: FSTTargetID
//
// target_documents:
//   - tableName: string = "target_document"
//   - targetID: FSTTargetID
//   - path: ResourcePath
//
// document_targets:
//   - tableName: string = "document_target"
//   - path: ResourcePath
//   - targetID: FSTTargetID
//
// remote_documents:
//   - tableName: string = "remote_document"
//   - path: ResourcePath

/** Helpers for any LevelDB key. */
@interface FSTLevelDBKey : NSObject

/**
 * Parses the given key and returns a human readable description of its contents, suitable for
 * error messages and logging.
 */
+ (NSString *)descriptionForKey:(Firestore::StringView)key;

@end

/** A key to a singleton row storing the version of the schema. */
@interface FSTLevelDBVersionKey : NSObject

/** Returns the key pointing to the singleton row storing the schema version. */
+ (std::string)key;

@end

/** A key in the mutations table. */
@interface FSTLevelDBMutationKey : NSObject

/** Creates a key prefix that points just before the first key in the table. */
+ (std::string)keyPrefix;

/** Creates a key prefix that points just before the first key for the given userID. */
+ (std::string)keyPrefixWithUserID:(Firestore::StringView)userID;

/** Creates a complete key that points to a specific userID and batchID. */
+ (std::string)keyWithUserID:(Firestore::StringView)userID batchID:(FSTBatchID)batchID;

/**
 * Decodes the given complete key, storing the decoded values as properties of the receiver.
 *
 * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
 *     the receiver are in an undefined state until the next call to -decodeKey:.
 */
- (BOOL)decodeKey:(Firestore::StringView)key;

/** The user that owns the mutation batches. */
@property(nonatomic, assign, readonly) const std::string &userID;

/** The batchID of the batch. */
@property(nonatomic, assign, readonly) FSTBatchID batchID;

@end

/**
 * A key in the document mutations index, which stores the batches in which documents are mutated.
 */
@interface FSTLevelDBDocumentMutationKey : NSObject

/** Creates a key prefix that points just before the first key in the table. */
+ (std::string)keyPrefix;

/** Creates a key prefix that points just before the first key for the given userID. */
+ (std::string)keyPrefixWithUserID:(Firestore::StringView)userID;

/**
 * Creates a key prefix that points just before the first key for the userID and resource path.
 *
 * Note that this uses a ResourcePath rather than an FSTDocumentKey in order to allow prefix
 * scans over a collection. However a naive scan over those results isn't useful since it would
 * match both immediate children of the collection and any subcollections.
 */
+ (std::string)keyPrefixWithUserID:(Firestore::StringView)userID
                      resourcePath:(const firebase::firestore::model::ResourcePath &)resourcePath;

/** Creates a complete key that points to a specific userID, document key, and batchID. */
+ (std::string)keyWithUserID:(Firestore::StringView)userID
                 documentKey:(FSTDocumentKey *)documentKey
                     batchID:(FSTBatchID)batchID;

/**
 * Decodes the given complete key, storing the decoded values as properties of the receiver.
 *
 * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
 *     the receiver are in an undefined state until the next call to -decodeKey:.
 */
- (BOOL)decodeKey:(Firestore::StringView)key;

/** The user that owns the mutation batches. */
@property(nonatomic, assign, readonly) const std::string &userID;

/** The path to the document, as encoded in the key. */
@property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;

/** The batchID in which the document participates. */
@property(nonatomic, assign, readonly) FSTBatchID batchID;

@end

/**
 * A key in the mutation_queues table.
 *
 * Note that where mutation_queues contains one row about each queue, mutations contains the actual
 * mutation batches themselves.
 */
@interface FSTLevelDBMutationQueueKey : NSObject

/** Creates a key prefix that points just before the first key in the table. */
+ (std::string)keyPrefix;

/** Creates a complete key that points to a specific mutation queue entry for the given userID. */
+ (std::string)keyWithUserID:(Firestore::StringView)userID;

/**
 * Decodes the given complete key, storing the decoded values as properties of the receiver.
 *
 * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
 *     the receiver are in an undefined state until the next call to -decodeKey:.
 */
- (BOOL)decodeKey:(Firestore::StringView)key;

@property(nonatomic, assign, readonly) const std::string &userID;

@end

/** A key in the target globals table, a record of global values across all targets. */
@interface FSTLevelDBTargetGlobalKey : NSObject

/** Creates a key that points to the single target global row. */
+ (std::string)key;

/**
 * Decodes the contents of a target global key, essentially just verifying that the key has the
 * correct table name.
 */
- (BOOL)decodeKey:(Firestore::StringView)key;

@end

/** A key in the targets table. */
@interface FSTLevelDBTargetKey : NSObject

/** Creates a key prefix that points just before the first key in the table. */
+ (std::string)keyPrefix;

/** Creates a complete key that points to a specific target, by targetID. */
+ (std::string)keyWithTargetID:(FSTTargetID)targetID;

/**
 * Decodes the contents of a target key into properties on this instance.
 *
 * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
 *     the receiver are in an undefined state until the next call to -decodeKey:.
 */
- (BOOL)decodeKey:(Firestore::StringView)key;

/** The targetID identifying a target. */
@property(nonatomic, assign, readonly) FSTTargetID targetID;

@end

/**
 * A key in the query targets table, an index of canonicalIDs to the targets they may match. This
 * is not a unique mapping because canonicalID does not promise a unique name for all possible
 * queries.
 */
@interface FSTLevelDBQueryTargetKey : NSObject

/**
 * Creates a key that contains just the query targets table prefix and points just before the
 * first key.
 */
+ (std::string)keyPrefix;

/** Creates a key that points to the first query-target association for a canonicalID. */
+ (std::string)keyPrefixWithCanonicalID:(Firestore::StringView)canonicalID;

/** Creates a key that points to a specific query-target entry. */
+ (std::string)keyWithCanonicalID:(Firestore::StringView)canonicalID targetID:(FSTTargetID)targetID;

/** Decodes the contents of a query target key into properties on this instance. */
- (BOOL)decodeKey:(Firestore::StringView)key;

/** The canonicalID derived from the query. */
@property(nonatomic, assign, readonly) const std::string &canonicalID;

/** The targetID identifying a target. */
@property(nonatomic, assign, readonly) FSTTargetID targetID;

@end

/**
 * A key in the target documents table, an index of targetIDs to the documents they contain.
 */
@interface FSTLevelDBTargetDocumentKey : NSObject

/**
 * Creates a key that contains just the target documents table prefix and points just before the
 * first key.
 */
+ (std::string)keyPrefix;

/** Creates a key that points to the first target-document association for a targetID. */
+ (std::string)keyPrefixWithTargetID:(FSTTargetID)targetID;

/** Creates a key that points to a specific target-document entry. */
+ (std::string)keyWithTargetID:(FSTTargetID)targetID documentKey:(FSTDocumentKey *)documentKey;

/** Decodes the contents of a target document key into properties on this instance. */
- (BOOL)decodeKey:(Firestore::StringView)key;

/** The targetID identifying a target. */
@property(nonatomic, assign, readonly) FSTTargetID targetID;

/** The path to the document, as encoded in the key. */
@property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;

@end

/**
 * A key in the document targets table, an index from documents to the targets that contain them.
 */
@interface FSTLevelDBDocumentTargetKey : NSObject

/**
 * Creates a key that contains just the document targets table prefix and points just before the
 * first key.
 */
+ (std::string)keyPrefix;

/** Creates a key that points to the first document-target association for document. */
+ (std::string)keyPrefixWithResourcePath:
    (const firebase::firestore::model::ResourcePath &)resourcePath;

/** Creates a key that points to a specific document-target entry. */
+ (std::string)keyWithDocumentKey:(FSTDocumentKey *)documentKey targetID:(FSTTargetID)targetID;

/** Decodes the contents of a document target key into properties on this instance. */
- (BOOL)decodeKey:(Firestore::StringView)key;

/** The targetID identifying a target. */
@property(nonatomic, assign, readonly) FSTTargetID targetID;

/** The path to the document, as encoded in the key. */
@property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;

@end

/** A key in the remote documents table. */
@interface FSTLevelDBRemoteDocumentKey : NSObject

/**
 * Creates a key that contains just the remote documents table prefix and points just before the
 * first remote document key.
 */
+ (std::string)keyPrefix;

/**
 * Creates a complete key that points to a specific document. The documentKey must have an even
 * number of path segments.
 */
+ (std::string)keyWithDocumentKey:(FSTDocumentKey *)key;

/**
 * Creates a key prefix that contains a part of a document path. Odd numbers of segments create a
 * collection key prefix, while an even number of segments create a document key prefix. Note that
 * a document key prefix will match the document itself and any documents that exist in its
 * subcollections.
 */
+ (std::string)keyPrefixWithResourcePath:
    (const firebase::firestore::model::ResourcePath &)resourcePath;

/**
 * Decodes the contents of a remote document key into properties on this instance. This can only
 * decode complete document paths (i.e. the result of +keyWithDocumentKey:).
 *
 * @return YES if the key successfully decoded, NO otherwise. If NO is returned, the properties of
 *     the receiver are in an undefined state until the next call to -decodeKey:.
 */
- (BOOL)decodeKey:(Firestore::StringView)key;

/** The path to the document, as encoded in the key. */
@property(nonatomic, strong, readonly, nullable) FSTDocumentKey *documentKey;

@end

NS_ASSUME_NONNULL_END