[concurrency-interest] Timestamps-based ConcurrentMap

Bob Lee crazybob at crazybob.org
Sat Mar 11 14:05:37 EST 2006


On 3/10/06, Jean Morissette <jean.morissette at gmail.com> wrote:
> However, they need to reflect the
> state of the map at the creation of the iterator, independently of the
> map update operations.

Can you give a little more detail on your requirements? I've
implemented most of what you've said so far, but I'm not sure why
something like this would be useful. Not tested:

/**
 * Does not support null values.
 */
public class SnapshotMap<K, V> implements ConcurrentMap<K, V> {

  ConcurrentMap<K, V> delegate;
  volatile Undos undos;

  public SnapshotMap(ConcurrentMap<K, V> delegate) {
    this.delegate = delegate;
  }

  public V putIfAbsent(final K key, final V value) {
    V v = delegate.putIfAbsent(key, value);
    // assume absence if old value is null.
    if (v == null && undos != null)
      undos.add(new Undo<K, V>() {
        public void undo(Map<K, V> map) {
          map.remove(key);
        }
      });
    return v;
  }

  public boolean remove(final Object key, final Object value) {
    boolean removed = delegate.remove(key, value);
    if (removed && undos != null)
      undos.add(new Undo<K, V>() {
        public void undo(Map<K, V> map) {
          map.put((K) key, (V) value);
        }
      });
    return removed;
  }

  public boolean replace(final K key, final V oldValue, V newValue) {
    boolean replaced = delegate.replace(key, oldValue, newValue);
    if (replaced && undos != null)
      undos.add(new Undo<K, V>() {
        public void undo(Map<K, V> map) {
          map.put(key, oldValue);
        }
      });
    return replaced;
  }

  public V replace(final K key, V value) {
    final V oldValue = delegate.replace(key, value);
    if (oldValue != null && undos != null)
      undos.add(new Undo<K, V>() {
        public void undo(Map<K, V> map) {
          map.put(key, oldValue);
        }
      });
    return oldValue;
  }

  public int size() {
    return delegate.size();
  }

  public boolean isEmpty() {
    return delegate.isEmpty();
  }

  public boolean containsKey(Object key) {
    return delegate.containsKey(key);
  }

  public boolean containsValue(Object value) {
    return delegate.containsValue(value);
  }

  public V get(Object key) {
    return delegate.get(key);
  }

  public V put(final K key, V value) {
    final V oldValue = delegate.put(key, value);
    if (oldValue != null && undos != null)
      undos.add(new Undo<K, V>() {
        public void undo(Map<K, V> map) {
          map.put(key, oldValue);
        }
      });
    return oldValue;
  }

  public V remove(final Object key) {
    final V oldValue = delegate.remove(key);
    if (oldValue != null && undos != null)
      undos.add(new Undo<K, V>() {
        public void undo(Map<K, V> map) {
          map.put((K) key, oldValue);
        }
      });
    return oldValue;
  }

  public void putAll(Map<? extends K, ? extends V> t) {
    // not sure what to do here. synchronize?
    delegate.putAll(t);
  }

  public void clear() {
    // ditto.
    delegate.clear();
  }

  public Set<K> keySet() {
    return snapshot().keySet();
  }

  public Collection<V> values() {
    return snapshot().values();
  }

  public Set<Entry<K, V>> entrySet() {
    return snapshot().entrySet();
  }

  Map<K, V> snapshot() {
    Undos undos;
    Map<K, V> snapshot;
    synchronized (this) {
      this.undos = new Undos();
      snapshot = new HashMap<K, V>(delegate);
      this.undos.disable();
      undos = this.undos;
      this.undos = null;
    }
    for (Undo<K, V> undo : undos)
      undo.undo(snapshot);
    return snapshot;
  }

  public boolean equals(Object o) {
    return delegate.equals(o);
  }

  public int hashCode() {
    return delegate.hashCode();
  }

  class Undos extends LinkedList<Undo<K, V>> {

    boolean enabled = true;

    public synchronized boolean add(Undo<K, V> undo) {
      return enabled && super.add(undo);
    }

    synchronized void disable() {
      enabled = false;
    }
  }

  interface Undo<K, V> {
    void undo(Map<K, V> map);
  }
}



More information about the Concurrency-interest mailing list