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.util.Optional;
19 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
21 final class INode<K, V> extends BasicNode {
22 static final Object KEY_PRESENT = new Object ();
23 static final Object KEY_ABSENT = new Object ();
26 * Virtual result for lookup methods indicating that the lookup needs to be restarted. This is a faster version
27 * of throwing a checked exception to control the restart.
29 static final Object RESTART = new Object();
31 @SuppressWarnings("rawtypes")
32 private static final AtomicReferenceFieldUpdater<INode, MainNode> MAINNODE_UPDATER =
33 AtomicReferenceFieldUpdater.newUpdater(INode.class, MainNode.class, "mainnode");
37 private volatile MainNode<K, V> mainnode;
39 INode(final Gen gen, final MainNode<K, V> mainnode) {
41 this.mainnode = mainnode;
44 MainNode<K, V> gcasRead(final TrieMap<K, V> ct) {
48 MainNode<K, V> GCAS_READ(final TrieMap<K, V> ct) {
49 MainNode<K, V> m = /* READ */ mainnode;
50 MainNode<K, V> prevval = /* READ */ m.READ_PREV();
51 if (prevval == null) {
55 return GCAS_Complete(m, ct);
58 private MainNode<K, V> GCAS_Complete(MainNode<K, V> m, final TrieMap<K, V> ct) {
65 final MainNode<K, V> prev = /* READ */ m.READ_PREV();
66 final INode<K, V> ctr = ct.readRoot(true);
71 if (prev instanceof FailedNode) {
72 // try to commit to previous value
73 FailedNode<K, V> fn = (FailedNode<K, V>) prev;
74 if (MAINNODE_UPDATER.compareAndSet(this, m, fn.READ_PREV())) {
75 return fn.READ_PREV();
78 // Tail recursion: return GCAS_Complete (/* READ */ mainnode, ct);
79 m = /* READ */ mainnode;
83 // Assume that you've read the root from the generation G.
84 // Assume that the snapshot algorithm is correct.
85 // ==> you can only reach nodes in generations <= G.
87 // We know that `ctr.gen` is >= G.
88 // ==> if `ctr.gen` = `gen` then they are both equal to G.
89 // ==> otherwise, we know that either `ctr.gen` > G, `gen` < G,
91 if (ctr.gen == gen && !ct.isReadOnly()) {
93 if (m.CAS_PREV(prev, null)) {
97 // Tail recursion: return GCAS_Complete (m, ct);
102 m.CAS_PREV(prev, new FailedNode<>(prev));
104 // Tail recursion: return GCAS_Complete(/* READ */ mainnode, ct);
105 m = /* READ */ mainnode;
109 private boolean GCAS(final MainNode<K, V> old, final MainNode<K, V> n, final TrieMap<K, V> ct) {
111 if (MAINNODE_UPDATER.compareAndSet(this, old, n)) {
112 GCAS_Complete(n, ct);
113 return /* READ */ n.READ_PREV() == null;
119 private INode<K, V> inode(final MainNode<K, V> cn) {
120 return new INode<>(gen, cn);
123 INode<K, V> copyToGen(final Gen ngen, final TrieMap<K, V> ct) {
124 return new INode<>(ngen, GCAS_READ(ct));
128 * Inserts a key value pair, overwriting the old pair if the keys match.
130 * @return true if successful, false otherwise
132 boolean rec_insert(final K k, final V v, final int hc, final int lev, final INode<K, V> parent, final Gen startgen,
133 final TrieMap<K, V> ct) {
135 final MainNode<K, V> m = GCAS_READ (ct); // use -Yinline!
137 if (m instanceof CNode) {
138 // 1) a multiway node
139 final CNode<K, V> cn = (CNode<K, V>) m;
140 final int idx = (hc >>> lev) & 0x1f;
141 final int flag = 1 << idx;
142 final int bmp = cn.bitmap;
143 final int mask = flag - 1;
144 final int pos = Integer.bitCount(bmp & mask);
145 if ((bmp & flag) != 0) {
147 final BasicNode cnAtPos = cn.array[pos];
148 if (cnAtPos instanceof INode) {
149 final INode<K, V> in = (INode<K, V>) cnAtPos;
150 if (startgen == in.gen) {
151 return in.rec_insert(k, v, hc, lev + 5, this, startgen, ct);
153 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
154 // Tail recursion: return rec_insert (k, v, hc, lev, parent, startgen, ct);
159 } else if (cnAtPos instanceof SNode) {
160 final SNode<K, V> sn = (SNode<K, V>) cnAtPos;
161 if (sn.hc == hc && ct.equal(sn.k, k)) {
162 return GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct);
165 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
166 final MainNode<K, V> nn = rn.updatedAt(pos, inode(CNode.dual(sn, sn.hc, new SNode<>(k, v, hc),
167 hc, lev + 5, gen)), gen);
168 return GCAS (cn, nn, ct);
171 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
172 MainNode<K, V> ncnode = rn.insertedAt(pos, flag, new SNode<> (k, v, hc), gen);
173 return GCAS (cn, ncnode, ct);
175 } else if (m instanceof TNode) {
176 clean(parent, ct, lev - 5);
178 } else if (m instanceof LNode) {
179 LNode<K, V> ln = (LNode<K, V>) m;
180 MainNode<K, V> nn = ln.inserted(k, v);
181 return GCAS(ln, nn, ct);
183 throw new IllegalStateException("Unhandled node " + m);
186 throw new RuntimeException ("Should not happen");
191 * Inserts a new key value pair, given that a specific condition is met.
194 * null - don't care if the key was there
195 * KEY_ABSENT - key wasn't there
196 * KEY_PRESENT - key was there
197 * other value `v` - key must be bound to `v`
198 * @return null if unsuccessful, Option[V] otherwise (indicating
199 * previous value bound to the key)
201 Optional<V> rec_insertif(final K k, final V v, final int hc, final Object cond, final int lev,
202 final INode<K, V> parent, final Gen startgen, final TrieMap<K, V> ct) {
204 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
206 if (m instanceof CNode) {
207 // 1) a multiway node
208 final CNode<K, V> cn = (CNode<K, V>) m;
209 final int idx = (hc >>> lev) & 0x1f;
210 final int flag = 1 << idx;
211 final int bmp = cn.bitmap;
212 final int mask = flag - 1;
213 final int pos = Integer.bitCount(bmp & mask);
215 if ((bmp & flag) != 0) {
217 final BasicNode cnAtPos = cn.array[pos];
218 if (cnAtPos instanceof INode) {
219 final INode<K, V> in = (INode<K, V>) cnAtPos;
220 if (startgen == in.gen) {
221 return in.rec_insertif(k, v, hc, cond, lev + 5, this, startgen, ct);
224 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
225 // Tail recursion: return rec_insertif (k, v, hc, cond, lev, parent, startgen, ct);
230 } else if (cnAtPos instanceof SNode) {
231 final SNode<K, V> sn = (SNode<K, V>) cnAtPos;
233 if (sn.hc == hc && ct.equal(sn.k, k)) {
234 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
235 return Optional.of(sn.v);
241 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
242 final MainNode<K, V> nn = rn.updatedAt(pos, inode (CNode.dual(sn, sn.hc,
243 new SNode<>(k, v, hc), hc, lev + 5, gen)), gen);
244 if (GCAS(cn, nn, ct)) {
245 return Optional.empty();
249 } else if (cond == INode.KEY_ABSENT) {
250 if (sn.hc == hc && ct.equal(sn.k, k)) {
251 return Optional.of(sn.v);
254 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
255 final MainNode<K, V> nn = rn.updatedAt(pos, inode (CNode.dual(sn, sn.hc,
256 new SNode<>(k, v, hc), hc, lev + 5, gen)), gen);
257 if (GCAS(cn, nn, ct)) {
258 return Optional.empty();
262 } else if (cond == INode.KEY_PRESENT) {
263 if (sn.hc == hc && ct.equal(sn.k, k)) {
264 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
265 return Optional.of(sn.v);
270 return Optional.empty();
272 if (sn.hc == hc && ct.equal(sn.k, k) && cond.equals(sn.v)) {
273 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
274 return Optional.of(sn.v);
280 return Optional.empty();
283 } else if (cond == null || cond == INode.KEY_ABSENT) {
284 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
285 final CNode<K, V> ncnode = rn.insertedAt (pos, flag, new SNode<>(k, v, hc), gen);
286 if (GCAS(cn, ncnode, ct)) {
287 return Optional.empty();
291 } else if (cond == INode.KEY_PRESENT) {
292 return Optional.empty();
294 return Optional.empty();
296 } else if (m instanceof TNode) {
297 clean(parent, ct, lev - 5);
299 } else if (m instanceof LNode) {
301 final LNode<K, V> ln = (LNode<K, V>) m;
303 final Optional<V> optv = ln.get(k);
304 if (insertln(ln, k, v, ct)) {
308 } else if (cond == INode.KEY_ABSENT) {
309 final Optional<V> t = ln.get(k);
313 if (insertln(ln, k, v, ct)) {
314 return Optional.empty();
317 } else if (cond == INode.KEY_PRESENT) {
318 final Optional<V> t = ln.get(k);
319 if (!t.isPresent()) {
322 if (insertln(ln, k, v, ct)) {
327 final Optional<V> t = ln.get(k);
329 if (cond.equals(t.get())) {
330 if (insertln(ln, k, v, ct)) {
331 // Difference from Scala: we choose to reuse the object returned from LNode,
332 // as the identity of the value does not matter in this call graph.
340 return Optional.empty();
343 throw new IllegalStateException("Unhandled node " + m);
346 throw new RuntimeException("Should never happen");
350 boolean insertln(final LNode<K, V> ln, final K k, final V v, final TrieMap<K, V> ct) {
351 final LNode<K, V> nn = ln.inserted (k, v);
352 return GCAS(ln, nn, ct);
356 * Looks up the value associated with the key.
358 * @return null if no value has been found, RESTART if the operation
359 * wasn't successful, or any other value otherwise
361 Object rec_lookup(final K k, final int hc, final int lev, final INode<K, V> parent, final Gen startgen,
362 final TrieMap<K, V> ct) {
364 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
366 if (m instanceof CNode) {
368 final CNode<K, V> cn = (CNode<K, V>) m;
369 final int idx = (hc >>> lev) & 0x1f;
370 final int flag = 1 << idx;
371 final int bmp = cn.bitmap;
372 if ((bmp & flag) == 0) {
373 // 1a) bitmap shows no binding
377 // 1b) bitmap contains a value - descend
378 final int pos = (bmp == 0xffffffff) ? idx : Integer.bitCount(bmp & (flag - 1));
379 final BasicNode sub = cn.array[pos];
380 if (sub instanceof INode) {
381 final INode<K, V> in = (INode<K, V>) sub;
382 if (ct.isReadOnly() || (startgen == in.gen)) {
383 return in.rec_lookup(k, hc, lev + 5, this, startgen, ct);
386 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
387 // Tail recursion: return rec_lookup(k, hc, lev, parent, startgen, ct);
391 // used to be throw RestartException
393 } else if (sub instanceof SNode) {
395 final SNode<K, V> sn = (SNode<K, V>) sub;
396 if (sn.hc == hc && ct.equal(sn.k, k)) {
402 } else if (m instanceof TNode) {
404 return cleanReadOnly((TNode<K, V>) m, lev, parent, ct, k, hc);
405 } else if (m instanceof LNode) {
407 return ((LNode<K, V>) m).get(k).orElse(null);
409 throw new IllegalStateException("Unhandled node " + m);
412 throw new RuntimeException ("Should not happen");
416 private Object cleanReadOnly(final TNode<K, V> tn, final int lev, final INode<K, V> parent,
417 final TrieMap<K, V> ct, final K k, final int hc) {
418 if (ct.isReadOnly()) {
419 if (tn.hc == hc && ct.equal(tn.k, k)) {
426 // used to be throw RestartException
427 clean(parent, ct, lev - 5);
432 * Removes the key associated with the given value.
435 * if null, will remove the key regardless of the value;
436 * otherwise removes only if binding contains that exact key
438 * @return null if not successful, an Option[V] indicating the previous
441 Optional<V> rec_remove(final K k, final V v, final int hc, final int lev, final INode<K, V> parent,
442 final Gen startgen, final TrieMap<K, V> ct) {
443 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
445 if (m instanceof CNode) {
446 final CNode<K, V> cn = (CNode<K, V>) m;
447 final int idx = (hc >>> lev) & 0x1f;
448 final int bmp = cn.bitmap;
449 final int flag = 1 << idx;
450 if ((bmp & flag) == 0) {
451 return Optional.empty();
454 final int pos = Integer.bitCount(bmp & (flag - 1));
455 final BasicNode sub = cn.array[pos];
456 Optional<V> res = null;
457 if (sub instanceof INode) {
458 final INode<K, V> in = (INode<K, V>) sub;
459 if (startgen == in.gen) {
460 res = in.rec_remove(k, v, hc, lev + 5, this, startgen, ct);
462 if (GCAS(cn, cn.renewed (startgen, ct), ct)) {
463 res = rec_remove(k, v, hc, lev, parent, startgen, ct);
469 } else if (sub instanceof SNode) {
470 final SNode<K, V> sn = (SNode<K, V>) sub;
471 if (sn.hc == hc && ct.equal(sn.k, k) && (v == null || v.equals(sn.v))) {
472 final MainNode<K, V> ncn = cn.removedAt(pos, flag, gen).toContracted(lev);
473 if (GCAS(cn, ncn, ct)) {
474 res = Optional.of(sn.v);
479 res = Optional.empty();
483 if (res == null || !res.isPresent()) {
487 if (parent != null) {
488 // never tomb at root
489 final MainNode<K, V> n = GCAS_READ(ct);
490 if (n instanceof TNode) {
491 cleanParent(n, parent, ct, hc, lev, startgen);
496 } else if (m instanceof TNode) {
497 clean(parent, ct, lev - 5);
499 } else if (m instanceof LNode) {
500 final LNode<K, V> ln = (LNode<K, V>) m;
502 final Optional<V> optv = ln.get(k);
503 final MainNode<K, V> nn = ln.removed(k, ct);
504 if (GCAS(ln, nn, ct)) {
511 final Optional<V> tmp = ln.get(k);
512 if (tmp.isPresent() && v.equals(tmp.get())) {
513 final MainNode<K, V> nn = ln.removed(k, ct);
514 if (GCAS(ln, nn, ct)) {
521 // Key not found or value does not match: we have not removed anything
522 return Optional.empty();
524 throw new IllegalStateException("Unhandled node " + m);
528 private void cleanParent(final Object nonlive, final INode<K, V> parent, final TrieMap<K, V> ct, final int hc,
529 final int lev, final Gen startgen) {
531 final MainNode<K, V> pm = parent.GCAS_READ(ct);
532 if ((!(pm instanceof CNode))) {
533 // parent is no longer a cnode, we're done
537 final CNode<K, V> cn = (CNode<K, V>) pm;
538 final int idx = (hc >>> (lev - 5)) & 0x1f;
539 final int bmp = cn.bitmap;
540 final int flag = 1 << idx;
541 if ((bmp & flag) == 0) {
542 // somebody already removed this i-node, we're done
546 final int pos = Integer.bitCount(bmp & (flag - 1));
547 final BasicNode sub = cn.array[pos];
549 if (nonlive instanceof TNode) {
550 final TNode<K, V> tn = (TNode<K, V>) nonlive;
551 MainNode<K, V> ncn = cn.updatedAt(pos, tn.copyUntombed(), gen).toContracted(lev - 5);
552 if (!parent.GCAS(cn, ncn, ct)) {
553 if (ct.readRoot().gen == startgen) {
554 // Tail recursion: cleanParent(nonlive, parent, ct, hc, lev, startgen);
564 private void clean(final INode<K, V> nd, final TrieMap<K, V> ct, final int lev) {
565 final MainNode<K, V> m = nd.GCAS_READ(ct);
566 if (m instanceof CNode) {
567 final CNode<K, V> cn = (CNode<K, V>) m;
568 nd.GCAS(cn, cn.toCompressed(ct, lev, gen), ct);
572 int cachedSize(final TrieMap<K, V> ct) {
573 MainNode<K, V> m = GCAS_READ(ct);
574 return m.cachedSize(ct);
577 // /* this is a quiescent method! */
578 // def string(lev: Int) = "%sINode -> %s".format(" " * lev, mainnode
580 // case null => "<null>"
581 // case tn: TNode[_, _] => "TNode(%s, %s, %d, !)".format(tn.k, tn.v,
583 // case cn: CNode[_, _] => cn.string(lev)
584 // case ln: LNode[_, _] => ln.string(lev)
585 // case x => "<elem: %s>".format(x)
589 String string(final int lev) {