Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.sync; |
michael@0 | 6 | |
michael@0 | 7 | import java.util.Collections; |
michael@0 | 8 | import java.util.HashMap; |
michael@0 | 9 | import java.util.Map; |
michael@0 | 10 | import java.util.Map.Entry; |
michael@0 | 11 | |
michael@0 | 12 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * Fetches the timestamp information in <code>info/collections</code> on the |
michael@0 | 16 | * Sync server. Provides access to those timestamps, along with logic to check |
michael@0 | 17 | * for whether a collection requires an update. |
michael@0 | 18 | */ |
michael@0 | 19 | public class InfoCollections { |
michael@0 | 20 | private static final String LOG_TAG = "InfoCollections"; |
michael@0 | 21 | |
michael@0 | 22 | /** |
michael@0 | 23 | * Fields fetched from the server, or <code>null</code> if not yet fetched. |
michael@0 | 24 | * <p> |
michael@0 | 25 | * Rather than storing decimal/double timestamps, as provided by the server, |
michael@0 | 26 | * we convert immediately to milliseconds since epoch. |
michael@0 | 27 | */ |
michael@0 | 28 | final Map<String, Long> timestamps; |
michael@0 | 29 | |
michael@0 | 30 | public InfoCollections() { |
michael@0 | 31 | this(new ExtendedJSONObject()); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | public InfoCollections(final ExtendedJSONObject record) { |
michael@0 | 35 | Logger.debug(LOG_TAG, "info/collections is " + record.toJSONString()); |
michael@0 | 36 | HashMap<String, Long> map = new HashMap<String, Long>(); |
michael@0 | 37 | |
michael@0 | 38 | for (Entry<String, Object> entry : record.entrySet()) { |
michael@0 | 39 | final String key = entry.getKey(); |
michael@0 | 40 | final Object value = entry.getValue(); |
michael@0 | 41 | |
michael@0 | 42 | // These objects are most likely going to be Doubles. Regardless, we |
michael@0 | 43 | // want to get them in a more sane time format. |
michael@0 | 44 | if (value instanceof Double) { |
michael@0 | 45 | map.put(key, Utils.decimalSecondsToMilliseconds((Double) value)); |
michael@0 | 46 | continue; |
michael@0 | 47 | } |
michael@0 | 48 | if (value instanceof Long) { |
michael@0 | 49 | map.put(key, Utils.decimalSecondsToMilliseconds((Long) value)); |
michael@0 | 50 | continue; |
michael@0 | 51 | } |
michael@0 | 52 | if (value instanceof Integer) { |
michael@0 | 53 | map.put(key, Utils.decimalSecondsToMilliseconds((Integer) value)); |
michael@0 | 54 | continue; |
michael@0 | 55 | } |
michael@0 | 56 | Logger.warn(LOG_TAG, "Skipping info/collections entry for " + key); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | this.timestamps = Collections.unmodifiableMap(map); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Return the timestamp for the given collection, or null if the timestamps |
michael@0 | 64 | * have not been fetched or the given collection does not have a timestamp. |
michael@0 | 65 | * |
michael@0 | 66 | * @param collection |
michael@0 | 67 | * The collection to inspect. |
michael@0 | 68 | * @return the timestamp in milliseconds since epoch. |
michael@0 | 69 | */ |
michael@0 | 70 | public Long getTimestamp(String collection) { |
michael@0 | 71 | if (timestamps == null) { |
michael@0 | 72 | return null; |
michael@0 | 73 | } |
michael@0 | 74 | return timestamps.get(collection); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | /** |
michael@0 | 78 | * Test if a given collection needs to be updated. |
michael@0 | 79 | * |
michael@0 | 80 | * @param collection |
michael@0 | 81 | * The collection to test. |
michael@0 | 82 | * @param lastModified |
michael@0 | 83 | * Timestamp when local record was last modified. |
michael@0 | 84 | */ |
michael@0 | 85 | public boolean updateNeeded(String collection, long lastModified) { |
michael@0 | 86 | Logger.trace(LOG_TAG, "Testing " + collection + " for updateNeeded. Local last modified is " + lastModified + "."); |
michael@0 | 87 | |
michael@0 | 88 | // No local record of modification time? Need an update. |
michael@0 | 89 | if (lastModified <= 0) { |
michael@0 | 90 | return true; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | // No meta/global on the server? We need an update. The server fetch will fail and |
michael@0 | 94 | // then we will upload a fresh meta/global. |
michael@0 | 95 | Long serverLastModified = getTimestamp(collection); |
michael@0 | 96 | if (serverLastModified == null) { |
michael@0 | 97 | return true; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | // Otherwise, we need an update if our modification time is stale. |
michael@0 | 101 | return (serverLastModified.longValue() > lastModified); |
michael@0 | 102 | } |
michael@0 | 103 | } |