Clean up LocalStore initialization
@@ -158,31 +158,36 @@ public final class LocalStore implements BundleCallback {
hardAssert(
persistence.isStarted(), "LocalStore was passed an unstarted persistence implementation");
this.persistence = persistence;
+ this.queryEngine = queryEngine;
+ this.indexBackfiller = indexBackfiller;
+
targetCache = persistence.getTargetCache();
bundleCache = persistence.getBundleCache();
targetIdGenerator = TargetIdGenerator.forTargetCache(targetCache.getHighestTargetId());
- indexManager = persistence.getIndexManager(initialUser);
- mutationQueue = persistence.getMutationQueue(initialUser, indexManager);
- documentOverlayCache = persistence.getDocumentOverlay(initialUser);
remoteDocuments = persistence.getRemoteDocumentCache();
- localDocuments =
- new LocalDocumentsView(remoteDocuments, mutationQueue, documentOverlayCache, indexManager);
- this.queryEngine = queryEngine;
- queryEngine.initialize(localDocuments, indexManager);
-
localViewReferences = new ReferenceSet();
+ queryDataByTarget = new SparseArray<>();
+ targetIdByTarget = new HashMap<>();
+
persistence.getReferenceDelegate().setInMemoryPins(localViewReferences);
- remoteDocuments.setIndexManager(indexManager);
+ initializeUserComponents(initialUser);
+ }
- this.indexBackfiller = indexBackfiller;
+ private void initializeUserComponents(User user) {
+ // TODO(indexing): Add spec tests that test these components change after a user change
+ indexManager = persistence.getIndexManager(user);
+ mutationQueue = persistence.getMutationQueue(user, indexManager);
+ documentOverlayCache = persistence.getDocumentOverlay(user);
+ localDocuments =
+ new LocalDocumentsView(remoteDocuments, mutationQueue, documentOverlayCache, indexManager);
+
+ remoteDocuments.setIndexManager(indexManager);
+ queryEngine.initialize(localDocuments, indexManager);
if (indexBackfiller != null) {
indexBackfiller.setIndexManager(indexManager);
indexBackfiller.setLocalDocumentsView(localDocuments);
}
-
- queryDataByTarget = new SparseArray<>();
- targetIdByTarget = new HashMap<>();
}
public void start() {
@@ -205,27 +210,12 @@ public final class LocalStore implements BundleCallback {
// Swap out the mutation queue, grabbing the pending mutation batches before and after.
List<MutationBatch> oldBatches = mutationQueue.getAllMutationBatches();
- indexManager = persistence.getIndexManager(user);
- mutationQueue = persistence.getMutationQueue(user, indexManager);
- documentOverlayCache = persistence.getDocumentOverlay(user);
-
+ initializeUserComponents(user);
startIndexManager();
startMutationQueue();
List<MutationBatch> newBatches = mutationQueue.getAllMutationBatches();
- // Recreate our LocalDocumentsView using the new MutationQueue.
- localDocuments =
- new LocalDocumentsView(remoteDocuments, mutationQueue, documentOverlayCache, indexManager);
- queryEngine.initialize(localDocuments, indexManager);
-
- // TODO(indexing): Add spec tests that test these components change after a user change
- remoteDocuments.setIndexManager(indexManager);
- if (indexBackfiller != null) {
- indexBackfiller.setIndexManager(indexManager);
- indexBackfiller.setLocalDocumentsView(localDocuments);
- }
-
// Union the old/new changed keys.
ImmutableSortedSet<DocumentKey> changedKeys = DocumentKey.emptyKeySet();
for (List<MutationBatch> batches : asList(oldBatches, newBatches)) {
