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.Constants.LEVEL_BITS;
19 import static org.opendaylight.yangtools.triemap.LookupResult.RESTART;
20 import static org.opendaylight.yangtools.triemap.PresencePredicate.ABSENT;
21 import static org.opendaylight.yangtools.triemap.PresencePredicate.PRESENT;
23 import com.google.common.base.VerifyException;
24 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25 import java.util.Optional;
26 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
28 final class INode<K, V> extends BasicNode {
29 @SuppressWarnings("rawtypes")
30 private static final AtomicReferenceFieldUpdater<INode, MainNode> MAINNODE_UPDATER =
31 AtomicReferenceFieldUpdater.newUpdater(INode.class, MainNode.class, "mainnode");
33 private final Gen gen;
35 private volatile MainNode<K, V> mainnode;
37 INode(final Gen gen, final MainNode<K, V> mainnode) {
39 this.mainnode = mainnode;
42 MainNode<K, V> gcasRead(final TrieMap<?, ?> ct) {
46 private MainNode<K, V> GCAS_READ(final TrieMap<?, ?> ct) {
47 MainNode<K, V> m = /* READ */ mainnode;
48 MainNode<K, V> prevval = /* READ */ m.READ_PREV();
49 if (prevval == null) {
53 return GCAS_Complete(m, ct);
56 private MainNode<K, V> GCAS_Complete(MainNode<K, V> m, final TrieMap<?, ?> ct) {
63 final MainNode<K, V> prev = /* READ */ m.READ_PREV();
64 final Gen rdgen = ct.readRoot(true).gen;
69 if (prev instanceof FailedNode) {
70 // try to commit to previous value
71 FailedNode<K, V> fn = (FailedNode<K, V>) prev;
72 if (MAINNODE_UPDATER.compareAndSet(this, m, fn.READ_PREV())) {
73 return fn.READ_PREV();
76 // Tail recursion: return GCAS_Complete(/* READ */ mainnode, ct);
77 m = /* READ */ mainnode;
81 // Assume that you've read the root from the generation G.
82 // Assume that the snapshot algorithm is correct.
83 // ==> you can only reach nodes in generations <= G.
85 // We know that `ctr.gen` is >= G.
86 // ==> if `ctr.gen` = `gen` then they are both equal to G.
87 // ==> otherwise, we know that either `ctr.gen` > G, `gen` < G,
89 if (rdgen == gen && !ct.isReadOnly()) {
91 if (m.CAS_PREV(prev, null)) {
95 // Tail recursion: return GCAS_Complete(m, ct);
100 m.CAS_PREV(prev, new FailedNode<>(prev));
102 // Tail recursion: return GCAS_Complete(/* READ */ mainnode, ct);
103 m = /* READ */ mainnode;
107 private boolean GCAS(final MainNode<K, V> old, final MainNode<K, V> n, final TrieMap<?, ?> ct) {
109 if (MAINNODE_UPDATER.compareAndSet(this, old, n)) {
110 GCAS_Complete(n, ct);
111 return /* READ */ n.READ_PREV() == null;
117 private INode<K, V> inode(final MainNode<K, V> cn) {
118 return new INode<>(gen, cn);
121 INode<K, V> copyToGen(final Gen ngen, final TrieMap<?, ?> ct) {
122 return new INode<>(ngen, GCAS_READ(ct));
126 * Inserts a key value pair, overwriting the old pair if the keys match.
128 * @return true if successful, false otherwise
130 boolean rec_insert(final K key, final V value, final int hc, final int lev, final INode<K, V> parent,
131 final TrieMap<K, V> ct) {
132 return rec_insert(key, value, hc, lev, parent, gen, ct);
135 private boolean rec_insert(final K k, final V v, final int hc, final int lev, final INode<K, V> parent,
136 final Gen startgen, final TrieMap<K, V> ct) {
138 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
140 if (m instanceof CNode) {
141 // 1) a multiway node
142 final CNode<K, V> cn = (CNode<K, V>) m;
143 final int idx = (hc >>> lev) & 0x1f;
144 final int flag = 1 << idx;
145 final int bmp = cn.bitmap;
146 final int mask = flag - 1;
147 final int pos = Integer.bitCount(bmp & mask);
149 if ((bmp & flag) != 0) {
151 final BasicNode cnAtPos = cn.array[pos];
152 if (cnAtPos instanceof INode) {
153 final INode<K, V> in = (INode<K, V>) cnAtPos;
154 if (startgen == in.gen) {
155 return in.rec_insert(k, v, hc, lev + LEVEL_BITS, this, startgen, ct);
157 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
158 // Tail recursion: return rec_insert(k, v, hc, lev, parent, startgen, ct);
163 } else if (cnAtPos instanceof SNode) {
164 final SNode<K, V> sn = (SNode<K, V>) cnAtPos;
165 if (sn.hc == hc && ct.equal(sn.k, k)) {
166 return GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct);
169 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
170 final MainNode<K, V> nn = rn.updatedAt(pos, inode(
171 CNode.dual(sn, k, v, hc, lev + LEVEL_BITS, gen)), gen);
172 return GCAS(cn, nn, ct);
174 throw CNode.invalidElement(cnAtPos);
178 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
179 final MainNode<K, V> ncnode = rn.insertedAt(pos, flag, new SNode<>(k, v, hc), gen);
180 return GCAS(cn, ncnode, ct);
181 } else if (m instanceof TNode) {
182 clean(parent, ct, lev - LEVEL_BITS);
184 } else if (m instanceof LNode) {
185 final LNode<K, V> ln = (LNode<K, V>) m;
186 final LNodeEntry<K, V> entry = ln.get(ct.equiv(), k);
187 return entry != null ? replaceln(ln, entry, v, ct) : insertln(ln, k, v, ct);
189 throw invalidElement(m);
194 private static VerifyException invalidElement(final BasicNode elem) {
195 throw new VerifyException("An INode can host only a CNode, a TNode or an LNode, not " + elem);
198 @SuppressFBWarnings(value = "NP_OPTIONAL_RETURN_NULL",
199 justification = "Returning null Optional indicates the need to restart.")
200 private Optional<V> insertDual(final TrieMap<K, V> ct, final CNode<K, V> cn, final int pos, final SNode<K, V> sn,
201 final K k, final V v, final int hc, final int lev) {
202 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
203 final MainNode<K, V> nn = rn.updatedAt(pos, inode(CNode.dual(sn, k, v, hc, lev + LEVEL_BITS, gen)), gen);
204 return GCAS(cn, nn, ct) ? Optional.empty() : null;
208 * Inserts a new key value pair, given that a specific condition is met.
211 * null - don't care if the key was there
212 * KEY_ABSENT - key wasn't there
213 * KEY_PRESENT - key was there
214 * other value `v` - key must be bound to `v`
215 * @return null if unsuccessful, Option[V] otherwise (indicating
216 * previous value bound to the key)
218 Optional<V> rec_insertif(final K k, final V v, final int hc, final Object cond, final int lev,
219 final INode<K, V> parent, final TrieMap<K, V> ct) {
220 return rec_insertif(k, v, hc, cond, lev, parent, gen, ct);
223 @SuppressFBWarnings(value = "NP_OPTIONAL_RETURN_NULL",
224 justification = "Returning null Optional indicates the need to restart.")
225 private Optional<V> rec_insertif(final K k, final V v, final int hc, final Object cond, final int lev,
226 final INode<K, V> parent, final Gen startgen, final TrieMap<K, V> ct) {
228 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
230 if (m instanceof CNode) {
231 // 1) a multiway node
232 final CNode<K, V> cn = (CNode<K, V>) m;
233 final int idx = (hc >>> lev) & 0x1f;
234 final int flag = 1 << idx;
235 final int bmp = cn.bitmap;
236 final int mask = flag - 1;
237 final int pos = Integer.bitCount(bmp & mask);
239 if ((bmp & flag) != 0) {
241 final BasicNode cnAtPos = cn.array[pos];
242 if (cnAtPos instanceof INode) {
243 final INode<K, V> in = (INode<K, V>) cnAtPos;
244 if (startgen == in.gen) {
245 return in.rec_insertif(k, v, hc, cond, lev + LEVEL_BITS, this, startgen, ct);
248 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
249 // Tail recursion: return rec_insertif(k, v, hc, cond, lev, parent, startgen, ct);
254 } else if (cnAtPos instanceof SNode) {
255 final SNode<K, V> sn = (SNode<K, V>) cnAtPos;
257 if (sn.hc == hc && ct.equal(sn.k, k)) {
258 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
259 return Optional.of(sn.v);
265 return insertDual(ct, cn, pos, sn, k, v, hc, lev);
266 } else if (cond == ABSENT) {
267 if (sn.hc == hc && ct.equal(sn.k, k)) {
268 return Optional.of(sn.v);
271 return insertDual(ct, cn, pos, sn, k, v, hc, lev);
272 } else if (cond == PRESENT) {
273 if (sn.hc == hc && ct.equal(sn.k, k)) {
274 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
275 return Optional.of(sn.v);
280 return Optional.empty();
282 if (sn.hc == hc && ct.equal(sn.k, k) && cond.equals(sn.v)) {
283 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
284 return Optional.of(sn.v);
290 return Optional.empty();
293 throw CNode.invalidElement(cnAtPos);
295 } else if (cond == null || cond == ABSENT) {
296 final CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
297 final CNode<K, V> ncnode = rn.insertedAt(pos, flag, new SNode<>(k, v, hc), gen);
298 if (GCAS(cn, ncnode, ct)) {
299 return Optional.empty();
304 return Optional.empty();
306 } else if (m instanceof TNode) {
307 clean(parent, ct, lev - LEVEL_BITS);
309 } else if (m instanceof LNode) {
311 final LNode<K, V> ln = (LNode<K, V>) m;
312 final LNodeEntry<K, V> entry = ln.get(ct.equiv(), k);
316 return replaceln(ln, entry, v, ct) ? Optional.of(entry.getValue()) : null;
319 return insertln(ln, k, v, ct) ? Optional.empty() : null;
320 } else if (cond == ABSENT) {
322 return Optional.of(entry.getValue());
325 return insertln(ln, k, v, ct) ? Optional.empty() : null;
326 } else if (cond == PRESENT) {
328 return Optional.empty();
331 return replaceln(ln, entry, v, ct) ? Optional.of(entry.getValue()) : null;
333 if (entry == null || !cond.equals(entry.getValue())) {
334 return Optional.empty();
337 return replaceln(ln, entry, v, ct) ? Optional.of(entry.getValue()) : null;
340 throw invalidElement(m);
345 private boolean insertln(final LNode<K, V> ln, final K k, final V v, final TrieMap<K, V> ct) {
346 return GCAS(ln, ln.insertChild(k, v), ct);
349 private boolean replaceln(final LNode<K, V> ln, final LNodeEntry<K, V> entry, final V v, final TrieMap<K, V> ct) {
350 return GCAS(ln, ln.replaceChild(entry, v), ct);
354 * Looks up the value associated with the key.
356 * @return null if no value has been found, RESTART if the operation
357 * wasn't successful, or any other value otherwise
359 Object rec_lookup(final K k, final int hc, final int lev, final INode<K, V> parent, final TrieMap<K, V> ct) {
360 return rec_lookup(k, hc, lev, parent, gen, ct);
363 private Object rec_lookup(final K k, final int hc, final int lev, final INode<K, V> parent, final Gen startgen,
364 final TrieMap<K, V> ct) {
366 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
368 if (m instanceof CNode) {
370 final CNode<K, V> cn = (CNode<K, V>) m;
371 final int idx = (hc >>> lev) & 0x1f;
372 final int flag = 1 << idx;
373 final int bmp = cn.bitmap;
375 if ((bmp & flag) == 0) {
376 // 1a) bitmap shows no binding
380 // 1b) bitmap contains a value - descend
381 final int pos = (bmp == 0xffffffff) ? idx : Integer.bitCount(bmp & (flag - 1));
382 final BasicNode sub = cn.array[pos];
383 if (sub instanceof INode) {
384 final INode<K, V> in = (INode<K, V>) sub;
385 if (ct.isReadOnly() || (startgen == in.gen)) {
386 return in.rec_lookup(k, hc, lev + LEVEL_BITS, this, startgen, ct);
389 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
390 // Tail recursion: return rec_lookup(k, hc, lev, parent, startgen, ct);
395 } else if (sub instanceof SNode) {
397 final SNode<K, V> sn = (SNode<K, V>) sub;
398 if (sn.hc == hc && ct.equal(sn.k, k)) {
404 throw CNode.invalidElement(sub);
406 } else if (m instanceof TNode) {
408 return cleanReadOnly((TNode<K, V>) m, lev, parent, ct, k, hc);
409 } else if (m instanceof LNode) {
411 final LNodeEntry<K, V> entry = ((LNode<K, V>) m).get(ct.equiv(), k);
412 return entry != null ? entry.getValue() : null;
414 throw invalidElement(m);
419 private Object cleanReadOnly(final TNode<K, V> tn, final int lev, final INode<K, V> parent,
420 final TrieMap<K, V> ct, final K k, final int hc) {
421 if (ct.isReadOnly()) {
422 if (tn.hc == hc && ct.equal(tn.k, k)) {
429 clean(parent, ct, lev - LEVEL_BITS);
434 * Removes the key associated with the given value.
437 * if null, will remove the key regardless of the value;
438 * otherwise removes only if binding contains that exact key
440 * @return null if not successful, an Optional indicating the previous
443 Optional<V> rec_remove(final K k, final Object cond, final int hc, final int lev, final INode<K, V> parent,
444 final TrieMap<K, V> ct) {
445 return rec_remove(k, cond, hc, lev, parent, gen, ct);
448 @SuppressFBWarnings(value = "NP_OPTIONAL_RETURN_NULL",
449 justification = "Returning null Optional indicates the need to restart.")
450 private Optional<V> rec_remove(final K k, final Object cond, final int hc, final int lev, final INode<K, V> parent,
451 final Gen startgen, final TrieMap<K, V> ct) {
452 final MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
454 if (m instanceof CNode) {
455 final CNode<K, V> cn = (CNode<K, V>) m;
456 final int idx = (hc >>> lev) & 0x1f;
457 final int bmp = cn.bitmap;
458 final int flag = 1 << idx;
459 if ((bmp & flag) == 0) {
460 return Optional.empty();
463 final int pos = Integer.bitCount(bmp & (flag - 1));
464 final BasicNode sub = cn.array[pos];
465 final Optional<V> res;
466 if (sub instanceof INode) {
467 final INode<K, V> in = (INode<K, V>) sub;
468 if (startgen == in.gen) {
469 res = in.rec_remove(k, cond, hc, lev + LEVEL_BITS, this, startgen, ct);
471 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
472 res = rec_remove(k, cond, hc, lev, parent, startgen, ct);
477 } else if (sub instanceof SNode) {
478 final SNode<K, V> sn = (SNode<K, V>) sub;
479 if (sn.hc == hc && ct.equal(sn.k, k) && (cond == null || cond.equals(sn.v))) {
480 final MainNode<K, V> ncn = cn.removedAt(pos, flag, gen).toContracted(lev);
481 if (GCAS(cn, ncn, ct)) {
482 res = Optional.of(sn.v);
487 res = Optional.empty();
490 throw CNode.invalidElement(sub);
493 if (res == null || !res.isPresent()) {
497 if (parent != null) {
498 // never tomb at root
499 final MainNode<K, V> n = GCAS_READ(ct);
500 if (n instanceof TNode) {
501 cleanParent(n, parent, ct, hc, lev, startgen);
506 } else if (m instanceof TNode) {
507 clean(parent, ct, lev - LEVEL_BITS);
509 } else if (m instanceof LNode) {
510 final LNode<K, V> ln = (LNode<K, V>) m;
511 final LNodeEntry<K, V> entry = ln.get(ct.equiv(), k);
513 // Key was not found, hence no modification is needed
514 return Optional.empty();
517 final V value = entry.getValue();
518 if (cond != null && !cond.equals(value)) {
519 // Value does not match
520 return Optional.empty();
523 return GCAS(ln, ln.removeChild(entry, hc), ct) ? Optional.of(value) : null;
525 throw invalidElement(m);
529 private void cleanParent(final Object nonlive, final INode<K, V> parent, final TrieMap<K, V> ct, final int hc,
530 final int lev, final Gen startgen) {
532 final MainNode<K, V> pm = parent.GCAS_READ(ct);
533 if ((!(pm instanceof CNode))) {
534 // parent is no longer a cnode, we're done
538 final CNode<K, V> cn = (CNode<K, V>) pm;
539 final int idx = (hc >>> (lev - LEVEL_BITS)) & 0x1f;
540 final int bmp = cn.bitmap;
541 final int flag = 1 << idx;
542 if ((bmp & flag) == 0) {
543 // somebody already removed this i-node, we're done
547 final int pos = Integer.bitCount(bmp & (flag - 1));
548 final BasicNode sub = cn.array[pos];
550 if (nonlive instanceof TNode) {
551 final TNode<?, ?> tn = (TNode<?, ?>) nonlive;
552 final MainNode<K, V> ncn = cn.updatedAt(pos, tn.copyUntombed(), gen).toContracted(lev - LEVEL_BITS);
553 if (!parent.GCAS(cn, ncn, ct)) {
554 if (ct.readRoot().gen == startgen) {
555 // Tail recursion: cleanParent(nonlive, parent, ct, hc, lev, startgen);
565 private void clean(final INode<K, V> nd, final TrieMap<K, V> ct, final int lev) {
566 final MainNode<K, V> m = nd.GCAS_READ(ct);
567 if (m instanceof CNode) {
568 final CNode<K, V> cn = (CNode<K, V>) m;
569 nd.GCAS(cn, cn.toCompressed(ct, lev, gen), ct);
573 int size(final ImmutableTrieMap<?, ?> ct) {
574 return GCAS_READ(ct).size(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) {