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 org.opendaylight.yangtools.triemap.LookupResult.RESTART;
19 import static org.opendaylight.yangtools.triemap.PresencePredicate.ABSENT;
20 import static org.opendaylight.yangtools.triemap.PresencePredicate.PRESENT;
22 import java.util.Optional;
23 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
25 final class INode<K, V> extends BasicNode {
26 @SuppressWarnings("rawtypes")
27 private static final AtomicReferenceFieldUpdater<INode, MainNode> MAINNODE_UPDATER =
28 AtomicReferenceFieldUpdater.newUpdater(INode.class, MainNode.class, "mainnode");
30 private final Gen gen;
32 private volatile MainNode<K, V> mainnode;
34 INode(final Gen gen, final MainNode<K, V> mainnode) {
36 this.mainnode = mainnode;
39 MainNode<K, V> gcasRead(final TrieMap<?, ?> ct) {
43 private MainNode<K, V> GCAS_READ(final TrieMap<?, ?> ct) {
44 MainNode<K, V> m = /* READ */ mainnode;
45 MainNode<K, V> prevval = /* READ */ m.READ_PREV();
46 if (prevval == null) {
50 return GCAS_Complete(m, ct);
53 private MainNode<K, V> GCAS_Complete(MainNode<K, V> m, final TrieMap<?, ?> ct) {
60 final MainNode<K, V> prev = /* READ */ m.READ_PREV();
61 final Gen rdgen = ct.readRoot(true).gen;
66 if (prev instanceof FailedNode) {
67 // try to commit to previous value
68 FailedNode<K, V> fn = (FailedNode<K, V>) prev;
69 if (MAINNODE_UPDATER.compareAndSet(this, m, fn.READ_PREV())) {
70 return fn.READ_PREV();
73 // Tail recursion: return GCAS_Complete (/* READ */ mainnode, ct);
74 m = /* READ */ mainnode;
78 // Assume that you've read the root from the generation G.
79 // Assume that the snapshot algorithm is correct.
80 // ==> you can only reach nodes in generations <= G.
82 // We know that `ctr.gen` is >= G.
83 // ==> if `ctr.gen` = `gen` then they are both equal to G.
84 // ==> otherwise, we know that either `ctr.gen` > G, `gen` < G,
86 if (rdgen == gen && !ct.isReadOnly()) {
88 if (m.CAS_PREV(prev, null)) {
92 // Tail recursion: return GCAS_Complete (m, ct);
97 m.CAS_PREV(prev, new FailedNode<>(prev));
99 // Tail recursion: return GCAS_Complete(/* READ */ mainnode, ct);
100 m = /* READ */ mainnode;
104 private boolean GCAS(final MainNode<K, V> old, final MainNode<K, V> n, final TrieMap<?, ?> ct) {
106 if (MAINNODE_UPDATER.compareAndSet(this, old, n)) {
107 GCAS_Complete(n, ct);
108 return /* READ */ n.READ_PREV() == null;
114 private INode<K, V> inode(final MainNode<K, V> cn) {
115 return new INode<>(gen, cn);
118 INode<K, V> copyToGen(final Gen ngen, final TrieMap<?, ?> ct) {
119 return new INode<>(ngen, GCAS_READ(ct));
123 * Inserts a key value pair, overwriting the old pair if the keys match.
125 * @return true if successful, false otherwise
127 boolean rec_insert(final K k, final V v, final int hc, final int lev, final INode<K, V> parent,
128 final TrieMap<K, V> ct) {
129 return rec_insert(k, v, hc, lev, parent, gen, ct);
132 private boolean rec_insert(final K k, final V v, final int hc, final int lev, final INode<K, V> parent,
133 final Gen startgen, 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 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
172 final 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 final LNode<K, V> ln = (LNode<K, V>) m;
180 final LNodeEntry<K, V> entry = ln.get(ct.equiv(), k);
181 return entry != null ? replaceln(ln, entry, v, ct) : insertln(ln, k, v, 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 TrieMap<K, V> ct) {
203 return rec_insertif(k, v, hc, cond, lev, parent, gen, ct);
206 private Optional<V> rec_insertif(final K k, final V v, final int hc, final Object cond, final int lev,
207 final INode<K, V> parent, final Gen startgen, final TrieMap<K, V> ct) {
209 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
211 if (m instanceof CNode) {
212 // 1) a multiway node
213 final CNode<K, V> cn = (CNode<K, V>) m;
214 final int idx = (hc >>> lev) & 0x1f;
215 final int flag = 1 << idx;
216 final int bmp = cn.bitmap;
217 final int mask = flag - 1;
218 final int pos = Integer.bitCount(bmp & mask);
220 if ((bmp & flag) != 0) {
222 final BasicNode cnAtPos = cn.array[pos];
223 if (cnAtPos instanceof INode) {
224 final INode<K, V> in = (INode<K, V>) cnAtPos;
225 if (startgen == in.gen) {
226 return in.rec_insertif(k, v, hc, cond, lev + 5, this, startgen, ct);
229 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
230 // Tail recursion: return rec_insertif (k, v, hc, cond, lev, parent, startgen, ct);
235 } else if (cnAtPos instanceof SNode) {
236 final SNode<K, V> sn = (SNode<K, V>) cnAtPos;
238 if (sn.hc == hc && ct.equal(sn.k, k)) {
239 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
240 return Optional.of(sn.v);
246 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
247 final MainNode<K, V> nn = rn.updatedAt(pos, inode (CNode.dual(sn, sn.hc,
248 new SNode<>(k, v, hc), hc, lev + 5, gen)), gen);
249 if (GCAS(cn, nn, ct)) {
250 return Optional.empty();
254 } else if (cond == ABSENT) {
255 if (sn.hc == hc && ct.equal(sn.k, k)) {
256 return Optional.of(sn.v);
259 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
260 final MainNode<K, V> nn = rn.updatedAt(pos, inode (CNode.dual(sn, sn.hc,
261 new SNode<>(k, v, hc), hc, lev + 5, gen)), gen);
262 if (GCAS(cn, nn, ct)) {
263 return Optional.empty();
267 } else if (cond == PRESENT) {
268 if (sn.hc == hc && ct.equal(sn.k, k)) {
269 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
270 return Optional.of(sn.v);
275 return Optional.empty();
277 if (sn.hc == hc && ct.equal(sn.k, k) && cond.equals(sn.v)) {
278 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
279 return Optional.of(sn.v);
285 return Optional.empty();
288 } else if (cond == null || cond == ABSENT) {
289 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
290 final CNode<K, V> ncnode = rn.insertedAt (pos, flag, new SNode<>(k, v, hc), gen);
291 if (GCAS(cn, ncnode, ct)) {
292 return Optional.empty();
297 return Optional.empty();
299 } else if (m instanceof TNode) {
300 clean(parent, ct, lev - 5);
302 } else if (m instanceof LNode) {
304 final LNode<K, V> ln = (LNode<K, V>) m;
305 final LNodeEntry<K, V> entry = ln.get(ct.equiv(), k);
309 return replaceln(ln, entry, v, ct) ? Optional.of(entry.value()) : null;
312 return insertln(ln, k, v, ct) ? Optional.empty() : null;
313 } else if (cond == ABSENT) {
315 return Optional.of(entry.value());
318 return insertln(ln, k, v, ct) ? Optional.empty() : null;
319 } else if (cond == PRESENT) {
321 return Optional.empty();
324 return replaceln(ln, entry, v, ct) ? Optional.of(entry.value()) : null;
326 if (entry == null || !cond.equals(entry.value())) {
327 return Optional.empty();
330 return replaceln(ln, entry, v, ct) ? Optional.of(entry.value()) : null;
333 throw new IllegalStateException("Unhandled node " + m);
336 throw new RuntimeException("Should never happen");
340 private boolean insertln(final LNode<K, V> ln, final K k, final V v, final TrieMap<K, V> ct) {
341 return GCAS(ln, ln.insertChild(k, v), ct);
344 private boolean replaceln(final LNode<K, V> ln, final LNodeEntry<K, V> entry, final V v, final TrieMap<K, V> ct) {
345 return GCAS(ln, ln.replaceChild(entry, v), ct);
349 * Looks up the value associated with the key.
351 * @return null if no value has been found, RESTART if the operation
352 * wasn't successful, or any other value otherwise
354 Object rec_lookup(final K k, final int hc, final int lev, final INode<K, V> parent, final TrieMap<K, V> ct) {
355 return rec_lookup(k, hc, lev, parent, gen, ct);
358 private Object rec_lookup(final K k, final int hc, final int lev, final INode<K, V> parent, final Gen startgen,
359 final TrieMap<K, V> ct) {
361 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
363 if (m instanceof CNode) {
365 final CNode<K, V> cn = (CNode<K, V>) m;
366 final int idx = (hc >>> lev) & 0x1f;
367 final int flag = 1 << idx;
368 final int bmp = cn.bitmap;
369 if ((bmp & flag) == 0) {
370 // 1a) bitmap shows no binding
374 // 1b) bitmap contains a value - descend
375 final int pos = (bmp == 0xffffffff) ? idx : Integer.bitCount(bmp & (flag - 1));
376 final BasicNode sub = cn.array[pos];
377 if (sub instanceof INode) {
378 final INode<K, V> in = (INode<K, V>) sub;
379 if (ct.isReadOnly() || (startgen == in.gen)) {
380 return in.rec_lookup(k, hc, lev + 5, this, startgen, ct);
383 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
384 // Tail recursion: return rec_lookup(k, hc, lev, parent, startgen, ct);
389 } else if (sub instanceof SNode) {
391 final SNode<K, V> sn = (SNode<K, V>) sub;
392 if (sn.hc == hc && ct.equal(sn.k, k)) {
398 } else if (m instanceof TNode) {
400 return cleanReadOnly((TNode<K, V>) m, lev, parent, ct, k, hc);
401 } else if (m instanceof LNode) {
403 final LNodeEntry<K, V> entry = ((LNode<K, V>) m).get(ct.equiv(), k);
404 return entry != null ? entry.value() : null;
406 throw new IllegalStateException("Unhandled node " + m);
409 throw new RuntimeException ("Should not happen");
413 private Object cleanReadOnly(final TNode<K, V> tn, final int lev, final INode<K, V> parent,
414 final TrieMap<K, V> ct, final K k, final int hc) {
415 if (ct.isReadOnly()) {
416 if (tn.hc == hc && ct.equal(tn.k, k)) {
423 clean(parent, ct, lev - 5);
428 * Removes the key associated with the given value.
431 * if null, will remove the key regardless of the value;
432 * otherwise removes only if binding contains that exact key
434 * @return null if not successful, an Optional indicating the previous
437 Optional<V> rec_remove(final K k, final Object cond, final int hc, final int lev, final INode<K, V> parent,
438 final TrieMap<K, V> ct) {
439 return rec_remove(k, cond, hc, lev, parent, gen, ct);
442 private Optional<V> rec_remove(final K k, final Object cond, final int hc, final int lev, final INode<K, V> parent,
443 final Gen startgen, final TrieMap<K, V> ct) {
444 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
446 if (m instanceof CNode) {
447 final CNode<K, V> cn = (CNode<K, V>) m;
448 final int idx = (hc >>> lev) & 0x1f;
449 final int bmp = cn.bitmap;
450 final int flag = 1 << idx;
451 if ((bmp & flag) == 0) {
452 return Optional.empty();
455 final int pos = Integer.bitCount(bmp & (flag - 1));
456 final BasicNode sub = cn.array[pos];
457 Optional<V> res = null;
458 if (sub instanceof INode) {
459 final INode<K, V> in = (INode<K, V>) sub;
460 if (startgen == in.gen) {
461 res = in.rec_remove(k, cond, hc, lev + 5, this, startgen, ct);
463 if (GCAS(cn, cn.renewed (startgen, ct), ct)) {
464 res = rec_remove(k, cond, hc, lev, parent, startgen, ct);
470 } else if (sub instanceof SNode) {
471 final SNode<K, V> sn = (SNode<K, V>) sub;
472 if (sn.hc == hc && ct.equal(sn.k, k) && (cond == null || cond.equals(sn.v))) {
473 final MainNode<K, V> ncn = cn.removedAt(pos, flag, gen).toContracted(lev);
474 if (GCAS(cn, ncn, ct)) {
475 res = Optional.of(sn.v);
480 res = Optional.empty();
484 if (res == null || !res.isPresent()) {
488 if (parent != null) {
489 // never tomb at root
490 final MainNode<K, V> n = GCAS_READ(ct);
491 if (n instanceof TNode) {
492 cleanParent(n, parent, ct, hc, lev, startgen);
497 } else if (m instanceof TNode) {
498 clean(parent, ct, lev - 5);
500 } else if (m instanceof LNode) {
501 final LNode<K, V> ln = (LNode<K, V>) m;
502 final LNodeEntry<K, V> entry = ln.get(ct.equiv(), k);
504 // Key was not found, hence no modification is needed
505 return Optional.empty();
508 final V value = entry.value();
509 if (cond != null && !cond.equals(value)) {
510 // Value does not match
511 return Optional.empty();
514 return GCAS(ln, ln.removeChild(entry, hc), ct) ? Optional.of(value) : null;
516 throw new IllegalStateException("Unhandled node " + m);
520 private void cleanParent(final Object nonlive, final INode<K, V> parent, final TrieMap<K, V> ct, final int hc,
521 final int lev, final Gen startgen) {
523 final MainNode<K, V> pm = parent.GCAS_READ(ct);
524 if ((!(pm instanceof CNode))) {
525 // parent is no longer a cnode, we're done
529 final CNode<K, V> cn = (CNode<K, V>) pm;
530 final int idx = (hc >>> (lev - 5)) & 0x1f;
531 final int bmp = cn.bitmap;
532 final int flag = 1 << idx;
533 if ((bmp & flag) == 0) {
534 // somebody already removed this i-node, we're done
538 final int pos = Integer.bitCount(bmp & (flag - 1));
539 final BasicNode sub = cn.array[pos];
541 if (nonlive instanceof TNode) {
542 final TNode<?, ?> tn = (TNode<?, ?>) nonlive;
543 final MainNode<K, V> ncn = cn.updatedAt(pos, tn.copyUntombed(), gen).toContracted(lev - 5);
544 if (!parent.GCAS(cn, ncn, ct)) {
545 if (ct.readRoot().gen == startgen) {
546 // Tail recursion: cleanParent(nonlive, parent, ct, hc, lev, startgen);
556 private void clean(final INode<K, V> nd, final TrieMap<K, V> ct, final int lev) {
557 final MainNode<K, V> m = nd.GCAS_READ(ct);
558 if (m instanceof CNode) {
559 final CNode<K, V> cn = (CNode<K, V>) m;
560 nd.GCAS(cn, cn.toCompressed(ct, lev, gen), ct);
564 int cachedSize(final TrieMap<?, ?> ct) {
565 return GCAS_READ(ct).cachedSize(ct);
568 // /* this is a quiescent method! */
569 // def string(lev: Int) = "%sINode -> %s".format(" " * lev, mainnode
571 // case null => "<null>"
572 // case tn: TNode[_, _] => "TNode(%s, %s, %d, !)".format(tn.k, tn.v,
574 // case cn: CNode[_, _] => cn.string(lev)
575 // case ln: LNode[_, _] => ln.string(lev)
576 // case x => "<elem: %s>".format(x)
580 String string(final int lev) {