Skip to content
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

Moves Async entity Implementation to Core #2732

Merged
merged 8 commits into from
Apr 10, 2024
27 changes: 5 additions & 22 deletions app/src/org/commcare/models/AsyncEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import static org.commcare.models.database.user.models.EntityStorageCache.getCacheKey;

import net.sqlcipher.database.SQLiteDatabase;

import org.commcare.CommCareApplication;
import org.commcare.cases.entity.Entity;
import org.commcare.logging.XPathErrorLogger;
import org.commcare.models.database.user.models.EntityStorageCache;
import org.commcare.suite.model.DetailField;
import org.commcare.suite.model.Text;
import org.commcare.utils.SessionUnavailableException;
import org.commcare.utils.StringUtils;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.TreeReference;
Expand Down Expand Up @@ -128,24 +124,14 @@ public String getNormalizedField(int i) {

@Override
public String getSortField(int i) {
//Get a db handle so we can get an outer lock
SQLiteDatabase db;
try {
db = CommCareApplication.instance().getUserDbHandle();
} catch (SessionUnavailableException e) {
return null;
}

//get the db lock
db.beginTransaction();
try {
if (EntityStorageCache.lockCache()) {
snopoke marked this conversation as resolved.
Show resolved Hide resolved
//get our second lock.
synchronized (mAsyncLock) {
if (sortData[i] == null) {
// sort data not in search field cache; load and store it
Text sortText = fields[i].getSort();
if (sortText == null) {
db.setTransactionSuccessful();
EntityStorageCache.releaseCache();
return null;
}

Expand All @@ -156,7 +142,7 @@ public String getSortField(int i) {
String value = mEntityStorageCache.retrieveCacheValue(mCacheIndex, cacheKey);
if (value != null) {
this.setSortData(i, value);
db.setTransactionSuccessful();
EntityStorageCache.releaseCache();
return sortData[i];
}
}
Expand All @@ -177,14 +163,11 @@ public String getSortField(int i) {
sortData[i] = "<invalid xpath: " + xpe.getMessage() + ">";
}
}
db.setTransactionSuccessful();
EntityStorageCache.releaseCache();
return sortData[i];
}

} finally {
//free the db lock.
db.endTransaction();
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import org.commcare.AppUtils;
import org.commcare.CommCareApplication;
import org.commcare.android.logging.ReportingUtils;
import org.commcare.models.AsyncEntity;
import org.commcare.models.database.DbUtil;
import org.commcare.modern.database.TableBuilder;
Expand All @@ -18,6 +17,7 @@
import org.commcare.modern.util.Pair;
import org.commcare.suite.model.Detail;
import org.commcare.suite.model.DetailField;
import org.commcare.utils.SessionUnavailableException;

import java.util.Collection;
import java.util.Hashtable;
Expand Down Expand Up @@ -71,6 +71,31 @@ public static void createIndexes(SQLiteDatabase db) {
db.execSQL(DatabaseIndexingUtils.indexOnTableCommand("NAME_ENTITY_KEY", TABLE_NAME, COL_CACHE_NAME + ", " + COL_ENTITY_KEY + ", " + COL_CACHE_KEY));
}

public static boolean lockCache() {
//Get a db handle so we can get an outer lock
SQLiteDatabase db;
try {
db = CommCareApplication.instance().getUserDbHandle();
} catch (SessionUnavailableException e) {
return false;
}

//get the db lock
db.beginTransaction();
return true;
}

public static void releaseCache() {
SQLiteDatabase db;
try {
db = CommCareApplication.instance().getUserDbHandle();
db.setTransactionSuccessful();
db.endTransaction();
} catch (SessionUnavailableException e) {
// do nothing
}
}

//TODO: We should do some synchronization to make it the case that nothing can hold
//an object for the same cache at once

Expand Down