FSTDatastore.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <memory>
  18. #include <vector>
  19. #import "Firestore/Source/Core/FSTTypes.h"
  20. #include "Firestore/core/src/firebase/firestore//remote/watch_stream.h"
  21. #include "Firestore/core/src/firebase/firestore//remote/write_stream.h"
  22. #include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
  23. #include "Firestore/core/src/firebase/firestore/core/database_info.h"
  24. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  25. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  26. #include "Firestore/core/src/firebase/firestore/remote/datastore.h"
  27. #include "Firestore/core/src/firebase/firestore/util/async_queue.h"
  28. #include "absl/memory/memory.h"
  29. #include "absl/strings/string_view.h"
  30. @class FSTMutation;
  31. @class FSTMutationResult;
  32. @class FSTQueryData;
  33. @class FSTSerializerBeta;
  34. @class FSTWatchChange;
  35. NS_ASSUME_NONNULL_BEGIN
  36. /**
  37. * FSTDatastore represents a proxy for the remote server, hiding details of the RPC layer. It:
  38. *
  39. * - Manages connections to the server
  40. * - Authenticates to the server
  41. * - Manages threading and keeps higher-level code running on the worker queue
  42. * - Serializes internal model objects to and from protocol buffers
  43. *
  44. * The FSTDatastore is generally not responsible for understanding the higher-level protocol
  45. * involved in actually making changes or reading data, and aside from the connections it manages
  46. * is otherwise stateless.
  47. */
  48. @interface FSTDatastore : NSObject
  49. /** Creates a new Datastore instance with the given database info. */
  50. + (instancetype)datastoreWithDatabase:(const firebase::firestore::core::DatabaseInfo *)databaseInfo
  51. workerQueue:(firebase::firestore::util::AsyncQueue *)workerQueue
  52. credentials:(firebase::firestore::auth::CredentialsProvider *)
  53. credentials; // no passing ownership
  54. - (instancetype)init __attribute__((unavailable("Use a static constructor method.")));
  55. - (instancetype)initWithDatabaseInfo:(const firebase::firestore::core::DatabaseInfo *)databaseInfo
  56. workerQueue:(firebase::firestore::util::AsyncQueue *)workerQueue
  57. credentials:(firebase::firestore::auth::CredentialsProvider *)
  58. credentials // no passing ownership
  59. NS_DESIGNATED_INITIALIZER;
  60. - (void)shutdown;
  61. /** Converts the error to a FIRFirestoreErrorDomain error. */
  62. + (NSError *)firestoreErrorForError:(NSError *)error;
  63. /** Returns YES if the given error is a GRPC ABORTED error. **/
  64. + (BOOL)isAbortedError:(NSError *)error;
  65. /**
  66. * Determines whether an error code represents a permanent error when received in response to a
  67. * non-write operation.
  68. *
  69. * See +isPermanentWriteError for classifying write errors.
  70. */
  71. + (BOOL)isPermanentError:(NSError *)error;
  72. /**
  73. * Determines whether an error code represents a permanent error when received in response to a
  74. * write operation.
  75. *
  76. * Write operations must be handled specially because as of b/119437764, ABORTED errors on the write
  77. * stream should be retried too (even though ABORTED errors are not generally retryable).
  78. *
  79. * Note that during the initial handshake on the write stream an ABORTED error signals that we
  80. * should discard our stream token (i.e. it is permanent). This means a handshake error should be
  81. * classified with isPermanentError, above.
  82. */
  83. + (BOOL)isPermanentWriteError:(NSError *)error;
  84. /** Looks up a list of documents in datastore. */
  85. - (void)lookupDocuments:(const std::vector<firebase::firestore::model::DocumentKey> &)keys
  86. completion:(FSTVoidMaybeDocumentArrayErrorBlock)completion;
  87. /** Commits data to datastore. */
  88. - (void)commitMutations:(NSArray<FSTMutation *> *)mutations
  89. completion:(FSTVoidErrorBlock)completion;
  90. /** Creates a new watch stream. */
  91. - (std::shared_ptr<firebase::firestore::remote::WatchStream>)createWatchStreamWithDelegate:
  92. (id<FSTWatchStreamDelegate>)delegate;
  93. /** Creates a new write stream. */
  94. - (std::shared_ptr<firebase::firestore::remote::WriteStream>)createWriteStreamWithDelegate:
  95. (id<FSTWriteStreamDelegate>)delegate;
  96. /** The name of the database and the backend. */
  97. // Does not own this DatabaseInfo.
  98. @property(nonatomic, assign, readonly) const firebase::firestore::core::DatabaseInfo *databaseInfo;
  99. @end
  100. NS_ASSUME_NONNULL_END