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 com.google.common.base.Preconditions.checkNotNull;
19 import static com.google.common.base.Preconditions.checkState;
20 import static org.opendaylight.yangtools.triemap.PresencePredicate.ABSENT;
21 import static org.opendaylight.yangtools.triemap.PresencePredicate.PRESENT;
23 import com.google.common.annotations.Beta;
24 import com.google.common.base.Verify;
25 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
26 import java.util.Optional;
27 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
32 * @author Robert Varga
34 * @param <K> the type of keys maintained by this map
35 * @param <V> the type of mapped values
38 final class MutableTrieMap<K, V> extends TrieMap<K, V> {
39 private static final long serialVersionUID = 1L;
41 @SuppressWarnings("rawtypes")
42 private static final AtomicReferenceFieldUpdater<MutableTrieMap, Object> ROOT_UPDATER =
43 AtomicReferenceFieldUpdater.newUpdater(MutableTrieMap.class, Object.class, "root");
45 private volatile Object root;
47 MutableTrieMap(final Equivalence<? super K> equiv) {
48 this(equiv, newRootNode());
51 MutableTrieMap(final Equivalence<? super K> equiv, final INode<K, V> root) {
53 this.root = checkNotNull(root);
60 final INode<K, V> r = RDCSS_READ_ROOT();
61 success = RDCSS_ROOT(r, r.gcasRead(this), newRootNode());
66 public V put(final K key, final V value) {
67 final K k = checkNotNull(key);
68 return toNullable(insertifhc(k, computeHash(k), checkNotNull(value), null));
72 public V putIfAbsent(final K key, final V value) {
73 final K k = checkNotNull(key);
74 return toNullable(insertifhc(k, computeHash(k), checkNotNull(value), ABSENT));
78 public V remove(final Object key) {
79 @SuppressWarnings("unchecked")
80 final K k = (K) checkNotNull(key);
81 return toNullable(removehc(k, null, computeHash(k)));
84 @SuppressFBWarnings(value = "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE",
85 justification = "API contract allows null value, but we are not")
87 public boolean remove(final Object key, final Object value) {
88 @SuppressWarnings("unchecked")
89 final K k = (K) checkNotNull(key);
90 return removehc(k, checkNotNull(value), computeHash(k)).isPresent();
94 public boolean replace(final K key, final V oldValue, final V newValue) {
95 final K k = checkNotNull(key);
96 return insertifhc(k, computeHash(k), checkNotNull(newValue), checkNotNull(oldValue)).isPresent();
100 public V replace(final K key, final V value) {
101 final K k = checkNotNull(key);
102 return toNullable(insertifhc(k, computeHash(k), checkNotNull(value), PRESENT));
107 return immutableSnapshot().size();
111 public ImmutableTrieMap<K, V> immutableSnapshot() {
113 final INode<K, V> r = RDCSS_READ_ROOT();
114 final MainNode<K, V> expmain = r.gcasRead(this);
115 if (RDCSS_ROOT(r, expmain, r.copyToGen (new Gen(), this))) {
116 return new ImmutableTrieMap<>(r, equiv());
119 // Tail recursion: return readOnlySnapshot();
124 public MutableTrieMap<K, V> mutableSnapshot() {
126 final INode<K, V> r = RDCSS_READ_ROOT();
127 final MainNode<K, V> expmain = r.gcasRead(this);
128 if (RDCSS_ROOT(r, expmain, r.copyToGen(new Gen(), this))) {
129 return new MutableTrieMap<>(equiv(), r.copyToGen(new Gen(), this));
132 // Tail recursion: return snapshot();
137 boolean isReadOnly() {
142 INode<K, V> RDCSS_READ_ROOT(final boolean abort) {
143 final Object r = /* READ */ root;
144 if (r instanceof INode) {
145 return (INode<K, V>) r;
148 checkState(r instanceof RDCSS_Descriptor, "Unhandled root %s", r);
149 return RDCSS_Complete(abort);
152 void add(final K key, final V value) {
153 final K k = checkNotNull(key);
154 inserthc(k, computeHash(k), checkNotNull(value));
157 private static <K,V> INode<K, V> newRootNode() {
158 final Gen gen = new Gen();
159 return new INode<>(gen, new CNode<>(gen));
162 private void inserthc(final K k, final int hc, final V v) {
163 // TODO: this is called from serialization only, which means we should not be observing any races,
164 // hence we should not need to pass down the entire tree, just equality (I think).
165 final boolean success = RDCSS_READ_ROOT().rec_insert(k, v, hc, 0, null, this);
166 Verify.verify(success, "Concurrent modification during serialization of map %s", this);
169 private Optional<V> insertifhc(final K k, final int hc, final V v, final Object cond) {
172 // Keep looping as long as we do not get a reply
173 res = RDCSS_READ_ROOT().rec_insertif(k, v, hc, cond, 0, null, this);
174 } while (res == null);
179 private Optional<V> removehc(final K k, final Object cond, final int hc) {
182 // Keep looping as long as we do not get a reply
183 res = RDCSS_READ_ROOT().rec_remove(k, cond, hc, 0, null, this);
184 } while (res == null);
189 private boolean CAS_ROOT(final Object ov, final Object nv) {
190 return ROOT_UPDATER.compareAndSet(this, ov, nv);
193 private boolean RDCSS_ROOT(final INode<K, V> ov, final MainNode<K, V> expectedmain, final INode<K, V> nv) {
194 final RDCSS_Descriptor<K, V> desc = new RDCSS_Descriptor<> (ov, expectedmain, nv);
195 if (CAS_ROOT(ov, desc)) {
196 RDCSS_Complete(false);
197 return /* READ */desc.committed;
203 private INode<K, V> RDCSS_Complete(final boolean abort) {
205 final Object r = /* READ */ root;
206 if (r instanceof INode) {
207 return (INode<K, V>) r;
210 checkState(r instanceof RDCSS_Descriptor, "Unhandled root %s", r);
211 @SuppressWarnings("unchecked")
212 final RDCSS_Descriptor<K, V> desc = (RDCSS_Descriptor<K, V>) r;
213 final INode<K, V> ov = desc.old;
214 final MainNode<K, V> exp = desc.expectedmain;
215 final INode<K, V> nv = desc.nv;
218 if (CAS_ROOT(desc, ov)) {
222 // Tail recursion: return RDCSS_Complete(abort);
226 final MainNode<K, V> oldmain = ov.gcasRead(this);
227 if (oldmain == exp) {
228 if (CAS_ROOT(desc, nv)) {
229 desc.committed = true;
233 // Tail recursion: return RDCSS_Complete(abort);
237 if (CAS_ROOT(desc, ov)) {
241 // Tail recursion: return RDCSS_Complete(abort);
245 private static final class RDCSS_Descriptor<K, V> {
246 final INode<K, V> old;
247 final MainNode<K, V> expectedmain;
248 final INode<K, V> nv;
250 volatile boolean committed = false;
252 RDCSS_Descriptor (final INode<K, V> old, final MainNode<K, V> expectedmain, final INode<K, V> nv) {
254 this.expectedmain = expectedmain;