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 java.io.IOException;
19 import java.io.ObjectInputStream;
20 import java.io.ObjectOutputStream;
21 import java.io.Serializable;
22 import java.lang.reflect.Field;
23 import java.util.AbstractMap;
24 import java.util.AbstractSet;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Iterator;
28 import java.util.List;
30 import java.util.NoSuchElementException;
32 import java.util.concurrent.ConcurrentMap;
33 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
36 * This is a port of Scala's TrieMap class from the Scala Collections library.
38 * @author Roman Levenstein <romixlev@gmail.com>
43 @SuppressWarnings({"unchecked", "rawtypes", "unused"})
44 public class TrieMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,V>, Serializable {
45 private static final AtomicReferenceFieldUpdater<TrieMap, Object> ROOT_UPDATER = AtomicReferenceFieldUpdater.newUpdater(TrieMap.class, Object.class, "root");
46 private static final long serialVersionUID = 1L;
47 private static final Field READONLY_FIELD;
48 private static final TrieMap EMPTY = new TrieMap();
53 f = TrieMap.class.getDeclaredField("readOnly");
54 } catch (NoSuchFieldException e) {
55 throw new ExceptionInInitializerError(e);
56 } catch (SecurityException e) {
57 throw new ExceptionInInitializerError(e);
59 f.setAccessible(true);
66 private transient final EntrySet entrySet = new EntrySet ();
68 public static <K,V> TrieMap<K,V> empty () {
72 // static class MangledHashing<K> extends Hashing<K> {
74 // return util.hashing.byteswap32(k);
78 private static class RDCSS_Descriptor<K, V> {
80 MainNode<K, V> expectedmain;
82 volatile boolean committed = false;
84 public RDCSS_Descriptor (final INode<K, V> old, final MainNode<K, V> expectedmain, final INode<K, V> nv) {
86 this.expectedmain = expectedmain;
91 private final Hashing<K> hashingobj;
92 private final Equiv<K> equalityobj;
94 Hashing<K> hashing () {
98 Equiv<K> equality () {
102 private transient volatile Object root;
103 private final transient boolean readOnly;
105 TrieMap (final Hashing<K> hashf, final Equiv<K> ef, final boolean readOnly) {
106 this.hashingobj = hashf;
107 this.equalityobj = ef;
108 this.readOnly = readOnly;
111 TrieMap (final Object r, final Hashing<K> hashf, final Equiv<K> ef, final boolean readOnly) {
112 this(hashf, ef, readOnly);
116 public TrieMap (final Hashing<K> hashf, final Equiv<K> ef) {
117 this(newRootNode(), hashf, ef, false);
121 this (new Hashing.Default<K>(), Equiv.universal);
124 /* internal methods */
126 // private void writeObject(java.io.ObjectOutputStream out) {
127 // out.writeObject(hashf);
128 // out.writeObject(ef);
130 // Iterator it = iterator();
131 // while (it.hasNext) {
132 // val (k, v) = it.next();
133 // out.writeObject(k);
134 // out.writeObject(v);
136 // out.writeObject(TrieMapSerializationEnd);
139 // private TrieMap readObject(java.io.ObjectInputStream in) {
140 // root = INode.newRootNode();
141 // rootupdater = AtomicReferenceFieldUpdater.newUpdater(TrieMap.class,
142 // Object.class, "root");
144 // hashingobj = in.readObject();
145 // equalityobj = in.readObject();
147 // Object obj = null;
149 // obj = in.readObject();
150 // if (obj != TrieMapSerializationEnd) {
152 // V = (V)in.readObject();
155 // } while (obj != TrieMapSerializationEnd);
158 private static <K,V> INode<K,V> newRootNode() {
159 final Gen gen = new Gen();
160 return new INode<>(gen, new CNode<>(gen));
163 final boolean CAS_ROOT (final Object ov, final Object nv) {
165 throw new IllegalStateException("Attempted to modify a read-only snapshot");
167 return ROOT_UPDATER.compareAndSet (this, ov, nv);
170 // FIXME: abort = false by default
171 final INode<K, V> readRoot (final boolean abort) {
172 return RDCSS_READ_ROOT (abort);
175 final INode<K, V> readRoot () {
176 return RDCSS_READ_ROOT (false);
179 final INode<K, V> RDCSS_READ_ROOT () {
180 return RDCSS_READ_ROOT (false);
183 final INode<K, V> RDCSS_READ_ROOT (final boolean abort) {
184 Object r = /* READ */root;
185 if (r instanceof INode) {
186 return (INode<K, V>) r;
187 } else if (r instanceof RDCSS_Descriptor) {
188 return RDCSS_Complete (abort);
190 throw new RuntimeException ("Should not happen");
193 private final INode<K, V> RDCSS_Complete (final boolean abort) {
195 Object v = /* READ */root;
196 if (v instanceof INode) {
197 return (INode<K, V>) v;
198 } else if (v instanceof RDCSS_Descriptor) {
199 RDCSS_Descriptor<K, V> desc = (RDCSS_Descriptor<K, V>) v;
200 INode<K, V> ov = desc.old;
201 MainNode<K, V> exp = desc.expectedmain;
202 INode<K, V> nv = desc.nv;
205 if (CAS_ROOT (desc, ov)) {
208 // return RDCSS_Complete (abort);
213 MainNode<K, V> oldmain = ov.gcasRead (this);
214 if (oldmain == exp) {
215 if (CAS_ROOT (desc, nv)) {
216 desc.committed = true;
219 // return RDCSS_Complete (abort);
224 if (CAS_ROOT (desc, ov)) {
227 // return RDCSS_Complete (abort);
236 throw new RuntimeException ("Should not happen");
240 private boolean RDCSS_ROOT (final INode<K, V> ov, final MainNode<K, V> expectedmain, final INode<K, V> nv) {
241 RDCSS_Descriptor<K, V> desc = new RDCSS_Descriptor<> (ov, expectedmain, nv);
242 if (CAS_ROOT (ov, desc)) {
243 RDCSS_Complete (false);
244 return /* READ */desc.committed;
250 private void inserthc (final K k, final int hc, final V v) {
252 INode<K, V> r = RDCSS_READ_ROOT ();
253 if (!r.rec_insert (k, v, hc, 0, null, r.gen, this)) {
254 // inserthc (k, hc, v);
262 private Option<V> insertifhc (final K k, final int hc, final V v, final Object cond) {
264 INode<K, V> r = RDCSS_READ_ROOT ();
266 Option<V> ret = r.rec_insertif (k, v, hc, cond, 0, null, r.gen, this);
268 // return insertifhc (k, hc, v, cond);
277 private Object lookuphc(final K k, final int hc) {
279 final INode<K, V> r = RDCSS_READ_ROOT ();
280 final Object res = r.rec_lookup(k, hc, 0, null, r.gen, this);
281 if (!INode.RESTART.equals(res)) {
285 // Tail recursion: lookuphc(k, hc)
289 private Option<V> removehc (final K k, final V v, final int hc) {
291 INode<K, V> r = RDCSS_READ_ROOT ();
292 Option<V> res = r.rec_remove (k, v, hc, 0, null, r.gen, this);
296 // return removehc (k, v, hc);
304 * Ensure this instance is read-write, throw UnsupportedOperationException
305 * otherwise. Used by Map-type methods for quick check.
307 private void ensureReadWrite() {
309 throw new UnsupportedOperationException("Attempted to modify a read-only view");
313 public String string () {
314 // RDCSS_READ_ROOT().string(0);
320 // public Seq<V> seq() {
324 // override def par = new ParTrieMap(this)
326 // static TrieMap empty() {
327 // return new TrieMap();
330 final boolean isReadOnly () {
334 final boolean nonReadOnly () {
339 * Returns a snapshot of this TrieMap. This operation is lock-free and
342 * The snapshot is lazily updated - the first time some branch in the
343 * snapshot or this TrieMap are accessed, they are rewritten. This means
344 * that the work of rebuilding both the snapshot and this TrieMap is
345 * distributed across all the threads doing updates or accesses subsequent
346 * to the snapshot creation.
349 final public TrieMap<K, V> snapshot () {
351 INode<K, V> r = RDCSS_READ_ROOT ();
352 final MainNode<K, V> expmain = r.gcasRead (this);
353 if (RDCSS_ROOT (r, expmain, r.copyToGen (new Gen (), this))) {
354 return new TrieMap<> (r.copyToGen (new Gen (), this), hashing (), equality (), readOnly);
356 // return snapshot ();
364 * Returns a read-only snapshot of this TrieMap. This operation is lock-free
367 * The snapshot is lazily updated - the first time some branch of this
368 * TrieMap are accessed, it is rewritten. The work of creating the snapshot
369 * is thus distributed across subsequent updates and accesses on this
370 * TrieMap by all threads. Note that the snapshot itself is never rewritten
371 * unlike when calling the `snapshot` method, but the obtained snapshot
372 * cannot be modified.
374 * This method is used by other methods such as `size` and `iterator`.
376 final public TrieMap<K, V> readOnlySnapshot () {
377 // Is it a snapshot of a read-only snapshot?
378 if(!nonReadOnly ()) {
383 INode<K, V> r = RDCSS_READ_ROOT ();
384 MainNode<K, V> expmain = r.gcasRead (this);
385 if (RDCSS_ROOT (r, expmain, r.copyToGen (new Gen (), this))) {
386 return new TrieMap<> (r, hashing (), equality (), true);
388 // return readOnlySnapshot ();
395 final public void clear () {
397 INode<K, V> r = RDCSS_READ_ROOT ();
398 if (!RDCSS_ROOT(r, r.gcasRead(this), newRootNode())) {
407 int computeHash (final K k) {
408 return hashingobj.hash (k);
411 final V lookup (final K k) {
412 int hc = computeHash (k);
413 // return (V) lookuphc (k, hc);
414 Object o = lookuphc (k, hc);
415 if(o instanceof Some) {
416 return ((Some<V>)o).get ();
417 } else if(o instanceof None) {
424 final V apply (final K k) {
425 int hc = computeHash (k);
426 Object res = lookuphc (k, hc);
428 throw new NoSuchElementException ();
434 // final public Option<V> get (K k) {
435 // int hc = computeHash (k);
436 // return Option.makeOption ((V)lookuphc (k, hc));
440 final public V get (final Object k) {
444 final public Option<V> putOpt(final Object key, final Object value) {
445 int hc = computeHash ((K)key);
446 return insertifhc ((K)key, hc, (V)value, null);
450 final public V put (final Object key, final Object value) {
452 int hc = computeHash ((K)key);
453 Option<V> ov = insertifhc ((K)key, hc, (V)value, null);
454 if(ov instanceof Some) {
455 Some<V> sv = (Some<V>)ov;
462 final public void update (final K k, final V v) {
463 int hc = computeHash (k);
467 final public TrieMap<K, V> add (final K k, final V v) {
472 final Option<V> removeOpt (final K k) {
473 int hc = computeHash (k);
474 return removehc (k, (V) null, hc);
478 final public V remove (final Object k) {
480 int hc = computeHash ((K)k);
481 Option<V> ov = removehc ((K)k, (V) null, hc);
482 if(ov instanceof Some) {
483 Some<V> sv = (Some<V>)ov;
490 // final public TrieMap<K, V> remove (Object k) {
495 final public Option<V> putIfAbsentOpt (final K k, final V v) {
496 int hc = computeHash (k);
497 return insertifhc (k, hc, v, INode.KEY_ABSENT);
501 final public V putIfAbsent (final Object k, final Object v) {
503 int hc = computeHash ((K)k);
504 Option<V> ov = insertifhc ((K)k, hc, (V)v, INode.KEY_ABSENT);
505 if(ov instanceof Some) {
506 Some<V> sv = (Some<V>)ov;
514 public boolean remove (final Object k, final Object v) {
516 int hc = computeHash ((K)k);
517 return removehc ((K)k, (V)v, hc).nonEmpty ();
521 public boolean replace (final K k, final V oldvalue, final V newvalue) {
523 int hc = computeHash (k);
524 return insertifhc (k, hc, newvalue, oldvalue).nonEmpty ();
527 public Option<V> replaceOpt (final K k, final V v) {
528 int hc = computeHash (k);
529 return insertifhc (k, hc, v, INode.KEY_PRESENT);
533 public V replace (final Object k, final Object v) {
535 int hc = computeHash ((K)k);
536 Option<V> ov = insertifhc ((K)k, hc, (V)v, INode.KEY_PRESENT);
537 if(ov instanceof Some) {
538 Some<V> sv = (Some<V>)ov;
546 * Return an iterator over a TrieMap.
548 * If this is a read-only snapshot, it would return a read-only iterator.
550 * If it is the original TrieMap or a non-readonly snapshot, it would return
551 * an iterator that would allow for updates.
555 public Iterator<Map.Entry<K, V>> iterator () {
556 if (!nonReadOnly ()) {
557 return readOnlySnapshot ().readOnlyIterator ();
559 return new TrieMapIterator<> (0, this);
564 * Return an iterator over a TrieMap.
565 * This is a read-only iterator.
569 public Iterator<Map.Entry<K, V>> readOnlyIterator () {
570 if (nonReadOnly ()) {
571 return readOnlySnapshot ().readOnlyIterator ();
573 return new TrieMapReadOnlyIterator<> (0, this);
577 private int cachedSize () {
578 INode<K, V> r = RDCSS_READ_ROOT ();
579 return r.cachedSize (this);
584 if (nonReadOnly ()) {
585 return readOnlySnapshot ().size ();
587 return cachedSize ();
591 String stringPrefix () {
596 * This iterator is a read-only one and does not allow for any update
597 * operations on the underlying data structure.
602 private static class TrieMapReadOnlyIterator<K, V> extends TrieMapIterator<K, V> {
603 TrieMapReadOnlyIterator (final int level, final TrieMap<K, V> ct, final boolean mustInit) {
604 super (level, ct, mustInit);
607 TrieMapReadOnlyIterator (final int level, final TrieMap<K, V> ct) {
608 this (level, ct, true);
612 assert (ct.isReadOnly ());
617 public void remove () {
618 throw new UnsupportedOperationException ("Operation not supported for read-only iterators");
622 Map.Entry<K, V> nextEntry(final Map.Entry<K, V> rr) {
623 // Return non-updatable entry
628 private static class TrieMapIterator<K, V> implements java.util.Iterator<Map.Entry<K, V>> {
630 protected TrieMap<K, V> ct;
631 private final boolean mustInit;
632 private final BasicNode[][] stack = new BasicNode[7][];
633 private final int[] stackpos = new int[7];
634 private int depth = -1;
635 private Iterator<Map.Entry<K, V>> subiter = null;
636 private KVNode<K, V> current = null;
637 private Map.Entry<K, V> lastReturned = null;
639 TrieMapIterator (final int level, final TrieMap<K, V> ct, final boolean mustInit) {
642 this.mustInit = mustInit;
648 TrieMapIterator (final int level, final TrieMap<K, V> ct) {
649 this (level, ct, true);
654 public boolean hasNext () {
655 return (current != null) || (subiter != null);
659 public Map.Entry<K, V> next () {
661 Map.Entry<K, V> r = null;
662 if (subiter != null) {
666 r = current.kvPair ();
671 if(r instanceof Map.Entry) {
672 final Map.Entry<K, V> rr = r;
673 return nextEntry(rr);
677 // return Iterator.empty ().next ();
682 Map.Entry<K, V> nextEntry(final Map.Entry<K, V> rr) {
683 return new Map.Entry<K, V>() {
684 private V updated = null;
692 public V getValue () {
693 return (updated == null)?rr.getValue (): updated;
697 public V setValue (final V value) {
699 return ct.replace (getKey (), value);
704 private void readin (final INode<K, V> in) {
705 MainNode<K, V> m = in.gcasRead (ct);
706 if (m instanceof CNode) {
707 CNode<K, V> cn = (CNode<K, V>) m;
709 stack [depth] = cn.array;
710 stackpos [depth] = -1;
712 } else if (m instanceof TNode) {
713 current = (TNode<K, V>) m;
714 } else if (m instanceof LNode) {
715 subiter = ((LNode<K, V>) m).iterator();
717 } else if (m == null) {
723 private void checkSubiter () {
724 if (!subiter.hasNext ()) {
732 // assert (ct.isReadOnly ());
733 INode<K, V> r = ct.RDCSS_READ_ROOT ();
739 int npos = stackpos [depth] + 1;
740 if (npos < stack [depth].length) {
741 stackpos [depth] = npos;
742 BasicNode elem = stack [depth] [npos];
743 if (elem instanceof SNode) {
744 current = (SNode<K, V>) elem;
745 } else if (elem instanceof INode) {
746 readin ((INode<K, V>) elem);
757 protected TrieMapIterator<K, V> newIterator (final int _lev, final TrieMap<K, V> _ct, final boolean _mustInit) {
758 return new TrieMapIterator<> (_lev, _ct, _mustInit);
761 protected void dupTo (final TrieMapIterator<K, V> it) {
762 it.level = this.level;
764 it.depth = this.depth;
765 it.current = this.current;
767 // these need a deep copy
768 System.arraycopy (this.stack, 0, it.stack, 0, 7);
769 System.arraycopy (this.stackpos, 0, it.stackpos, 0, 7);
771 // this one needs to be evaluated
772 if (this.subiter == null) {
775 List<Map.Entry<K, V>> lst = toList (this.subiter);
776 this.subiter = lst.iterator ();
777 it.subiter = lst.iterator ();
781 // /** Returns a sequence of iterators over subsets of this iterator.
782 // * It's used to ease the implementation of splitters for a parallel
783 // version of the TrieMap.
785 // protected def subdivide(): Seq[Iterator[(K, V)]] = if (subiter ne
787 // // the case where an LNode is being iterated
793 // } else if (depth == -1) {
798 // while (d <= depth) {
799 // val rem = stack(d).length - 1 - stackpos(d)
801 // val (arr1, arr2) = stack(d).drop(stackpos(d) + 1).splitAt(rem / 2)
804 // val it = newIterator(level + 1, ct, false)
805 // it.stack(0) = arr2
806 // it.stackpos(0) = -1
808 // it.advance() // <-- fix it
810 // return Seq(this, it)
818 private List<Entry<K, V>> toList (final Iterator<Entry<K, V>> it) {
819 ArrayList<Entry<K, V>> list = new ArrayList<> ();
820 while (it.hasNext ()) {
821 list.add (it.next ());
827 System.out.println ("ctrie iterator");
828 System.out.println (Arrays.toString (stackpos));
829 System.out.println ("depth: " + depth);
830 System.out.println ("curr.: " + current);
831 // System.out.println(stack.mkString("\n"));
835 public void remove () {
836 if (lastReturned != null) {
837 ct.remove (lastReturned.getKey ());
840 throw new IllegalStateException();
846 /** Only used for ctrie serialization. */
847 // @SerialVersionUID(0L - 7237891413820527142L)
848 private static long TrieMapSerializationEnd = 0L - 7237891413820527142L;
852 public boolean containsKey (final Object key) {
853 return lookup ((K) key) != null;
858 public Set<Map.Entry<K, V>> entrySet () {
863 * Support for EntrySet operations required by the Map interface
866 final class EntrySet extends AbstractSet<Map.Entry<K, V>> {
869 public Iterator<Map.Entry<K, V>> iterator () {
870 return TrieMap.this.iterator ();
874 public final boolean contains (final Object o) {
875 if (!(o instanceof Map.Entry)) {
878 final Map.Entry<K, V> e = (Map.Entry<K, V>) o;
879 final K k = e.getKey ();
880 final V v = lookup (k);
885 public final boolean remove (final Object o) {
886 if (!(o instanceof Map.Entry)) {
889 final Map.Entry<K, V> e = (Map.Entry<K, V>) o;
890 final K k = e.getKey ();
891 return null != TrieMap.this.remove (k);
895 public final int size () {
897 for (final Iterator<?> i = iterator (); i.hasNext (); i.next ()) {
904 public final void clear () {
905 TrieMap.this.clear ();
909 private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
910 inputStream.defaultReadObject();
911 this.root = newRootNode();
913 final boolean ro = inputStream.readBoolean();
914 final int size = inputStream.readInt();
915 for (int i = 0; i < size; ++i) {
916 final K key = (K)inputStream.readObject();
917 final V value = (V)inputStream.readObject();
921 // Propagate the read-only bit
923 READONLY_FIELD.setBoolean(this, ro);
924 } catch (IllegalAccessException e) {
925 throw new IOException("Failed to set read-only flag", e);
929 private void writeObject(final ObjectOutputStream outputStream) throws IOException {
930 outputStream.defaultWriteObject();
932 final Map<K, V> ro = readOnlySnapshot();
933 outputStream.writeBoolean(isReadOnly());
934 outputStream.writeInt(ro.size());
936 for (Entry<K, V> e : ro.entrySet()) {
937 outputStream.writeObject(e.getKey());
938 outputStream.writeObject(e.getValue());