better cache invalidate handling

This commit is contained in:
Milos Kozak 2017-05-29 15:26:25 +02:00
parent c90de88ec1
commit 9032deaaf3

View file

@ -183,7 +183,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class); TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class); TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class); TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
latestTreatmentChange = 0L; updateLatestTreatmentChange(0);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -199,7 +199,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
try { try {
TableUtils.dropTable(connectionSource, Treatment.class, true); TableUtils.dropTable(connectionSource, Treatment.class, true);
TableUtils.createTableIfNotExists(connectionSource, Treatment.class); TableUtils.createTableIfNotExists(connectionSource, Treatment.class);
latestTreatmentChange = 0L; updateLatestTreatmentChange(0);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -220,6 +220,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
try { try {
TableUtils.dropTable(connectionSource, TemporaryBasal.class, true); TableUtils.dropTable(connectionSource, TemporaryBasal.class, true);
TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class); TableUtils.createTableIfNotExists(connectionSource, TemporaryBasal.class);
updateLatestTreatmentChange(0);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -230,13 +231,14 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
try { try {
TableUtils.dropTable(connectionSource, ExtendedBolus.class, true); TableUtils.dropTable(connectionSource, ExtendedBolus.class, true);
TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class); TableUtils.createTableIfNotExists(connectionSource, ExtendedBolus.class);
updateLatestTreatmentChange(0);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
scheduleExtendedBolusChange(); scheduleExtendedBolusChange();
} }
public void resetCareportalEvents() { public void resetCareportalEvents() {
try { try {
TableUtils.dropTable(connectionSource, CareportalEvent.class, true); TableUtils.dropTable(connectionSource, CareportalEvent.class, true);
TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class); TableUtils.createTableIfNotExists(connectionSource, CareportalEvent.class);
@ -453,7 +455,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
boolean historyChange = changeAffectingIobCob(treatment); boolean historyChange = changeAffectingIobCob(treatment);
status = getDaoTreatments().createOrUpdate(treatment); status = getDaoTreatments().createOrUpdate(treatment);
if (historyChange) if (historyChange)
latestTreatmentChange = treatment.date; updateLatestTreatmentChange(treatment.date);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -464,29 +466,21 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public void delete(Treatment treatment) { public void delete(Treatment treatment) {
try { try {
getDaoTreatments().delete(treatment); getDaoTreatments().delete(treatment);
latestTreatmentChange = treatment.date; updateLatestTreatmentChange(treatment.date);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
scheduleTreatmentChange(); scheduleTreatmentChange();
} }
public int deleteTreatmentById(String _id) { public void deleteTreatmentById(String _id) {
int removed = 0; Treatment stored = findTreatmentById(_id);
try { if (stored != null) {
Treatment stored = findTreatmentById(_id); log.debug("Removing TempTarget record from database: " + stored.log());
if (stored != null) { delete(stored);
log.debug("Removing TempTarget record from database: " + stored.log()); } else {
removed = getDaoTreatments().delete(stored); log.debug("Treatment not found database: " + _id);
latestTreatmentChange = stored.date;
scheduleTreatmentChange();
} else {
log.debug("Treatment not found database: " + _id);
}
} catch (SQLException e) {
e.printStackTrace();
} }
return removed;
} }
@Nullable @Nullable
@ -536,6 +530,16 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return null; return null;
} }
void updateLatestTreatmentChange(long newDate) {
if (latestTreatmentChange == null) {
latestTreatmentChange = newDate;
return;
}
if (newDate < latestTreatmentChange) {
latestTreatmentChange = newDate;
}
}
static public void scheduleTreatmentChange() { static public void scheduleTreatmentChange() {
class PostRunnable implements Runnable { class PostRunnable implements Runnable {
public void run() { public void run() {
@ -746,7 +750,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TempTarget record = list.get(0); TempTarget record = list.get(0);
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("Removing TempTarget record from database: " + record.log()); log.debug("Removing TempTarget record from database: " + record.log());
getDaoTempTargets().delete(record); delete(record);
} else { } else {
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("TempTarget not found database: " + _id); log.debug("TempTarget not found database: " + _id);
@ -816,7 +820,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
tempBasal.date = tempBasal.date - tempBasal.date % 1000; tempBasal.date = tempBasal.date - tempBasal.date % 1000;
try { try {
getDaoTemporaryBasal().createOrUpdate(tempBasal); getDaoTemporaryBasal().createOrUpdate(tempBasal);
latestTreatmentChange = tempBasal.date; updateLatestTreatmentChange(tempBasal.date);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -826,7 +830,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public void delete(TemporaryBasal tempBasal) { public void delete(TemporaryBasal tempBasal) {
try { try {
getDaoTemporaryBasal().delete(tempBasal); getDaoTemporaryBasal().delete(tempBasal);
latestTreatmentChange = tempBasal.date; updateLatestTreatmentChange(tempBasal.date);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -997,8 +1001,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
TemporaryBasal record = list.get(0); TemporaryBasal record = list.get(0);
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("Removing TempBasal record from database: " + record.log()); log.debug("Removing TempBasal record from database: " + record.log());
getDaoTemporaryBasal().delete(record); delete(record);
MainApp.bus().post(new EventTempTargetChange());
} else { } else {
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("TempBasal not found database: " + _id); log.debug("TempBasal not found database: " + _id);
@ -1014,7 +1017,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
extendedBolus.date = extendedBolus.date - extendedBolus.date % 1000; extendedBolus.date = extendedBolus.date - extendedBolus.date % 1000;
try { try {
getDaoExtendedBolus().createOrUpdate(extendedBolus); getDaoExtendedBolus().createOrUpdate(extendedBolus);
latestTreatmentChange = extendedBolus.date; updateLatestTreatmentChange(extendedBolus.date);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -1024,7 +1027,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
public void delete(ExtendedBolus extendedBolus) { public void delete(ExtendedBolus extendedBolus) {
try { try {
getDaoExtendedBolus().delete(extendedBolus); getDaoExtendedBolus().delete(extendedBolus);
latestTreatmentChange = extendedBolus.date; updateLatestTreatmentChange(extendedBolus.date);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -1060,8 +1063,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
ExtendedBolus record = list.get(0); ExtendedBolus record = list.get(0);
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("Removing ExtendedBolus record from database: " + record.log()); log.debug("Removing ExtendedBolus record from database: " + record.log());
getDaoExtendedBolus().delete(record); delete(record);
scheduleExtendedBolusChange();
} else { } else {
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("ExtendedBolus not found database: " + _id); log.debug("ExtendedBolus not found database: " + _id);
@ -1198,8 +1200,7 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
CareportalEvent record = list.get(0); CareportalEvent record = list.get(0);
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("Removing CareportalEvent record from database: " + record.log()); log.debug("Removing CareportalEvent record from database: " + record.log());
getDaoCareportalEvents().delete(record); delete(record);
scheduleCareportalEventChange();
} else { } else {
if (Config.logIncommingData) if (Config.logIncommingData)
log.debug("CareportalEvent not found database: " + _id); log.debug("CareportalEvent not found database: " + _id);