Skip to content

Commit 3d4dbe1

Browse files
authored
Merge 7a9b5e6 into c719f85
2 parents c719f85 + 7a9b5e6 commit 3d4dbe1

33 files changed

+11046
-279
lines changed

firebase-firestore/api.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,34 @@ package com.google.firebase.firestore {
316316
}
317317

318318
public final class MemoryCacheSettings implements com.google.firebase.firestore.LocalCacheSettings {
319+
method @NonNull public com.google.firebase.firestore.MemoryGarbageCollectorSettings getGarbageCollectorSettings();
319320
method @NonNull public static com.google.firebase.firestore.MemoryCacheSettings.Builder newBuilder();
320321
}
321322

322323
public static class MemoryCacheSettings.Builder {
323324
method @NonNull public com.google.firebase.firestore.MemoryCacheSettings build();
325+
method @NonNull public com.google.firebase.firestore.MemoryCacheSettings.Builder setGcSettings(@NonNull com.google.firebase.firestore.MemoryGarbageCollectorSettings);
326+
}
327+
328+
public final class MemoryEagerGcSettings implements com.google.firebase.firestore.MemoryGarbageCollectorSettings {
329+
method @NonNull public static com.google.firebase.firestore.MemoryEagerGcSettings.Builder newBuilder();
330+
}
331+
332+
public static class MemoryEagerGcSettings.Builder {
333+
method @NonNull public com.google.firebase.firestore.MemoryEagerGcSettings build();
334+
}
335+
336+
public interface MemoryGarbageCollectorSettings {
337+
}
338+
339+
public final class MemoryLruGcSettings implements com.google.firebase.firestore.MemoryGarbageCollectorSettings {
340+
method public long getSizeBytes();
341+
method @NonNull public static com.google.firebase.firestore.MemoryLruGcSettings.Builder newBuilder();
342+
}
343+
344+
public static class MemoryLruGcSettings.Builder {
345+
method @NonNull public com.google.firebase.firestore.MemoryLruGcSettings build();
346+
method public void setSizeBytes(long);
324347
}
325348

