Skip to content

Commit 7c9c133

Browse files
authored
Release Overlay (#3112)
1 parent 9466242 commit 7c9c133

File tree

16 files changed

+64
-239
lines changed

16 files changed

+64
-239
lines changed

firebase-firestore/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Android changes are not released automatically. Ensure that changes are released
22
by opting into a release at
33
[go/firebase-android-release](http:go/firebase-android-release) (Googlers only).
44

5-
# Unreleased
5+
# 24.1.0
6+
- [changed] Performance optimization for offline usage.
67
- [changed] Improved performance for queries with collections that contain
78
subcollections.
89

firebase-firestore/firebase-firestore.gradle

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@ android.libraryVariants.all { variant ->
112112
} else {
113113
variant.buildConfigField("boolean", "ENABLE_INDEXING", "false")
114114
}
115-
116-
// TODO(Overlay): Delete below once overlay is shipped.
117-
if (localProps['firestoreEnableOverlay']) {
118-
variant.buildConfigField("boolean", "ENABLE_OVERLAY", "true")
119-
} else {
120-
variant.buildConfigField("boolean", "ENABLE_OVERLAY", "false")
121-
}
122115
}
123116

124117
configurations.all {

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/testutil/IntegrationTestUtil.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,6 @@ public static FirebaseFirestore testFirestore(
255255
// This unfortunately is a global setting that affects existing Firestore clients.
256256
Logger.setLogLevel(logLevel);
257257

258-
// TODO(Overlay): Remove below once this is ready to ship.
259-
Persistence.OVERLAY_SUPPORT_ENABLED = true;
260258
Persistence.INDEXING_SUPPORT_ENABLED = true;
261259

262260
Context context = ApplicationProvider.getApplicationContext();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ private void recalculateAndSaveOverlays(Map<DocumentKey, MutableDocument> docs)
203203
// along the way.
204204
for (MutationBatch batch : batches) {
205205
for (DocumentKey key : batch.getKeys()) {
206-
FieldMask mask = batch.applyToLocalView(docs.get(key), masks.get(key));
206+
FieldMask mask = masks.containsKey(key) ? masks.get(key) : FieldMask.EMPTY;
207+
mask = batch.applyToLocalView(docs.get(key), mask);
207208
masks.put(key, mask);
208209
int batchId = batch.getBatchId();
209210
if (!documentsByBatchId.containsKey(batchId)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public abstract class Persistence {
5252

5353
/** Temporary setting for enabling document overlays. */
5454
// TODO(Overlay): Remove this.
55-
public static boolean OVERLAY_SUPPORT_ENABLED = BuildConfig.ENABLE_OVERLAY;
55+
public static boolean OVERLAY_SUPPORT_ENABLED = true;
5656
/** Constant string to indicate a data migration is required to support overlays. */
5757
public static String DATA_MIGRATION_BUILD_OVERLAYS = "BUILD_OVERLAYS";
5858

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,10 @@ class SQLiteSchema {
4949
* The version of the schema. Increase this by one for each migration added to runMigrations
5050
* below.
5151
*/
52-
static final int VERSION = 13;
53-
54-
static final int OVERLAY_SUPPORT_VERSION = VERSION + 1;
52+
static final int VERSION = 14;
5553

5654
// TODO(indexing): Remove this constant and increment VERSION to enable indexing support
57-
static final int INDEXING_SUPPORT_VERSION = OVERLAY_SUPPORT_VERSION + 1;
55+
static final int INDEXING_SUPPORT_VERSION = VERSION + 1;
5856

5957
/**
6058
* The batch size for data migrations.
@@ -75,14 +73,11 @@ class SQLiteSchema {
7573
}
7674

7775
void runSchemaUpgrades() {
78-
runSchemaUpgrades(0, VERSION);
76+
runSchemaUpgrades(0);
7977
}
8078

8179
void runSchemaUpgrades(int fromVersion) {
8280
int toVersion = VERSION;
83-
if (Persistence.OVERLAY_SUPPORT_ENABLED) {
84-
toVersion = OVERLAY_SUPPORT_VERSION;
85-
}
8681
if (Persistence.INDEXING_SUPPORT_ENABLED) {
8782
toVersion = INDEXING_SUPPORT_VERSION;
8883
}
@@ -178,8 +173,16 @@ void runSchemaUpgrades(int fromVersion, int toVersion) {
178173
ensurePathLength();
179174
}
180175

176+
if (fromVersion < 14 && toVersion >= 14) {
177+
Preconditions.checkState(
178+
Persistence.OVERLAY_SUPPORT_ENABLED || Persistence.INDEXING_SUPPORT_ENABLED);
179+
createOverlays();
180+
createDataMigrationTable();
181+
addPendingDataMigration(Persistence.DATA_MIGRATION_BUILD_OVERLAYS);
182+
}
183+
181184
/*
182-
* Adding a new migration? READ THIS FIRST!
185+
* Adding a new schema upgrade? READ THIS FIRST!
183186
*
184187
* Be aware that the SDK version may be downgraded then re-upgraded. This means that running
185188
* your new migration must not prevent older versions of the SDK from functioning. Additionally,
@@ -190,13 +193,6 @@ void runSchemaUpgrades(int fromVersion, int toVersion) {
190193
* maintained invariants from later versions, so migrations that update values cannot assume
191194
* that existing values have been properly maintained. Calculate them again, if applicable.
192195
*/
193-
if (fromVersion < OVERLAY_SUPPORT_VERSION && toVersion >= OVERLAY_SUPPORT_VERSION) {
194-
Preconditions.checkState(
195-
Persistence.OVERLAY_SUPPORT_ENABLED || Persistence.INDEXING_SUPPORT_ENABLED);
196-
createOverlays();
197-
createDataMigrationTable();
198-
addPendingDataMigration(Persistence.DATA_MIGRATION_BUILD_OVERLAYS);
199-
}
200196

201197
if (fromVersion < INDEXING_SUPPORT_VERSION && toVersion >= INDEXING_SUPPORT_VERSION) {
202198
Preconditions.checkState(Persistence.INDEXING_SUPPORT_ENABLED);
@@ -708,7 +704,9 @@ private void createDataMigrationTable() {
708704
}
709705

710706
private void addPendingDataMigration(String migration) {
711-
db.execSQL("INSERT INTO data_migrations (migration_name) VALUES (?)", new String[] {migration});
707+
db.execSQL(
708+
"INSERT OR IGNORE INTO data_migrations (migration_name) VALUES (?)",
709+
new String[] {migration});
712710
}
713711

714712
private boolean tableExists(String table) {

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/FieldMask.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.firestore.model.mutation;
1616

1717
import com.google.firebase.firestore.model.FieldPath;
18+
import java.util.HashSet;
1819
import java.util.Set;
1920

2021
/**
@@ -26,6 +27,8 @@
2627
* object foo. If foo is not an object, foo is replaced with an object containing foo.
2728
*/
2829
public final class FieldMask {
30+
public static FieldMask EMPTY = fromSet(new HashSet<>());
31+
2932
public static FieldMask fromSet(Set<FieldPath> mask) {
3033
return new FieldMask(mask);
3134
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.Map;
3131
import org.junit.After;
3232
import org.junit.Before;
33-
import org.junit.BeforeClass;
3433
import org.junit.Rule;
3534
import org.junit.Test;
3635
import org.junit.rules.TestName;
@@ -52,17 +51,6 @@ public abstract class DocumentOverlayCacheTestCase {
5251
private DocumentOverlayCache overlays;
5352
private static boolean overlayEnabled = false;
5453

55-
@BeforeClass
56-
public static void beforeClass() {
57-
overlayEnabled = Persistence.OVERLAY_SUPPORT_ENABLED;
58-
Persistence.OVERLAY_SUPPORT_ENABLED = true;
59-
}
60-
61-
@BeforeClass
62-
public static void afterClass() {
63-
Persistence.OVERLAY_SUPPORT_ENABLED = overlayEnabled;
64-
}
65-
6654
@Before
6755
public void setUp() {
6856
persistence = getPersistence();

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import static com.google.firebase.firestore.testutil.TestUtil.viewChanges;
3939
import static java.util.Arrays.asList;
4040
import static java.util.Collections.emptyList;
41-
import static java.util.Collections.singletonList;
4241
import static org.junit.Assert.assertEquals;
4342
import static org.junit.Assert.assertFalse;
4443
import static org.junit.Assert.assertNotNull;
@@ -75,16 +74,17 @@
7574
import com.google.firebase.firestore.testutil.TestUtil;
7675
import com.google.firebase.firestore.util.AsyncQueue;
7776
import java.util.ArrayList;
77+
import java.util.Arrays;
7878
import java.util.Collection;
7979
import java.util.Collections;
8080
import java.util.List;
8181
import java.util.Map.Entry;
8282
import java.util.Set;
83+
import java.util.stream.Collectors;
8384
import javax.annotation.Nullable;
8485
import org.junit.After;
8586
import org.junit.Assert;
8687
import org.junit.Before;
87-
import org.junit.Ignore;
8888
import org.junit.Test;
8989

9090
/**
@@ -155,23 +155,27 @@ private void udpateViews(int targetId, boolean fromCache) {
155155
notifyLocalViewChanges(viewChanges(targetId, fromCache, asList(), asList()));
156156
}
157157

158-
private void acknowledgeMutation(long documentVersion, @Nullable Object transformResult) {
158+
private void acknowledgeMutationWithTransformResults(
159+
long documentVersion, Object... transformResult) {
159160
MutationBatch batch = batches.remove(0);
160161
SnapshotVersion version = version(documentVersion);
161-
MutationResult mutationResult =
162-
new MutationResult(
163-
version,
164-
transformResult != null
165-
? Collections.singletonList(TestUtil.wrap(transformResult))
166-
: Collections.emptyList());
162+
List<MutationResult> mutationResults =
163+
Collections.singletonList(new MutationResult(version, emptyList()));
164+
165+
if (transformResult.length != 0) {
166+
mutationResults =
167+
Arrays.stream(transformResult)
168+
.map(r -> new MutationResult(version, Collections.singletonList(TestUtil.wrap(r))))
169+
.collect(Collectors.toList());
170+
}
171+
167172
MutationBatchResult result =
168-
MutationBatchResult.create(
169-
batch, version, singletonList(mutationResult), WriteStream.EMPTY_STREAM_TOKEN);
173+
MutationBatchResult.create(batch, version, mutationResults, WriteStream.EMPTY_STREAM_TOKEN);
170174
lastChanges = localStore.acknowledgeBatch(result);
171175
}
172176

173177
private void acknowledgeMutation(long documentVersion) {
174-
acknowledgeMutation(documentVersion, null);
178+
acknowledgeMutationWithTransformResults(documentVersion);
175179
}
176180

177181
private void rejectMutation() {
@@ -974,7 +978,10 @@ public void testReadsAllDocumentsForInitialCollectionQueries() {
974978
localStore.executeQuery(query, /* usePreviousResults= */ true);
975979

976980
assertRemoteDocumentsRead(/* byKey= */ 0, /* byQuery= */ 2);
977-
if (!Persistence.OVERLAY_SUPPORT_ENABLED) {
981+
if (Persistence.OVERLAY_SUPPORT_ENABLED) {
982+
// No mutations are read because only overlay is needed.
983+
assertMutationsRead(/* byKey= */ 0, /* byQuery= */ 0);
984+
} else {
978985
assertMutationsRead(/* byKey= */ 0, /* byQuery= */ 1);
979986
}
980987
}
@@ -1070,7 +1077,7 @@ public void testHandlesSetMutationThenAckThenTransformThenAckThenTransform() {
10701077
assertContains(doc("foo/bar", 1, map("sum", 1)).setHasLocalMutations());
10711078
assertChanged(doc("foo/bar", 1, map("sum", 1)).setHasLocalMutations());
10721079

1073-
acknowledgeMutation(2, 1);
1080+
acknowledgeMutationWithTransformResults(2, 1);
10741081
assertChanged(doc("foo/bar", 2, map("sum", 1)).setHasCommittedMutations());
10751082
assertContains(doc("foo/bar", 2, map("sum", 1)).setHasCommittedMutations());
10761083

@@ -1284,19 +1291,17 @@ public void testHandlesSetMutationThenTransformThenRemoteEventThenTransform() {
12841291
assertChanged(doc("foo/bar", 2, map("sum", 3)).setHasLocalMutations());
12851292
assertContains(doc("foo/bar", 2, map("sum", 3)).setHasLocalMutations());
12861293

1287-
acknowledgeMutation(3, 1);
1294+
acknowledgeMutationWithTransformResults(3, 1);
12881295
assertChanged(doc("foo/bar", 3, map("sum", 3)).setHasLocalMutations());
12891296
assertContains(doc("foo/bar", 3, map("sum", 3)).setHasLocalMutations());
12901297

1291-
acknowledgeMutation(4, 1339);
1298+
acknowledgeMutationWithTransformResults(4, 1339);
12921299
assertChanged(doc("foo/bar", 4, map("sum", 1339)).setHasCommittedMutations());
12931300
assertContains(doc("foo/bar", 4, map("sum", 1339)).setHasCommittedMutations());
12941301
}
12951302

12961303
@Test
1297-
@Ignore("Test fails in CI")
1298-
// TODO(Overlay): Fix me :)
1299-
public void testHoldsBackOnlyNonIdempotentTransforms() {
1304+
public void testHoldsBackTransforms() {
13001305
Query query = Query.atPath(ResourcePath.fromString("foo"));
13011306
allocateQuery(query);
13021307
assertTargetId(2);
@@ -1333,8 +1338,13 @@ public void testHoldsBackOnlyNonIdempotentTransforms() {
13331338
asList(2),
13341339
emptyList()));
13351340
assertChanged(
1336-
doc("foo/bar", 2, map("sum", 1, "array_union", asList("bar", "foo")))
1337-
.setHasLocalMutations());
1341+
doc("foo/bar", 2, map("sum", 1, "array_union", asList("foo"))).setHasLocalMutations());
1342+
1343+
acknowledgeMutationWithTransformResults(3, 1338, asList("bar", "foo"));
1344+
assertChanged(
1345+
doc("foo/bar", 3, map("sum", 1338, "array_union", asList("bar", "foo")))
1346+
.withReadTime(new SnapshotVersion(new Timestamp(0, 3000)))
1347+
.setHasCommittedMutations());
13381348
}
13391349

13401350
@Test

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package com.google.firebase.firestore.local;
1616

17-
import org.junit.AfterClass;
18-
import org.junit.BeforeClass;
1917
import org.junit.runner.RunWith;
2018
import org.robolectric.RobolectricTestRunner;
2119
import org.robolectric.annotation.Config;
@@ -25,17 +23,6 @@
2523
public class MemoryLocalStoreTest extends LocalStoreTestCase {
2624
private static boolean enabled = false;
2725

28-
@BeforeClass
29-
public static void beforeClass() {
30-
enabled = Persistence.OVERLAY_SUPPORT_ENABLED;
31-
Persistence.OVERLAY_SUPPORT_ENABLED = false;
32-
}
33-
34-
@AfterClass
35-
public static void afterClass() {
36-
Persistence.OVERLAY_SUPPORT_ENABLED = enabled;
37-
}
38-
3926
@Override
4027
Persistence getPersistence() {
4128
return PersistenceTestHelpers.createEagerGCMemoryPersistence();

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

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

0 commit comments

Comments
 (0)