2 * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.opendaylight.yangtools.triemap;
18 import static com.google.common.base.Preconditions.checkNotNull;
19 import static com.google.common.base.Preconditions.checkState;
21 import com.google.common.base.Verify;
22 import com.google.common.collect.Iterators;
23 import java.io.ObjectStreamException;
24 import java.io.Serializable;
25 import java.util.AbstractMap;
26 import java.util.AbstractSet;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.NoSuchElementException;
32 import java.util.Optional;
34 import java.util.concurrent.ConcurrentMap;
35 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
38 * This is a port of Scala's TrieMap class from the Scala Collections library. This implementation does not support
39 * null keys nor null values.
41 * @author Roman Levenstein <romixlev@gmail.com>
46 @SuppressWarnings({"unchecked", "rawtypes", "unused"})
47 public final class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,V>, Serializable {
48 private static final AtomicReferenceFieldUpdater<TrieMap, Object> ROOT_UPDATER =
49 AtomicReferenceFieldUpdater.newUpdater(TrieMap.class, Object.class, "root");
50 private static final long serialVersionUID = 1L;
55 private final EntrySet entrySet = new EntrySet();
56 private final Equivalence<? super K> equiv;
57 private final boolean readOnly;
59 private volatile Object root;
61 private TrieMap(final INode<K, V> r, final Equivalence<? super K> equiv, final boolean readOnly) {
64 this.readOnly = readOnly;
67 TrieMap(final Equivalence<? super K> equiv) {
68 this(newRootNode(), equiv, false);
72 this(Equivalence.equals());
75 /* internal methods */
77 final Equivalence<? super K> equiv() {
81 private static <K,V> INode<K, V> newRootNode() {
82 final Gen gen = new Gen();
83 return new INode<>(gen, new CNode<>(gen));
86 final boolean CAS_ROOT(final Object ov, final Object nv) {
87 checkState(!readOnly, "Attempted to modify a read-only snapshot");
88 return ROOT_UPDATER.compareAndSet (this, ov, nv);
91 // FIXME: abort = false by default
92 final INode<K, V> readRoot(final boolean abort) {
93 return RDCSS_READ_ROOT(abort);
96 final INode<K, V> readRoot() {
97 return RDCSS_READ_ROOT(false);
100 final INode<K, V> RDCSS_READ_ROOT() {
101 return RDCSS_READ_ROOT(false);
104 final INode<K, V> RDCSS_READ_ROOT(final boolean abort) {
105 final Object r = /* READ */root;
106 if (r instanceof INode) {
107 return (INode<K, V>) r;
110 checkState(r instanceof RDCSS_Descriptor, "Unhandled root %s", r);
111 return RDCSS_Complete(abort);
114 private INode<K, V> RDCSS_Complete(final boolean abort) {
116 final Object r = /* READ */root;
117 if (r instanceof INode) {
118 return (INode<K, V>) r;
121 checkState(r instanceof RDCSS_Descriptor, "Unhandled root %s", r);
122 final RDCSS_Descriptor<K, V> desc = (RDCSS_Descriptor<K, V>) r;
123 final INode<K, V> ov = desc.old;
124 final MainNode<K, V> exp = desc.expectedmain;
125 final INode<K, V> nv = desc.nv;
128 if (CAS_ROOT(desc, ov)) {
132 // Tail recursion: return RDCSS_Complete(abort);
136 final MainNode<K, V> oldmain = ov.gcasRead(this);
137 if (oldmain == exp) {
138 if (CAS_ROOT(desc, nv)) {
139 desc.committed = true;
143 // Tail recursion: return RDCSS_Complete(abort);
147 if (CAS_ROOT(desc, ov)) {
151 // Tail recursion: return RDCSS_Complete(abort);
155 private boolean RDCSS_ROOT(final INode<K, V> ov, final MainNode<K, V> expectedmain, final INode<K, V> nv) {
156 final RDCSS_Descriptor<K, V> desc = new RDCSS_Descriptor<> (ov, expectedmain, nv);
157 if (CAS_ROOT(ov, desc)) {
158 RDCSS_Complete(false);
159 return /* READ */desc.committed;
165 private void inserthc(final K k, final int hc, final V v) {
166 // TODO: this is called from serialization only, which means we should not be observing any races,
167 // hence we should not need to pass down the entire tree, just equality (I think).
168 final INode<K, V> r = RDCSS_READ_ROOT();
169 final boolean success = r.rec_insert(k, v, hc, 0, null, this);
170 Verify.verify(success, "Concurrent modification during serialization of map %s", this);
173 private Optional<V> insertifhc(final K k, final int hc, final V v, final Object cond) {
176 // Keep looping as long as we do not get a reply
177 res = RDCSS_READ_ROOT().rec_insertif(k, v, hc, cond, 0, null, this);
178 } while (res == null);
183 private V lookuphc(final K k, final int hc) {
186 // Keep looping as long as RESTART is being indicated
187 res = RDCSS_READ_ROOT().rec_lookup(k, hc, 0, null, this);
188 } while (INode.RESTART.equals(res));
193 private Optional<V> removehc(final K k, final V v, final int hc) {
196 // Keep looping as long as we do not get a reply
197 res = RDCSS_READ_ROOT().rec_remove(k, v, hc, 0, null, this);
198 } while (res == null);
204 * Ensure this instance is read-write, throw UnsupportedOperationException
205 * otherwise. Used by Map-type methods for quick check.
207 private void ensureReadWrite() {
209 throw new UnsupportedOperationException("Attempted to modify a read-only view");
213 boolean isReadOnly() {
220 * Returns a snapshot of this TrieMap. This operation is lock-free and
223 * The snapshot is lazily updated - the first time some branch in the
224 * snapshot or this TrieMap are accessed, they are rewritten. This means
225 * that the work of rebuilding both the snapshot and this TrieMap is
226 * distributed across all the threads doing updates or accesses subsequent
227 * to the snapshot creation.
229 public TrieMap<K, V> snapshot() {
231 final INode<K, V> r = RDCSS_READ_ROOT();
232 final MainNode<K, V> expmain = r.gcasRead(this);
233 if (RDCSS_ROOT(r, expmain, r.copyToGen(new Gen(), this))) {
234 return new TrieMap<> (r.copyToGen(new Gen(), this), equiv, readOnly);
237 // Tail recursion: return snapshot();
242 * Returns a read-only snapshot of this TrieMap. This operation is lock-free
245 * The snapshot is lazily updated - the first time some branch of this
246 * TrieMap are accessed, it is rewritten. The work of creating the snapshot
247 * is thus distributed across subsequent updates and accesses on this
248 * TrieMap by all threads. Note that the snapshot itself is never rewritten
249 * unlike when calling the `snapshot` method, but the obtained snapshot
250 * cannot be modified.
252 * This method is used by other methods such as `size` and `iterator`.
254 public TrieMap<K, V> readOnlySnapshot() {
255 // Is it a snapshot of a read-only snapshot?
261 final INode<K, V> r = RDCSS_READ_ROOT();
262 final MainNode<K, V> expmain = r.gcasRead(this);
263 if (RDCSS_ROOT(r, expmain, r.copyToGen (new Gen(), this))) {
264 return new TrieMap<>(r, equiv, true);
267 // Tail recursion: return readOnlySnapshot();
272 public void clear() {
275 final INode<K, V> r = RDCSS_READ_ROOT();
276 success = RDCSS_ROOT(r, r.gcasRead(this), newRootNode());
280 int computeHash(final K k) {
281 return equiv.hash(k);
284 boolean equal(final K k1, final K k2) {
285 return equiv.equivalent(k1, k2);
289 public V get(final Object key) {
290 final K k = (K) checkNotNull(key);
291 return lookuphc(k, computeHash(k));
295 public V put(final K key, final V value) {
297 final K k = checkNotNull(key);
298 return insertifhc(k, computeHash(k), checkNotNull(value), null).orElse(null);
301 void add(final K key, final V value) {
302 final K k = checkNotNull(key);
303 inserthc(k, computeHash(k), checkNotNull(value));
307 public V remove(final Object key) {
309 final K k = (K) checkNotNull(key);
310 return removehc(k, (V) null, computeHash(k)).orElse(null);
314 public V putIfAbsent(final K key, final V value) {
316 final K k = checkNotNull(key);
317 return insertifhc(k, computeHash(k), checkNotNull(value), INode.KEY_ABSENT).orElse(null);
321 public boolean remove(final Object key, final Object v) {
323 final K k = (K) checkNotNull(key);
324 return removehc(k, (V) checkNotNull(v), computeHash(k)).isPresent();
328 public boolean replace(final K key, final V oldValue, final V newValue) {
330 final K k = checkNotNull(key);
331 return insertifhc(k, computeHash(k), checkNotNull(newValue), checkNotNull(oldValue)).isPresent();
335 public V replace(final K key, final V value) {
337 final K k = checkNotNull(key);
338 return insertifhc (k, computeHash(k), checkNotNull(value), INode.KEY_PRESENT).orElse(null);
342 * Return an iterator over a TrieMap.
344 * If this is a read-only snapshot, it would return a read-only iterator.
346 * If it is the original TrieMap or a non-readonly snapshot, it would return
347 * an iterator that would allow for updates.
351 Iterator<Entry<K, V>> iterator() {
352 return readOnly ? new TrieMapReadOnlyIterator<>(0, this) : new TrieMapIterator<>(0, this);
356 * Return an iterator over a TrieMap.
357 * This is a read-only iterator.
361 Iterator<Entry<K, V>> readOnlyIterator() {
362 return new TrieMapReadOnlyIterator<>(0, readOnly ? this : readOnlySnapshot());
365 private int cachedSize() {
366 INode<K, V> r = RDCSS_READ_ROOT ();
367 return r.cachedSize (this);
372 return readOnly ? cachedSize() : readOnlySnapshot().size();
376 public boolean containsKey(final Object key) {
377 return get(key) != null;
381 public Set<Entry<K, V>> entrySet() {
385 private Object writeReplace() throws ObjectStreamException {
386 return new ExternalForm(this);
389 private static final class RDCSS_Descriptor<K, V> {
391 MainNode<K, V> expectedmain;
393 volatile boolean committed = false;
395 RDCSS_Descriptor (final INode<K, V> old, final MainNode<K, V> expectedmain, final INode<K, V> nv) {
397 this.expectedmain = expectedmain;
403 * This iterator is a read-only one and does not allow for any update
404 * operations on the underlying data structure.
409 private static final class TrieMapReadOnlyIterator<K, V> extends TrieMapIterator<K, V> {
410 TrieMapReadOnlyIterator (final int level, final TrieMap<K, V> ct, final boolean mustInit) {
411 super (level, ct, mustInit);
414 TrieMapReadOnlyIterator (final int level, final TrieMap<K, V> ct) {
415 this (level, ct, true);
419 assert (ct.isReadOnly ());
424 public void remove () {
425 throw new UnsupportedOperationException ("Operation not supported for read-only iterators");
429 Entry<K, V> nextEntry(final Entry<K, V> rr) {
430 // Return non-updatable entry
435 private static class TrieMapIterator<K, V> implements Iterator<Entry<K, V>> {
437 protected TrieMap<K, V> ct;
438 private final boolean mustInit;
439 private final BasicNode[][] stack = new BasicNode[7][];
440 private final int[] stackpos = new int[7];
441 private int depth = -1;
442 private Iterator<Entry<K, V>> subiter = null;
443 private KVNode<K, V> current = null;
444 private Entry<K, V> lastReturned = null;
446 TrieMapIterator (final int level, final TrieMap<K, V> ct, final boolean mustInit) {
449 this.mustInit = mustInit;
455 TrieMapIterator (final int level, final TrieMap<K, V> ct) {
456 this (level, ct, true);
461 public boolean hasNext() {
462 return (current != null) || (subiter != null);
466 public Entry<K, V> next() {
468 throw new NoSuchElementException();
471 Entry<K, V> r = null;
472 if (subiter != null) {
476 r = current.kvPair ();
482 final Entry<K, V> rr = r;
483 return nextEntry(rr);
488 Entry<K, V> nextEntry(final Entry<K, V> rr) {
489 return new Entry<K, V>() {
490 private V updated = null;
498 public V getValue () {
499 return (updated == null) ? rr.getValue (): updated;
503 public V setValue (final V value) {
505 return ct.replace (getKey (), value);
510 private void readin (final INode<K, V> in) {
511 MainNode<K, V> m = in.gcasRead (ct);
512 if (m instanceof CNode) {
513 CNode<K, V> cn = (CNode<K, V>) m;
515 stack [depth] = cn.array;
516 stackpos [depth] = -1;
518 } else if (m instanceof TNode) {
519 current = (TNode<K, V>) m;
520 } else if (m instanceof LNode) {
521 subiter = ((LNode<K, V>) m).iterator();
523 } else if (m == null) {
529 private void checkSubiter () {
530 if (!subiter.hasNext ()) {
538 // assert (ct.isReadOnly ());
539 INode<K, V> r = ct.RDCSS_READ_ROOT ();
545 int npos = stackpos [depth] + 1;
546 if (npos < stack [depth].length) {
547 stackpos [depth] = npos;
548 BasicNode elem = stack [depth] [npos];
549 if (elem instanceof SNode) {
550 current = (SNode<K, V>) elem;
551 } else if (elem instanceof INode) {
552 readin ((INode<K, V>) elem);
563 protected TrieMapIterator<K, V> newIterator (final int _lev, final TrieMap<K, V> _ct, final boolean _mustInit) {
564 return new TrieMapIterator<> (_lev, _ct, _mustInit);
567 protected void dupTo (final TrieMapIterator<K, V> it) {
568 it.level = this.level;
570 it.depth = this.depth;
571 it.current = this.current;
573 // these need a deep copy
574 System.arraycopy (this.stack, 0, it.stack, 0, 7);
575 System.arraycopy (this.stackpos, 0, it.stackpos, 0, 7);
577 // this one needs to be evaluated
578 if (this.subiter == null) {
581 List<Entry<K, V>> lst = toList (this.subiter);
582 this.subiter = lst.iterator ();
583 it.subiter = lst.iterator ();
587 // /** Returns a sequence of iterators over subsets of this iterator.
588 // * It's used to ease the implementation of splitters for a parallel
589 // version of the TrieMap.
591 // protected def subdivide(): Seq[Iterator[(K, V)]] = if (subiter ne
593 // // the case where an LNode is being iterated
599 // } else if (depth == -1) {
604 // while (d <= depth) {
605 // val rem = stack(d).length - 1 - stackpos(d)
607 // val (arr1, arr2) = stack(d).drop(stackpos(d) + 1).splitAt(rem / 2)
610 // val it = newIterator(level + 1, ct, false)
611 // it.stack(0) = arr2
612 // it.stackpos(0) = -1
614 // it.advance() // <-- fix it
616 // return Seq(this, it)
624 private List<Entry<K, V>> toList (final Iterator<Entry<K, V>> it) {
625 ArrayList<Entry<K, V>> list = new ArrayList<> ();
626 while (it.hasNext ()) {
627 list.add (it.next());
633 System.out.println ("ctrie iterator");
634 System.out.println (Arrays.toString (stackpos));
635 System.out.println ("depth: " + depth);
636 System.out.println ("curr.: " + current);
637 // System.out.println(stack.mkString("\n"));
641 public void remove() {
642 checkState(lastReturned != null);
643 ct.remove(lastReturned.getKey());
649 * Support for EntrySet operations required by the Map interface
651 private final class EntrySet extends AbstractSet<Entry<K, V>> {
653 public Iterator<Entry<K, V>> iterator() {
654 return TrieMap.this.iterator ();
658 public final boolean contains(final Object o) {
659 if (!(o instanceof Entry)) {
663 final Entry<?, ?> e = (Entry<?, ?>) o;
664 if (e.getKey() == null) {
667 final V v = get(e.getKey());
668 return v != null && v.equals(e.getValue());
672 public final boolean remove(final Object o) {
673 if (!(o instanceof Entry)) {
676 final Entry<?, ?> e = (Entry<K, V>) o;
677 final Object key = e.getKey();
681 final Object value = e.getValue();
686 return TrieMap.this.remove(key, value);
690 public final int size () {
691 return Iterators.size(iterator());
695 public final void clear () {
696 TrieMap.this.clear ();