Skip to content

Commit 990b16e

Browse files
authored
Merge pull request #3018 from dimagi/entityCachingFixes
Cache Prime Fix: Reuse entity factory for cache prime
2 parents 3600813 + f5d2a67 commit 990b16e

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

Diff for: app/src/org/commcare/entity/AndroidAsyncNodeEntityFactory.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class AndroidAsyncNodeEntityFactory(
5757
getCurrentCommandId(),
5858
detail,
5959
entityDatum,
60-
entities
60+
entities,
61+
this
6162
)
6263
schedulePrimeEntityCacheWorker()
6364
} else {
@@ -70,7 +71,8 @@ class AndroidAsyncNodeEntityFactory(
7071
getCurrentCommandId(),
7172
detail,
7273
entityDatum,
73-
entities
74+
entities,
75+
this
7476
)
7577
}
7678
}

Diff for: app/src/org/commcare/tasks/EntityLoaderHelper.kt

+23-20
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,29 @@ class EntityLoaderHelper(
2424
detail: Detail,
2525
sessionDatum: EntityDatum?,
2626
evalCtx: EvaluationContext,
27-
inBackground: Boolean
27+
inBackground: Boolean,
28+
var factory: NodeEntityFactory? = null
2829
) : Cancellable {
2930

3031
var focusTargetIndex: Int = -1
3132
private var stopLoading: Boolean = false
32-
var factory: NodeEntityFactory
3333

3434
init {
3535
evalCtx.addFunctionHandler(EntitySelectActivity.getHereFunctionHandler())
36-
if (detail.shouldOptimize()) {
37-
val entityStorageCache: EntityStorageCache = CommCareEntityStorageCache("case")
38-
factory = AndroidAsyncNodeEntityFactory(detail, sessionDatum, evalCtx, entityStorageCache, inBackground)
39-
} else if (detail.useAsyncStrategy()) {
40-
// legacy cache and index
41-
val entityStorageCache: EntityStorageCache = CommCareEntityStorageCache("case")
42-
factory = AsyncNodeEntityFactory(detail, evalCtx, entityStorageCache, inBackground)
43-
} else {
44-
factory = NodeEntityFactory(detail, evalCtx)
45-
if (DeveloperPreferences.collectAndDisplayEntityTraces()) {
46-
factory.activateDebugTraceOutput()
36+
if (factory == null) {
37+
if (detail.shouldOptimize()) {
38+
val entityStorageCache: EntityStorageCache = CommCareEntityStorageCache("case")
39+
factory =
40+
AndroidAsyncNodeEntityFactory(detail, sessionDatum, evalCtx, entityStorageCache, inBackground)
41+
} else if (detail.useAsyncStrategy()) {
42+
// legacy cache and index
43+
val entityStorageCache: EntityStorageCache = CommCareEntityStorageCache("case")
44+
factory = AsyncNodeEntityFactory(detail, evalCtx, entityStorageCache, inBackground)
45+
} else {
46+
factory = NodeEntityFactory(detail, evalCtx)
47+
if (DeveloperPreferences.collectAndDisplayEntityTraces()) {
48+
factory!!.activateDebugTraceOutput()
49+
}
4750
}
4851
}
4952
}
@@ -60,11 +63,11 @@ class EntityLoaderHelper(
6063
CommCareApplication.instance().currentApp.primeEntityCacheHelper.cancelWork()
6164
}
6265
try {
63-
val references = factory.expandReferenceList(nodeset)
66+
val references = factory!!.expandReferenceList(nodeset)
6467
val entities = loadEntitiesWithReferences(references, progressListener)
6568
entities?.let {
66-
factory.prepareEntities(entities)
67-
factory.printAndClearTraces("build")
69+
factory!!.prepareEntities(entities)
70+
factory!!.printAndClearTraces("build")
6871
return Pair<List<Entity<TreeReference>>, List<TreeReference>>(entities, references)
6972
}
7073
} finally {
@@ -80,14 +83,14 @@ class EntityLoaderHelper(
8083
* Primes the entity cache
8184
*/
8285
fun cacheEntities(nodeset: TreeReference): Pair<List<Entity<TreeReference>>, List<TreeReference>> {
83-
val references = factory.expandReferenceList(nodeset)
86+
val references = factory!!.expandReferenceList(nodeset)
8487
val entities = loadEntitiesWithReferences(references, null)
8588
cacheEntities(entities)
8689
return Pair<List<Entity<TreeReference>>, List<TreeReference>>(entities, references)
8790
}
8891

8992
fun cacheEntities(entities: MutableList<Entity<TreeReference>>?) {
90-
factory.cacheEntities(entities)
93+
factory!!.cacheEntities(entities)
9194
}
9295

9396
/**
@@ -109,7 +112,7 @@ class EntityLoaderHelper(
109112
if (stopLoading) {
110113
return null
111114
}
112-
val e = factory.getEntity(ref)
115+
val e = factory!!.getEntity(ref)
113116
if (e != null) {
114117
entities.add(e)
115118
if (e.shouldReceiveFocus()) {
@@ -123,6 +126,6 @@ class EntityLoaderHelper(
123126

124127
override fun cancel() {
125128
stopLoading = true
126-
factory.markAsCancelled()
129+
factory!!.markAsCancelled()
127130
}
128131
}

Diff for: app/src/org/commcare/tasks/EntityLoaderTask.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class EntityLoaderTask
4242
* @param evalCtx evaluation context
4343
*/
4444
public EntityLoaderTask(Detail detail, @Nullable EntityDatum entityDatum, EvaluationContext evalCtx) {
45-
entityLoaderHelper = new EntityLoaderHelper(detail, entityDatum, evalCtx, false);
45+
entityLoaderHelper = new EntityLoaderHelper(detail, entityDatum, evalCtx, false, null);
4646
// we only want to provide progress updates for the new caching config
4747
provideDetailProgressUpdates = detail.shouldOptimize();
4848
}

Diff for: app/src/org/commcare/tasks/PrimeEntityCacheHelper.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlinx.coroutines.flow.asSharedFlow
1515
import kotlinx.coroutines.runBlocking
1616
import org.commcare.AppUtils.getCurrentAppId
1717
import org.commcare.CommCareApplication
18+
import org.commcare.cases.entity.AsyncNodeEntityFactory
1819
import org.commcare.cases.entity.Entity
1920
import org.commcare.cases.entity.EntityLoadingProgressListener
2021
import org.commcare.suite.model.Detail
@@ -131,11 +132,12 @@ class PrimeEntityCacheHelper() : Cancellable, EntityLoadingProgressListener {
131132
commandId: String,
132133
detail: Detail,
133134
entityDatum: EntityDatum,
134-
entities: MutableList<Entity<TreeReference>>
135+
entities: MutableList<Entity<TreeReference>>,
136+
factory: AsyncNodeEntityFactory? = null
135137
) {
136138
checkPreConditions()
137139
try {
138-
primeCacheForDetail(commandId, detail, entityDatum, entities)
140+
primeCacheForDetail(commandId, detail, entityDatum, entities, false, factory)
139141
} finally {
140142
clearState()
141143
runBlocking {
@@ -184,12 +186,13 @@ class PrimeEntityCacheHelper() : Cancellable, EntityLoadingProgressListener {
184186
entityDatum: EntityDatum,
185187
entities: MutableList<Entity<TreeReference>>? = null,
186188
inBackground: Boolean = false,
189+
factory: AsyncNodeEntityFactory? = null
187190
) {
188191
if (!detail.isCacheEnabled() || cancelled) return
189192
currentDatumInProgress = entityDatum.dataId
190193
currentDetailInProgress = detail.id
191-
entityLoaderHelper = EntityLoaderHelper(detail, entityDatum, evalCtx(commandId), inBackground).also {
192-
it.factory.setEntityProgressListener(this)
194+
entityLoaderHelper = EntityLoaderHelper(detail, entityDatum, evalCtx(commandId), inBackground, factory).also {
195+
it.factory!!.setEntityProgressListener(this)
193196
}
194197
// Handle the cache operation based on the available input
195198
val cachedEntities = when {

0 commit comments

Comments
 (0)