Skip to content

Commit 4620993

Browse files
committed
Make local store final again, and take steps towards extracting logic about user state into a separate class.
1 parent c043afb commit 4620993

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public class IndexBackfiller {
3939

4040
private final Scheduler scheduler;
4141
private final Persistence persistence;
42-
private final LocalStore localStore;
42+
private final UserComponents components;
4343
private int maxDocumentsToProcess = MAX_DOCUMENTS_TO_PROCESS;
4444

45-
public IndexBackfiller(Persistence persistence, AsyncQueue asyncQueue, LocalStore localStore) {
45+
public IndexBackfiller(Persistence persistence, AsyncQueue asyncQueue, UserComponents components) {
4646
this.persistence = persistence;
47-
this.localStore = localStore;
47+
this.components = components;
4848
this.scheduler = new Scheduler(asyncQueue);
4949
}
5050

@@ -92,7 +92,7 @@ public int backfill() {
9292

9393
/** Writes index entries until the cap is reached. Returns the number of documents processed. */
9494
private int writeIndexEntries() {
95-
IndexManager indexManager = localStore.getIndexManager();
95+
IndexManager indexManager = components.getIndexManager();
9696
Set<String> processedCollectionGroups = new HashSet<>();
9797
int documentsRemaining = maxDocumentsToProcess;
9898
while (documentsRemaining > 0) {
@@ -112,8 +112,8 @@ private int writeIndexEntries() {
112112
*/
113113
private int writeEntriesForCollectionGroup(
114114
String collectionGroup, int documentsRemainingUnderCap) {
115-
IndexManager indexManager = localStore.getIndexManager();
116-
LocalDocumentsView localDocumentsView = localStore.getLocalDocuments();
115+
IndexManager indexManager = components.getIndexManager();
116+
LocalDocumentsView localDocumentsView = components.getLocalDocuments();
117117
// Use the earliest offset of all field indexes to query the local cache.
118118
IndexOffset existingOffset = indexManager.getMinOffset(collectionGroup);
119119

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
* <p>The LocalStore must be able to efficiently execute queries against its local cache of the
100100
* documents, to provide the initial set of results before any remote changes have been received.
101101
*/
102-
public class LocalStore implements BundleCallback {
102+
public final class LocalStore implements BundleCallback, UserComponents {
103103
/**
104104
* The maximum time to leave a resume token buffered without writing it out. This value is
105105
* arbitrary: it's long enough to avoid several writes (possibly indefinitely if updates come more
@@ -192,10 +192,12 @@ private void startMutationQueue() {
192192
persistence.runTransaction("Start MutationQueue", () -> mutationQueue.start());
193193
}
194194

195+
@Override
195196
public IndexManager getIndexManager() {
196197
return indexManager;
197198
}
198199

200+
@Override
199201
public LocalDocumentsView getLocalDocuments() {
200202
return localDocuments;
201203
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.google.firebase.firestore.local;
2+
3+
/**
4+
* Components that depends on user identity.
5+
*
6+
* To avoid holding a direct reference to instances, implementation will provide instances that
7+
* are updated according to user changes. This will require instantiating new instances, so clients
8+
* should never retain a copy, but instead always use get methods for the most up to date instance.
9+
*/
10+
public interface UserComponents {
11+
IndexManager getIndexManager();
12+
13+
LocalDocumentsView getLocalDocuments();
14+
}

firebase-firestore/src/test/java/com/google/firebase/firestore/local/IndexBackfillerTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,18 @@ public void setUp() {
8585
persistence.getMutationQueue(User.UNAUTHENTICATED, indexManager),
8686
documentOverlayCache,
8787
indexManager);
88-
LocalStore localStore = mock(LocalStore.class);
89-
when(localStore.getIndexManager()).thenReturn(indexManager);
90-
when(localStore.getLocalDocuments()).thenReturn(localDocumentsView);
91-
backfiller = new IndexBackfiller(persistence, new AsyncQueue(), localStore);
88+
UserComponents components = new UserComponents() {
89+
@Override
90+
public IndexManager getIndexManager() {
91+
return indexManager;
92+
}
93+
94+
@Override
95+
public LocalDocumentsView getLocalDocuments() {
96+
return localDocumentsView;
97+
}
98+
};
99+
backfiller = new IndexBackfiller(persistence, new AsyncQueue(), components);
92100
}
93101

94102
@After

0 commit comments

Comments
 (0)