Class VersionedProperties


  • public class VersionedProperties
    extends Object
    Version properties maintain a Map<String,String>; of property k,v pairs along with versioning information metadata.

    The metadata used to verify cached values match stored values. Storing the metadata with the properties allows for comparison of properties and can be used to ensure that values being written to the backend store have not changed. This metadata should be written / appear early in the encoded bytes and be uncompressed so that decisions can be made that may make deserialization unnecessary.

    Note: Avoid using -1 because that has significance in ZooKeeper - writing a ZooKeeper node with a version of -1 disables the ZooKeeper expected version checking and just overwrites the node.

    Instances of this class are immutable.

    • Constructor Detail

      • VersionedProperties

        public VersionedProperties()
        Instantiate an initial instance with default version info and empty map.
      • VersionedProperties

        public VersionedProperties​(Map<String,​String> props)
        Instantiate an initial instance with default version info and provided property map.
        Parameters:
        props - optional map of initial property key, value pairs. The properties are assumed to have been previously validated (if required)
      • VersionedProperties

        public VersionedProperties​(long zkDataVersion,
                                   Instant timestamp,
                                   Map<String,​String> props)
        Instantiate an instance and set the initial properties to the provided values.
        Parameters:
        zkDataVersion - the ZooKeeper node data version.
        timestamp - timestamp of this version.
        props - optional map of initial property key, value pairs. The properties are assumed to have been previously validated (if required)
    • Method Detail

      • asMap

        public @NonNull Map<String,​String> asMap()
        Get an unmodifiable map with all property key,value pairs.
        Returns:
        An unmodifiable view of the property key, value pairs.
      • getDataVersion

        public long getDataVersion()
        Get the current data version. The version should match the node version of the stored data. The value should be used on data writes as the expected version. If the data write fails do to an unexpected version, it signals that the node version has changed since the instance was instantiated and encoded.

        Implementation note: The data version is stored and returned is an unsigned 32-bit integer value. Internally, ZooKeeper stores the value as a 32-bit signed value that can roll-over and become negative. The can break applications that rely on the value to always increase. This class avoids a negative roll-over after 2^31 (it is still possible that the value could roll-over to 0).

        Returns:
        0 for initial version, otherwise the data version when the properties were read from ZooKeeper.
      • getTimestamp

        public Instant getTimestamp()
        The timestamp of the instance when created or last modified.
        Returns:
        the timestamp of the instance.
      • getTimestampISO

        public String getTimestampISO()
        The timestamp formatted as an ISO 8601 string with format of YYYY-MM-DDTHH:mm:ss.SSSSSSZ
        Returns:
        a formatted timestamp string.
      • addOrUpdate

        public VersionedProperties addOrUpdate​(String key,
                                               String value)
        Update a single property. If a property already exists it is overwritten.

        It is much more efficient to add multiple properties at a time rather than one by one.

        Because instances of this class are immutable, this method creates a new copy of the properties. Other processes will continue to see original values retrieved from the data store. Other processes will receive an update when the instance is encoded and stored in the data store and then retrieved with the normal store update mechanisms.

        Parameters:
        key - the property name.
        value - the property value.
        Returns:
        A new instance of this class with the property added or updated.
      • addOrUpdate

        public VersionedProperties addOrUpdate​(Map<String,​String> updates)
        Add or update multiple properties. If a property already exists it is overwritten.

        Because instances of this class are immutable, this method creates a new copy of the properties. Other processes will continue to see original values retrieved from the data store. Other processes will receive an update when the instance is encoded and stored in the data store and then retrieved with the normal store update mechanisms.

        Parameters:
        updates - A map of key, values pairs.
        Returns:
        A new instance of this class with the properties added or updated.
      • replaceAll

        public VersionedProperties replaceAll​(Map<String,​String> updates)
        Replaces all current properties. If a property already exists it is overwritten. If a property is not included in the updates map, the property will not be set.
        Parameters:
        updates - A map of key, values pairs.
        Returns:
        A new instance of this class with the replaced properties.
      • remove

        public VersionedProperties remove​(Collection<String> keys)
        Delete multiple properties provided as a collection of keys.

        Because instances of this class are immutable, this method creates a new copy of the properties. Other processes will continue to see original values retrieved from the data store. Other processes will receive an update when the instance is encoded and stored in the data store and then retrieved with the normal store update mechanisms.

        Parameters:
        keys - a collection of the keys that if they exist, will be removed.
        Returns:
        A new instance of this class.
      • print

        public String print​(boolean prettyPrint)
        Generate a formatted string for debugging, either as a single line or human-friendly, multi-line format.
        Parameters:
        prettyPrint - if true, generate human-friendly string
        Returns:
        a formatted string