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) {
53 final MainNode<K, V> prev = /* READ */ m.READ_PREV();
54 final 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 // Tail recursion: return GCAS_Complete (/* READ */ READ(), ct);
67 m = /* READ */ READ();
71 // Assume that you've read the root from the generation G.
72 // Assume that the snapshot algorithm is correct.
73 // ==> you can only reach nodes in generations <= G.
75 // We know that `ctr.gen` is >= G.
76 // ==> if `ctr.gen` = `gen` then they are both equal to G.
77 // ==> otherwise, we know that either `ctr.gen` > G, `gen` < G,
79 if ((ctr.gen == gen) && ct.nonReadOnly()) {
81 if (m.CAS_PREV(prev, null)) {
85 // Tail recursion: return GCAS_Complete (m, ct);
90 m.CAS_PREV(prev, new FailedNode<>(prev));
92 // Tail recursion: return GCAS_Complete(/* READ */ READ(), ct);
93 m = /* READ */ READ();
97 boolean GCAS(final MainNode<K, V> old, final MainNode<K, V> n, final TrieMap<K, V> ct) {
100 GCAS_Complete (n, ct);
101 return /* READ */ n.READ_PREV() == null;
107 private boolean equal(final K k1, final K k2, final TrieMap<K, V> ct) {
108 return ct.equality().equiv(k1, k2);
111 private INode<K, V> inode(final MainNode<K, V> cn) {
112 return new INode<>(gen, cn);
115 INode<K, V> copyToGen(final Gen ngen, final TrieMap<K, V> ct) {
116 return new INode<>(ngen, GCAS_READ(ct));
120 * Inserts a key value pair, overwriting the old pair if the keys match.
122 * @return true if successful, false otherwise
124 boolean rec_insert(final K k, final V v, final int hc, final int lev, final INode<K, V> parent, final Gen startgen,
125 final TrieMap<K, V> ct) {
127 MainNode<K, V> m = GCAS_READ (ct); // use -Yinline!
129 if (m instanceof CNode) {
130 // 1) a multiway node
131 CNode<K, V> cn = (CNode<K, V>) m;
132 int idx = (hc >>> lev) & 0x1f;
136 int pos = Integer.bitCount(bmp & mask);
137 if ((bmp & flag) != 0) {
139 BasicNode cnAtPos = cn.array[pos];
140 if (cnAtPos instanceof INode) {
141 INode<K, V> in = (INode<K, V>) cnAtPos;
142 if (startgen == in.gen) {
143 return in.rec_insert(k, v, hc, lev + 5, this, startgen, ct);
145 if (GCAS (cn, cn.renewed(startgen, ct), ct)) {
146 // return rec_insert (k, v, hc, lev, parent,
154 } else if (cnAtPos instanceof SNode) {
155 SNode<K, V> sn = (SNode<K, V>) cnAtPos;
156 if (sn.hc == hc && equal(sn.k, k, ct)) {
157 return GCAS (cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct);
159 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
160 MainNode<K, V> nn = rn.updatedAt(pos, inode(CNode.dual(sn, sn.hc, new SNode<>(k, v, hc),
161 hc, lev + 5, gen)), gen);
162 return GCAS (cn, nn, ct);
166 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed (gen, ct);
167 MainNode<K, V> ncnode = rn.insertedAt(pos, flag, new SNode<> (k, v, hc), gen);
168 return GCAS (cn, ncnode, ct);
170 } else if (m instanceof TNode) {
171 clean(parent, ct, lev - 5);
173 } else if (m instanceof LNode) {
174 LNode<K, V> ln = (LNode<K, V>) m;
175 MainNode<K, V> nn = ln.inserted(k, v);
176 return GCAS(ln, nn, ct);
179 throw new RuntimeException ("Should not happen");
184 * Inserts a new key value pair, given that a specific condition is met.
187 * null - don't care if the key was there
188 * KEY_ABSENT - key wasn't there
189 * KEY_PRESENT - key was there
190 * other value `v` - key must be bound to `v`
191 * @return null if unsuccessful, Option[V] otherwise (indicating
192 * previous value bound to the key)
194 Option<V> rec_insertif(final K k, final V v, final int hc, final Object cond, final int lev,
195 final INode<K, V> parent, final Gen startgen, final TrieMap<K, V> ct) {
197 MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
199 if (m instanceof CNode) {
200 // 1) a multiway node
201 CNode<K, V> cn = (CNode<K, V>) m;
202 int idx = (hc >>> lev) & 0x1f;
206 int pos = Integer.bitCount(bmp & mask);
208 if ((bmp & flag) != 0) {
210 BasicNode cnAtPos = cn.array [pos];
211 if (cnAtPos instanceof INode) {
212 INode<K, V> in = (INode<K, V>) cnAtPos;
213 if (startgen == in.gen) {
214 return in.rec_insertif(k, v, hc, cond, lev + 5, this, startgen, ct);
216 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
217 // return rec_insertif (k, v, hc, cond, lev,
218 // parent, startgen, ct);
225 } else if (cnAtPos instanceof SNode) {
226 SNode<K, V> sn = (SNode<K, V>) cnAtPos;
228 if (sn.hc == hc && equal(sn.k, k, ct)) {
229 if (GCAS(cn, cn.updatedAt(pos, new SNode<> (k, v, hc), gen), ct)) {
230 return Option.makeOption(sn.v);
235 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
236 MainNode<K, V> nn = rn.updatedAt(pos, inode (CNode.dual(sn, sn.hc,
237 new SNode<>(k, v, hc), hc, lev + 5, gen)), gen);
238 if (GCAS(cn, nn, ct)) {
239 return Option.makeOption(); // None;
245 } else if (cond == INode.KEY_ABSENT) {
246 if (sn.hc == hc && equal (sn.k, k, ct)) {
247 return Option.makeOption(sn.v);
249 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed (gen, ct);
250 MainNode<K, V> nn = rn.updatedAt(pos, inode (CNode.dual(sn, sn.hc,
251 new SNode<>(k, v, hc), hc, lev + 5, gen)), gen);
252 if (GCAS(cn, nn, ct)) {
253 return Option.makeOption(); // None
258 } else if (cond == INode.KEY_PRESENT) {
259 if (sn.hc == hc && equal (sn.k, k, ct)) {
260 if (GCAS(cn, cn.updatedAt(pos, new SNode<>(k, v, hc), gen), ct)) {
261 return Option.makeOption(sn.v);
268 return Option.makeOption();// None;
271 if (sn.hc == hc && equal (sn.k, k, ct) && sn.v == cond) {
272 if (GCAS(cn, cn.updatedAt (pos, new SNode<>(k, v, hc), gen), ct)) {
273 return Option.makeOption(sn.v);
279 return Option.makeOption(); // None
284 } else if (cond == null || cond == INode.KEY_ABSENT) {
285 CNode<K, V> rn = (cn.gen == gen) ? cn : cn.renewed(gen, ct);
286 CNode<K, V> ncnode = rn.insertedAt (pos, flag, new SNode<>(k, v, hc), gen);
287 if (GCAS(cn, ncnode, ct)) {
288 return Option.makeOption(); // None
292 } else if (cond == INode.KEY_PRESENT) {
293 return Option.makeOption(); // None;
296 return Option.makeOption(); // None
298 } else if (m instanceof TNode) {
299 clean(parent, ct, lev - 5);
301 } else if (m instanceof LNode) {
303 LNode<K, V> ln = (LNode<K, V>) m;
305 Option<V> optv = ln.get(k);
306 if (insertln(ln, k, v, ct)) {
311 } else if (cond == INode.KEY_ABSENT) {
312 Option<V> t = ln.get(k);
314 if (insertln(ln, k, v, ct)) {
315 return Option.makeOption();// None
322 } else if (cond == INode.KEY_PRESENT) {
323 Option<V> t = ln.get(k);
325 if (insertln(ln, k, v, ct)) {
334 Option<V> t = ln.get (k);
336 if (((Some<V>) t).get() == cond) {
337 if (insertln(ln, k, v, ct)) {
338 return new Some<>((V) cond);
343 return Option.makeOption ();
349 // throw new RuntimeException ("Should not happen");
353 boolean insertln(final LNode<K, V> ln, final K k, final V v, final TrieMap<K, V> ct) {
354 LNode<K, V> nn = ln.inserted (k, v);
355 return GCAS (ln, nn, ct);
359 * Looks up the value associated with the key.
361 * @return null if no value has been found, RESTART if the operation
362 * wasn't successful, or any other value otherwise
364 Object rec_lookup(final K k, final int hc, final int lev, final INode<K, V> parent, final Gen startgen,
365 final TrieMap<K, V> ct) {
367 MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
369 if (m instanceof CNode) {
371 final CNode<K, V> cn = (CNode<K, V>) m;
372 int idx = (hc >>> lev) & 0x1f;
375 if ((bmp & flag) == 0) {
376 return null; // 1a) bitmap shows no binding
377 } else { // 1b) bitmap contains a value - descend
378 int pos = (bmp == 0xffffffff) ? idx : Integer.bitCount(bmp & (flag - 1));
379 final BasicNode sub = cn.array[pos];
380 if (sub instanceof INode) {
381 INode<K, V> in = (INode<K, V>) sub;
382 if (ct.isReadOnly() || (startgen == ((INodeBase<K, V>) sub).gen)) {
383 return in.rec_lookup(k, hc, lev + 5, this, startgen, ct);
385 if (GCAS(cn, cn.renewed(startgen, ct), ct)) {
386 // return rec_lookup (k, hc, lev, parent,
391 return RESTART; // used to be throw RestartException
394 } else if (sub instanceof SNode) {
396 SNode<K, V> sn = (SNode<K, V>) sub;
397 if (((SNode) sub).hc == hc && equal (sn.k, k, ct)) {
404 } else if (m instanceof TNode) {
406 return cleanReadOnly((TNode<K, V>) m, lev, parent, ct, k, hc);
407 } else if (m instanceof LNode) {
409 Option<V> tmp = ((LNode<K, V>) m).get (k);
410 return (tmp != null) ? ((Option<V>) tmp) : null;
413 throw new RuntimeException ("Should not happen");
417 private Object cleanReadOnly(final TNode<K, V> tn, final int lev, final INode<K, V> parent,
418 final TrieMap<K, V> ct, final K k, final int hc) {
419 if (ct.nonReadOnly()) {
420 clean(parent, ct, lev - 5);
421 return RESTART; // used to be throw RestartException
423 if (tn.hc == hc && equal(tn.k, k, ct)) {
432 * Removes the key associated with the given value.
435 * if null, will remove the key irregardless 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 Option<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 MainNode<K, V> m = GCAS_READ(ct); // use -Yinline!
445 if (m instanceof CNode) {
446 CNode<K, V> cn = (CNode<K, V>) m;
447 int idx = (hc >>> lev) & 0x1f;
450 if ((bmp & flag) == 0) {
451 return Option.makeOption();
453 int pos = Integer.bitCount(bmp & (flag - 1));
454 BasicNode sub = cn.array [pos];
455 Option<V> res = null;
456 if (sub instanceof INode) {
457 INode<K, V> in = (INode<K, V>) sub;
458 if (startgen == in.gen) {
459 res = in.rec_remove(k, v, hc, lev + 5, this, startgen, ct);
461 if (GCAS(cn, cn.renewed (startgen, ct), ct)) {
462 res = rec_remove(k, v, hc, lev, parent, startgen, ct);
468 } else if (sub instanceof SNode) {
469 SNode<K, V> sn = (SNode<K, V>) sub;
470 if (sn.hc == hc && equal(sn.k, k, ct) && (v == null || v.equals(sn.v))) {
471 MainNode<K, V> ncn = cn.removedAt(pos, flag, gen).toContracted(lev);
472 if (GCAS(cn, ncn, ct)) {
473 res = Option.makeOption(sn.v);
478 res = Option.makeOption ();
482 if (res instanceof None || (res == null)) {
485 if (parent != null) { // never tomb at root
486 MainNode<K, V> n = GCAS_READ(ct);
487 if (n instanceof TNode) {
488 cleanParent(n, parent, ct, hc, lev, startgen);
495 } else if (m instanceof TNode) {
496 clean(parent, ct, lev - 5);
498 } else if (m instanceof LNode) {
499 LNode<K, V> ln = (LNode<K, V>) m;
501 Option<V> optv = ln.get(k);
502 MainNode<K, V> nn = ln.removed(k, ct);
503 if (GCAS (ln, nn, ct)) {
509 Option<V> tmp = ln.get(k);
510 if (tmp instanceof Some) {
511 Some<V> tmp1 = (Some<V>) tmp;
512 if (tmp1.get () == v) {
513 MainNode<K, V> nn = ln.removed(k, ct);
514 if (GCAS(ln, nn, ct)) {
523 throw new RuntimeException ("Should not happen");
526 void cleanParent(final Object nonlive, final INode<K, V> parent, final TrieMap<K, V> ct, final int hc,
527 final int lev, final Gen startgen) {
529 MainNode<K, V> pm = parent.GCAS_READ(ct);
530 if (pm instanceof CNode) {
531 CNode<K, V> cn = (CNode<K, V>) pm;
532 int idx = (hc >>> (lev - 5)) & 0x1f;
535 if ((bmp & flag) == 0) {
536 // somebody already removed this i-node, we're done
538 int pos = Integer.bitCount(bmp & (flag - 1));
539 BasicNode sub = cn.array[pos];
541 if (nonlive instanceof TNode) {
542 TNode<K, V> tn = (TNode<K, V>) nonlive;
543 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 // cleanParent (nonlive, parent, ct, hc,
556 // parent is no longer a cnode, we're done
562 private void clean(final INode<K, V> nd, final TrieMap<K, V> ct, final int lev) {
563 MainNode<K, V> m = nd.GCAS_READ(ct);
564 if (m instanceof CNode) {
565 CNode<K, V> cn = (CNode<K, V>) m;
566 nd.GCAS(cn, cn.toCompressed(ct, lev, gen), ct);
570 boolean isNullInode(final TrieMap<K, V> ct) {
571 return GCAS_READ(ct) == null;
574 int cachedSize(final TrieMap<K, V> ct) {
575 MainNode<K, V> m = GCAS_READ(ct);
576 return m.cachedSize(ct);
579 // /* this is a quiescent method! */
580 // def string(lev: Int) = "%sINode -> %s".format(" " * lev, mainnode
582 // case null => "<null>"
583 // case tn: TNode[_, _] => "TNode(%s, %s, %d, !)".format(tn.k, tn.v,
585 // case cn: CNode[_, _] => cn.string(lev)
586 // case ln: LNode[_, _] => ln.string(lev)
587 // case x => "<elem: %s>".format(x)
591 String string(final int lev) {