Skip to content

Commit 6ca237f

Browse files
author
Brian Chen
committed
resolve comments, simplify some abstractions
1 parent 90fcdc3 commit 6ca237f

18 files changed

+168
-294
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/core/ComponentProvider.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.google.firebase.firestore.auth.User;
2121
import com.google.firebase.firestore.local.LocalStore;
2222
import com.google.firebase.firestore.local.Persistence;
23-
import com.google.firebase.firestore.local.StartStopScheduler;
23+
import com.google.firebase.firestore.local.Scheduler;
2424
import com.google.firebase.firestore.remote.ConnectivityMonitor;
2525
import com.google.firebase.firestore.remote.Datastore;
2626
import com.google.firebase.firestore.remote.RemoteStore;
@@ -39,8 +39,8 @@ public abstract class ComponentProvider {
3939
private RemoteStore remoteStore;
4040
private EventManager eventManager;
4141
private ConnectivityMonitor connectivityMonitor;
42-
@Nullable private StartStopScheduler garbageCollectionScheduler;
43-
@Nullable private StartStopScheduler indexBackfillScheduler;
42+
@Nullable private Scheduler garbageCollectionScheduler;
43+
@Nullable private Scheduler indexBackfillScheduler;
4444

4545
/** Configuration options for the component provider. */
4646
public static class Configuration {
@@ -104,12 +104,12 @@ public Persistence getPersistence() {
104104
}
105105

106106
@Nullable
107-
public StartStopScheduler getGarbageCollectionScheduler() {
107+
public Scheduler getGarbageCollectionScheduler() {
108108
return garbageCollectionScheduler;
109109
}
110110

111111
@Nullable
112-
public StartStopScheduler getIndexBackfillScheduler() {
112+
public Scheduler getIndexBackfillScheduler() {
113113
return indexBackfillScheduler;
114114
}
115115

@@ -147,10 +147,9 @@ public void initialize(Configuration configuration) {
147147
indexBackfillScheduler = createIndexBackfillScheduler(configuration);
148148
}
149149

150-
protected abstract StartStopScheduler createGarbageCollectionScheduler(
151-
Configuration configuration);
150+
protected abstract Scheduler createGarbageCollectionScheduler(Configuration configuration);
152151

153-
protected abstract StartStopScheduler createIndexBackfillScheduler(Configuration configuration);
152+
protected abstract Scheduler createIndexBackfillScheduler(Configuration configuration);
154153

155154
protected abstract EventManager createEventManager(Configuration configuration);
156155

firebase-firestore/src/main/java/com/google/firebase/firestore/core/FirestoreClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import com.google.firebase.firestore.local.LocalStore;
3636
import com.google.firebase.firestore.local.Persistence;
3737
import com.google.firebase.firestore.local.QueryResult;
38-
import com.google.firebase.firestore.local.StartStopScheduler;
38+
import com.google.firebase.firestore.local.Scheduler;
3939
import com.google.firebase.firestore.model.Document;
4040
import com.google.firebase.firestore.model.DocumentKey;
4141
import com.google.firebase.firestore.model.FieldIndex;
@@ -74,9 +74,9 @@ public final class FirestoreClient {
7474
private EventManager eventManager;
7575

7676
// LRU-related
77-
@Nullable private StartStopScheduler gcScheduler;
77+
@Nullable private Scheduler gcScheduler;
7878

79-
@Nullable private StartStopScheduler indexScheduler;
79+
@Nullable private Scheduler indexScheduler;
8080

8181
public FirestoreClient(
8282
final Context context,
@@ -146,7 +146,7 @@ public Task<Void> terminate() {
146146
gcScheduler.stop();
147147
}
148148

149-
if (indexScheduler != null) {
149+
if (indexScheduler != null && Persistence.INDEXING_SUPPORT_ENABLED) {
150150
indexScheduler.stop();
151151
}
152152
});
@@ -272,7 +272,7 @@ private void initialize(Context context, User user, FirebaseFirestoreSettings se
272272
gcScheduler.start();
273273
}
274274

275-
if (indexScheduler != null) {
275+
if (indexScheduler != null && Persistence.INDEXING_SUPPORT_ENABLED) {
276276
indexScheduler.start();
277277
}
278278
}

firebase-firestore/src/main/java/com/google/firebase/firestore/core/MemoryComponentProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.google.firebase.firestore.local.LocalStore;
2121
import com.google.firebase.firestore.local.MemoryPersistence;
2222
import com.google.firebase.firestore.local.Persistence;
23-
import com.google.firebase.firestore.local.StartStopScheduler;
23+
import com.google.firebase.firestore.local.Scheduler;
2424
import com.google.firebase.firestore.model.DocumentKey;
2525
import com.google.firebase.firestore.model.mutation.MutationBatchResult;
2626
import com.google.firebase.firestore.remote.AndroidConnectivityMonitor;
@@ -36,13 +36,13 @@ public class MemoryComponentProvider extends ComponentProvider {
3636

3737
@Override
3838
@Nullable
39-
protected StartStopScheduler createGarbageCollectionScheduler(Configuration configuration) {
39+
protected Scheduler createGarbageCollectionScheduler(Configuration configuration) {
4040
return null;
4141
}
4242

4343
@Override
4444
@Nullable
45-
protected StartStopScheduler createIndexBackfillScheduler(Configuration configuration) {
45+
protected Scheduler createIndexBackfillScheduler(Configuration configuration) {
4646
return null;
4747
}
4848

firebase-firestore/src/main/java/com/google/firebase/firestore/core/SQLiteComponentProvider.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,23 @@
1919
import com.google.firebase.firestore.local.LruDelegate;
2020
import com.google.firebase.firestore.local.LruGarbageCollector;
2121
import com.google.firebase.firestore.local.Persistence;
22-
import com.google.firebase.firestore.local.SQLiteIndexBackfillerDelegate;
2322
import com.google.firebase.firestore.local.SQLitePersistence;
24-
import com.google.firebase.firestore.local.StartStopScheduler;
23+
import com.google.firebase.firestore.local.Scheduler;
2524
import com.google.firebase.firestore.remote.RemoteSerializer;
2625

2726
/** Provides all components needed for Firestore with SQLite persistence. */
2827
public class SQLiteComponentProvider extends MemoryComponentProvider {
2928

3029
@Override
31-
protected StartStopScheduler createGarbageCollectionScheduler(Configuration configuration) {
30+
protected Scheduler createGarbageCollectionScheduler(Configuration configuration) {
3231
LruDelegate lruDelegate = ((SQLitePersistence) getPersistence()).getReferenceDelegate();
3332
LruGarbageCollector gc = lruDelegate.getGarbageCollector();
3433
return gc.newScheduler(configuration.getAsyncQueue(), getLocalStore());
3534
}
3635

3736
@Override
38-
protected StartStopScheduler createIndexBackfillScheduler(Configuration configuration) {
39-
SQLiteIndexBackfillerDelegate indexBackfillDelegate =
40-
((SQLitePersistence) getPersistence()).getIndexBackfillDelegate();
41-
IndexBackfiller indexBackfiller = indexBackfillDelegate.getIndexBackfiller();
37+
protected Scheduler createIndexBackfillScheduler(Configuration configuration) {
38+
IndexBackfiller indexBackfiller = ((SQLitePersistence) getPersistence()).getIndexBackfiller();
4239
return indexBackfiller.newScheduler(configuration.getAsyncQueue(), getLocalStore());
4340
}
4441

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

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,25 @@
1515
package com.google.firebase.firestore.local;
1616

1717
import androidx.annotation.Nullable;
18+
import androidx.annotation.VisibleForTesting;
19+
import com.google.firebase.firestore.index.IndexEntry;
1820
import com.google.firebase.firestore.util.AsyncQueue;
1921
import java.util.concurrent.TimeUnit;
2022

23+
/**
24+
* Implements the steps for backfilling indexes.
25+
*/
2126
public class IndexBackfiller {
22-
/** How long we wait to try running LRU GC after SDK initialization. */
23-
private static final long INITIAL_GC_DELAY_MS = TimeUnit.MINUTES.toMillis(1);
24-
/** Minimum amount of time between GC checks, after the first one. */
25-
private static final long REGULAR_GC_DELAY_MS = TimeUnit.MINUTES.toMillis(5);
27+
/** How long we wait to try running index backfill after SDK initialization. */
28+
private static final long INITIAL_BACKFILL_DELAY_MS = TimeUnit.SECONDS.toMillis(15);
29+
/** Minimum amount of time between backfill checks, after the first one. */
30+
private static final long REGULAR_BACKFILL_DELAY_MS = TimeUnit.MINUTES.toMillis(1);
31+
32+
private final SQLitePersistence persistence;
33+
34+
public IndexBackfiller(SQLitePersistence sqLitePersistence) {
35+
this.persistence = sqLitePersistence;
36+
}
2637

2738
public static class Results {
2839
private final boolean hasRun;
@@ -53,13 +64,13 @@ public int getEntriesRemoved() {
5364
}
5465
}
5566

56-
public class Scheduler implements StartStopScheduler {
67+
public class BackfillScheduler implements Scheduler {
5768
private final AsyncQueue asyncQueue;
5869
private final LocalStore localStore;
5970
private boolean hasRun = false;
60-
@Nullable private AsyncQueue.DelayedTask gcTask;
71+
@Nullable private AsyncQueue.DelayedTask backfillTask;
6172

62-
public Scheduler(AsyncQueue asyncQueue, LocalStore localStore) {
73+
public BackfillScheduler(AsyncQueue asyncQueue, LocalStore localStore) {
6374
this.asyncQueue = asyncQueue;
6475
this.localStore = localStore;
6576
}
@@ -71,16 +82,16 @@ public void start() {
7182

7283
@Override
7384
public void stop() {
74-
if (gcTask != null) {
75-
gcTask.cancel();
85+
if (backfillTask != null) {
86+
backfillTask.cancel();
7687
}
7788
}
7889

7990
private void scheduleBackfill() {
80-
long delay = hasRun ? REGULAR_GC_DELAY_MS : INITIAL_GC_DELAY_MS;
81-
gcTask =
91+
long delay = hasRun ? REGULAR_BACKFILL_DELAY_MS : INITIAL_BACKFILL_DELAY_MS;
92+
backfillTask =
8293
asyncQueue.enqueueAfterDelay(
83-
AsyncQueue.TimerId.GARBAGE_COLLECTION,
94+
AsyncQueue.TimerId.INDEX_BACKFILL,
8495
delay,
8596
() -> {
8697
localStore.backfillIndexes(IndexBackfiller.this);
@@ -90,21 +101,52 @@ private void scheduleBackfill() {
90101
}
91102
}
92103

93-
private final IndexBackfillerDelegate delegate;
94-
95-
IndexBackfiller(IndexBackfillerDelegate delegate) {
96-
this.delegate = delegate;
97-
}
98-
99-
public Scheduler newScheduler(AsyncQueue asyncQueue, LocalStore localStore) {
100-
return new Scheduler(asyncQueue, localStore);
104+
public BackfillScheduler newScheduler(AsyncQueue asyncQueue, LocalStore localStore) {
105+
return new BackfillScheduler(asyncQueue, localStore);
101106
}
102107

103108
// TODO: Figure out which index entries to backfill.
104-
Results backfill() {
109+
public Results backfill() {
105110
int numIndexesWritten = 0;
106111
int numIndexesRemoved = 0;
107-
delegate.addIndexEntry();
108112
return new Results(/* hasRun= */ true, numIndexesWritten, numIndexesRemoved);
109113
}
114+
115+
public void addIndexEntry(IndexEntry entry) {
116+
persistence.execute(
117+
"INSERT OR IGNORE INTO index_entries ("
118+
+ "index_id, "
119+
+ "index_value, "
120+
+ "uid, "
121+
+ "document_id) VALUES(?, ?, ?, ?)",
122+
entry.getIndexId(),
123+
entry.getIndexValue(),
124+
entry.getUid(),
125+
entry.getDocumentId());
126+
}
127+
128+
public void removeIndexEntry(int indexId, String uid, String documentId) {
129+
persistence.execute(
130+
"DELETE FROM index_entries "
131+
+ "WHERE index_id = ? "
132+
+ "AND uid = ?"
133+
+ "AND document_id = ?",
134+
indexId,
135+
uid,
136+
documentId);
137+
;
138+
}
139+
140+
@Nullable
141+
@VisibleForTesting
142+
public IndexEntry getIndexEntry(int indexId) {
143+
return persistence
144+
.query("SELECT index_value, uid, document_id FROM index_entries WHERE index_id = ?")
145+
.binding(indexId)
146+
.firstValue(
147+
row ->
148+
row == null
149+
? null
150+
: new IndexEntry(indexId, row.getBlob(0), row.getString(1), row.getString(2)));
151+
}
110152
}

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ public int getDocumentsRemoved() {
111111
* This class is responsible for the scheduling of LRU garbage collection. It handles checking
112112
* whether or not GC is enabled, as well as which delay to use before the next run.
113113
*/
114-
public class Scheduler implements StartStopScheduler {
114+
public class GCScheduler implements Scheduler {
115115
private final AsyncQueue asyncQueue;
116116
private final LocalStore localStore;
117117
private boolean hasRun = false;
118118
@Nullable private AsyncQueue.DelayedTask gcTask;
119119

120-
public Scheduler(AsyncQueue asyncQueue, LocalStore localStore) {
120+
public GCScheduler(AsyncQueue asyncQueue, LocalStore localStore) {
121121
this.asyncQueue = asyncQueue;
122122
this.localStore = localStore;
123123
}
@@ -159,8 +159,8 @@ private void scheduleGC() {
159159
}
160160

161161
/** A helper method to create a new scheduler. */
162-
public Scheduler newScheduler(AsyncQueue asyncQueue, LocalStore localStore) {
163-
return new Scheduler(asyncQueue, localStore);
162+
public GCScheduler newScheduler(AsyncQueue asyncQueue, LocalStore localStore) {
163+
return new GCScheduler(asyncQueue, localStore);
164164
}
165165

166166
/** Given a percentile of target to collect, returns the number of targets to collect. */

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)