diff options
author | blafoo <github@blafoo.de> | 2012-01-24 22:48:19 +0100 |
---|---|---|
committer | blafoo <github@blafoo.de> | 2012-01-24 22:48:19 +0100 |
commit | 0a3f72f92fb970952eb29a9cb56075acacb7c3f9 (patch) | |
tree | 3e46af91b761d382b38aed71eae62ce93452bd39 | |
parent | beb7f7be8f127fba5ad333d6c31cefd1596157b6 (diff) | |
download | cgeo-0a3f72f92fb970952eb29a9cb56075acacb7c3f9.zip cgeo-0a3f72f92fb970952eb29a9cb56075acacb7c3f9.tar.gz cgeo-0a3f72f92fb970952eb29a9cb56075acacb7c3f9.tar.bz2 |
Stronger use of CacheCache
-rw-r--r-- | main/src/cgeo/geocaching/CacheCache.java | 20 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/VisitCacheActivity.java | 5 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgData.java | 51 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/cgeoapplication.java | 43 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/maps/CGeoMap.java | 2 |
5 files changed, 51 insertions, 70 deletions
diff --git a/main/src/cgeo/geocaching/CacheCache.java b/main/src/cgeo/geocaching/CacheCache.java index ec6e6db..9c02b28 100644 --- a/main/src/cgeo/geocaching/CacheCache.java +++ b/main/src/cgeo/geocaching/CacheCache.java @@ -3,9 +3,6 @@ package cgeo.geocaching; import cgeo.geocaching.cgData.StorageLocation; import cgeo.geocaching.utils.LeastRecentlyUsedCache; -import java.util.ArrayList; -import java.util.List; - /** * Cache for Caches. Every cache is stored in memory while c:geo is active to * speed up the app and to minimize network request - which are slow. @@ -74,21 +71,4 @@ public class CacheCache { return null; } - /** - * @param geocode - * Geocode of the cache to retrieve from the cache - * @return cache if found, null else - */ - public List<cgCache> getCachesFromCache(final List<String> geocodes) { - if (geocodes == null || geocodes.isEmpty()) { - return null; - } - - ArrayList<cgCache> caches = new ArrayList<cgCache>(); - for (String geocode : geocodes) { - caches.add(getCacheFromCache(geocode)); - } - return caches; - } - } diff --git a/main/src/cgeo/geocaching/VisitCacheActivity.java b/main/src/cgeo/geocaching/VisitCacheActivity.java index f3a1cbe..073e347 100644 --- a/main/src/cgeo/geocaching/VisitCacheActivity.java +++ b/main/src/cgeo/geocaching/VisitCacheActivity.java @@ -711,11 +711,6 @@ public class VisitCacheActivity extends AbstractActivity implements DateDialog.D } } - if (cache != null) { - cgeoapplication.putCacheInCache(cache); - } else { - cgeoapplication.removeCacheFromCache(geocode); - } } if (status == StatusCode.NO_ERROR) { diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index e592f7c..05bdfde 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -1245,6 +1245,10 @@ public class cgData { return cacheid; } + /** + * @param cache + * @return true = cache saved successfully to the DB + */ public boolean saveCache(cgCache cache) { //LeeB - writing to the DB is slow if (cache == null) { @@ -1255,6 +1259,11 @@ public class cgData { // this way we also remove any stale instance from the caches cache cgeoapplication.putCacheInCache(cache); + // only save fully detailed caches in the database + if (!cache.isDetailed()) { + return false; + } + ContentValues values = new ContentValues(); if (cache.getUpdated() == 0) { @@ -1839,22 +1848,43 @@ public class cgData { */ public cgCache loadCache(final String geocode, final EnumSet<LoadFlag> loadFlags) { - Object[] geocodes = new Object[1]; - - if (StringUtils.isNotBlank(geocode)) { - geocodes[0] = geocode; - } else { - geocodes = null; + if (StringUtils.isBlank(geocode)) { + return null; } - Set<cgCache> caches = loadCaches(geocodes, null, null, null, null, loadFlags); + Set<String> geocodes = new HashSet<String>(); + geocodes.add(geocode); + + Set<cgCache> caches = loadCaches(geocodes, loadFlags); return cgBase.getFirstElementFromSet(caches); } - public Set<cgCache> loadCaches(final Object[] geocodes, final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final EnumSet<LoadFlag> loadFlags) { + public Set<cgCache> loadCaches(final Set<String> geocodes, final EnumSet<LoadFlag> loadFlags) { + if (geocodes == null || geocodes.size() == 0) { + return new HashSet<cgCache>(); + } + + Set<cgCache> result = new HashSet<cgCache>(); + Set<String> remainingGeocodes = new HashSet<String>(); + for (String geocode : geocodes) { + cgCache cache = cgeoapplication.getCacheFromCache(geocode); + if (cache != null) { + result.add(cache); + } else { + remainingGeocodes.add(geocode); + } + } + Set<cgCache> cachesFromDB = loadCaches(remainingGeocodes, null, null, null, null, loadFlags); + if (cachesFromDB != null) { + result.addAll(cachesFromDB); + } + return result; + } + + public Set<cgCache> loadCaches(final Set<String> geocodes, final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final EnumSet<LoadFlag> loadFlags) { init(); // Using more than one of the parametersets results in overly comlex wheres - if ((geocodes != null && geocodes.length > 0) + if ((geocodes != null && geocodes.size() > 0) && centerLat != null && centerLon != null && spanLat != null @@ -1866,7 +1896,7 @@ public class cgData { Set<cgCache> caches = new HashSet<cgCache>(); try { - if (geocodes != null && geocodes.length > 0) { + if (geocodes != null && geocodes.size() > 0) { StringBuilder all = new StringBuilder(); for (Object one : geocodes) { if (all.length() > 0) { @@ -1989,6 +2019,7 @@ public class cgData { cache.setLogOffline(hasLogOffline(cache.getGeocode())); } cache.addStorageLocation(StorageLocation.DATABASE); + cgeoapplication.putCacheInCache(cache); caches.add(cache); } while (cursor.moveToNext()); diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java index 827e29f..c876d13 100644 --- a/main/src/cgeo/geocaching/cgeoapplication.java +++ b/main/src/cgeo/geocaching/cgeoapplication.java @@ -260,19 +260,9 @@ public class cgeoapplication extends Application { } public cgCache getCacheByGeocode(final String geocode, final EnumSet<LoadFlag> loadFlags) { - cgCache cache = CacheCache.getInstance().getCacheFromCache(geocode); - if (cache != null) { - return cache; - } - - cache = storage.loadCache(geocode, loadFlags); - if (cache != null && cache.isDetailed() && loadFlags == LoadFlags.LOADALL) { - // "Store" cache for the next access in the cache - CacheCache.getInstance().putCacheInCache(cache); - } + return storage.loadCache(geocode, loadFlags); - return cache; } public cgTrackable getTrackableByGeocode(String geocode) { @@ -294,6 +284,10 @@ public class cgeoapplication extends Application { CacheCache.getInstance().putCacheInCache(cache); } + public static cgCache getCacheFromCache(final String geocode) { + return CacheCache.getInstance().getCacheFromCache(geocode); + } + public String[] geocodesInCache() { return storage.allDetailedThere(); } @@ -342,36 +336,19 @@ public class cgeoapplication extends Application { * @return */ public Set<cgCache> getCaches(final SearchResult search, final boolean loadWaypoints) { - return getCaches(search, null, null, null, null, loadWaypoints ? EnumSet.of(LoadFlag.LOADWAYPOINTS, LoadFlag.LOADOFFLINELOG) : EnumSet.of(LoadFlag.LOADOFFLINELOG)); + return storage.loadCaches(search.getGeocodes(), loadWaypoints ? EnumSet.of(LoadFlag.LOADWAYPOINTS, LoadFlag.LOADOFFLINELOG) : EnumSet.of(LoadFlag.LOADOFFLINELOG)); } public Set<cgCache> getCaches(final SearchResult search, Long centerLat, Long centerLon, Long spanLat, Long spanLon) { - return getCaches(search, centerLat, centerLon, spanLat, spanLon, EnumSet.of(LoadFlag.LOADWAYPOINTS, LoadFlag.LOADOFFLINELOG)); - } - - public Set<cgCache> getCaches(final SearchResult search, final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final EnumSet<LoadFlag> loadFlags) { if (search == null) { - Set<cgCache> cachesOut = new HashSet<cgCache>(); - - final Set<cgCache> cachesPre = storage.loadCaches(null, centerLat, centerLon, spanLat, spanLon, loadFlags); - if (cachesPre != null) { - cachesOut.addAll(cachesPre); - } - - return cachesOut; + final Set<cgCache> cachesPre = storage.loadCaches(null, centerLat, centerLon, spanLat, spanLon, EnumSet.of(LoadFlag.LOADWAYPOINTS, LoadFlag.LOADOFFLINELOG)); + return cachesPre != null ? cachesPre : new HashSet<cgCache>(); } - Set<cgCache> cachesOut = new HashSet<cgCache>(); - - final Set<String> geocodeList = search.getGeocodes(); - // The list of geocodes is sufficient. more parameters generate an overly complex select. - final Set<cgCache> cachesPre = storage.loadCaches(geocodeList.toArray(), null, null, null, null, loadFlags); - if (cachesPre != null) { - cachesOut.addAll(cachesPre); - } + final Set<cgCache> cachesPre = storage.loadCaches(search.getGeocodes(), EnumSet.of(LoadFlag.LOADWAYPOINTS, LoadFlag.LOADOFFLINELOG)); + return cachesPre != null ? cachesPre : new HashSet<cgCache>(); - return cachesOut; } public SearchResult getBatchOfStoredCaches(final boolean detailedOnly, final Geopoint coords, final CacheType cacheType, final int list) { diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 6592a3f..dece401 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -1289,8 +1289,6 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory return; } - //TODO Portree Only overwrite if we got some. Otherwise maybe error icon - //TODO Merge not to show locally found caches caches = app.getCaches(search, centerLat, centerLon, spanLat, spanLon); if (stop) { |