-
Notifications
You must be signed in to change notification settings - Fork 615
Encapsulate scheduling in an inner class #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,16 +14,24 @@ | |
|
||
package com.google.firebase.firestore.local; | ||
|
||
import android.support.annotation.Nullable; | ||
import android.util.SparseArray; | ||
import com.google.firebase.firestore.FirebaseFirestoreSettings; | ||
import com.google.firebase.firestore.core.ListenSequence; | ||
import com.google.firebase.firestore.util.AsyncQueue; | ||
import com.google.firebase.firestore.util.Logger; | ||
import java.util.Comparator; | ||
import java.util.Locale; | ||
import java.util.PriorityQueue; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** Implements the steps for LRU garbage collection. */ | ||
public class LruGarbageCollector { | ||
/** How long we wait to try running LRU GC after SDK initialization. */ | ||
private static final long INITIAL_GC_DELAY_MS = TimeUnit.MINUTES.toMillis(1); | ||
/** Minimum amount of time between GC checks, after the first one. */ | ||
private static final long REGULAR_GC_DELAY_MS = TimeUnit.MINUTES.toMillis(5); | ||
|
||
public static class Params { | ||
private static final long COLLECTION_DISABLED = FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED; | ||
private static final long DEFAULT_CACHE_SIZE_BYTES = 100 * 1024 * 1024; // 100mb | ||
|
@@ -99,6 +107,43 @@ public int getDocumentsRemoved() { | |
} | ||
} | ||
|
||
public class Scheduler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this PR! While there is some special-casing in FirestoreClient with this PR, I don't think it's necessarily more than before, and some of it might even go away if we always run LRU (for Memory persistence for example). The encapsulation in |
||
private final AsyncQueue asyncQueue; | ||
private final LocalStore localStore; | ||
private boolean hasRun = false; | ||
@Nullable private AsyncQueue.DelayedTask gcTask; | ||
|
||
public Scheduler(AsyncQueue asyncQueue, LocalStore localStore) { | ||
this.asyncQueue = asyncQueue; | ||
this.localStore = localStore; | ||
} | ||
|
||
public void start() { | ||
if (params.minBytesThreshold != Params.COLLECTION_DISABLED) { | ||
scheduleGC(); | ||
} | ||
} | ||
|
||
public void stop() { | ||
if (gcTask != null) { | ||
gcTask.cancel(); | ||
} | ||
} | ||
|
||
private void scheduleGC() { | ||
long delay = hasRun ? REGULAR_GC_DELAY_MS : INITIAL_GC_DELAY_MS; | ||
gcTask = | ||
asyncQueue.enqueueAfterDelay( | ||
AsyncQueue.TimerId.GARBAGE_COLLECTION, | ||
delay, | ||
() -> { | ||
localStore.collectGarbage(LruGarbageCollector.this); | ||
hasRun = true; | ||
scheduleGC(); | ||
}); | ||
} | ||
} | ||
|
||
private final LruDelegate delegate; | ||
private final Params params; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really strange to me. Can this be
new gc.Scheduler()
? I am not sure if that would work.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can't, but I wrapped it in a static method so I guess it's a little less weird?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Er, not static. Because it's not a static class. But a helper method.