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();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user