Skip to content

Commit 1b92ef0

Browse files
authored
Merge branch 'master' into daymon-temp-fix-for-release-artifacts
2 parents 02c736f + 8a92291 commit 1b92ef0

File tree

12 files changed

+316
-119
lines changed

12 files changed

+316
-119
lines changed

encoders/firebase-encoders-json/firebase-encoders-json.gradle

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

1515
plugins {
1616
id 'firebase-library'
17+
id 'kotlin-android'
1718
}
1819

1920
firebaseLibrary {
@@ -24,16 +25,19 @@ firebaseLibrary {
2425
android {
2526
compileSdkVersion project.targetSdkVersion
2627
defaultConfig {
27-
minSdkVersion project.minSdkVersion
28-
targetSdkVersion project.targetSdkVersion
29-
versionName version
30-
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
28+
minSdkVersion project.minSdkVersion
29+
targetSdkVersion project.targetSdkVersion
30+
versionName version
31+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3132
}
3233

3334
compileOptions {
3435
sourceCompatibility JavaVersion.VERSION_1_8
3536
targetCompatibility JavaVersion.VERSION_1_8
3637
}
38+
kotlinOptions {
39+
jvmTarget = '1.8'
40+
}
3741
testOptions {
3842
unitTests {
3943
includeAndroidResources = true

encoders/firebase-encoders-json/src/main/java/com/google/firebase/encoders/json/JsonValueObjectEncoderContext.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ JsonValueObjectEncoderContext add(@Nullable Object o, boolean inline) throws IOE
317317

318318
// Process enum last if it does not have a custom encoder registered.
319319
if (o instanceof Enum) {
320-
add(((Enum) o).name());
320+
if (o instanceof NumberedEnum) {
321+
add(((NumberedEnum) o).getNumber());
322+
} else {
323+
add(((Enum<?>) o).name());
324+
}
321325
return this;
322326
}
323327

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.encoders.json
18+
19+
/** Represents an explicitly numbered enum for json serialization. */
20+
interface NumberedEnum {
21+
val number: Int
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.encoders.json
18+
19+
internal enum class DummyEnum(override val number: Int) : NumberedEnum {
20+
VALUE_1(1),
21+
VALUE_2(2),
22+
}

encoders/firebase-encoders-json/src/test/java/com/google/firebase/encoders/json/JsonValueObjectEncoderContextTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ public void testEncodingEnum_withCustomEncoder() {
219219
assertThat(result).isEqualTo("[\"value_1\",\"value_2\"]");
220220
}
221221

222+
@Test
223+
public void testEncodingNumberedEnum() {
224+
String result =
225+
new JsonDataEncoderBuilder()
226+
.build()
227+
.encode(
228+
new DummyEnum[] {
229+
DummyEnum.VALUE_1, DummyEnum.VALUE_2, DummyEnum.VALUE_1, DummyEnum.VALUE_1
230+
});
231+
assertThat(result).isEqualTo("[1,2,1,1]");
232+
}
233+
222234
@Test
223235
public void testEncodingCollection() throws IOException {
224236
ObjectEncoder<InnerDummyClass> anotherObjectEncoder =

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ private DocumentChangeResult populateDocumentChanges(
555555
* have to be too frequent.
556556
*/
557557
private static boolean shouldPersistTargetData(
558-
TargetData oldTargetData, TargetData newTargetData, TargetChange change) {
558+
TargetData oldTargetData, TargetData newTargetData, @Nullable TargetChange change) {
559559
// Always persist query data if we don't already have a resume token.
560560
if (oldTargetData.getResumeToken().isEmpty()) return true;
561561

@@ -568,10 +568,20 @@ private static boolean shouldPersistTargetData(
568568
long timeDelta = newSeconds - oldSeconds;
569569
if (timeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS) return true;
570570

571+
// Update the target cache if sufficient time has passed since the last
572+
// LastLimboFreeSnapshotVersion
573+
long newLimboFreeSeconds =
574+
newTargetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
575+
long oldLimboFreeSeconds =
576+
oldTargetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
577+
long limboFreeTimeDelta = newLimboFreeSeconds - oldLimboFreeSeconds;
578+
if (limboFreeTimeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS) return true;
579+
571580
// Otherwise if the only thing that has changed about a target is its resume token it's not
572581
// worth persisting. Note that the RemoteStore keeps an in-memory view of the currently active
573582
// targets which includes the current resume token, so stream failure or user changes will still
574583
// use an up-to-date resume token regardless of what we do here.
584+
if (change == null) return false;
575585
int changes =
576586
change.getAddedDocuments().size()
577587
+ change.getModifiedDocuments().size()
@@ -607,17 +617,7 @@ public void notifyLocalViewChanges(List<LocalViewChanges> viewChanges) {
607617
targetData.withLastLimboFreeSnapshotVersion(lastLimboFreeSnapshotVersion);
608618
queryDataByTarget.put(targetId, updatedTargetData);
609619

610-
long newSeconds =
611-
updatedTargetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
612-
613-
long oldSeconds =
614-
targetData.getLastLimboFreeSnapshotVersion().getTimestamp().getSeconds();
615-
616-
long timeDelta = newSeconds - oldSeconds;
617-
618-
// Update the target cache if sufficient time has passed since the last
619-
// LastLimboFreeSnapshotVersion
620-
if (timeDelta >= RESUME_TOKEN_MAX_AGE_SECONDS) {
620+
if (shouldPersistTargetData(targetData, updatedTargetData, /*change*/ null)) {
621621
targetCache.updateTargetData(updatedTargetData);
622622
}
623623
}

firebase-firestore/src/test/java/com/google/firebase/firestore/spec/SpecTestCase.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -990,12 +990,14 @@ private void validateExpectedState(@Nullable JSONObject expectedState) throws JS
990990
expectedActiveTargets.put(targetId, new ArrayList<>());
991991
for (int i = 0; i < queryArrayJson.length(); i++) {
992992
Query query = parseQuery(queryArrayJson.getJSONObject(i));
993-
// TODO: populate the purpose of the target once it's possible to encode that in the
994-
// spec tests. For now, hard-code that it's a listen despite the fact that it's not
995-
// always the right value.
993+
994+
QueryPurpose purpose = QueryPurpose.LISTEN;
995+
if (queryDataJson.has("targetPurpose")) {
996+
purpose = QueryPurpose.values()[queryDataJson.getInt("targetPurpose")];
997+
}
998+
996999
TargetData targetData =
997-
new TargetData(
998-
query.toTarget(), targetId, ARBITRARY_SEQUENCE_NUMBER, QueryPurpose.LISTEN);
1000+
new TargetData(query.toTarget(), targetId, ARBITRARY_SEQUENCE_NUMBER, purpose);
9991001
if (queryDataJson.has("resumeToken")) {
10001002
targetData =
10011003
targetData.withResumeToken(
@@ -1134,9 +1136,12 @@ private void validateActiveTargets() {
11341136
TargetData expectedTarget = expectedQueries.get(0);
11351137
TargetData actualTarget = actualTargets.get(expected.getKey());
11361138

1137-
// TODO: validate the purpose of the target once it's possible to encode that in the
1138-
// spec tests. For now, only validate properties that can be validated.
1139+
// TODO: Replace the assertEquals() checks on the individual properties of TargetData below
1140+
// with the single assertEquals on the TargetData objects themselves if the sequenceNumber is
1141+
// ever made to be consistent.
11391142
// assertEquals(expectedTarget, actualTarget);
1143+
1144+
assertEquals(expectedTarget.getPurpose(), actualTarget.getPurpose());
11401145
assertEquals(expectedTarget.getTarget(), actualTarget.getTarget());
11411146
assertEquals(expectedTarget.getTargetId(), actualTarget.getTargetId());
11421147
assertEquals(expectedTarget.getSnapshotVersion(), actualTarget.getSnapshotVersion());

firebase-firestore/src/test/resources/json/bundle_spec_test.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,8 @@
11651165
"path": "collection/a"
11661166
}
11671167
],
1168-
"resumeToken": ""
1168+
"resumeToken": "",
1169+
"targetPurpose": 2
11691170
},
11701171
"2": {
11711172
"queries": [

firebase-firestore/src/test/resources/json/existence_filter_spec_test.json

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,24 @@
154154
"path": "collection"
155155
}
156156
}
157-
]
157+
],
158+
"expectedState": {
159+
"activeTargets": {
160+
"2": {
161+
"queries": [
162+
{
163+
"filters": [
164+
],
165+
"orderBys": [
166+
],
167+
"path": "collection"
168+
}
169+
],
170+
"resumeToken": "",
171+
"targetPurpose": 1
172+
}
173+
}
174+
}
158175
},
159176
{
160177
"restart": true,
@@ -414,7 +431,8 @@
414431
"path": "collection"
415432
}
416433
],
417-
"resumeToken": ""
434+
"resumeToken": "",
435+
"targetPurpose": 1
418436
}
419437
}
420438
}
@@ -910,7 +928,8 @@
910928
"path": "collection"
911929
}
912930
],
913-
"resumeToken": ""
931+
"resumeToken": "",
932+
"targetPurpose": 1
914933
}
915934
}
916935
}
@@ -976,7 +995,8 @@
976995
"path": "collection/2"
977996
}
978997
],
979-
"resumeToken": ""
998+
"resumeToken": "",
999+
"targetPurpose": 2
9801000
},
9811001
"2": {
9821002
"queries": [
@@ -988,7 +1008,8 @@
9881008
"path": "collection"
9891009
}
9901010
],
991-
"resumeToken": ""
1011+
"resumeToken": "",
1012+
"targetPurpose": 1
9921013
}
9931014
}
9941015
}
@@ -1043,7 +1064,8 @@
10431064
"path": "collection"
10441065
}
10451066
],
1046-
"resumeToken": ""
1067+
"resumeToken": "",
1068+
"targetPurpose": 1
10471069
}
10481070
}
10491071
}
@@ -1475,7 +1497,8 @@
14751497
"path": "collection"
14761498
}
14771499
],
1478-
"resumeToken": ""
1500+
"resumeToken": "",
1501+
"targetPurpose": 1
14791502
}
14801503
}
14811504
}
@@ -1541,7 +1564,8 @@
15411564
"path": "collection/2"
15421565
}
15431566
],
1544-
"resumeToken": ""
1567+
"resumeToken": "",
1568+
"targetPurpose": 2
15451569
},
15461570
"2": {
15471571
"queries": [
@@ -1553,7 +1577,8 @@
15531577
"path": "collection"
15541578
}
15551579
],
1556-
"resumeToken": ""
1580+
"resumeToken": "",
1581+
"targetPurpose": 1
15571582
}
15581583
}
15591584
}
@@ -1618,7 +1643,8 @@
16181643
"path": "collection"
16191644
}
16201645
],
1621-
"resumeToken": ""
1646+
"resumeToken": "",
1647+
"targetPurpose": 1
16221648
}
16231649
}
16241650
}
@@ -1822,7 +1848,8 @@
18221848
"path": "collection"
18231849
}
18241850
],
1825-
"resumeToken": ""
1851+
"resumeToken": "",
1852+
"targetPurpose": 1
18261853
}
18271854
}
18281855
}
@@ -1888,7 +1915,8 @@
18881915
"path": "collection/2"
18891916
}
18901917
],
1891-
"resumeToken": ""
1918+
"resumeToken": "",
1919+
"targetPurpose": 2
18921920
},
18931921
"2": {
18941922
"queries": [
@@ -1900,7 +1928,8 @@
19001928
"path": "collection"
19011929
}
19021930
],
1903-
"resumeToken": ""
1931+
"resumeToken": "",
1932+
"targetPurpose": 1
19041933
}
19051934
}
19061935
}
@@ -1965,7 +1994,8 @@
19651994
"path": "collection"
19661995
}
19671996
],
1968-
"resumeToken": ""
1997+
"resumeToken": "",
1998+
"targetPurpose": 1
19691999
}
19702000
}
19712001
}
@@ -2219,7 +2249,24 @@
22192249
"path": "collection"
22202250
}
22212251
}
2222-
]
2252+
],
2253+
"expectedState": {
2254+
"activeTargets": {
2255+
"2": {
2256+
"queries": [
2257+
{
2258+
"filters": [
2259+
],
2260+
"orderBys": [
2261+
],
2262+
"path": "collection"
2263+
}
2264+
],
2265+
"resumeToken": "",
2266+
"targetPurpose": 1
2267+
}
2268+
}
2269+
}
22232270
}
22242271
]
22252272
}

0 commit comments

Comments
 (0)