SMILA (incubation) API documentation

org.eclipse.smila.zookeeper
Class ZkConcurrentMap

java.lang.Object
  extended by org.eclipse.smila.zookeeper.ZkConcurrentMap

public class ZkConcurrentMap
extends java.lang.Object

Provides a map view to the zookeeper structure below an existing root node path. Additionally it has methods to "merge" values with existing ones (e.g. add() to sum ints) Methods are similar (but not equal) to ConcurrentHashMap. ZkConcurrentMap also uses no locks.

Author:
aweber

Constructor Summary
ZkConcurrentMap(ZkConnection zk, java.lang.String rootNodePath)
           
ZkConcurrentMap(ZooKeeperService service, java.lang.String rootNodePath)
           
 
Method Summary
 java.lang.Integer add(java.lang.String key, int value)
          add value to old value of given key.
 void clear()
          Removes all mappings from this map.
 boolean containsKey(java.lang.String key)
           
 java.lang.Integer decIfGreaterThan(java.lang.String key, long min)
          dec value of given key, but only if value is not negative and value > min.
 java.lang.Integer getInt(java.lang.String key)
           
 java.lang.String getString(java.lang.String key)
           
 java.lang.Integer getVersion(java.lang.String key)
          revision of the key node.
 java.lang.Integer incIfLessThan(java.lang.String key, long max)
          inc value of given key, but only if value is not negative and value < max.
 java.util.Set<java.lang.String> keySet()
           
 boolean put(java.lang.String key, java.lang.String value)
          set value for given key.
 java.lang.String putIfAbsent(java.lang.String key, java.lang.String value)
          set value for given key if key doesn't exist yet.
 void remove(java.lang.String key)
          Remove given key / value mapping.
 boolean replace(java.lang.String key, java.lang.String oldValue, java.lang.String newValue)
          Set new value for given key if key exists and has given old value.
 java.lang.Integer replaceAndGetVersion(java.lang.String key, java.lang.String oldValue, java.lang.String newValue)
          Set new value for given key if key exists and has given old value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ZkConcurrentMap

public ZkConcurrentMap(ZooKeeperService service,
                       java.lang.String rootNodePath)
Parameters:
service - the zookeeper service
rootNodePath - the existing(!) zookeeper root node path from where to start the map view.

ZkConcurrentMap

public ZkConcurrentMap(ZkConnection zk,
                       java.lang.String rootNodePath)
Parameters:
zk - shared ZooKeeper connection
rootNodePath - the existing(!) zookeeper root node path from where to start the map view.
Throws:
java.lang.IllegalArgumentException - if root node doesn't exist in zookeeper
Method Detail

put

public boolean put(java.lang.String key,
                   java.lang.String value)
set value for given key. If key exists, value is overwritten.

Parameters:
key - not allowed to be 'null'
value - not allowed to be 'null'
Returns:
'true', if successful, otherwise 'false'

putIfAbsent

public java.lang.String putIfAbsent(java.lang.String key,
                                    java.lang.String value)
set value for given key if key doesn't exist yet.

Parameters:
key - not allowed to be 'null'
value - not allowed to be 'null'
Returns:
the value of the node after the put operation. Should be used to check for expected result.

replace

public boolean replace(java.lang.String key,
                       java.lang.String oldValue,
                       java.lang.String newValue)
Set new value for given key if key exists and has given old value.

Parameters:
key - the key, not allowed to be 'null'
oldValue - the old value, not allowed to be 'null'
newValue - the new value, not allowed to be 'null'
Returns:
'true', if successful, otherwise 'false'

replaceAndGetVersion

public java.lang.Integer replaceAndGetVersion(java.lang.String key,
                                              java.lang.String oldValue,
                                              java.lang.String newValue)
Set new value for given key if key exists and has given old value.

Parameters:
key - the key, not allowed to be 'null'
oldValue - the old value, not allowed to be 'null'
newValue - the new value, not allowed to be 'null'
Returns:
the revision number for the key node, 'null' if not successful.

getString

public java.lang.String getString(java.lang.String key)
Parameters:
key - the key, not allowed to be 'null'
Returns:
value of the given key as String, or 'null' if key doesn't exist.

getInt

public java.lang.Integer getInt(java.lang.String key)
Parameters:
key - the key, not allowed to be 'null'
Returns:
value of the given key as Integer, or 'null' if mapping for key doesn't exist.

getVersion

public java.lang.Integer getVersion(java.lang.String key)
revision of the key node. Useful to detect updates.

Parameters:
key - the key, not allowed to be 'null'
Returns:
key node version.

remove

public void remove(java.lang.String key)
Remove given key / value mapping. If key doesn't exist, ignore call.

Parameters:
key - the key, not allowed to be 'null'

add

public java.lang.Integer add(java.lang.String key,
                             int value)
add value to old value of given key. If key doesn't exist, create it.

Parameters:
key - the key, not allowed to be 'null'
value - the value to add
Returns:
'newValue', if successful. If operation failed because of too many zookeeper conflicts, return 'null'.

keySet

public java.util.Set<java.lang.String> keySet()
Returns:
a set view of the keys contained in this map

containsKey

public boolean containsKey(java.lang.String key)
Returns:
'true' if this map contains a mapping for the specified key.

clear

public void clear()
Removes all mappings from this map.


incIfLessThan

public java.lang.Integer incIfLessThan(java.lang.String key,
                                       long max)
inc value of given key, but only if value is not negative and value < max. If value >= max or value < 0 ignore call and return -1. If key doesn't exist and max > 0, create entry with value '1'.

Parameters:
key - the key, not allowed to be 'null'
max - the maximum value
Returns:
incremented value if old value was not negative and old value < max, otherwise '-1'. If operation failed because of too many zookeeper conflicts, return 'null'.

decIfGreaterThan

public java.lang.Integer decIfGreaterThan(java.lang.String key,
                                          long min)
dec value of given key, but only if value is not negative and value > min. If value <= min or value < 0 ignore call and return -1. If key doesn't exist, create entry with value '0'.

Parameters:
key - the key, not allowed to be 'null'
min - the minimum value
Returns:
decremented value if old value was not negative and old value > min, otherwise '-1'. If operation failed because of too many zookeeper conflicts, return 'null'.

SMILA (incubation) API documentation