326349
public enum MetadataChanges {

firebase-firestore/ktx/api.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ package com.google.firebase.firestore.ktx {
1212
method public static inline <reified T> T getField(@NonNull com.google.firebase.firestore.DocumentSnapshot, @NonNull com.google.firebase.firestore.FieldPath fieldPath, @NonNull com.google.firebase.firestore.DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior);
1313
method @NonNull public static com.google.firebase.firestore.FirebaseFirestore getFirestore(@NonNull com.google.firebase.ktx.Firebase);
1414
method @NonNull public static com.google.firebase.firestore.MemoryCacheSettings memoryCacheSettings(@NonNull kotlin.jvm.functions.Function1<? super com.google.firebase.firestore.MemoryCacheSettings.Builder,kotlin.Unit> init);
15+
method @NonNull public static com.google.firebase.firestore.MemoryEagerGcSettings memoryEagerGcSettings(@NonNull kotlin.jvm.functions.Function1<? super com.google.firebase.firestore.MemoryEagerGcSettings.Builder,kotlin.Unit> init);
16+
method @NonNull public static com.google.firebase.firestore.MemoryLruGcSettings memoryLruGcSettings(@NonNull kotlin.jvm.functions.Function1<? super com.google.firebase.firestore.MemoryLruGcSettings.Builder,kotlin.Unit> init);
1517
method @NonNull public static com.google.firebase.firestore.PersistentCacheSettings persistentCacheSettings(@NonNull kotlin.jvm.functions.Function1<? super com.google.firebase.firestore.PersistentCacheSettings.Builder,kotlin.Unit> init);
1618
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.firestore.DocumentSnapshot> snapshots(@NonNull com.google.firebase.firestore.DocumentReference, @NonNull com.google.firebase.firestore.MetadataChanges metadataChanges = com.google.firebase.firestore.MetadataChanges.EXCLUDE);
1719
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.firestore.QuerySnapshot> snapshots(@NonNull com.google.firebase.firestore.Query, @NonNull com.google.firebase.firestore.MetadataChanges metadataChanges = com.google.firebase.firestore.MetadataChanges.EXCLUDE);

firebase-firestore/ktx/src/main/kotlin/com/google/firebase/firestore/ktx/Firestore.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ fun memoryCacheSettings(init: MemoryCacheSettings.Builder.() -> Unit): MemoryCac
182182
return builder.build()
183183
}
184184

185+
fun memoryEagerGcSettings(init: MemoryEagerGcSettings.Builder.() -> Unit): MemoryEagerGcSettings {
186+
val builder = MemoryEagerGcSettings.newBuilder()
187+
builder.init()
188+
return builder.build()
189+
}
190+
191+
fun memoryLruGcSettings(init: MemoryLruGcSettings.Builder.() -> Unit): MemoryLruGcSettings {
192+
val builder = MemoryLruGcSettings.newBuilder()
193+
builder.init()
194+
return builder.build()
195+
}
196+
185197
fun persistentCacheSettings(
186198
init: PersistentCacheSettings.Builder.() -> Unit
187199
): PersistentCacheSettings {

firebase-firestore/ktx/src/test/kotlin/com/google/firebase/firestore/ktx/FirestoreTests.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,34 @@ class FirestoreTests : BaseTestCase() {
124124
assertThat(otherSettings.isSslEnabled).isEqualTo(true)
125125
assertThat(otherSettings.isPersistenceEnabled).isFalse()
126126
}
127+
128+
@Test
129+
fun `MemoryCacheSettings Garbage Collector builder works`() {
130+
val host = "http://10.0.2.2:8080"
131+
val isSslEnabled = false
132+
133+
val settings = firestoreSettings {
134+
this.host = host
135+
this.isSslEnabled = isSslEnabled
136+
this.setLocalCacheSettings(memoryCacheSettings {})
137+
}
138+
139+
assertThat(host).isEqualTo(settings.host)
140+
assertThat(isSslEnabled).isEqualTo(settings.isSslEnabled)
141+
assertThat(settings.isPersistenceEnabled).isFalse()
142+
assertThat(settings.cacheSettings)
143+
.isEqualTo(memoryCacheSettings { setGcSettings(memoryEagerGcSettings {}) })
144+
145+
val otherSettings = firestoreSettings {
146+
this.setLocalCacheSettings(
147+
memoryCacheSettings { setGcSettings(memoryLruGcSettings { setSizeBytes(1_000) }) }
148+
)
149+
}
150+
151+
assertThat(otherSettings.host).isEqualTo(FirebaseFirestoreSettings.DEFAULT_HOST)
152+
assertThat(otherSettings.isPersistenceEnabled).isFalse()
153+
assertThat(otherSettings.cacheSizeBytes).isEqualTo(1_000)
154+
}
127155
}
128156

129157
@RunWith(RobolectricTestRunner::class)

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/FirestoreTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,4 +1298,43 @@ public void testNewCacheConfigForPersistentCache() {
12981298
DocumentSnapshot snap = waitFor(instance.document("coll/doc").get(Source.CACHE));
12991299
assertEquals(map("foo", "bar"), snap.getData());
13001300
}
1301+
1302+
@Test
1303+
public void testCanGetDocumentWithMemoryLruGCEnabled() {
1304+
FirebaseFirestore db = testFirestore();
1305+
db.setFirestoreSettings(
1306+
new FirebaseFirestoreSettings.Builder(db.getFirestoreSettings())
1307+
.setLocalCacheSettings(
1308+
MemoryCacheSettings.newBuilder()
1309+
.setGcSettings(MemoryLruGcSettings.newBuilder().build())
1310+
.build())
1311+
.build());
1312+
1313+
DocumentReference doc = db.collection("abc").document("123");
1314+
waitFor(doc.set(map("key", "value")));
1315+
1316+
DocumentSnapshot snapshot = waitFor(doc.get(Source.CACHE));
1317+
assertTrue(snapshot.exists());
1318+
assertTrue(snapshot.getMetadata().isFromCache());
1319+
assertEquals(snapshot.getData(), map("key", "value"));
1320+
}
1321+
1322+
@Test
1323+
public void testCannotGetDocumentWithMemoryEagerGcEnabled() {
1324+
FirebaseFirestore db = testFirestore();
1325+
db.setFirestoreSettings(
1326+
new FirebaseFirestoreSettings.Builder(db.getFirestoreSettings())
1327+
.setLocalCacheSettings(
1328+
MemoryCacheSettings.newBuilder()
1329+
.setGcSettings(MemoryEagerGcSettings.newBuilder().build())
1330+
.build())
1331+
.build());
1332+
1333+
DocumentReference doc = db.collection("abc").document("123");
1334+
waitFor(doc.set(map("key", "value")));
1335+
1336+
Exception e = waitForException(doc.get(Source.CACHE));
1337+
assertTrue(e instanceof FirebaseFirestoreException);
1338+
assertEquals(((FirebaseFirestoreException) e).getCode(), Code.UNAVAILABLE);
1339+
}
13011340
}

