mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-234 fixing CHMUseCaseTest
This commit is contained in:
@@ -203,12 +203,11 @@ public interface ChronicleMap<K, V> extends ConcurrentMap<K, V>, ChronicleHash {
|
||||
* Apply a mutator to the value for a key and return a result. A write lock is assumed. <p> If
|
||||
* there is no entry for this key null will be returned
|
||||
*
|
||||
* @param <R> return type.
|
||||
* @param key to apply the mapping to
|
||||
* @param mutator to alter the value and calculate a result
|
||||
* @return the result of the function.
|
||||
*/
|
||||
<R> R putWith(K key, @NotNull Mutator<? super V, R> mutator);
|
||||
V putWith(K key, @NotNull Mutator<V> mutator);
|
||||
|
||||
/**
|
||||
* exports all the entries to a {@link java.io.File} storing them in JSON format, an attempt is
|
||||
|
||||
@@ -8,15 +8,15 @@ import java.io.Serializable;
|
||||
* <p>This is not functional as it can alter an argument.
|
||||
*
|
||||
* @param <T> the type of the mutable input
|
||||
* @param <R> the type of the result of the mutator
|
||||
|
||||
*/
|
||||
|
||||
public interface Mutator<T, R> extends Serializable {
|
||||
public interface Mutator<T> extends Serializable {
|
||||
/**
|
||||
* Applies this mutator to the given mutable argument.
|
||||
*
|
||||
* @param t the mutator argument
|
||||
* @return the result
|
||||
*/
|
||||
R update(T t);
|
||||
T update(T t);
|
||||
}
|
||||
|
||||
@@ -763,7 +763,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
|
||||
// notify the context that the entry was created
|
||||
if (lock instanceof WriteLocked)
|
||||
((WriteLocked) lock).wasPresent(false);
|
||||
((WriteLocked) lock).created(false);
|
||||
|
||||
return result;
|
||||
|
||||
|
||||
@@ -490,7 +490,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <R> R putWith(@Nullable K key, @NotNull Mutator<? super V, R> mutator) {
|
||||
public V putWith(@Nullable K key, @NotNull Mutator<V> mutator) {
|
||||
if (key == null)
|
||||
throw keyNotNullNPE();
|
||||
return fetchObject(UPDATE_FOR_KEY, key, mutator);
|
||||
|
||||
@@ -1440,7 +1440,7 @@ class StatelessServerConnector<K, V> {
|
||||
@Nullable
|
||||
public Work updateForKey(@NotNull ByteBufferBytes reader, @NotNull Bytes writer, long sizeLocation) {
|
||||
final K key = keyReaderWithSize.read(reader, null);
|
||||
final Mutator<V, ?> mutator = (Mutator<V, ?>) reader.readObject();
|
||||
final Mutator<V> mutator = (Mutator<V>) reader.readObject();
|
||||
try {
|
||||
Object result = map.putWith(key, mutator);
|
||||
writer.writeObject(result);
|
||||
|
||||
@@ -559,51 +559,29 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> R putWith(K key, @NotNull Mutator<? super V, R> mutator) {
|
||||
public V putWith(K key, @NotNull Mutator<V> mutator) {
|
||||
|
||||
|
||||
if (!(CharSequence.class.isAssignableFrom(vClass))) {
|
||||
final V using = (vClass.equals(CharSequence.class)) ? (V) new StringBuilder() : null;
|
||||
|
||||
try (WriteContext<K, V> entry = lookupUsing(key, null,
|
||||
false, false, LockType.WRITE_LOCK)) {
|
||||
try (WriteContext<K, V> entry = lookupUsing(key, using,
|
||||
false, false, LockType.WRITE_LOCK)) {
|
||||
|
||||
if (entry.value() == null)
|
||||
if (CharSequence.class.isAssignableFrom(vClass)) {
|
||||
CharSequence value = (CharSequence) entry.value();
|
||||
if (entry.value() == null || value.length() == 0)
|
||||
return null;
|
||||
|
||||
if (!entry.wasPresent())
|
||||
return null;
|
||||
|
||||
R result = mutator.update(entry.value());
|
||||
((MutableLockedEntry) entry).value(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// special case - spoke to peter about this and he wants to handle strings as a
|
||||
// special case
|
||||
CharSequence using;
|
||||
if (vClass.equals(CharSequence.class))
|
||||
using = new StringBuilder();
|
||||
else
|
||||
using = null;
|
||||
|
||||
try (WriteContext<K, V> entry = lookupUsing(key, (V) using,
|
||||
false, using == null, LockType.WRITE_LOCK)) {
|
||||
|
||||
CharSequence value = (CharSequence) entry.value();
|
||||
if (entry.value() == null || value.length() == 0)
|
||||
if (entry.value() == null)
|
||||
return null;
|
||||
|
||||
final R result = mutator.update(entry.value());
|
||||
|
||||
if (value instanceof StringBuilder) {
|
||||
StringBuilder using1 = (StringBuilder) using;
|
||||
using1.setLength(0);
|
||||
using1.append((CharSequence) result);
|
||||
V result = mutator.update(entry.value());
|
||||
if (entry.value() instanceof StringBuilder) {
|
||||
((StringBuilder) using).setLength(0);
|
||||
((StringBuilder) using).append((CharSequence) result);
|
||||
} else
|
||||
((MutableLockedEntry) entry).value(result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1699,7 +1677,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
|
||||
// notify the context that the entry was created
|
||||
if (lock instanceof WriteLocked)
|
||||
((WriteLocked) lock).wasPresent(true);
|
||||
((WriteLocked) lock).created(true);
|
||||
|
||||
return result;
|
||||
} finally {
|
||||
@@ -2707,7 +2685,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
extends MutableLockedEntry<K, KI, MKI, V, VI, MVI>
|
||||
implements WriteContext<K, V> {
|
||||
|
||||
private boolean wasPresent = true;
|
||||
private boolean created = false;
|
||||
|
||||
WriteLocked(SegmentState segmentState) {
|
||||
super(segmentState);
|
||||
@@ -2716,12 +2694,12 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
/**
|
||||
* @return if the entry was created
|
||||
*/
|
||||
public boolean wasPresent() {
|
||||
return wasPresent;
|
||||
public boolean created() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void wasPresent(boolean wasPresent) {
|
||||
this.wasPresent = wasPresent;
|
||||
public void created(boolean created) {
|
||||
this.created = created;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,6 @@ public interface WriteContext<K, V> extends Context<K, V> {
|
||||
/**
|
||||
* @returns true if the entry was previously present
|
||||
*/
|
||||
boolean wasPresent();
|
||||
boolean created();
|
||||
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ public class CHMUseCasesTest {
|
||||
map.put("Hello", "World");
|
||||
|
||||
|
||||
map.putWith("Hello", new Mutator<String, String>() {
|
||||
map.putWith("Hello", new Mutator<String>() {
|
||||
@Override
|
||||
public String update(String s) {
|
||||
return "New " + s;
|
||||
@@ -126,7 +126,7 @@ public class CHMUseCasesTest {
|
||||
}));
|
||||
|
||||
assertEquals("New World !!", map.putWith(new StringBuilder("Hello"), new
|
||||
Mutator<CharSequence, CharSequence>() {
|
||||
Mutator<CharSequence>() {
|
||||
@Override
|
||||
public CharSequence update(CharSequence s) {
|
||||
((StringBuilder) s).append(" !!");
|
||||
@@ -137,7 +137,7 @@ public class CHMUseCasesTest {
|
||||
assertEquals("New World !!", map.get("Hello").toString());
|
||||
|
||||
assertEquals(null, map.putWith(new StringBuilder("no-key"), new
|
||||
Mutator<CharSequence, CharSequence>() {
|
||||
Mutator<CharSequence>() {
|
||||
@Override
|
||||
public CharSequence update(CharSequence s) {
|
||||
((StringBuilder) s).append("!!");
|
||||
@@ -283,7 +283,7 @@ public class CHMUseCasesTest {
|
||||
}));
|
||||
|
||||
try {
|
||||
map.putWith(1, new Mutator<Integer, Integer>() {
|
||||
map.putWith(1, new Mutator<Integer>() {
|
||||
@Override
|
||||
public Integer update(Integer s) {
|
||||
return s + 1;
|
||||
@@ -326,7 +326,7 @@ public class CHMUseCasesTest {
|
||||
}));
|
||||
|
||||
try {
|
||||
map.putWith(1L, new Mutator<Long, Long>() {
|
||||
map.putWith(1L, new Mutator<Long>() {
|
||||
@Override
|
||||
public Long update(Long s) {
|
||||
return s + 1;
|
||||
@@ -369,7 +369,7 @@ public class CHMUseCasesTest {
|
||||
}));
|
||||
|
||||
try {
|
||||
map.putWith(1.0, new Mutator<Double, Double>() {
|
||||
map.putWith(1.0, new Mutator<Double>() {
|
||||
@Override
|
||||
public Double update(Double s) {
|
||||
return s + 1;
|
||||
@@ -415,7 +415,7 @@ public class CHMUseCasesTest {
|
||||
}
|
||||
}));
|
||||
|
||||
assertTrue(Arrays.equals(new byte[]{12, 10}, map.putWith(key1, new Mutator<byte[], byte[]>() {
|
||||
assertTrue(Arrays.equals(new byte[]{12, 10}, map.putWith(key1, new Mutator<byte[]>() {
|
||||
@Override
|
||||
public byte[] update(byte[] s) {
|
||||
s[0]++;
|
||||
@@ -461,7 +461,7 @@ public class CHMUseCasesTest {
|
||||
assertBBEquals(ByteBuffer.wrap(new byte[]{11, 11}), map.mapForKey(key1, function));
|
||||
assertEquals(null, map.mapForKey(key2, function));
|
||||
|
||||
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator<ByteBuffer, ByteBuffer>() {
|
||||
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator<ByteBuffer>() {
|
||||
@Override
|
||||
public ByteBuffer update(ByteBuffer s) {
|
||||
s.put(0, (byte) (s.get(0) + 1));
|
||||
@@ -556,7 +556,7 @@ public class CHMUseCasesTest {
|
||||
assertBBEquals(ByteBuffer.wrap(new byte[]{11, 11}), map.mapForKey(key1, function));
|
||||
assertEquals(null, map.mapForKey(key2, function));
|
||||
|
||||
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator<ByteBuffer, ByteBuffer>() {
|
||||
assertBBEquals(ByteBuffer.wrap(new byte[]{12, 10}), map.putWith(key1, new Mutator<ByteBuffer>() {
|
||||
@Override
|
||||
public ByteBuffer update(ByteBuffer s) {
|
||||
s.put(0, (byte) (s.get(0) + 1));
|
||||
|
||||
@@ -223,7 +223,7 @@ public class ReplicationCheckingMap<K, V> implements ChronicleMap<K, V> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> R putWith(K key, @NotNull Mutator<? super V, R> mutator) {
|
||||
public V putWith(K key, @NotNull Mutator<V> mutator) {
|
||||
return map1.putWith(key, mutator);
|
||||
}
|
||||
|
||||
|
||||
@@ -175,7 +175,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> R putWith(K key, @NotNull Mutator<? super V, R> mutator) {
|
||||
public V putWith(K key, @NotNull Mutator<V> mutator) {
|
||||
return d.putWith(key, mutator);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user