org.apache.blur.zookeeper
Class ZkCachedMap

java.lang.Object
  extended by org.apache.blur.CachedMap
      extended by org.apache.blur.zookeeper.ZkCachedMap

public class ZkCachedMap
extends CachedMap

This is an simple implementation of a set-once map of string-to-string that is backed by ZooKeeper. Meaning that once the value is set a single time it cannot be set to a different value. The clear cache method is called when the internal cache is to be cleared and re-read from ZooKeeper.

Usage:

ZkCachedMap map = new ZkCachedMap(zooKeeper, path);
String key = "key";
String newValue = "value";
String value = map.get(key);
if (value == null) {
  if (map.putIfMissing(key, newValue)) {
    System.out.println("Yay! My value was taken.");
    value = newValue;
  } else {
    System.out.println("Boo! Someone beat me to it.");
    value = map.get(key);
  }
}
System.out.println("key [" + key + "] value [" + value + "]");


Constructor Summary
ZkCachedMap(org.apache.zookeeper.ZooKeeper zooKeeper, String basePath)
           
 
Method Summary
 void clearCache()
          Clears the in memory cache of the map, this forces a re-read from the source.
 Map<String,String> fetchAllFromSource()
          Fetches all the keys and values for the map from the source.
 String get(String key)
          Checks the in memory map first, then fetches from ZooKeeper.
 boolean putIfMissing(String key, String value)
          Checks the in memory map first, if it exists then return true.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ZkCachedMap

public ZkCachedMap(org.apache.zookeeper.ZooKeeper zooKeeper,
                   String basePath)
Method Detail

clearCache

public void clearCache()
Description copied from class: CachedMap
Clears the in memory cache of the map, this forces a re-read from the source.

Specified by:
clearCache in class CachedMap

get

public String get(String key)
           throws IOException
Checks the in memory map first, then fetches from ZooKeeper.

Specified by:
get in class CachedMap
Parameters:
key - the key.
Returns:
the value, null if it does not exist.
Throws:
IOException - if there is an io error.

putIfMissing

public boolean putIfMissing(String key,
                            String value)
                     throws IOException
Checks the in memory map first, if it exists then return true. If missing then check ZooKeeper.

Specified by:
putIfMissing in class CachedMap
Parameters:
key - the key.
value - the value.
Returns:
boolean, true if the put was successful, false if a value already exists.
Throws:
IOException - if there is an io error.

fetchAllFromSource

public Map<String,String> fetchAllFromSource()
                                      throws IOException
Description copied from class: CachedMap
Fetches all the keys and values for the map from the source. That means this an expensive operation and should be used sparingly.

Specified by:
fetchAllFromSource in class CachedMap
Returns:
the map of all keys to values.
Throws:
IOException


Copyright © 2012-2014 The Apache Software Foundation. All Rights Reserved.