Skip to content

Commit 7c7926d

Browse files
committed
Clean up MutableTrieMap method layout
We have a few improvements that we can make: - rename insertifhc() to insertIf() and move it below replace() - rename removehc() to removeIf() and move it below insertIf() - inline add() into SerializationProxy - compute hc in insertIf()/removeIf() Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
1 parent 31e130d commit 7c7926d

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

triemap/src/main/java/tech/pantheon/triemap/MutableTrieMap.java

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,20 @@ public void clear() {
6969
@Override
7070
public V put(final K key, final V value) {
7171
final var k = requireNonNull(key);
72-
return insertifhc(k, computeHash(k), requireNonNull(value), null).orNull();
72+
return insertIf(k, requireNonNull(value), null).orNull();
7373
}
7474

7575
@Override
7676
public V putIfAbsent(final K key, final V value) {
7777
final var k = requireNonNull(key);
78-
return insertifhc(k, computeHash(k), requireNonNull(value), ABSENT).orNull();
78+
return insertIf(k, requireNonNull(value), ABSENT).orNull();
7979
}
8080

8181
@Override
8282
public V remove(final Object key) {
8383
@SuppressWarnings("unchecked")
8484
final var k = (K) requireNonNull(key);
85-
return removehc(k, null, computeHash(k)).orNull();
85+
return removeIf(k, null).orNull();
8686
}
8787

8888
@SuppressFBWarnings(value = "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE",
@@ -91,19 +91,45 @@ public V remove(final Object key) {
9191
public boolean remove(final Object key, final Object value) {
9292
@SuppressWarnings("unchecked")
9393
final var k = (K) requireNonNull(key);
94-
return removehc(k, requireNonNull(value), computeHash(k)).isPresent();
94+
return removeIf(k, requireNonNull(value)).isPresent();
9595
}
9696

9797
@Override
9898
public boolean replace(final K key, final V oldValue, final V newValue) {
9999
final var k = requireNonNull(key);
100-
return insertifhc(k, computeHash(k), requireNonNull(newValue), requireNonNull(oldValue)).isPresent();
100+
return insertIf(k, requireNonNull(newValue), requireNonNull(oldValue)).isPresent();
101101
}
102102

103103
@Override
104104
public V replace(final K key, final V value) {
105105
final var k = requireNonNull(key);
106-
return insertifhc(k, computeHash(k), requireNonNull(value), PRESENT).orNull();
106+
return insertIf(k, requireNonNull(value), PRESENT).orNull();
107+
}
108+
109+
private @NonNull Result<V> insertIf(final K key, final V value, final Object cond) {
110+
final int hc = computeHash(key);
111+
112+
Result<V> res;
113+
do {
114+
// Keep looping as long as we do not get a reply
115+
final var r = readRoot();
116+
res = r.insertIf(this, r.gen, hc, key, value, cond, 0, null);
117+
} while (res == null);
118+
119+
return res;
120+
}
121+
122+
private @NonNull Result<V> removeIf(final K key, final Object cond) {
123+
final int hc = computeHash(key);
124+
125+
Result<V> res;
126+
do {
127+
// Keep looping as long as we do not get a reply
128+
final var r = readRoot();
129+
res = r.remove(this, r.gen, hc, key, cond, 0, null);
130+
} while (res == null);
131+
132+
return res;
107133
}
108134

109135
@Override
@@ -164,47 +190,11 @@ INode<K, V> rdcssReadRoot(final boolean abort) {
164190
}
165191
}
166192

167-
void add(final K key, final V value) {
168-
final K k = requireNonNull(key);
169-
inserthc(k, computeHash(k), requireNonNull(value));
170-
}
171-
172193
private static <K, V> INode<K, V> newRootNode() {
173194
final var gen = new Gen();
174195
return new INode<>(gen, new CNode<>(gen));
175196
}
176197

177-
private void inserthc(final K key, final int hc, final V value) {
178-
// TODO: this is called from serialization only, which means we should not be observing any races,
179-
// hence we should not need to pass down the entire tree, just equality (I think).
180-
final var r = readRoot();
181-
if (!r.insert(this, r.gen, hc, key, value, 0, null)) {
182-
throw new VerifyException("Concurrent modification during serialization of map " + this);
183-
}
184-
}
185-
186-
private @NonNull Result<V> insertifhc(final K key, final int hc, final V value, final Object cond) {
187-
Result<V> res;
188-
do {
189-
// Keep looping as long as we do not get a reply
190-
final var r = readRoot();
191-
res = r.insertIf(this, r.gen, hc, key, value, cond, 0, null);
192-
} while (res == null);
193-
194-
return res;
195-
}
196-
197-
private @NonNull Result<V> removehc(final K key, final Object cond, final int hc) {
198-
Result<V> res;
199-
do {
200-
// Keep looping as long as we do not get a reply
201-
final var r = readRoot();
202-
res = r.remove(this, r.gen, hc, key, cond, 0, null);
203-
} while (res == null);
204-
205-
return res;
206-
}
207-
208198
private Root<K, V> casRoot(final Root<K, V> prev, final Root<K, V> next) {
209199
return (Root<K, V>) VH.compareAndExchange(this, prev, next);
210200
}

triemap/src/main/java/tech/pantheon/triemap/SerializationProxy.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ public void readExternal(final ObjectInput in) throws IOException, ClassNotFound
7575
}
7676

7777
for (int i = 0; i < size; ++i) {
78-
tmp.add(in.readObject(), in.readObject());
78+
final var k = requireNonNull(in.readObject());
79+
final var v = requireNonNull(in.readObject());
80+
final var r = tmp.readRoot();
81+
if (!r.insert(tmp, r.gen, TrieMap.computeHash(k), k, v, 0, null)) {
82+
throw new VerifyException("Concurrent modification during serialization");
83+
}
7984
}
8085

8186
map = in.readBoolean() ? tmp.immutableSnapshot() : tmp;

0 commit comments

Comments
 (0)