Skip to content

Commit

Permalink
delete unsolved quests that have been downloaded more than one month …
Browse files Browse the repository at this point in the history
…ago (#766)
  • Loading branch information
westnordost committed Nov 18, 2018
1 parent c2eed9c commit 741fb3f
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,20 @@ public void testDeleteAllClosed()
assertEquals(2,dao.deleteAllClosed(System.currentTimeMillis() + 10000L));
}

public void testDeleteAllUnsolved()
{
OsmQuest quest1 = createNewQuest(1, Element.Type.NODE);
quest1.setStatus(QuestStatus.NEW);
OsmQuest quest2 = createNewQuest(2, Element.Type.NODE);
quest2.setStatus(QuestStatus.HIDDEN);
OsmQuest quest3 = createNewQuest(3, Element.Type.NODE);
quest3.setStatus(QuestStatus.ANSWERED);

addToDaos(quest1, quest2, quest3);

assertEquals(2,dao.deleteAllUnsolved(System.currentTimeMillis() + 10000L));
}

public void testDeleteReverted()
{
ElementGeometry geom = new ElementGeometry(new OsmLatLon(5,5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ public class ApplicationConstants

/** a "best before" duration for quests. Quests will not be downloaded again for any tile
* before the time expired */
public static final int REFRESH_QUESTS_AFTER = 7*24*60*60*1000; // one week in ms
public static final long REFRESH_QUESTS_AFTER = 7L*24*60*60*1000; // 1 week in ms
/** the duration after which quests will be deleted from the database if unsolved */
public static final long DELETE_UNSOLVED_QUESTS_AFTER = 1L*30*24*60*60*1000; // 1 months in ms

/** the max age of the undo history - one cannot undo changes older than X */
public static final long MAX_QUEST_UNDO_HISTORY_AGE = 24*60*60*1000; // 1 day in ms

public static final String AVATARS_CACHE_DIRECTORY = "osm_user_avatars";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ public void onServiceDisconnected(ComponentName className)
mapFragment = (QuestsMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(BuildConfig.MAPZEN_API_KEY);
updateMapQuestOffsets();

if(savedInstanceState == null)
{
questController.deleteOld();
}
}

@Override public void onStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ public int deleteAllClosed(long olderThan)
QuestStatus.CLOSED.name(), QuestStatus.REVERT.name(), String.valueOf(olderThan)});
}

public int deleteAllUnsolved(long olderThan)
{
String statusCol = getQuestStatusColumnName();
String query = "(" + statusCol + " = ? OR " + statusCol + " = ?) AND " +
getLastChangedColumnName() + " < ?";

SQLiteDatabase db = dbHelper.getWritableDatabase();
return db.delete(getTableName(), query, new String[]{
QuestStatus.NEW.name(), QuestStatus.HIDDEN.name(), String.valueOf(olderThan)});
}

public int addAll(Collection<T> quests)
{
return insertAll(quests, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,22 @@ public void upload()
{
context.startService(new Intent(context, QuestChangesUploadService.class));
}

public void deleteOld()
{
workerHandler.post(() ->
{
long timestamp = System.currentTimeMillis() - ApplicationConstants.DELETE_UNSOLVED_QUESTS_AFTER;
int deleted = osmQuestDB.deleteAllUnsolved(timestamp);
deleted += osmNoteQuestDB.deleteAllUnsolved(timestamp);

if(deleted > 0)
{
Log.d(TAG, "Deleted "+ deleted + " old unsolved quests");

osmElementDB.deleteUnreferenced();
geometryDB.deleteUnreferenced();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public boolean download(final OsmElementQuestType questType, BoundingBox bbox,
// turns out to be slow if done for every quest type
geometryDB.deleteUnreferenced();
elementDB.deleteUnreferenced();
questType.cleanMetadata();

int obsoleteAmount = previousQuests.size();
Log.i(TAG, getQuestTypeName(questType) + ": " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ public synchronized void upload(AtomicBoolean cancelState)

private void cleanUp(Set<OsmElementQuestType> questTypes)
{
long yesterday = System.currentTimeMillis() - 24 * 60 * 60 * 1000;
int deletedQuests = questDB.deleteAllClosed(yesterday);
long timestamp = System.currentTimeMillis() - ApplicationConstants.MAX_QUEST_UNDO_HISTORY_AGE;
int deletedQuests = questDB.deleteAllClosed(timestamp);
if(deletedQuests > 0)
{
elementGeometryDB.deleteUnreferenced();
Expand Down

0 comments on commit 741fb3f

Please sign in to comment.