Skip to content

Commit 3942034

Browse files
Clean up LocalStore initialization
This removes duplicate code from the LocalStore constructor and handleUserChange
1 parent 99f6865 commit 3942034

File tree

1 file changed

+21
-29
lines changed
  • firebase-firestore/src/main/java/com/google/firebase/firestore/local

1 file changed

+21
-29
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalStore.java

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public final class LocalStore implements BundleCallback {
115115
private IndexManager indexManager;
116116

117117
/** Manages field index backfill. */
118-
private final @Nullable IndexBackfiller indexBackfiller;
118+
private @Nullable IndexBackfiller indexBackfiller;
119119

120120
/** The set of all mutations that have been sent but not yet been applied to the backend. */
121121
private MutationQueue mutationQueue;
@@ -158,31 +158,38 @@ public LocalStore(
158158
hardAssert(
159159
persistence.isStarted(), "LocalStore was passed an unstarted persistence implementation");
160160
this.persistence = persistence;
161+
this.queryEngine = queryEngine;
162+
this.indexBackfiller = indexBackfiller;
163+
161164
targetCache = persistence.getTargetCache();
162165
bundleCache = persistence.getBundleCache();
163166
targetIdGenerator = TargetIdGenerator.forTargetCache(targetCache.getHighestTargetId());
164-
indexManager = persistence.getIndexManager(initialUser);
165-
mutationQueue = persistence.getMutationQueue(initialUser, indexManager);
166-
documentOverlayCache = persistence.getDocumentOverlay(initialUser);
167167
remoteDocuments = persistence.getRemoteDocumentCache();
168+
localViewReferences = new ReferenceSet();
169+
queryDataByTarget = new SparseArray<>();
170+
targetIdByTarget = new HashMap<>();
171+
172+
persistence.getReferenceDelegate().setInMemoryPins(localViewReferences);
173+
174+
initializeUserComponents(initialUser);
175+
}
176+
177+
private void initializeUserComponents(User user) {
178+
indexManager = persistence.getIndexManager(user);
179+
mutationQueue = persistence.getMutationQueue(user, indexManager);
180+
documentOverlayCache = persistence.getDocumentOverlay(user);
181+
182+
// Recreate our LocalDocumentsView using the new MutationQueue.
168183
localDocuments =
169184
new LocalDocumentsView(remoteDocuments, mutationQueue, documentOverlayCache, indexManager);
170-
this.queryEngine = queryEngine;
171185
queryEngine.initialize(localDocuments, indexManager);
172186

173-
localViewReferences = new ReferenceSet();
174-
persistence.getReferenceDelegate().setInMemoryPins(localViewReferences);
175-
187+
// TODO(indexing): Add spec tests that test these components change after a user change
176188
remoteDocuments.setIndexManager(indexManager);
177-
178-
this.indexBackfiller = indexBackfiller;
179189
if (indexBackfiller != null) {
180190
indexBackfiller.setIndexManager(indexManager);
181191
indexBackfiller.setLocalDocumentsView(localDocuments);
182192
}
183-
184-
queryDataByTarget = new SparseArray<>();
185-
targetIdByTarget = new HashMap<>();
186193
}
187194

188195
public void start() {
@@ -205,27 +212,12 @@ public ImmutableSortedMap<DocumentKey, Document> handleUserChange(User user) {
205212
// Swap out the mutation queue, grabbing the pending mutation batches before and after.
206213
List<MutationBatch> oldBatches = mutationQueue.getAllMutationBatches();
207214

208-
indexManager = persistence.getIndexManager(user);
209-
mutationQueue = persistence.getMutationQueue(user, indexManager);
210-
documentOverlayCache = persistence.getDocumentOverlay(user);
211-
215+
initializeUserComponents(user);
212216
startIndexManager();
213217
startMutationQueue();
214218

215219
List<MutationBatch> newBatches = mutationQueue.getAllMutationBatches();
216220

217-
// Recreate our LocalDocumentsView using the new MutationQueue.
218-
localDocuments =
219-
new LocalDocumentsView(remoteDocuments, mutationQueue, documentOverlayCache, indexManager);
220-
queryEngine.initialize(localDocuments, indexManager);
221-
222-
// TODO(indexing): Add spec tests that test these components change after a user change
223-
remoteDocuments.setIndexManager(indexManager);
224-
if (indexBackfiller != null) {
225-
indexBackfiller.setIndexManager(indexManager);
226-
indexBackfiller.setLocalDocumentsView(localDocuments);
227-
}
228-
229221
// Union the old/new changed keys.
230222
ImmutableSortedSet<DocumentKey> changedKeys = DocumentKey.emptyKeySet();
231223
for (List<MutationBatch> batches : asList(oldBatches, newBatches)) {

0 commit comments

Comments
 (0)