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 final class INode<K, V> extends INodeBase<K, V> {
19 static final Object KEY_PRESENT = new Object ();
20 static final Object KEY_ABSENT = new Object ();
23 * Virtual result for lookup methods indicating that the lookup needs to be restarted. This is a faster version
24 * of throwing a checked exception to control the restart.
26 static final Object RESTART = new Object();
28 INode(final Gen gen, final MainNode<K, V> bn) {
32 MainNode<K, V> gcasRead(final TrieMap<K, V> ct) {
36 MainNode<K, V> GCAS_READ(final TrieMap<K, V> ct) {
37 MainNode<K, V> m = /* READ */ READ();
38 MainNode<K, V> prevval = /* READ */ m.READ_PREV();
39 if (prevval == null) {
42 return GCAS_Complete(m, ct);
46 private MainNode<K, V> GCAS_Complete(MainNode<K, V> m, final TrieMap<K, V> ct) {
52 final MainNode<K, V> prev = /* READ */ m.READ_PREV();
53 INode<K, V> ctr = ct.readRoot(true);
59 if (prev instanceof FailedNode) {
60 // try to commit to previous value
61 FailedNode<K, V> fn = (FailedNode<K, V>) prev;
62 if (CAS(m, fn.READ_PREV())) {
63 return fn.READ_PREV();
66 // return GCAS_Complete (/* READ */mainnode, ct);
67 m = /* READ */ READ();
71 // Assume that you've read the root from the generation
73 // Assume that the snapshot algorithm is correct.
74 // ==> you can only reach nodes in generations <= G.
76 // We know that `ctr.gen` is >= G.
77 // ==> if `ctr.gen` = `gen` then they are both equal to
79 // ==> otherwise, we know that either `ctr.gen` > G,
83 if ((ctr.gen == gen) && ct.nonReadOnly()) {
85 if (m.CAS_PREV(prev, null)) {
88 // return GCAS_Complete (m, ct);
94 m.CAS_PREV(prev, new FailedNode<>(prev));
95 return GCAS_Complete(/* READ */ READ(), ct);
102 boolean GCAS(final MainNode<K, V> old, final MainNode<K, V> n, final TrieMap<K, V> ct) {
105 GCAS_Complete (n, ct);
106 return /* READ */ n.READ_PREV() == null;
112 private boolean equal(final K k1, final K k2, final TrieMap<K, V> ct) {
113 return ct.equality().equiv(k1, k2);
116 private INode<K, V> inode(final MainNode<K, V> cn) {
117 return new INode<>(gen, cn);
120 INode<K, V> copyToGen(final Gen ngen, final TrieMap<K, V> ct) {
121 return new INode<>(ngen, GCAS_READ(ct));
125 * Inserts a key value pair, overwriting the old pair if the keys match.
127 * @return true if successful, false otherwise
129 boolean rec_insert(final K k, final V v, final int hc, final int lev, final INode<K, V> parent, final Gen startgen,
130 final TrieMap<K, V> ct) {
132 MainNode<K, V> m = GCAS_READ (ct); // use -Yinline!
134 if (m instanceof CNode) {
135 // 1) a multiway node
136 CNode<K, V> cn = (CNode<K, V>) m;
137 int idx = (hc >>> lev) & 0x1f;
141 int pos = Integer.bitCount(bmp & mask);
142 if ((bmp & flag) != 0) {
144 BasicNode cnAtPos = cn.array[pos];
145 if (cnAtPos instanceof INode) {
146 INode<K, V> in = (INode<K, V>) cnAtPos;
147 if (startgen == in.gen) {
148 return in.rec_insert(k, v, hc, lev + 5, this, startgen, ct);
150 if (GCAS (cn, cn.renewed(startgen, ct), ct)) {
151 // return rec_insert (k, v, hc, lev, parent,
159 } else if (cnAtPos instanceof SNode) {
160 SNode<K, V> sn = (SNode<K, V>) cnAtPos;
161 if (sn.hc == hc && equal(sn.k, k, ct)) {
162 return GCAS (cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct);
164 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
165 MainNode<K, V> nn = rn.updatedAt(pos, inode(CNode.dual(sn, sn.hc, new SNode<>(k, v, hc),
166 hc, lev + 5, gen)), gen);
167 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);
184 throw new RuntimeException ("Should not happen");
189 * Inserts a new key value pair, given that a specific condition is met.
192 * null - don't care if the key was there
193 * KEY_ABSENT - key wasn't there
194 * KEY_PRESENT - key was there
195 * other value `v` - key must be bound to `v`
196 * @return null if unsuccessful, Option[V] otherwise (indicating
197 * previous value bound to the key)
199 Option<V> rec_insertif(final K k, final V v, final int hc, final Object cond, final int lev,
200 final INode<K, V> parent, final Gen startgen, final TrieMap<K, V> ct) {
202 MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
204 if (m instanceof CNode) {
205 // 1) a multiway node
206 CNode<K, V> cn = (CNode<K, V>) m;
207 int idx = (hc >>> lev) & 0x1f;
211 int pos = Integer.bitCount(bmp & mask);
213 if ((bmp & flag) != 0) {
215 BasicNode cnAtPos = cn.array [pos];
216 if (cnAtPos instanceof INode) {
217 INode<K, V> in = (INode<K, V>) cnAtPos;
218 if (startgen == in.gen) {
219 return in.rec_insertif(k, v, hc, cond, lev + 5, this, startgen, ct);
221 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
222 // return rec_insertif (k, v, hc, cond, lev,
223 // parent, startgen, ct);
230 } else if (cnAtPos instanceof SNode) {
231 SNode<K, V> sn = (SNode<K, V>) cnAtPos;
233 if (sn.hc == hc && equal(sn.k, k, ct)) {
234 if (GCAS(cn, cn.updatedAt(pos, new SNode<> (k, v, hc), gen), ct)) {
235 return Option.makeOption(sn.v);
240 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
241 MainNode<K, V> nn = rn.updatedAt(pos, inode (CNode.dual(sn, sn.hc,
242 new SNode<>(k, v, hc), hc, lev + 5, gen)), gen);
243 if (GCAS(cn, nn, ct)) {
244 return Option.makeOption(); // None;
250 } else if (cond == INode.KEY_ABSENT) {
251 if (sn.hc == hc && equal (sn.k, k, ct)) {
252 return Option.makeOption(sn.v);
254 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed (gen, ct);
255 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 Option.makeOption(); // None
263 } else if (cond == INode.KEY_PRESENT) {
264 if (sn.hc == hc && equal (sn.k, k, ct)) {
265 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
266 return Option.makeOption(sn.v);
273 return Option.makeOption();// None;
276 if (sn.hc == hc && equal (sn.k, k, ct) && sn.v == cond) {
277 if (GCAS(cn, cn.updatedAt (pos, new SNode<>(k, v, hc), gen), ct)) {
278 return Option.makeOption(sn.v);
284 return Option.makeOption(); // None
289 } else if (cond == null || cond == INode.KEY_ABSENT) {
290 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
291 CNode<K, V> ncnode = rn.insertedAt (pos, flag, new SNode<>(k, v, hc), gen);
292 if (GCAS(cn, ncnode, ct)) {
293 return Option.makeOption(); // None
297 } else if (cond == INode.KEY_PRESENT) {
298 return Option.makeOption(); // None;
301 return Option.makeOption(); // None
303 } else if (m instanceof TNode) {
304 clean(parent, ct, lev - 5);
306 } else if (m instanceof LNode) {
308 LNode<K, V> ln = (LNode<K, V>) m;
310 Option<V> optv = ln.get(k);
311 if (insertln(ln, k, v, ct)) {
316 } else if (cond == INode.KEY_ABSENT) {
317 Option<V> t = ln.get(k);
319 if (insertln(ln, k, v, ct)) {
320 return Option.makeOption();// None
327 } else if (cond == INode.KEY_PRESENT) {
328 Option<V> t = ln.get(k);
330 if (insertln(ln, k, v, ct)) {
339 Option<V> t = ln.get (k);
341 if (((Some<V>) t).get() == cond) {
342 if (insertln(ln, k, v, ct)) {
343 return new Some<>((V) cond);
348 return Option.makeOption ();
354 // throw new RuntimeException ("Should not happen");
358 boolean insertln(final LNode<K, V> ln, final K k, final V v, final TrieMap<K, V> ct) {
359 LNode<K, V> nn = ln.inserted (k, v);
360 return GCAS (ln, nn, ct);
364 * Looks up the value associated with the key.
366 * @return null if no value has been found, RESTART if the operation
367 * wasn't successful, or any other value otherwise
369 Object rec_lookup(final K k, final int hc, final int lev, final INode<K, V> parent, final Gen startgen,
370 final TrieMap<K, V> ct) {
372 MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
374 if (m instanceof CNode) {
376 final CNode<K, V> cn = (CNode<K, V>) m;
377 int idx = (hc >>> lev) & 0x1f;
380 if ((bmp & flag) == 0) {
381 return null; // 1a) bitmap shows no binding
382 } else { // 1b) bitmap contains a value - descend
383 int pos = (bmp == 0xffffffff) ? idx : Integer.bitCount(bmp & (flag - 1));
384 final BasicNode sub = cn.array[pos];
385 if (sub instanceof INode) {
386 INode<K, V> in = (INode<K, V>) sub;
387 if (ct.isReadOnly() || (startgen == ((INodeBase<K, V>) sub).gen)) {
388 return in.rec_lookup(k, hc, lev + 5, this, startgen, ct);
390 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
391 // return rec_lookup (k, hc, lev, parent,
396 return RESTART; // used to be throw RestartException
399 } else if (sub instanceof SNode) {
401 SNode<K, V> sn = (SNode<K, V>) sub;
402 if (((SNode) sub).hc == hc && equal (sn.k, k, ct)) {
409 } else if (m instanceof TNode) {
411 return cleanReadOnly((TNode<K, V>) m, lev, parent, ct, k, hc);
412 } else if (m instanceof LNode) {
414 Option<V> tmp = ((LNode<K, V>) m).get (k);
415 return (tmp != null) ? ((Option<V>) tmp) : null;
418 throw new RuntimeException ("Should not happen");
422 private Object cleanReadOnly(final TNode<K, V> tn, final int lev, final INode<K, V> parent,
423 final TrieMap<K, V> ct, final K k, final int hc) {
424 if (ct.nonReadOnly()) {
425 clean(parent, ct, lev - 5);
426 return RESTART; // used to be throw RestartException
428 if (tn.hc == hc && equal(tn.k, k, ct)) {
437 * Removes the key associated with the given value.
440 * if null, will remove the key irregardless of the value;
441 * otherwise removes only if binding contains that exact key
443 * @return null if not successful, an Option[V] indicating the previous
446 Option<V> rec_remove(final K k, final V v, final int hc, final int lev, final INode<K, V> parent,
447 final Gen startgen, final TrieMap<K, V> ct) {
448 MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
450 if (m instanceof CNode) {
451 CNode<K, V> cn = (CNode<K, V>) m;
452 int idx = (hc >>> lev) & 0x1f;
455 if ((bmp & flag) == 0) {
456 return Option.makeOption();
458 int pos = Integer.bitCount(bmp & (flag - 1));
459 BasicNode sub = cn.array [pos];
460 Option<V> res = null;
461 if (sub instanceof INode) {
462 INode<K, V> in = (INode<K, V>) sub;
463 if (startgen == in.gen) {
464 res = in.rec_remove(k, v, hc, lev + 5, this, startgen, ct);
466 if (GCAS(cn, cn.renewed (startgen, ct), ct)) {
467 res = rec_remove(k, v, hc, lev, parent, startgen, ct);
473 } else if (sub instanceof SNode) {
474 SNode<K, V> sn = (SNode<K, V>) sub;
475 if (sn.hc == hc && equal(sn.k, k, ct) && (v == null || v.equals(sn.v))) {
476 MainNode<K, V> ncn = cn.removedAt(pos, flag, gen).toContracted(lev);
477 if (GCAS(cn, ncn, ct)) {
478 res = Option.makeOption(sn.v);
483 res = Option.makeOption ();
487 if (res instanceof None || (res == null)) {
490 if (parent != null) { // never tomb at root
491 MainNode<K, V> n = GCAS_READ(ct);
492 if (n instanceof TNode) {
493 cleanParent(n, parent, ct, hc, lev, startgen);
500 } else if (m instanceof TNode) {
501 clean(parent, ct, lev - 5);
503 } else if (m instanceof LNode) {
504 LNode<K, V> ln = (LNode<K, V>) m;
506 Option<V> optv = ln.get(k);
507 MainNode<K, V> nn = ln.removed(k, ct);
508 if (GCAS (ln, nn, ct)) {
514 Option<V> tmp = ln.get(k);
515 if (tmp instanceof Some) {
516 Some<V> tmp1 = (Some<V>) tmp;
517 if (tmp1.get () == v) {
518 MainNode<K, V> nn = ln.removed(k, ct);
519 if (GCAS(ln, nn, ct)) {
528 throw new RuntimeException ("Should not happen");
531 void cleanParent(final Object nonlive, final INode<K, V> parent, final TrieMap<K, V> ct, final int hc,
532 final int lev, final Gen startgen) {
534 MainNode<K, V> pm = parent.GCAS_READ(ct);
535 if (pm instanceof CNode) {
536 CNode<K, V> cn = (CNode<K, V>) pm;
537 int idx = (hc >>> (lev - 5)) & 0x1f;
540 if ((bmp & flag) == 0) {
541 // somebody already removed this i-node, we're done
543 int pos = Integer.bitCount(bmp & (flag - 1));
544 BasicNode sub = cn.array[pos];
546 if (nonlive instanceof TNode) {
547 TNode<K, V> tn = (TNode<K, V>) nonlive;
548 MainNode<K, V> ncn = cn.updatedAt(pos, tn.copyUntombed (), gen).toContracted (lev - 5);
549 if (!parent.GCAS(cn, ncn, ct)) {
550 if (ct.readRoot().gen == startgen) {
551 // cleanParent (nonlive, parent, ct, hc,
561 // parent is no longer a cnode, we're done
567 private void clean(final INode<K, V> nd, final TrieMap<K, V> ct, final int lev) {
568 MainNode<K, V> m = nd.GCAS_READ(ct);
569 if (m instanceof CNode) {
570 CNode<K, V> cn = (CNode<K, V>) m;
571 nd.GCAS(cn, cn.toCompressed(ct, lev, gen), ct);
575 boolean isNullInode(final TrieMap<K, V> ct) {
576 return GCAS_READ(ct) == null;
579 int cachedSize(final TrieMap<K, V> ct) {
580 MainNode<K, V> m = GCAS_READ(ct);
581 return m.cachedSize(ct);
584 // /* this is a quiescent method! */
585 // def string(lev: Int) = "%sINode -> %s".format(" " * lev, mainnode
587 // case null => "<null>"
588 // case tn: TNode[_, _] => "TNode(%s, %s, %d, !)".format(tn.k, tn.v,
590 // case cn: CNode[_, _] => cn.string(lev)
591 // case ln: LNode[_, _] => ln.string(lev)
592 // case x => "<elem: %s>".format(x)
596 String string(final int lev) {