Skip to content

Commit ae246af

Browse files
committed
Java impl
1 parent 5f72f15 commit ae246af

File tree

6 files changed

+218
-99
lines changed

6 files changed

+218
-99
lines changed

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

Lines changed: 55 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
/** Settings used to configure a {@link FirebaseFirestore} instance. */
2525
public final class FirebaseFirestoreSettings {
2626
/**
27-
* Constant to use with
28-
* {@link FirebaseFirestoreSettings.Builder#setCacheSizeBytes(long)} to
27+
* Constant to use with {@link FirebaseFirestoreSettings.Builder#setCacheSizeBytes(long)} to
2928
* disable garbage collection.
3029
*/
3130
public static final long CACHE_SIZE_UNLIMITED = -1;
@@ -42,7 +41,6 @@ public static final class Builder {
4241
private boolean sslEnabled;
4342
private boolean persistenceEnabled;
4443

45-
private boolean memoryLruGcEnabled;
4644
private long cacheSizeBytes;
4745
private LocalCacheSettings cacheSettings;
4846

@@ -53,13 +51,11 @@ public Builder() {
5351
host = DEFAULT_HOST;
5452
sslEnabled = true;
5553
persistenceEnabled = true;
56-
memoryLruGcEnabled = false;
5754
cacheSizeBytes = DEFAULT_CACHE_SIZE_BYTES;
5855
}
5956

6057
/**
61-
* Constructs a new {@code FirebaseFirestoreSettings} Builder based on an
62-
* existing {@code
58+
* Constructs a new {@code FirebaseFirestoreSettings} Builder based on an existing {@code
6359
* FirebaseFirestoreSettings} object.
6460
*/
6561
public Builder(@NonNull FirebaseFirestoreSettings settings) {
@@ -105,16 +101,13 @@ public Builder setSslEnabled(boolean value) {
105101
}
106102

107103
/**
108-
* Enables or disables local persistent storage. The default is to use local
109-
* persistent storage.
104+
* Enables or disables local persistent storage. The default is to use local persistent storage.
110105
*
111-
* @return A settings object that uses local persistent storage as specified by
112-
* the given
113-
* <tt>value</tt>.
106+
* @return A settings object that uses local persistent storage as specified by the given
107+
* <tt>value</tt>.
114108
* @deprecated Use {@link
115-
* FirebaseFirestoreSettings.Builder#setLocalCacheSettings(LocalCacheSettings)}
116-
* to configure
117-
* SDK cache instead.
109+
* FirebaseFirestoreSettings.Builder#setLocalCacheSettings(LocalCacheSettings)} to configure
110+
* SDK cache instead.
118111
*/
119112
@NonNull
120113
@Deprecated
@@ -128,32 +121,19 @@ public Builder setPersistenceEnabled(boolean value) {
128121
return this;
129122
}
130123

131-
@NonNull
132-
public Builder setMemoryLruGcEnabled(boolean value) {
133-
this.memoryLruGcEnabled = value;
134-
return this;
135-
}
136-
137124
/**
138-
* Sets an approximate cache size threshold for the on-disk data. If the cache
139-
* grows beyond this
140-
* size, Cloud Firestore will start removing data that hasn't been recently
141-
* used. The size is
142-
* not a guarantee that the cache will stay below that size, only that if the
143-
* cache exceeds the
125+
* Sets an approximate cache size threshold for the on-disk data. If the cache grows beyond this
126+
* size, Cloud Firestore will start removing data that hasn't been recently used. The size is
127+
* not a guarantee that the cache will stay below that size, only that if the cache exceeds the
144128
* given size, cleanup will be attempted.
145129
*
146-
* <p>
147-
* By default, collection is enabled with a cache size of 100 MB. The minimum
148-
* value is 1 MB.
130+
* <p>By default, collection is enabled with a cache size of 100 MB. The minimum value is 1 MB.
149131
*
150-
* @return A settings object on which the cache size is configured as specified
151-
* by the given
152-
* {@code value}.
132+
* @return A settings object on which the cache size is configured as specified by the given
133+
* {@code value}.
153134
* @deprecated Use {@link
154-
* FirebaseFirestoreSettings.Builder#setLocalCacheSettings(LocalCacheSettings)}
155-
* to configure
156-
* SDK cache instead.
135+
* FirebaseFirestoreSettings.Builder#setLocalCacheSettings(LocalCacheSettings)} to configure
136+
* SDK cache instead.
157137
*/
158138
@NonNull
159139
@Deprecated
@@ -172,24 +152,18 @@ public Builder setCacheSizeBytes(long value) {
172152
}
173153

174154
/**
175-
* Specifies the cache used by the SDK. Available options are
176-
* {@link PersistentCacheSettings}
155+
* Specifies the cache used by the SDK. Available options are {@link PersistentCacheSettings}
177156
* and {@link MemoryCacheSettings}, each with different configuration options.
178157
*
179-
* <p>
180-
* When unspecified, {@link PersistentCacheSettings} will be used by default.
158+
* <p>When unspecified, {@link PersistentCacheSettings} will be used by default.
181159
*
182-
* <p>
183-
* NOTE: Calling this setter and {@code setPersistenceEnabled()} or @{code
184-
* setCacheSizeBytes()} at the same time will throw an exception during SDK
185-
* initialization.
186-
* Instead, use the configuration in the {@link PersistentCacheSettings} object
187-
* to specify the
160+
* <p>NOTE: Calling this setter and {@code setPersistenceEnabled()} or @{code
161+
* setCacheSizeBytes()} at the same time will throw an exception during SDK initialization.
162+
* Instead, use the configuration in the {@link PersistentCacheSettings} object to specify the
188163
* cache size.
189164
*
190-
* @return A settings object on which the cache settings is configured as
191-
* specified by the given
192-
* {@code settings}.
165+
* @return A settings object on which the cache settings is configured as specified by the given
166+
* {@code settings}.
193167
*/
194168
@NonNull
195169
public Builder setLocalCacheSettings(@NonNull LocalCacheSettings settings) {
@@ -221,11 +195,9 @@ public boolean isSslEnabled() {
221195
}
222196

223197
/**
224-
* @return boolean indicating whether local persistent storage is enabled or
225-
* not.
226-
* @deprecated Build the {@code FirebaseFirestoreSettings} instance to check
227-
* cache
228-
* configurations.
198+
* @return boolean indicating whether local persistent storage is enabled or not.
199+
* @deprecated Build the {@code FirebaseFirestoreSettings} instance to check cache
200+
* configurations.
229201
*/
230202
@Deprecated
231203
public boolean isPersistenceEnabled() {
@@ -234,9 +206,8 @@ public boolean isPersistenceEnabled() {
234206

235207
/**
236208
* @return cache size for on-disk data.
237-
* @deprecated Build the {@code FirebaseFirestoreSettings} instance to check
238-
* cache
239-
* configurations.
209+
* @deprecated Build the {@code FirebaseFirestoreSettings} instance to check cache
210+
* configurations.
240211
*/
241212
@Deprecated
242213
public long getCacheSizeBytes() {
@@ -256,15 +227,11 @@ public FirebaseFirestoreSettings build() {
256227
private final String host;
257228
private final boolean sslEnabled;
258229
private final boolean persistenceEnabled;
259-
private final boolean memoryLruGcEnabled;
260230
private final long cacheSizeBytes;
261231

262232
private LocalCacheSettings cacheSettings;
263233

264-
/**
265-
* Constructs a {@code FirebaseFirestoreSettings} object based on the values in
266-
* the Builder.
267-
*/
234+
/** Constructs a {@code FirebaseFirestoreSettings} object based on the values in the Builder. */
268235
private FirebaseFirestoreSettings(Builder builder) {
269236
host = builder.host;
270237
sslEnabled = builder.sslEnabled;
@@ -275,21 +242,15 @@ private FirebaseFirestoreSettings(Builder builder) {
275242

276243
@Override
277244
public boolean equals(Object o) {
278-
if (this == o)
279-
return true;
280-
if (o == null || getClass() != o.getClass())
281-
return false;
245+
if (this == o) return true;
246+
if (o == null || getClass() != o.getClass()) return false;
282247

283248
FirebaseFirestoreSettings that = (FirebaseFirestoreSettings) o;
284249

285-
if (sslEnabled != that.sslEnabled)
286-
return false;
287-
if (persistenceEnabled != that.persistenceEnabled)
288-
return false;
289-
if (cacheSizeBytes != that.cacheSizeBytes)
290-
return false;
291-
if (!host.equals(that.host))
292-
return false;
250+
if (sslEnabled != that.sslEnabled) return false;
251+
if (persistenceEnabled != that.persistenceEnabled) return false;
252+
if (cacheSizeBytes != that.cacheSizeBytes) return false;
253+
if (!host.equals(that.host)) return false;
293254
return Objects.equals(cacheSettings, that.cacheSettings);
294255
}
295256

@@ -307,18 +268,19 @@ public int hashCode() {
307268
@NonNull
308269
public String toString() {
309270
return "FirebaseFirestoreSettings{"
310-
+ "host="
311-
+ host
312-
+ ", sslEnabled="
313-
+ sslEnabled
314-
+ ", persistenceEnabled="
315-
+ persistenceEnabled
316-
+ ", cacheSizeBytes="
317-
+ cacheSizeBytes
318-
+ ", cacheSettings="
319-
+ cacheSettings == null
320-
? "null"
321-
: cacheSettings.toString() + "}";
271+
+ "host="
272+
+ host
273+
+ ", sslEnabled="
274+
+ sslEnabled
275+
+ ", persistenceEnabled="
276+
+ persistenceEnabled
277+
+ ", cacheSizeBytes="
278+
+ cacheSizeBytes
279+
+ ", cacheSettings="
280+
+ cacheSettings
281+
== null
282+
? "null"
283+
: cacheSettings.toString() + "}";
322284
}
323285

324286
/** Returns the host of the Cloud Firestore backend. */
@@ -346,13 +308,8 @@ public boolean isPersistenceEnabled() {
346308
return persistenceEnabled;
347309
}
348310

349-
public boolean isMemoryLruGcEnabled() {
350-
return memoryLruGcEnabled;
351-
}
352-
353311
/**
354-
* Returns the threshold for the cache size above which the SDK will attempt to
355-
* collect the least
312+
* Returns the threshold for the cache size above which the SDK will attempt to collect the least
356313
* recently used documents.
357314
*
358315
* @deprecated Use {@link FirebaseFirestoreSettings#getCacheSettings()} instead.
@@ -363,6 +320,12 @@ public long getCacheSizeBytes() {
363320
if (cacheSettings instanceof PersistentCacheSettings) {
364321
return ((PersistentCacheSettings) cacheSettings).getSizeBytes();
365322
} else {
323+
MemoryCacheSettings memorySettings = (MemoryCacheSettings) cacheSettings;
324+
if (memorySettings.getGarbageCollectorSettings() instanceof MemoryLruGcSettings) {
325+
return ((MemoryLruGcSettings) memorySettings.getGarbageCollectorSettings())
326+
.getSizeBytes();
327+
}
328+
366329
return CACHE_SIZE_UNLIMITED;
367330
}
368331
}
@@ -371,8 +334,7 @@ public long getCacheSizeBytes() {
371334
}
372335

373336
/**
374-
* Returns the cache settings configured for the SDK. Returns null if it is not
375-
* configured, in
337+
* Returns the cache settings configured for the SDK. Returns null if it is not configured, in
376338
* which case a default {@link PersistentCacheSettings} instance is used.
377339
*/
378340
@Nullable

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+
}

0 commit comments

Comments
 (0)