Skip to content

Clean up LocalStore initialization #3344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,36 @@ public LocalStore(
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() {
Expand All @@ -205,27 +210,12 @@ public ImmutableSortedMap<DocumentKey, Document> handleUserChange(User user) {
// 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)) {
Expand Down