aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2014-04-24 09:15:58 +0200
committerSamuel Tardieu <sam@rfc1149.net>2014-04-24 09:19:23 +0200
commit06cd3b2c0e6d7e0a8eb6f495f932068120cd3dab (patch)
treefae9668812169e009c0a7aad46e1593b28cbd763 /main/src
parent0fa22217a447ff4191e4e3b8a6d8932d86f91863 (diff)
downloadcgeo-06cd3b2c0e6d7e0a8eb6f495f932068120cd3dab.zip
cgeo-06cd3b2c0e6d7e0a8eb6f495f932068120cd3dab.tar.gz
cgeo-06cd3b2c0e6d7e0a8eb6f495f932068120cd3dab.tar.bz2
Revert "fix #3759: OOM while loading caches"
This reverts commit 2f3e8ddd18303123a1b81995357f27461fa1d586, as the symptom of the out of memory error might not be its cause, and it slows down the loading of lists.
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/DataStore.java114
-rw-r--r--main/src/cgeo/geocaching/utils/MiscUtils.java40
2 files changed, 56 insertions, 98 deletions
diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java
index 623301b..302bfc3 100644
--- a/main/src/cgeo/geocaching/DataStore.java
+++ b/main/src/cgeo/geocaching/DataStore.java
@@ -20,7 +20,6 @@ import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.ui.dialog.Dialogs;
import cgeo.geocaching.utils.FileUtils;
import cgeo.geocaching.utils.Log;
-import cgeo.geocaching.utils.MiscUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
@@ -1314,7 +1313,7 @@ public class DataStore {
*/
private static void removeOutdatedWaypointsOfCache(final @NonNull Geocache cache, final @NonNull Collection<String> remainingWaypointIds) {
final String idList = StringUtils.join(remainingWaypointIds, ',');
- database.delete(dbTableWaypoints, "geocode = ? AND _id NOT in (" + idList + ")", new String[]{cache.getGeocode()});
+ database.delete(dbTableWaypoints, "geocode = ? AND _id NOT in (" + idList + ")", new String[] { cache.getGeocode() });
}
/**
@@ -1559,8 +1558,8 @@ public class DataStore {
return new HashSet<Geocache>();
}
- final Set<Geocache> result = new HashSet<Geocache>();
- final List<String> remaining = new LinkedList<String>(geocodes);
+ Set<Geocache> result = new HashSet<Geocache>();
+ Set<String> remaining = new HashSet<String>(geocodes);
if (loadFlags.contains(LoadFlag.LOAD_CACHE_BEFORE)) {
for (String geocode : new HashSet<String>(remaining)) {
@@ -1580,7 +1579,7 @@ public class DataStore {
loadFlags.contains(LoadFlag.LOAD_INVENTORY) ||
loadFlags.contains(LoadFlag.LOAD_OFFLINE_LOG)) {
- final Collection<Geocache> cachesFromDB = loadCachesFromGeocodes(remaining, loadFlags);
+ final Set<Geocache> cachesFromDB = loadCachesFromGeocodes(remaining, loadFlags);
result.addAll(cachesFromDB);
for (final Geocache cache : cachesFromDB) {
remaining.remove(cache.getGeocode());
@@ -1606,11 +1605,15 @@ public class DataStore {
/**
* Load caches.
*
- * @param allGeocodes
+ * @param geocodes
* @param loadFlags
* @return Set of loaded caches. Never null.
*/
- private static Collection<Geocache> loadCachesFromGeocodes(final List<String> allGeocodes, final EnumSet<LoadFlag> loadFlags) {
+ private static Set<Geocache> loadCachesFromGeocodes(final Set<String> geocodes, final EnumSet<LoadFlag> loadFlags) {
+ if (CollectionUtils.isEmpty(geocodes)) {
+ return Collections.emptySet();
+ }
+
// do not log the entire collection of geo codes to the debug log. This can be more than 100 KB of text for large lists!
init();
@@ -1625,73 +1628,68 @@ public class DataStore {
}
query.append(" WHERE ").append(dbTableCaches).append('.');
+ query.append(DataStore.whereGeocodeIn(geocodes));
- final String queryBegin = query.toString();
- final List<Geocache> result = new ArrayList<Geocache>(allGeocodes.size());
-
- for (List<String> geocodes: MiscUtils.buffer(allGeocodes, 50)) {
- final Cursor cursor = database.rawQuery(queryBegin + DataStore.whereGeocodeIn(geocodes), null);
- try {
- final Set<Geocache> caches = new HashSet<Geocache>();
- int logIndex = -1;
+ Cursor cursor = database.rawQuery(query.toString(), null);
+ try {
+ final Set<Geocache> caches = new HashSet<Geocache>();
+ int logIndex = -1;
- while (cursor.moveToNext()) {
- final Geocache cache = DataStore.createCacheFromDatabaseContent(cursor);
+ while (cursor.moveToNext()) {
+ Geocache cache = DataStore.createCacheFromDatabaseContent(cursor);
- if (loadFlags.contains(LoadFlag.LOAD_ATTRIBUTES)) {
- cache.setAttributes(loadAttributes(cache.getGeocode()));
- }
+ if (loadFlags.contains(LoadFlag.LOAD_ATTRIBUTES)) {
+ cache.setAttributes(loadAttributes(cache.getGeocode()));
+ }
- if (loadFlags.contains(LoadFlag.LOAD_WAYPOINTS)) {
- final List<Waypoint> waypoints = loadWaypoints(cache.getGeocode());
- if (CollectionUtils.isNotEmpty(waypoints)) {
- cache.setWaypoints(waypoints, false);
- }
+ if (loadFlags.contains(LoadFlag.LOAD_WAYPOINTS)) {
+ final List<Waypoint> waypoints = loadWaypoints(cache.getGeocode());
+ if (CollectionUtils.isNotEmpty(waypoints)) {
+ cache.setWaypoints(waypoints, false);
}
+ }
- if (loadFlags.contains(LoadFlag.LOAD_SPOILERS)) {
- final List<Image> spoilers = loadSpoilers(cache.getGeocode());
- cache.setSpoilers(spoilers);
- }
+ if (loadFlags.contains(LoadFlag.LOAD_SPOILERS)) {
+ final List<Image> spoilers = loadSpoilers(cache.getGeocode());
+ cache.setSpoilers(spoilers);
+ }
- if (loadFlags.contains(LoadFlag.LOAD_LOGS)) {
- cache.setLogs(loadLogs(cache.getGeocode()));
- final Map<LogType, Integer> logCounts = loadLogCounts(cache.getGeocode());
- if (MapUtils.isNotEmpty(logCounts)) {
- cache.getLogCounts().clear();
- cache.getLogCounts().putAll(logCounts);
- }
+ if (loadFlags.contains(LoadFlag.LOAD_LOGS)) {
+ cache.setLogs(loadLogs(cache.getGeocode()));
+ final Map<LogType, Integer> logCounts = loadLogCounts(cache.getGeocode());
+ if (MapUtils.isNotEmpty(logCounts)) {
+ cache.getLogCounts().clear();
+ cache.getLogCounts().putAll(logCounts);
}
+ }
- if (loadFlags.contains(LoadFlag.LOAD_INVENTORY)) {
- final List<Trackable> inventory = loadInventory(cache.getGeocode());
- if (CollectionUtils.isNotEmpty(inventory)) {
- if (cache.getInventory() == null) {
- cache.setInventory(new ArrayList<Trackable>());
- } else {
- cache.getInventory().clear();
- }
- cache.getInventory().addAll(inventory);
+ if (loadFlags.contains(LoadFlag.LOAD_INVENTORY)) {
+ final List<Trackable> inventory = loadInventory(cache.getGeocode());
+ if (CollectionUtils.isNotEmpty(inventory)) {
+ if (cache.getInventory() == null) {
+ cache.setInventory(new ArrayList<Trackable>());
+ } else {
+ cache.getInventory().clear();
}
+ cache.getInventory().addAll(inventory);
}
+ }
- if (loadFlags.contains(LoadFlag.LOAD_OFFLINE_LOG)) {
- if (logIndex < 0) {
- logIndex = cursor.getColumnIndex("log");
- }
- cache.setLogOffline(!cursor.isNull(logIndex));
+ if (loadFlags.contains(LoadFlag.LOAD_OFFLINE_LOG)) {
+ if (logIndex < 0) {
+ logIndex = cursor.getColumnIndex("log");
}
- cache.addStorageLocation(StorageLocation.DATABASE);
- cacheCache.putCacheInCache(cache);
-
- caches.add(cache);
+ cache.setLogOffline(!cursor.isNull(logIndex));
}
- result.addAll(caches);
- } finally {
- cursor.close();
+ cache.addStorageLocation(StorageLocation.DATABASE);
+ cacheCache.putCacheInCache(cache);
+
+ caches.add(cache);
}
+ return caches;
+ } finally {
+ cursor.close();
}
- return result;
}
diff --git a/main/src/cgeo/geocaching/utils/MiscUtils.java b/main/src/cgeo/geocaching/utils/MiscUtils.java
deleted file mode 100644
index 122c4eb..0000000
--- a/main/src/cgeo/geocaching/utils/MiscUtils.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package cgeo.geocaching.utils;
-
-import org.apache.commons.collections4.iterators.IteratorIterable;
-import org.apache.commons.lang3.NotImplementedException;
-
-import java.util.Iterator;
-import java.util.List;
-
-final public class MiscUtils {
-
- private MiscUtils() {} // Do not instantiate
-
- public static <T> Iterable<List<T>> buffer(final List<T> original, final int n) {
- if (n <= 0) {
- throw new IllegalArgumentException("buffer size must be positive");
- }
- return new IteratorIterable<List<T>>(new Iterator<List<T>>() {
- final int size = original.size();
- int next = 0;
-
- @Override
- public boolean hasNext() {
- return next < size;
- }
-
- @Override
- public List<T> next() {
- final List<T> result = original.subList(next, Math.min(next + n, size));
- next += n;
- return result;
- }
-
- @Override
- public void remove() {
- throw new NotImplementedException("remove");
- }
- });
- }
-
-}