-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
Primes Entity cache ahead of time #2928
Draft
shubham1g5
wants to merge
6
commits into
refactorEntityLoaderTask
Choose a base branch
from
primeCacheAheadOfTime
base: refactorEntityLoaderTask
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2518b23
Adds a worker task to prime entity cache
shubham1g5 878c09c
Schedule prime cache task after 412 and on initializing user session
shubham1g5 c1e6a35
Set up progress indicator on entity list screen
shubham1g5 9aa1220
Creates a Android specific async factory to bind to any existing prim…
shubham1g5 c4a68a6
Move methods to schedule/cancel cache work to cache helper
shubham1g5 d78960d
Re-schedule cancelled work when expediting a detail sepcific cache
shubham1g5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/src/org/commcare/entity/AndroidAsyncNodeEntityFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.commcare.entity | ||
|
||
import android.util.Pair | ||
import org.commcare.cases.entity.AsyncNodeEntityFactory | ||
import org.commcare.cases.entity.Entity | ||
import org.commcare.cases.entity.EntityStorageCache | ||
import org.commcare.suite.model.Detail | ||
import org.commcare.tasks.PrimeEntityCacheHelper | ||
import org.commcare.util.LogTypes | ||
import org.javarosa.core.model.condition.EvaluationContext | ||
import org.javarosa.core.model.instance.TreeReference | ||
import org.javarosa.core.services.Logger | ||
|
||
/** | ||
* Android Specific Implementation of AsyncNodeEntityFactory | ||
* Uses [PrimeEntityCacheHelper] to prime entity cache blocking the user when required | ||
*/ | ||
class AndroidAsyncNodeEntityFactory(d: Detail, ec: EvaluationContext?, entityStorageCache: EntityStorageCache?) : | ||
AsyncNodeEntityFactory(d, ec, entityStorageCache), PrimeEntityCacheListener { | ||
|
||
companion object { | ||
const val TWO_MINUTES = 2 * 60 * 1000 | ||
} | ||
|
||
private var cachedEntities: List<Entity<TreeReference>>? = null | ||
private var completedCachePrime = false | ||
|
||
override fun prepareEntitiesInternal(entities: MutableList<Entity<TreeReference>>) { | ||
if (detail.shouldCache()) { | ||
// we only want to block if lazy load is not enabled | ||
if (!detail.shouldLazyLoad()) { | ||
val primeEntityCacheHelper = PrimeEntityCacheHelper.getInstance() | ||
if (primeEntityCacheHelper.isInProgress()) { | ||
// if we are priming something else at the moment, expedite the current detail | ||
if (!primeEntityCacheHelper.isDetailInProgress(detail.id)) { | ||
primeEntityCacheHelper.expediteDetailWithId(detail, entities) | ||
} else { | ||
// otherwise wait for existing prime process to complete | ||
primeEntityCacheHelper.setListener(this) | ||
waitForCachePrimeWork(entities, primeEntityCacheHelper) | ||
if (cachedEntities != null) { | ||
entities.clear() | ||
entities.addAll(cachedEntities!!) | ||
} | ||
} | ||
} else { | ||
// we either have not started priming or already completed. In both cases | ||
// we want to re-prime to make sure we calculate any uncalculated data first | ||
primeEntityCacheHelper.primeEntityCacheForDetail(detail, entities) | ||
} | ||
} | ||
} else { | ||
super.prepareEntitiesInternal(entities) | ||
} | ||
} | ||
|
||
private fun waitForCachePrimeWork( | ||
entities: MutableList<Entity<TreeReference>>, | ||
primeEntityCacheHelper: PrimeEntityCacheHelper | ||
) { | ||
val startTime = System.currentTimeMillis() | ||
while (!completedCachePrime && (System.currentTimeMillis() - startTime) < TWO_MINUTES) { | ||
// wait for it to be completed | ||
try { | ||
Thread.sleep(100) | ||
} catch (_: InterruptedException) { | ||
} | ||
} | ||
if (!completedCachePrime) { | ||
Logger.log(LogTypes.TYPE_MAINTENANCE, "Still Waiting for cache priming work to complete") | ||
// confirm if we are still priming in the worker. If yes, wait more | ||
// otherwise recall prepareEntitiesInternal to re-evaluate the best thing to do | ||
if (primeEntityCacheHelper.isInProgress() && primeEntityCacheHelper.isDetailInProgress(detail.id)) { | ||
waitForCachePrimeWork(entities, primeEntityCacheHelper) | ||
} else { | ||
prepareEntitiesInternal(entities) | ||
} | ||
} | ||
} | ||
|
||
override fun onPrimeEntityCacheComplete( | ||
currentDetailInProgress: String, | ||
cachedEntitiesWithRefs: Pair<List<Entity<TreeReference>>, List<TreeReference>> | ||
) { | ||
if (detail.id!!.contentEquals(currentDetailInProgress)) { | ||
cachedEntities = cachedEntitiesWithRefs.first | ||
completedCachePrime = true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.commcare.entity | ||
|
||
import android.util.Pair | ||
import org.commcare.cases.entity.Entity | ||
import org.javarosa.core.model.instance.TreeReference | ||
|
||
interface PrimeEntityCacheListener { | ||
|
||
fun onPrimeEntityCacheComplete( | ||
currentDetailInProgress: String, | ||
cachedEntitiesWithRefs: Pair<List<Entity<TreeReference>>, List<TreeReference>> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion
Avoid long sleep loops
waitForCachePrimeWork uses Thread.sleep(100) in a loop, potentially hogging system resources. Consider using notifications, signals, or coroutines for more efficient waiting.