aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/Core/FSTFirestoreClient.mm
blob: da19960fe2dbf78f215838445fae7b24f0b581e4 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * 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 "Firestore/Source/Core/FSTFirestoreClient.h"

#include <future>  // NOLINT(build/c++11)
#include <memory>
#include <utility>

#import "FIRFirestoreErrors.h"
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
#import "Firestore/Source/API/FIRQuery+Internal.h"
#import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
#import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
#import "Firestore/Source/Core/FSTEventManager.h"
#import "Firestore/Source/Core/FSTQuery.h"
#import "Firestore/Source/Core/FSTSyncEngine.h"
#import "Firestore/Source/Core/FSTTransaction.h"
#import "Firestore/Source/Core/FSTView.h"
#import "Firestore/Source/Local/FSTEagerGarbageCollector.h"
#import "Firestore/Source/Local/FSTLevelDB.h"
#import "Firestore/Source/Local/FSTLocalSerializer.h"
#import "Firestore/Source/Local/FSTLocalStore.h"
#import "Firestore/Source/Local/FSTMemoryPersistence.h"
#import "Firestore/Source/Local/FSTNoOpGarbageCollector.h"
#import "Firestore/Source/Model/FSTDocument.h"
#import "Firestore/Source/Model/FSTDocumentSet.h"
#import "Firestore/Source/Remote/FSTDatastore.h"
#import "Firestore/Source/Remote/FSTRemoteStore.h"
#import "Firestore/Source/Remote/FSTSerializerBeta.h"
#import "Firestore/Source/Util/FSTAssert.h"
#import "Firestore/Source/Util/FSTClasses.h"
#import "Firestore/Source/Util/FSTDispatchQueue.h"

#include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
#include "Firestore/core/src/firebase/firestore/core/database_info.h"
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
#include "Firestore/core/src/firebase/firestore/util/log.h"
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"

namespace util = firebase::firestore::util;
using firebase::firestore::auth::CredentialsProvider;
using firebase::firestore::auth::User;
using firebase::firestore::core::DatabaseInfo;
using firebase::firestore::model::DatabaseId;
using firebase::firestore::model::DocumentKeySet;

using firebase::firestore::util::internal::Executor;

NS_ASSUME_NONNULL_BEGIN

@interface FSTFirestoreClient () {
  DatabaseInfo _databaseInfo;
}

- (instancetype)initWithDatabaseInfo:(const DatabaseInfo &)databaseInfo
                      usePersistence:(BOOL)usePersistence
                 credentialsProvider:
                     (CredentialsProvider *)credentialsProvider  // no passing ownership
                        userExecutor:(std::unique_ptr<Executor>)userExecutor
                 workerDispatchQueue:(FSTDispatchQueue *)queue NS_DESIGNATED_INITIALIZER;

@property(nonatomic, assign, readonly) const DatabaseInfo *databaseInfo;
@property(nonatomic, strong, readonly) FSTEventManager *eventManager;
@property(nonatomic, strong, readonly) id<FSTPersistence> persistence;
@property(nonatomic, strong, readonly) FSTSyncEngine *syncEngine;
@property(nonatomic, strong, readonly) FSTRemoteStore *remoteStore;
@property(nonatomic, strong, readonly) FSTLocalStore *localStore;

/**
 * Dispatch queue responsible for all of our internal processing. When we get incoming work from
 * the user (via public API) or the network (incoming GRPC messages), we should always dispatch
 * onto this queue. This ensures our internal data structures are never accessed from multiple
 * threads simultaneously.
 */
@property(nonatomic, strong, readonly) FSTDispatchQueue *workerDispatchQueue;

// Does not own the CredentialsProvider instance.
@property(nonatomic, assign, readonly) CredentialsProvider *credentialsProvider;

@end

@implementation FSTFirestoreClient {
  std::unique_ptr<Executor> _userExecutor;
}

- (Executor *)userExecutor {
  return _userExecutor.get();
}

+ (instancetype)clientWithDatabaseInfo:(const DatabaseInfo &)databaseInfo
                        usePersistence:(BOOL)usePersistence
                   credentialsProvider:
                       (CredentialsProvider *)credentialsProvider  // no passing ownership
                          userExecutor:(std::unique_ptr<Executor>)userExecutor
                   workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue {
  return [[FSTFirestoreClient alloc] initWithDatabaseInfo:databaseInfo
                                           usePersistence:usePersistence
                                      credentialsProvider:credentialsProvider
                                             userExecutor:std::move(userExecutor)
                                      workerDispatchQueue:workerDispatchQueue];
}

