Class SyncDB

java.lang.Object
tripleplay.syncdb.SyncDB

public abstract class SyncDB extends Object
A database of key/value pairs that is synced (via a server) across multiple devices. The suggested usage pattern is to subclass SyncDB and define your properties like so:

 public class FooDB extends SyncDB {
   public final Value<String> name = value("name", (String)null, Codec.STRING, Resolver.SERVER);
   public final Value<Integer> xp = value("xp", 0, Codec.INT, Resolver.INTMAX);
   public final Value<Integer> difficultyLevel = value("diff", 0, Codec.INT, Resolver.SERVER);
   public final RSet<String> badges = set("badges", Codec.STRING, SetResolver.UNION);
   public final RMap<String,Integer> items = map("it", Codec.STRING, Codec.INT, Resolver.INTMAX);
   // etc.
 }
 
  • Method Details

    • version

      public int version()
      Returns the version at which this database was last synced.
    • hasUnsyncedChanges

      public boolean hasUnsyncedChanges()
      Returns true if this database contains changes that have not been synced.
    • getDelta

      public Map<String,String> getDelta()
      Returns a map of properties that have changed since our last sync. These can be sent to the server (along with our current version) to sync our state with the server.
    • getMods

      public Map<String,Integer> getMods()
      Returns a snapshot of our current mapping of modified properties to modification count. This must be taken at the same time as a call to getDelta(), and then provided to the call to noteSync(int, java.util.Map<java.lang.String, java.lang.Integer>) if the delta was successfully used in a sync.
    • noteSync

      public void noteSync(int version, Map<String,Integer> syncedMods)
      Notes that we synced cleanly with the server. Updates our local version to the latest sync version and notes that we no longer have unsynced modifications.
    • containsMerges

      public boolean containsMerges(Map<String,String> delta)
      Returns whether the supplied delta contains changes to any properties for which unsynced changes also exist.
    • applyDelta

      public void applyDelta(int version, Map<String,String> delta)
      Applies the supplied changes to this database. Any conflicts will be resolved according to the configured policies. If the resolved value differs from the supplied value, the property will remain marked as locally modified. Thus changes may need to be flushed again after calling this method. After applying the delta and resolving conflicts, the local version will be updated to the supplied version.
      Parameters:
      version - the latest version.
      delta - the modifications from our local version to the latest version.
    • prepareToMeld

      public void prepareToMeld()
      Prepares this database to be melded into a pre-existing database. Resets this database's version to zero and marks all properties as modified. The database can then be synced with another database which will merge the other database into this one and then push remaining changes back up to the server. This is used to merge the database between two clients.
    • processPurges

      public void processPurges()
      Processes any subdbs that have been queued for purging. This requires a pass over all keys in the storage system, and thus benefits from aggregating purges prior to performing them.