Skip to content

Commit 5f81686

Browse files
MartinRieseclaude
andcommitted
Add an optional storageCacheIdentifier
Add an optional storageCacheIdentifier constructor parameter. When set, getStorageCacheName() returns "casedb:<identifier>" instead of plain "casedb". Existing constructors pass null (backward compatible). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5c5df44 commit 5f81686

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

src/main/java/org/commcare/cases/instance/CaseInstanceTreeElement.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,35 @@ public class CaseInstanceTreeElement extends StorageInstanceTreeElement<Case, Ca
5656
@Nullable
5757
private final CaseIndexTable caseIndexTable;
5858

59+
@Nullable
60+
private final String storageCacheIdentifier;
61+
5962
//We're storing this here for now because this is a safe lifecycle object that must represent
6063
//a single snapshot of the case database, but it could be generalized later.
6164
private Hashtable<String, LinkedHashSet<Integer>> mIndexCache = new Hashtable<>();
6265

6366
public CaseInstanceTreeElement(AbstractTreeElement instanceRoot,
6467
IStorageUtilityIndexed<Case> storage){
65-
this(instanceRoot, storage, null);
68+
this(instanceRoot, storage, null, null);
6669
}
6770

6871
public CaseInstanceTreeElement(AbstractTreeElement instanceRoot,
6972
IStorageUtilityIndexed<Case> storage, CaseIndexTable caseIndexTable) {
73+
this(instanceRoot, storage, caseIndexTable, null);
74+
}
75+
76+
/**
77+
* @param storageCacheIdentifier Optional identifier to distinguish this instance's entries
78+
* in the shared RecordObjectCache from other CaseInstanceTreeElement instances
79+
* (e.g. the case search table name). When null, the cache name is just "casedb".
80+
*/
81+
public CaseInstanceTreeElement(AbstractTreeElement instanceRoot,
82+
IStorageUtilityIndexed<Case> storage,
83+
CaseIndexTable caseIndexTable,
84+
@Nullable String storageCacheIdentifier) {
7085
super(instanceRoot, storage, MODEL_NAME, "case");
7186
this.caseIndexTable = caseIndexTable;
87+
this.storageCacheIdentifier = storageCacheIdentifier;
7288
}
7389

7490
@Override
@@ -143,7 +159,10 @@ protected Hashtable<XPathPathExpr, String> getStorageIndexMap() {
143159
}
144160

145161
public String getStorageCacheName() {
146-
return CaseInstanceTreeElement.MODEL_NAME;
162+
if (storageCacheIdentifier == null || storageCacheIdentifier.isBlank()) {
163+
return MODEL_NAME;
164+
}
165+
return MODEL_NAME + ":" + storageCacheIdentifier;
147166
}
148167

149168
@Override
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.commcare.cases.instance;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotEquals;
5+
6+
import org.javarosa.core.model.instance.InstanceBase;
7+
import org.junit.Test;
8+
9+
10+
public class CaseInstanceTreeElementTest {
11+
12+
@Test
13+
public void testGetStorageCacheName() {
14+
InstanceBase base = new InstanceBase("test");
15+
16+
// No identifier — returns plain model name
17+
CaseInstanceTreeElement withoutId = new CaseInstanceTreeElement(base, null, null, null);
18+
assertEquals("casedb", withoutId.getStorageCacheName());
19+
20+
// With identifier — returns model name + identifier
21+
CaseInstanceTreeElement withId = new CaseInstanceTreeElement(base, null, null, "search_table_1");
22+
assertEquals("casedb:search_table_1", withId.getStorageCacheName());
23+
24+
// Two instances with different identifiers don't collide
25+
CaseInstanceTreeElement withId2 = new CaseInstanceTreeElement(base, null, null, "search_table_2");
26+
assertNotEquals(withId.getStorageCacheName(), withId2.getStorageCacheName());
27+
28+
// Instance without identifier doesn't collide with one that has
29+
assertNotEquals(withoutId.getStorageCacheName(), withId.getStorageCacheName());
30+
31+
// Legacy 2-arg constructor — same as null identifier
32+
CaseInstanceTreeElement legacy = new CaseInstanceTreeElement(base, null);
33+
assertEquals("casedb", legacy.getStorageCacheName());
34+
}
35+
}

0 commit comments

Comments
 (0)