- (instancetype)initWithDatabaseInfo:(const DatabaseInfo &)databaseInfo
                      usePersistence:(BOOL)usePersistence
                 credentialsProvider:
                     (CredentialsProvider *)credentialsProvider  // no passing ownership
                        userExecutor:(std::unique_ptr<Executor>)userExecutor
                 workerDispatchQueue:(FSTDispatchQueue *)workerDispatchQueue {
  if (self = [super init]) {
    _databaseInfo = databaseInfo;
    _credentialsProvider = credentialsProvider;
    _userExecutor = std::move(userExecutor);
    _workerDispatchQueue = workerDispatchQueue;

    auto userPromise = std::make_shared<std::promise<User>>();

    __weak typeof(self) weakSelf = self;
    auto userChangeListener = [initialized = false, userPromise, weakSelf,
                               workerDispatchQueue](User user) mutable {
      typeof(self) strongSelf = weakSelf;
      if (!strongSelf) return;

      if (!initialized) {
        initialized = true;
        userPromise->set_value(user);
      } else {
        [workerDispatchQueue dispatchAsync:^{
          [strongSelf userDidChange:user];
        }];
      }
    };

    _credentialsProvider->SetUserChangeListener(userChangeListener);

    // Defer initialization until we get the current user from the userChangeListener. This is
    // guaranteed to be synchronously dispatched onto our worker queue, so we will be initialized
    // before any subsequently queued work runs.
    [_workerDispatchQueue dispatchAsync:^{
      User user = userPromise->get_future().get();
      [self initializeWithUser:user usePersistence:usePersistence];
    }];
  }
  return self;
}

- (void)initializeWithUser:(const User &)user usePersistence:(BOOL)usePersistence {
  // Do all of our initialization on our own dispatch queue.
  [self.workerDispatchQueue verifyIsCurrentQueue];

  // Note: The initialization work must all be synchronous (we can't dispatch more work) since
  // external write/listen operations could get queued to run before that subsequent work
  // completes.
  id<FSTGarbageCollector> garbageCollector;
  if (usePersistence) {
    // TODO(http://b/33384523): For now we just disable garbage collection when persistence is
    // enabled.
    garbageCollector = [[FSTNoOpGarbageCollector alloc] init];

    NSString *dir = [FSTLevelDB storageDirectoryForDatabaseInfo:*self.databaseInfo
                                             documentsDirectory:[FSTLevelDB documentsDirectory]];

    FSTSerializerBeta *remoteSerializer =
        [[FSTSerializerBeta alloc] initWithDatabaseID:&self.databaseInfo->database_id()];
    FSTLocalSerializer *serializer =
        [[FSTLocalSerializer alloc] initWithRemoteSerializer:remoteSerializer];

    _persistence = [[FSTLevelDB alloc] initWithDirectory:dir serializer:serializer];
  } else {
    garbageCollector = [[FSTEagerGarbageCollector alloc] init];
    _persistence = [FSTMemoryPersistence persistence];
  }

  NSError *error;
  if (![_persistence start:&error]) {
    // If local storage fails to start then just throw up our hands: the error is unrecoverable.
    // There's nothing an end-user can do and nearly all failures indicate the developer is doing
    // something grossly wrong so we should stop them cold in their tracks with a failure they
    // can't ignore.
    [NSException raise:NSInternalInconsistencyException format:@"Failed to open DB: %@", error];
  }

  _localStore = [[FSTLocalStore alloc] initWithPersistence:_persistence
                                          garbageCollector:garbageCollector
                                               initialUser:user];

  FSTDatastore *datastore = [FSTDatastore datastoreWithDatabase:self.databaseInfo
                                            workerDispatchQueue:self.workerDispatchQueue
                                                    credentials:_credentialsProvider];

  _remoteStore = [[FSTRemoteStore alloc] initWithLocalStore:_localStore
                                                  datastore:datastore
                                        workerDispatchQueue:self.workerDispatchQueue];

  _syncEngine = [[FSTSyncEngine alloc] initWithLocalStore:_localStore
                                              remoteStore:_remoteStore
                                              initialUser:user];

  _eventManager = [FSTEventManager eventManagerWithSyncEngine:_syncEngine];

  // Setup wiring for remote store.
  _remoteStore.syncEngine = _syncEngine;

  _remoteStore.onlineStateDelegate = self;

  // NOTE: RemoteStore depends on LocalStore (for persisting stream tokens, refilling mutation
  // queue, etc.) so must be started after LocalStore.
  [_localStore start];
  [_remoteStore start];
}

- (void)userDidChange:(const User &)user {
  [self.workerDispatchQueue verifyIsCurrentQueue];

  LOG_DEBUG("User Changed: %s", user.uid());
  [self.syncEngine userDidChange:user];
}

- (void)applyChangedOnlineState:(FSTOnlineState)onlineState {
  [self.syncEngine applyChangedOnlineState:onlineState];
  [self.eventManager applyChangedOnlineState:onlineState];
}

- (void)disableNetworkWithCompletion:(nullable FSTVoidErrorBlock)completion {
  [self.workerDispatchQueue dispatchAsync:^{
    [self.remoteStore disableNetwork];
    if (completion) {
      self->_userExecutor->Execute([=] { completion(nil); });
    }
  }];
}

- (void)enableNetworkWithCompletion:(nullable FSTVoidErrorBlock)completion {
  [self.workerDispatchQueue dispatchAsync:^{
    [self.remoteStore enableNetwork];
    if (completion) {
      self->_userExecutor->Execute([=] { completion(nil); });
    }
  }];
}