firebase-firestore/src/main/java/com/google/firebase/firestore/FirebaseFirestoreSettings.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static final class Builder {
4040
private String host;
4141
private boolean sslEnabled;
4242
private boolean persistenceEnabled;
43+
4344
private long cacheSizeBytes;
4445
private LocalCacheSettings cacheSettings;
4546

@@ -319,6 +320,12 @@ public long getCacheSizeBytes() {
319320
if (cacheSettings instanceof PersistentCacheSettings) {
320321
return ((PersistentCacheSettings) cacheSettings).getSizeBytes();
321322
} else {
323+
MemoryCacheSettings memorySettings = (MemoryCacheSettings) cacheSettings;
324+
if (memorySettings.getGarbageCollectorSettings() instanceof MemoryLruGcSettings) {
325+
return ((MemoryLruGcSettings) memorySettings.getGarbageCollectorSettings())
326+
.getSizeBytes();
327+
}
328+
322329
return CACHE_SIZE_UNLIMITED;
323330
}
324331
}

firebase-firestore/src/main/java/com/google/firebase/firestore/MemoryCacheSettings.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,58 @@
2727
* `FirebaseFirestoreSettings` instance to configure Firestore SDK.
2828
*/
2929
public final class MemoryCacheSettings implements LocalCacheSettings {
30+
private MemoryGarbageCollectorSettings gcSettings;
3031

3132
/** Returns a new instance of {@link MemoryCacheSettings.Builder} with default configurations. */
3233
@NonNull
3334
public static MemoryCacheSettings.Builder newBuilder() {
3435
return new MemoryCacheSettings.Builder();
3536
}
3637

37-
private MemoryCacheSettings() {}
38+
private MemoryCacheSettings(MemoryGarbageCollectorSettings settings) {
39+
gcSettings = settings;
40+
}
3841

3942
@Override
4043
public int hashCode() {
41-
return super.hashCode();
44+
return gcSettings.hashCode();
4245
}
4346

4447
@Override
4548
public boolean equals(@Nullable Object obj) {
4649
if (this == obj) return true;
4750
if (obj == null || getClass() != obj.getClass()) return false;
4851

49-
return true;
52+
return getGarbageCollectorSettings()
53+
.equals(((MemoryCacheSettings) obj).getGarbageCollectorSettings());
5054
}
5155

5256
@Override
5357
public String toString() {
54-
return "MemoryCacheSettings{}";
58+
return "MemoryCacheSettings{gcSettings=" + getGarbageCollectorSettings() + "}";
59+
}
60+
61+
@NonNull
62+
public MemoryGarbageCollectorSettings getGarbageCollectorSettings() {
63+
return gcSettings;
5564
}
5665

5766
/** A Builder for creating {@code MemoryCacheSettings} instance. */
5867
public static class Builder {
68+
private MemoryGarbageCollectorSettings gcSettings = MemoryEagerGcSettings.newBuilder().build();
5969

6070
private Builder() {}
6171

6272
/** Creates a {@code MemoryCacheSettings} instance. */
6373
@NonNull
6474
public MemoryCacheSettings build() {
65-
return new MemoryCacheSettings();
75+
return new MemoryCacheSettings(gcSettings);
76+
}
77+
78+
@NonNull
79+
public Builder setGcSettings(@NonNull MemoryGarbageCollectorSettings gcSettings) {
80+
this.gcSettings = gcSettings;
81+
return this;
6682
}
6783
}
6884
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore;
16+
17+
import androidx.annotation.NonNull;
18+
import androidx.annotation.Nullable;
19+
20+
public final class MemoryEagerGcSettings implements MemoryGarbageCollectorSettings {
21+
private MemoryEagerGcSettings() {}
22+
23+
public static class Builder {
24+
private Builder() {}
25+
26+
@NonNull
27+
public MemoryEagerGcSettings build() {
28+
return new MemoryEagerGcSettings();
29+
}
30+
}
31+
32+
@Override
33+
public int hashCode() {
34+
return super.hashCode();
35+
}
36+
37+
@Override
38+
public boolean equals(@Nullable Object obj) {
39+
if (this == obj) return true;
40+
if (obj == null || getClass() != obj.getClass()) return false;
41+
42+
return true;
43+
}
44+
45+
@NonNull
46+
@Override
47+
public String toString() {
48+
return "MemoryEagerGcSettings{}";
49+
}
50+
51+
@NonNull
52+
public static MemoryEagerGcSettings.Builder newBuilder() {
53+
return new Builder();
54+
}
55+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore;
16+
17+
public interface MemoryGarbageCollectorSettings {}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore;
16+
17+
import androidx.annotation.NonNull;
18+
19+
public final class MemoryLruGcSettings implements MemoryGarbageCollectorSettings {
20+
21+
private long sizeBytes;
22+
23+
public static class Builder {
24+
private long sizeBytes = FirebaseFirestoreSettings.DEFAULT_CACHE_SIZE_BYTES;
25+
26+
private Builder() {}
27+
28+
@NonNull
29+
public MemoryLruGcSettings build() {
30+
return new MemoryLruGcSettings(sizeBytes);
31+
}
32+
33+
public void setSizeBytes(long size) {
34+
sizeBytes = size;
35+
}
36+
}
37+
38+
private MemoryLruGcSettings(long size) {
39+
sizeBytes = size;
40+
}
41+
42+
@NonNull
43+
public static MemoryLruGcSettings.Builder newBuilder() {
44+
return new Builder();
45+
}
46+
47+
public long getSizeBytes() {
48+
return sizeBytes;
49+
}
50+
51+
@Override
52+
public boolean equals(Object o) {
53+
if (this == o) return true;
54+
if (o == null || getClass() != o.getClass()) return false;
55+
56+
MemoryLruGcSettings that = (MemoryLruGcSettings) o;
57+
58+
return sizeBytes == that.sizeBytes;
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return (int) (sizeBytes ^ (sizeBytes >>> 32));
64+
}
65+
66+
@NonNull
67+
@Override
68+
public String toString() {
69+
return "MemoryLruGcSettings{cacheSize=" + getSizeBytes() + "}";
70+
}
71+
}

0 commit comments

Comments
 (0)