diff options
Diffstat (limited to 'main/src')
| -rw-r--r-- | main/src/cgeo/geocaching/DataStore.java | 73 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/SearchResult.java | 20 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCMap.java | 9 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCParser.java | 15 |
4 files changed, 75 insertions, 42 deletions
diff --git a/main/src/cgeo/geocaching/DataStore.java b/main/src/cgeo/geocaching/DataStore.java index a5d15a4..662ce8b 100644 --- a/main/src/cgeo/geocaching/DataStore.java +++ b/main/src/cgeo/geocaching/DataStore.java @@ -1012,36 +1012,67 @@ public class DataStore { * the Cache to save in the CacheCache/DB * @param saveFlags * - * @return true = cache saved successfully to the CacheCache/DB */ - public static boolean saveCache(Geocache cache, EnumSet<LoadFlags.SaveFlag> saveFlags) { - if (cache == null) { - throw new IllegalArgumentException("cache must not be null"); + public static void saveCache(Geocache cache, EnumSet<LoadFlags.SaveFlag> saveFlags) { + saveCaches(Collections.singletonList(cache), saveFlags); + } + + /** + * Save/store a cache to the CacheCache + * + * @param caches + * the caches to save in the CacheCache/DB + * @param saveFlags + * + */ + public static void saveCaches(Collection<Geocache> caches, EnumSet<LoadFlags.SaveFlag> saveFlags) { + if (CollectionUtils.isEmpty(caches)) { + return; + } + final ArrayList<String> cachesFromDatabase = new ArrayList<String>(); + final HashMap<String, Geocache> existingCaches = new HashMap<String, Geocache>(); + + // first check which caches are in the memory cache + for (Geocache cache : caches) { + final String geocode = cache.getGeocode(); + final Geocache cacheFromCache = cacheCache.getCacheFromCache(geocode); + if (cacheFromCache == null) { + cachesFromDatabase.add(geocode); + } + else { + existingCaches.put(geocode, cacheFromCache); + } + } + + // then load all remaining caches from the database in one step + for (Geocache cacheFromDatabase : loadCaches(cachesFromDatabase, LoadFlags.LOAD_ALL_DB_ONLY)) { + existingCaches.put(cacheFromDatabase.getGeocode(), cacheFromDatabase); } + final ArrayList<Geocache> toBeStored = new ArrayList<Geocache>(); // Merge with the data already stored in the CacheCache or in the database if // the cache had not been loaded before, and update the CacheCache. // Also, a DB update is required if the merge data comes from the CacheCache // (as it may be more recent than the version in the database), or if the // version coming from the database is different than the version we are entering // into the cache (that includes absence from the database). - final String geocode = cache.getGeocode(); - final Geocache cacheFromCache = cacheCache.getCacheFromCache(geocode); - final boolean dbUpdateRequired = - !cache.gatherMissingFrom(cacheFromCache != null ? - cacheFromCache : - loadCache(geocode, LoadFlags.LOAD_ALL_DB_ONLY)) || - cacheFromCache != null; - cache.addStorageLocation(StorageLocation.CACHE); - cacheCache.putCacheInCache(cache); - - // Only save the cache in the database if it is requested by the caller and - // the cache contains detailed information. - if (!saveFlags.contains(SaveFlag.SAVE_DB)) { - return true; + for (Geocache cache : caches) { + final String geocode = cache.getGeocode(); + final Geocache existingCache = existingCaches.get(geocode); + final boolean dbUpdateRequired = !cache.gatherMissingFrom(existingCache) || cacheCache.getCacheFromCache(geocode) != null; + cache.addStorageLocation(StorageLocation.CACHE); + cacheCache.putCacheInCache(cache); + + // Only save the cache in the database if it is requested by the caller and + // the cache contains detailed information. + if (saveFlags.contains(SaveFlag.SAVE_DB) && cache.isDetailed() && dbUpdateRequired) { + toBeStored.add(cache); + } } - return cache.isDetailed() && dbUpdateRequired && storeIntoDatabase(cache); + for (Geocache geocache : toBeStored) { + storeIntoDatabase(geocache); + } } private static boolean storeIntoDatabase(final Geocache cache) { @@ -2870,8 +2901,8 @@ public class DataStore { return result; } - public static boolean saveChangedCache(Geocache cache) { - return DataStore.saveCache(cache, cache.getStorageLocation().contains(StorageLocation.DATABASE) ? LoadFlags.SAVE_ALL : EnumSet.of(SaveFlag.SAVE_CACHE)); + public static void saveChangedCache(Geocache cache) { + DataStore.saveCache(cache, cache.getStorageLocation().contains(StorageLocation.DATABASE) ? LoadFlags.SAVE_ALL : EnumSet.of(SaveFlag.SAVE_CACHE)); } private static class PreparedStatements { diff --git a/main/src/cgeo/geocaching/SearchResult.java b/main/src/cgeo/geocaching/SearchResult.java index 5d63a2d..131a01c 100644 --- a/main/src/cgeo/geocaching/SearchResult.java +++ b/main/src/cgeo/geocaching/SearchResult.java @@ -126,9 +126,7 @@ public class SearchResult implements Parcelable { */ public SearchResult(final Collection<Geocache> caches) { this(); - for (final Geocache cache : caches) { - addAndPutInCache(cache); - } + addAndPutInCache(caches); } @Override @@ -209,7 +207,7 @@ public class SearchResult implements Parcelable { SearchResult result = new SearchResult(this); result.geocodes.clear(); - final ArrayList<Geocache> cachesForVote = new ArrayList<Geocache>(); + final ArrayList<Geocache> includedCaches = new ArrayList<Geocache>(); final Set<Geocache> caches = DataStore.loadCaches(geocodes, LoadFlags.LOAD_CACHE_OR_DB); int excluded = 0; for (Geocache cache : caches) { @@ -220,13 +218,13 @@ public class SearchResult implements Parcelable { if (excludeCache) { excluded++; } else { - result.addAndPutInCache(cache); - cachesForVote.add(cache); + includedCaches.add(cache); } } + result.addAndPutInCache(includedCaches); // decrease maximum number of caches by filtered ones result.setTotalCountGC(result.getTotalCountGC() - excluded); - GCVote.loadRatings(cachesForVote); + GCVote.loadRatings(includedCaches); return result; } @@ -252,9 +250,11 @@ public class SearchResult implements Parcelable { } /** Add the cache geocode to the search and store the cache in the CacheCache */ - public boolean addAndPutInCache(final Geocache cache) { - addGeocode(cache.getGeocode()); - return DataStore.saveCache(cache, EnumSet.of(SaveFlag.SAVE_CACHE)); + public void addAndPutInCache(final Collection<Geocache> caches) { + for (Geocache geocache : caches) { + addGeocode(geocache.getGeocode()); + } + DataStore.saveCaches(caches, EnumSet.of(SaveFlag.SAVE_CACHE)); } public boolean isEmpty() { diff --git a/main/src/cgeo/geocaching/connector/gc/GCMap.java b/main/src/cgeo/geocaching/connector/gc/GCMap.java index aabeb56..6c94150 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCMap.java +++ b/main/src/cgeo/geocaching/connector/gc/GCMap.java @@ -69,6 +69,7 @@ public class GCMap { throw new JSONException("No data inside JSON"); } + final ArrayList<Geocache> caches = new ArrayList<Geocache>(); for (int j = 0; j < dataArray.length(); j++) { final Geocache cache = new Geocache(); @@ -93,9 +94,9 @@ public class GCMap { JSONObject ownerObj = dataObject.getJSONObject("owner"); cache.setOwnerDisplayName(ownerObj.getString("text")); - result.addAndPutInCache(cache); - + caches.add(cache); } + result.addAndPutInCache(caches); } catch (JSONException e) { result.setError(StatusCode.UNKNOWN_ERROR); } catch (ParseException e) { @@ -200,6 +201,7 @@ public class GCMap { } } + final ArrayList<Geocache> caches = new ArrayList<Geocache>(); for (Entry<String, List<UTFGridPosition>> entry : positions.entrySet()) { String id = entry.getKey(); List<UTFGridPosition> pos = entry.getValue(); @@ -231,9 +233,10 @@ public class GCMap { exclude = true; } if (!exclude) { - searchResult.addAndPutInCache(cache); + caches.add(cache); } } + searchResult.addAndPutInCache(caches); Log.d("Retrieved " + searchResult.getCount() + " caches for tile " + tile.toString()); } catch (RuntimeException e) { diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java index f05bc04..62ccb14 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCParser.java +++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java @@ -121,6 +121,7 @@ public abstract class GCParser { final int rows_count = rows.length; int excludedCaches = 0; + final ArrayList<Geocache> caches = new ArrayList<Geocache>(); for (int z = 1; z < rows_count; z++) { final Geocache cache = new Geocache(); final String row = rows[z]; @@ -261,8 +262,9 @@ public abstract class GCParser { Log.w("GCParser.parseSearch: Failed to parse favorite count"); } - searchResult.addAndPutInCache(cache); + caches.add(cache); } + searchResult.addAndPutInCache(caches); // total caches found try { @@ -324,8 +326,8 @@ public abstract class GCParser { // get direction images if (Settings.getLoadDirImg()) { - final Set<Geocache> caches = searchResult.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); - for (final Geocache cache : caches) { + final Set<Geocache> cachesReloaded = searchResult.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); + for (final Geocache cache : cachesReloaded) { if (cache.getCoords() == null && StringUtils.isNotEmpty(cache.getDirectionImg())) { DirectionImage.getDrawable(cache.getDirectionImg()); } @@ -748,15 +750,12 @@ public abstract class GCParser { } cache.setDetailedUpdatedNow(); - searchResult.addAndPutInCache(cache); + searchResult.addAndPutInCache(Collections.singletonList(cache)); return searchResult; } private static String getNumberString(final String numberWithPunctuation) { - if (numberWithPunctuation == null) { - return null; - } - return numberWithPunctuation.replaceAll("[.,]", ""); + return StringUtils.replaceChars(numberWithPunctuation, ".,", ""); } public static SearchResult searchByNextPage(final SearchResult search, boolean showCaptcha, RecaptchaReceiver recaptchaReceiver) { |