- (void)shutdownWithCompletion:(nullable FSTVoidErrorBlock)completion {
  [self.workerDispatchQueue dispatchAsync:^{
    self->_credentialsProvider->SetUserChangeListener(nullptr);

    [self.remoteStore shutdown];
    [self.persistence shutdown];
    if (completion) {
      self->_userExecutor->Execute([=] { completion(nil); });
    }
  }];
}

- (FSTQueryListener *)listenToQuery:(FSTQuery *)query
                            options:(FSTListenOptions *)options
                viewSnapshotHandler:(FSTViewSnapshotHandler)viewSnapshotHandler {
  FSTQueryListener *listener = [[FSTQueryListener alloc] initWithQuery:query
                                                               options:options
                                                   viewSnapshotHandler:viewSnapshotHandler];

  [self.workerDispatchQueue dispatchAsync:^{
    [self.eventManager addListener:listener];
  }];

  return listener;
}

- (void)removeListener:(FSTQueryListener *)listener {
  [self.workerDispatchQueue dispatchAsync:^{
    [self.eventManager removeListener:listener];
  }];
}

- (void)getDocumentFromLocalCache:(FIRDocumentReference *)doc
                       completion:(void (^)(FIRDocumentSnapshot *_Nullable document,
                                            NSError *_Nullable error))completion {
  [self.workerDispatchQueue dispatchAsync:^{
    FSTMaybeDocument *maybeDoc = [self.localStore readDocument:doc.key];
    if (maybeDoc) {
      completion([FIRDocumentSnapshot snapshotWithFirestore:doc.firestore
                                                documentKey:doc.key
                                                   document:(FSTDocument *)maybeDoc
                                                  fromCache:YES],
                 nil);
    } else {
      completion(nil,
                 [NSError errorWithDomain:FIRFirestoreErrorDomain
                                     code:FIRFirestoreErrorCodeUnavailable
                                 userInfo:@{
                                   NSLocalizedDescriptionKey :
                                       @"Failed to get document from cache. (However, this "
                                       @"document may exist on the server. Run again without "
                                       @"setting source to FIRFirestoreSourceCache to attempt to "
                                       @"retrieve the document from the server.)",
                                 }]);
    }
  }];
}

- (void)getDocumentsFromLocalCache:(FIRQuery *)query
                        completion:(void (^)(FIRQuerySnapshot *_Nullable query,
                                             NSError *_Nullable error))completion {
  [self.workerDispatchQueue dispatchAsync:^{
    FSTDocumentDictionary *docs = [self.localStore executeQuery:query.query];

    FSTView *view = [[FSTView alloc] initWithQuery:query.query remoteDocuments:DocumentKeySet{}];
    FSTViewDocumentChanges *viewDocChanges = [view computeChangesWithDocuments:docs];
    FSTViewChange *viewChange = [view applyChangesToDocuments:viewDocChanges];
    FSTAssert(viewChange.limboChanges.count == 0,
              @"View returned limbo documents during local-only query execution.");

    FSTViewSnapshot *snapshot = viewChange.snapshot;
    FIRSnapshotMetadata *metadata =
        [FIRSnapshotMetadata snapshotMetadataWithPendingWrites:snapshot.hasPendingWrites
                                                     fromCache:snapshot.fromCache];

    completion([FIRQuerySnapshot snapshotWithFirestore:query.firestore
                                         originalQuery:query.query
                                              snapshot:snapshot
                                              metadata:metadata],
               nil);
  }];
}

- (void)writeMutations:(NSArray<FSTMutation *> *)mutations
            completion:(nullable FSTVoidErrorBlock)completion {
  [self.workerDispatchQueue dispatchAsync:^{
    if (mutations.count == 0) {
      if (completion) {
        self->_userExecutor->Execute([=] { completion(nil); });
      }
    } else {
      [self.syncEngine writeMutations:mutations
                           completion:^(NSError *error) {
                             // Dispatch the result back onto the user dispatch queue.
                             if (completion) {
                               self->_userExecutor->Execute([=] { completion(error); });
                             }
                           }];
    }
  }];
};

- (void)transactionWithRetries:(int)retries
                   updateBlock:(FSTTransactionBlock)updateBlock
                    completion:(FSTVoidIDErrorBlock)completion {
  [self.workerDispatchQueue dispatchAsync:^{
    [self.syncEngine
        transactionWithRetries:retries
           workerDispatchQueue:self.workerDispatchQueue
                   updateBlock:updateBlock
                    completion:^(id _Nullable result, NSError *_Nullable error) {
                      // Dispatch the result back onto the user dispatch queue.
                      if (completion) {
                        self->_userExecutor->Execute([=] { completion(result, error); });
                      }
                    }];
  }];
}

- (const DatabaseInfo *)databaseInfo {
  return &_databaseInfo;
}

- (const DatabaseId *)databaseID {
  return &_databaseInfo.database_id();
}

@end

NS_ASSUME_NONNULL_END