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);
159 private static <K,V> INode<K,V> newRootNode() {
160 final Gen gen = new Gen();
161 final CNode<K, V> cn = new CNode<>(0, new BasicNode[] {}, gen);
162 return new INode<>(gen, cn);
165 final boolean CAS_ROOT (final Object ov, final Object nv) {
167 throw new IllegalStateException("Attempted to modify a read-only snapshot");
169 return ROOT_UPDATER.compareAndSet (this, ov, nv);
172 // FIXME: abort = false by default
173 final INode<K, V> readRoot (final boolean abort) {
174 return RDCSS_READ_ROOT (abort);
177 final INode<K, V> readRoot () {
178 return RDCSS_READ_ROOT (false);
181 final INode<K, V> RDCSS_READ_ROOT () {
182 return RDCSS_READ_ROOT (false);
185 final INode<K, V> RDCSS_READ_ROOT (final boolean abort) {
186 Object r = /* READ */root;
187 if (r instanceof INode) {
188 return (INode<K, V>) r;
189 } else if (r instanceof RDCSS_Descriptor) {
190 return RDCSS_Complete (abort);
192 throw new RuntimeException ("Should not happen");
195 private final INode<K, V> RDCSS_Complete (final boolean abort) {
197 Object v = /* READ */root;
198 if (v instanceof INode) {
199 return (INode<K, V>) v;
200 } else if (v instanceof RDCSS_Descriptor) {
201 RDCSS_Descriptor<K, V> desc = (RDCSS_Descriptor<K, V>) v;
202 INode<K, V> ov = desc.old;
203 MainNode<K, V> exp = desc.expectedmain;
204 INode<K, V> nv = desc.nv;
207 if (CAS_ROOT (desc, ov)) {
210 // return RDCSS_Complete (abort);
215 MainNode<K, V> oldmain = ov.gcasRead (this);
216 if (oldmain == exp) {
217 if (CAS_ROOT (desc, nv)) {
218 desc.committed = true;
221 // return RDCSS_Complete (abort);
226 if (CAS_ROOT (desc, ov)) {
229 // return RDCSS_Complete (abort);
238 throw new RuntimeException ("Should not happen");
242 private boolean RDCSS_ROOT (final INode<K, V> ov, final MainNode<K, V> expectedmain, final INode<K, V> nv) {
243 RDCSS_Descriptor<K, V> desc = new RDCSS_Descriptor<> (ov, expectedmain, nv);
244 if (CAS_ROOT (ov, desc)) {
245 RDCSS_Complete (false);
246 return /* READ */desc.committed;
252 private void inserthc (final K k, final int hc, final V v) {
254 INode<K, V> r = RDCSS_READ_ROOT ();
255 if (!r.rec_insert (k, v, hc, 0, null, r.gen, this)) {
256 // inserthc (k, hc, v);
264 private Option<V> insertifhc (final K k, final int hc, final V v, final Object cond) {
266 INode<K, V> r = RDCSS_READ_ROOT ();
268 Option<V> ret = r.rec_insertif (k, v, hc, cond, 0, null, r.gen, this);
270 // return insertifhc (k, hc, v, cond);
279 private Object lookuphc(final K k, final int hc) {
281 final INode<K, V> r = RDCSS_READ_ROOT ();
282 final Object res = r.rec_lookup(k, hc, 0, null, r.gen, this);
283 if (!INode.RESTART.equals(res)) {
287 // Tail recursion: lookuphc(k, hc)
291 private Option<V> removehc (final K k, final V v, final int hc) {
293 INode<K, V> r = RDCSS_READ_ROOT ();
294 Option<V> res = r.rec_remove (k, v, hc, 0, null, r.gen, this);
298 // return removehc (k, v, hc);
306 * Ensure this instance is read-write, throw UnsupportedOperationException
307 * otherwise. Used by Map-type methods for quick check.
309 private void ensureReadWrite() {
311 throw new UnsupportedOperationException("Attempted to modify a read-only view");
315 public String string () {
316 // RDCSS_READ_ROOT().string(0);
322 // public Seq<V> seq() {
326 // override def par = new ParTrieMap(this)
328 // static TrieMap empty() {
329 // return new TrieMap();
332 final boolean isReadOnly () {
336 final boolean nonReadOnly () {
341 * Returns a snapshot of this TrieMap. This operation is lock-free and
344 * The snapshot is lazily updated - the first time some branch in the
345 * snapshot or this TrieMap are accessed, they are rewritten. This means
346 * that the work of rebuilding both the snapshot and this TrieMap is
347 * distributed across all the threads doing updates or accesses subsequent
348 * to the snapshot creation.
351 final public TrieMap<K, V> snapshot () {
353 INode<K, V> r = RDCSS_READ_ROOT ();
354 final MainNode<K, V> expmain = r.gcasRead (this);
355 if (RDCSS_ROOT (r, expmain, r.copyToGen (new Gen (), this))) {
356 return new TrieMap<> (r.copyToGen (new Gen (), this), hashing (), equality (), readOnly);
358 // return snapshot ();
366 * Returns a read-only snapshot of this TrieMap. This operation is lock-free
369 * The snapshot is lazily updated - the first time some branch of this
370 * TrieMap are accessed, it is rewritten. The work of creating the snapshot
371 * is thus distributed across subsequent updates and accesses on this
372 * TrieMap by all threads. Note that the snapshot itself is never rewritten
373 * unlike when calling the `snapshot` method, but the obtained snapshot
374 * cannot be modified.
376 * This method is used by other methods such as `size` and `iterator`.
378 final public TrieMap<K, V> readOnlySnapshot () {
379 // Is it a snapshot of a read-only snapshot?
380 if(!nonReadOnly ()) {
385 INode<K, V> r = RDCSS_READ_ROOT ();
386 MainNode<K, V> expmain = r.gcasRead (this);
387 if (RDCSS_ROOT (r, expmain, r.copyToGen (new Gen (), this))) {
388 return new TrieMap<> (r, hashing (), equality (), true);
390 // return readOnlySnapshot ();
397 final public void clear () {
399 INode<K, V> r = RDCSS_READ_ROOT ();
400 if (!RDCSS_ROOT(r, r.gcasRead(this), newRootNode())) {
409 int computeHash (final K k) {
410 return hashingobj.hash (k);
413 final V lookup (final K k) {
414 int hc = computeHash (k);
415 // return (V) lookuphc (k, hc);
416 Object o = lookuphc (k, hc);
417 if(o instanceof Some) {
418 return ((Some<V>)o).get ();
419 } else if(o instanceof None) {
426 final V apply (final K k) {
427 int hc = computeHash (k);
428 Object res = lookuphc (k, hc);
430 throw new NoSuchElementException ();
436 // final public Option<V> get (K k) {
437 // int hc = computeHash (k);
438 // return Option.makeOption ((V)lookuphc (k, hc));
442 final public V get (final Object k) {
446 final public Option<V> putOpt(final Object key, final Object value) {
447 int hc = computeHash ((K)key);
448 return insertifhc ((K)key, hc, (V)value, null);
452 final public V put (final Object key, final Object value) {
454 int hc = computeHash ((K)key);
455 Option<V> ov = insertifhc ((K)key, hc, (V)value, null);
456 if(ov instanceof Some) {
457 Some<V> sv = (Some<V>)ov;
464 final public void update (final K k, final V v) {
465 int hc = computeHash (k);
469 final public TrieMap<K, V> add (final K k, final V v) {
474 final Option<V> removeOpt (final K k) {
475 int hc = computeHash (k);
476 return removehc (k, (V) null, hc);
480 final public V remove (final Object k) {
482 int hc = computeHash ((K)k);
483 Option<V> ov = removehc ((K)k, (V) null, hc);
484 if(ov instanceof Some) {
485 Some<V> sv = (Some<V>)ov;
492 // final public TrieMap<K, V> remove (Object k) {
497 final public Option<V> putIfAbsentOpt (final K k, final V v) {
498 int hc = computeHash (k);
499 return insertifhc (k, hc, v, INode.KEY_ABSENT);
503 final public V putIfAbsent (final Object k, final Object v) {
505 int hc = computeHash ((K)k);
506 Option<V> ov = insertifhc ((K)k, hc, (V)v, INode.KEY_ABSENT);
507 if(ov instanceof Some) {
508 Some<V> sv = (Some<V>)ov;
516 public boolean remove (final Object k, final Object v) {
518 int hc = computeHash ((K)k);
519 return removehc ((K)k, (V)v, hc).nonEmpty ();
523 public boolean replace (final K k, final V oldvalue, final V newvalue) {
525 int hc = computeHash (k);
526 return insertifhc (k, hc, newvalue, oldvalue).nonEmpty ();
529 public Option<V> replaceOpt (final K k, final V v) {
530 int hc = computeHash (k);
531 return insertifhc (k, hc, v, INode.KEY_PRESENT);
535 public V replace (final Object k, final Object v) {
537 int hc = computeHash ((K)k);
538 Option<V> ov = insertifhc ((K)k, hc, (V)v, INode.KEY_PRESENT);
539 if(ov instanceof Some) {
540 Some<V> sv = (Some<V>)ov;
548 * Return an iterator over a TrieMap.
550 * If this is a read-only snapshot, it would return a read-only iterator.
552 * If it is the original TrieMap or a non-readonly snapshot, it would return
553 * an iterator that would allow for updates.
557 public Iterator<Map.Entry<K, V>> iterator () {
558 if (!nonReadOnly ()) {
559 return readOnlySnapshot ().readOnlyIterator ();
561 return new TrieMapIterator<> (0, this);
566 * Return an iterator over a TrieMap.
567 * This is a read-only iterator.
571 public Iterator<Map.Entry<K, V>> readOnlyIterator () {
572 if (nonReadOnly ()) {
573 return readOnlySnapshot ().readOnlyIterator ();
575 return new TrieMapReadOnlyIterator<> (0, this);
579 private int cachedSize () {
580 INode<K, V> r = RDCSS_READ_ROOT ();
581 return r.cachedSize (this);
586 if (nonReadOnly ()) {
587 return readOnlySnapshot ().size ();
589 return cachedSize ();
593 String stringPrefix () {
598 * This iterator is a read-only one and does not allow for any update
599 * operations on the underlying data structure.
604 private static class TrieMapReadOnlyIterator<K, V> extends TrieMapIterator<K, V> {
605 TrieMapReadOnlyIterator (final int level, final TrieMap<K, V> ct, final boolean mustInit) {
606 super (level, ct, mustInit);
609 TrieMapReadOnlyIterator (final int level, final TrieMap<K, V> ct) {
610 this (level, ct, true);
614 assert (ct.isReadOnly ());
619 public void remove () {
620 throw new UnsupportedOperationException ("Operation not supported for read-only iterators");
624 Map.Entry<K, V> nextEntry(final Map.Entry<K, V> rr) {
625 // Return non-updatable entry
630 private static class TrieMapIterator<K, V> implements java.util.Iterator<Map.Entry<K, V>> {
632 protected TrieMap<K, V> ct;
633 private final boolean mustInit;
634 private final BasicNode[][] stack = new BasicNode[7][];
635 private final int[] stackpos = new int[7];
636 private int depth = -1;
637 private Iterator<Map.Entry<K, V>> subiter = null;
638 private KVNode<K, V> current = null;
639 private Map.Entry<K, V> lastReturned = null;
641 TrieMapIterator (final int level, final TrieMap<K, V> ct, final boolean mustInit) {
644 this.mustInit = mustInit;
650 TrieMapIterator (final int level, final TrieMap<K, V> ct) {
651 this (level, ct, true);
656 public boolean hasNext () {
657 return (current != null) || (subiter != null);
661 public Map.Entry<K, V> next () {
663 Map.Entry<K, V> r = null;
664 if (subiter != null) {
668 r = current.kvPair ();
673 if(r instanceof Map.Entry) {
674 final Map.Entry<K, V> rr = r;
675 return nextEntry(rr);
679 // return Iterator.empty ().next ();
684 Map.Entry<K, V> nextEntry(final Map.Entry<K, V> rr) {
685 return new Map.Entry<K, V>() {
686 private V updated = null;
694 public V getValue () {
695 return (updated == null)?rr.getValue (): updated;
699 public V setValue (final V value) {
701 return ct.replace (getKey (), value);
706 private void readin (final INode<K, V> in) {
707 MainNode<K, V> m = in.gcasRead (ct);
708 if (m instanceof CNode) {
709 CNode<K, V> cn = (CNode<K, V>) m;
711 stack [depth] = cn.array;
712 stackpos [depth] = -1;
714 } else if (m instanceof TNode) {
715 current = (TNode<K, V>) m;
716 } else if (m instanceof LNode) {
717 subiter = ((LNode<K, V>) m).iterator();
719 } else if (m == null) {
725 private void checkSubiter () {
726 if (!subiter.hasNext ()) {
734 // assert (ct.isReadOnly ());
735 INode<K, V> r = ct.RDCSS_READ_ROOT ();
741 int npos = stackpos [depth] + 1;
742 if (npos < stack [depth].length) {
743 stackpos [depth] = npos;
744 BasicNode elem = stack [depth] [npos];
745 if (elem instanceof SNode) {
746 current = (SNode<K, V>) elem;
747 } else if (elem instanceof INode) {
748 readin ((INode<K, V>) elem);
759 protected TrieMapIterator<K, V> newIterator (final int _lev, final TrieMap<K, V> _ct, final boolean _mustInit) {
760 return new TrieMapIterator<> (_lev, _ct, _mustInit);
763 protected void dupTo (final TrieMapIterator<K, V> it) {
764 it.level = this.level;
766 it.depth = this.depth;
767 it.current = this.current;
769 // these need a deep copy
770 System.arraycopy (this.stack, 0, it.stack, 0, 7);
771 System.arraycopy (this.stackpos, 0, it.stackpos, 0, 7);
773 // this one needs to be evaluated
774 if (this.subiter == null) {
777 List<Map.Entry<K, V>> lst = toList (this.subiter);
778 this.subiter = lst.iterator ();
779 it.subiter = lst.iterator ();
783 // /** Returns a sequence of iterators over subsets of this iterator.
784 // * It's used to ease the implementation of splitters for a parallel
785 // version of the TrieMap.
787 // protected def subdivide(): Seq[Iterator[(K, V)]] = if (subiter ne
789 // // the case where an LNode is being iterated
795 // } else if (depth == -1) {
800 // while (d <= depth) {
801 // val rem = stack(d).length - 1 - stackpos(d)
803 // val (arr1, arr2) = stack(d).drop(stackpos(d) + 1).splitAt(rem / 2)
806 // val it = newIterator(level + 1, ct, false)
807 // it.stack(0) = arr2
808 // it.stackpos(0) = -1
810 // it.advance() // <-- fix it
812 // return Seq(this, it)
820 private List<Entry<K, V>> toList (final Iterator<Entry<K, V>> it) {
821 ArrayList<Entry<K, V>> list = new ArrayList<> ();
822 while (it.hasNext ()) {
823 list.add (it.next ());
829 System.out.println ("ctrie iterator");
830 System.out.println (Arrays.toString (stackpos));
831 System.out.println ("depth: " + depth);
832 System.out.println ("curr.: " + current);
833 // System.out.println(stack.mkString("\n"));
837 public void remove () {
838 if (lastReturned != null) {
839 ct.remove (lastReturned.getKey ());
842 throw new IllegalStateException();
848 /** Only used for ctrie serialization. */
849 // @SerialVersionUID(0L - 7237891413820527142L)
850 private static long TrieMapSerializationEnd = 0L - 7237891413820527142L;
854 public boolean containsKey (final Object key) {
855 return lookup ((K) key) != null;
860 public Set<Map.Entry<K, V>> entrySet () {
865 * Support for EntrySet operations required by the Map interface
868 final class EntrySet extends AbstractSet<Map.Entry<K, V>> {
871 public Iterator<Map.Entry<K, V>> iterator () {
872 return TrieMap.this.iterator ();
876 public final boolean contains (final Object o) {
877 if (!(o instanceof Map.Entry)) {
880 final Map.Entry<K, V> e = (Map.Entry<K, V>) o;
881 final K k = e.getKey ();
882 final V v = lookup (k);
887 public final boolean remove (final Object o) {
888 if (!(o instanceof Map.Entry)) {
891 final Map.Entry<K, V> e = (Map.Entry<K, V>) o;
892 final K k = e.getKey ();
893 return null != TrieMap.this.remove (k);
897 public final int size () {
899 for (final Iterator<?> i = iterator (); i.hasNext (); i.next ()) {
906 public final void clear () {
907 TrieMap.this.clear ();
911 private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
912 inputStream.defaultReadObject();
913 this.root = newRootNode();
915 final boolean ro = inputStream.readBoolean();
916 final int size = inputStream.readInt();
917 for (int i = 0; i < size; ++i) {
918 final K key = (K)inputStream.readObject();
919 final V value = (V)inputStream.readObject();
923 // Propagate the read-only bit
925 READONLY_FIELD.setBoolean(this, ro);
926 } catch (IllegalAccessException e) {
927 throw new IOException("Failed to set read-only flag", e);
931 private void writeObject(final ObjectOutputStream outputStream) throws IOException {
932 outputStream.defaultWriteObject();
934 final Map<K, V> ro = readOnlySnapshot();
935 outputStream.writeBoolean(isReadOnly());
936 outputStream.writeInt(ro.size());
938 for (Entry<K, V> e : ro.entrySet()) {
939 outputStream.writeObject(e.getKey());
940 outputStream.writeObject(e.getValue());