Skip to content

Commit e8b0c1f

Browse files
authored
C++ migration: make all methods of FSTRemoteStore delegate to C++ (#2337)
1 parent 9b3654b commit e8b0c1f

File tree

3 files changed

+158
-123
lines changed

3 files changed

+158
-123
lines changed

Firestore/Source/Remote/FSTRemoteStore.mm

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "Firestore/core/src/firebase/firestore/auth/user.h"
3333
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
34+
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
3435
#include "Firestore/core/src/firebase/firestore/model/mutation_batch.h"
3536
#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
3637
#include "Firestore/core/src/firebase/firestore/remote/online_state_tracker.h"
@@ -52,7 +53,6 @@
5253
using firebase::firestore::model::DocumentKeySet;
5354
using firebase::firestore::model::OnlineState;
5455
using firebase::firestore::model::SnapshotVersion;
55-
using firebase::firestore::model::DocumentKeySet;
5656
using firebase::firestore::model::TargetId;
5757
using firebase::firestore::remote::Datastore;
5858
using firebase::firestore::remote::WatchStream;
@@ -75,9 +75,6 @@
7575
#pragma mark - FSTRemoteStore
7676

7777
@implementation FSTRemoteStore {
78-
/** The client-side proxy for interacting with the backend. */
79-
std::shared_ptr<Datastore> _datastore;
80-
8178
std::unique_ptr<RemoteStore> _remoteStore;
8279
}
8380

@@ -86,12 +83,8 @@ - (instancetype)initWithLocalStore:(FSTLocalStore *)localStore
8683
workerQueue:(AsyncQueue *)queue
8784
onlineStateHandler:(std::function<void(OnlineState)>)onlineStateHandler {
8885
if (self = [super init]) {
89-
_datastore = std::move(datastore);
90-
_datastore->Start();
91-
92-
_remoteStore = absl::make_unique<RemoteStore>(localStore, _datastore.get(), queue,
86+
_remoteStore = absl::make_unique<RemoteStore>(localStore, std::move(datastore), queue,
9387
std::move(onlineStateHandler));
94-
_remoteStore->set_is_network_enabled(false);
9588
}
9689
return self;
9790
}
@@ -101,75 +94,27 @@ - (void)setSyncEngine:(id<FSTRemoteSyncer>)syncEngine {
10194
}
10295

10396
- (void)start {
104-
// For now, all setup is handled by enableNetwork(). We might expand on this in the future.
105-
[self enableNetwork];
97+
_remoteStore->Start();
10698
}
10799

108100
#pragma mark Online/Offline state
109101

110102
- (void)enableNetwork {
111-
_remoteStore->set_is_network_enabled(true);
112-
113-
if (_remoteStore->CanUseNetwork()) {
114-
// Load any saved stream token from persistent storage
115-
_remoteStore->write_stream().SetLastStreamToken([_remoteStore->local_store() lastStreamToken]);
116-
117-
if (_remoteStore->ShouldStartWatchStream()) {
118-
_remoteStore->StartWatchStream();
119-
} else {
120-
_remoteStore->online_state_tracker().UpdateState(OnlineState::Unknown);
121-
}
122-
123-
// This will start the write stream if necessary.
124-
[self fillWritePipeline];
125-
}
103+
_remoteStore->EnableNetwork();
126104
}
127105

128106
- (void)disableNetwork {
129-
_remoteStore->set_is_network_enabled(false);
130-
[self disableNetworkInternal];
131-
132-
// Set the OnlineState to Offline so get()s return from cache, etc.
133-
_remoteStore->online_state_tracker().UpdateState(OnlineState::Offline);
134-
}
135-
136-
/** Disables the network, setting the OnlineState to the specified targetOnlineState. */
137-
- (void)disableNetworkInternal {
138-
_remoteStore->watch_stream().Stop();
139-
_remoteStore->write_stream().Stop();
140-
141-
if (!_remoteStore->write_pipeline().empty()) {
142-
LOG_DEBUG("Stopping write stream with %s pending writes",
143-
_remoteStore->write_pipeline().size());
144-
_remoteStore->write_pipeline().clear();
145-
}
146-
147-
_remoteStore->CleanUpWatchStreamState();
107+
_remoteStore->DisableNetwork();
148108
}
149109

150110
#pragma mark Shutdown
151111

152112
- (void)shutdown {
153-
LOG_DEBUG("FSTRemoteStore %s shutting down", (__bridge void *)self);
154-
_remoteStore->set_is_network_enabled(false);
155-
[self disableNetworkInternal];
156-
// Set the OnlineState to Unknown (rather than Offline) to avoid potentially triggering
157-
// spurious listener events with cached data, etc.
158-
_remoteStore->online_state_tracker().UpdateState(OnlineState::Unknown);
159-
_datastore->Shutdown();
113+
_remoteStore->Shutdown();
160114
}
161115

162116
- (void)credentialDidChange {
163-
if (_remoteStore->CanUseNetwork()) {
164-
// Tear down and re-create our network streams. This will ensure we get a fresh auth token
165-
// for the new user and re-fill the write pipeline with new mutations from the LocalStore
166-
// (since mutations are per-user).
167-
LOG_DEBUG("FSTRemoteStore %s restarting streams for new credential", (__bridge void *)self);
168-
_remoteStore->set_is_network_enabled(false);
169-
[self disableNetworkInternal];
170-
_remoteStore->online_state_tracker().UpdateState(OnlineState::Unknown);
171-
[self enableNetwork];
172-
}
117+
_remoteStore->HandleCredentialChange();
173118
}
174119

175120
#pragma mark Watch Stream
@@ -201,7 +146,7 @@ - (void)addBatchToWritePipeline:(FSTMutationBatch *)batch {
201146
}
202147

203148
- (FSTTransaction *)transaction {
204-
return [FSTTransaction transactionWithDatastore:_datastore.get()];
149+
return _remoteStore->CreateTransaction();
205150
}
206151

207152
@end

Firestore/core/src/firebase/firestore/remote/remote_store.h

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
@class FSTMutationBatch;
4444
@class FSTMutationBatchResult;
4545
@class FSTQueryData;
46+
@class FSTTransaction;
4647

4748
NS_ASSUME_NONNULL_BEGIN
4849

@@ -112,48 +113,72 @@ class RemoteStore : public TargetMetadataProvider,
112113
public WriteStreamCallback {
113114
public:
114115
RemoteStore(FSTLocalStore* local_store,
115-
Datastore* datastore,
116+
std::shared_ptr<Datastore> datastore,
116117
util::AsyncQueue* worker_queue,
117118
std::function<void(model::OnlineState)> online_state_handler);
118119

119-
// TODO(varconst): remove the getters and setters
120-
121-
id<FSTRemoteSyncer> sync_engine() {
122-
return sync_engine_;
123-
}
124120
void set_sync_engine(id<FSTRemoteSyncer> sync_engine) {
125121
sync_engine_ = sync_engine;
126122
}
127123

128-
FSTLocalStore* local_store() {
129-
return local_store_;
130-
}
124+
/**
125+
* Starts up the remote store, creating streams, restoring state from
126+
* `FSTLocalStore`, etc.
127+
*/
128+
void Start();
131129

132-
OnlineStateTracker& online_state_tracker() {
133-
return online_state_tracker_;
134-
}
130+
/**
131+
* Shuts down the remote store, tearing down connections and otherwise
132+
* cleaning up.
133+
*/
134+
void Shutdown();
135135

136-
void set_is_network_enabled(bool value) {
137-
is_network_enabled_ = value;
138-
}
136+
/**
137+
* Temporarily disables the network. The network can be re-enabled using
138+
* 'EnableNetwork'.
139+
*/
140+
void DisableNetwork();
139141

140-
WatchStream& watch_stream() {
141-
return *watch_stream_;
142-
}
143-
WriteStream& write_stream() {
144-
return *write_stream_;
145-
}
142+
/**
143+
* Re-enables the network. Only to be called as the counterpart to
144+
* 'DisableNetwork'.
145+
*/
146+
void EnableNetwork();
146147

147-
std::vector<FSTMutationBatch*>& write_pipeline() {
148-
return write_pipeline_;
149-
}
148+
/**
149+
* Tells the `RemoteStore` that the currently authenticated user has changed.
150+
*
151+
* In response the remote store tears down streams and clears up any tracked
152+
* operations that should not persist across users. Restarts the streams if
153+
* appropriate.
154+
*/
155+
void HandleCredentialChange();
150156

151157
/** Listens to the target identified by the given `FSTQueryData`. */
152158
void Listen(FSTQueryData* query_data);
153159

154160
/** Stops listening to the target with the given target ID. */
155161
void StopListening(model::TargetId target_id);
156162

163+
/**
164+
* Attempts to fill our write pipeline with writes from the `FSTLocalStore`.
165+
*
166+
* Called internally to bootstrap or refill the write pipeline and by
167+
* `FSTSyncEngine` whenever there are new mutations to process.
168+
*
169+
* Starts the write stream if necessary.
170+
*/
171+
void FillWritePipeline();
172+
173+
/**
174+
* Queues additional writes to be sent to the write stream, sending them
175+
* immediately if the write stream is established.
176+
*/
177+
void AddToWritePipeline(FSTMutationBatch* batch);
178+
179+
/** Returns a new transaction backed by this remote store. */
180+
FSTTransaction* CreateTransaction();
181+
157182
model::DocumentKeySet GetRemoteKeysForTarget(
158183
model::TargetId target_id) const override;
159184
FSTQueryData* GetQueryDataForTarget(model::TargetId target_id) const override;
@@ -171,37 +196,9 @@ class RemoteStore : public TargetMetadataProvider,
171196
model::SnapshotVersion commit_version,
172197
std::vector<FSTMutationResult*> mutation_results) override;
173198

174-
// TODO(varconst): make the following methods private.
175-
176-
bool CanUseNetwork() const;
177-
178-
void StartWatchStream();
179-
180-
/**
181-
* Returns true if the network is enabled, the watch stream has not yet been
182-
* started and there are active watch targets.
183-
*/
184-
bool ShouldStartWatchStream() const;
185-
186-
void CleanUpWatchStreamState();
187-
188-
/**
189-
* Attempts to fill our write pipeline with writes from the `FSTLocalStore`.
190-
*
191-
* Called internally to bootstrap or refill the write pipeline and by
192-
* `FSTSyncEngine` whenever there are new mutations to process.
193-
*
194-
* Starts the write stream if necessary.
195-
*/
196-
void FillWritePipeline();
197-
198-
/**
199-
* Queues additional writes to be sent to the write stream, sending them
200-
* immediately if the write stream is established.
201-
*/
202-
void AddToWritePipeline(FSTMutationBatch* batch);
203-
204199
private:
200+
void DisableNetworkInternal();
201+
205202
void SendWatchRequest(FSTQueryData* query_data);
206203
void SendUnwatchRequest(model::TargetId target_id);
207204

@@ -231,14 +228,29 @@ class RemoteStore : public TargetMetadataProvider,
231228
void HandleHandshakeError(const util::Status& status);
232229
void HandleWriteError(const util::Status& status);
233230

231+
bool CanUseNetwork() const;
232+
233+
void StartWatchStream();
234+
235+
/**
236+
* Returns true if the network is enabled, the watch stream has not yet been
237+
* started and there are active watch targets.
238+
*/
239+
bool ShouldStartWatchStream() const;
240+
241+
void CleanUpWatchStreamState();
242+
234243
id<FSTRemoteSyncer> sync_engine_ = nil;
235244

236245
/**
237246
* The local store, used to fill the write pipeline with outbound mutations
238-
* and resolve existence filter mismatches. Immutable after initialization.
247+
* and resolve existence filter mismatches.
239248
*/
240249
FSTLocalStore* local_store_ = nil;
241250

251+
/** The client-side proxy for interacting with the backend. */
252+
std::shared_ptr<Datastore> datastore_;
253+
242254
/**
243255
* A mapping of watched targets that the client cares about tracking and the
244256
* user has explicitly called a 'listen' for this target.
@@ -253,8 +265,8 @@ class RemoteStore : public TargetMetadataProvider,
253265
OnlineStateTracker online_state_tracker_;
254266

255267
/**
256-
* Set to true by `EnableNetwork` and false by `DisableNetworkInternal` and
257-
* indicates the user-preferred network state.
268+
* Set to true by `EnableNetwork` and false by `DisableNetwork` and indicates
269+
* the user-preferred network state.
258270
*/
259271
bool is_network_enabled_ = false;
260272

0 commit comments

Comments
 (0)