added net.openhft.chronicle.map.StatelessChronicleMap.getUsing

This commit is contained in:
Rob Austin
2014-12-26 17:59:52 +00:00
parent 62535824fb
commit af44d25788
4 changed files with 50 additions and 17 deletions
@@ -27,6 +27,7 @@ final class ReaderWithSize<T> {
private final SizeMarshaller sizeMarshaller;
private final BytesReader<T> originalReader;
private final Provider<BytesReader<T>> readerProvider;
ReaderWithSize(SerializationBuilder<T> serializationBuilder) {
sizeMarshaller = serializationBuilder.sizeMarshaller();
originalReader = serializationBuilder.reader();
@@ -37,17 +38,17 @@ final class ReaderWithSize<T> {
return readerProvider.getCopies(copies);
}
public T read(Bytes in, @Nullable ThreadLocalCopies copies) {
public T read(Bytes in, @Nullable ThreadLocalCopies copies, @Nullable T using) {
long size = sizeMarshaller.readSize(in);
copies = readerProvider.getCopies(copies);
BytesReader<T> reader = readerProvider.get(copies, originalReader);
return reader.read(in, size);
return reader.read(in, size, using);
}
public T readNullable(Bytes in, @Nullable ThreadLocalCopies copies) {
public T readNullable(Bytes in, @Nullable ThreadLocalCopies copies, T using) {
if (in.readBoolean())
return null;
return read(in, copies);
return read(in, copies, using);
}
public BytesReader<T> readerForLoop(@Nullable ThreadLocalCopies copies) {
@@ -492,10 +492,31 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
return fetchObject(vClass, GET, (K) key);
}
@NotNull
@Nullable
public V getUsing(K key, V usingValue) {
throw new UnsupportedOperationException("getUsing() is not supported for stateless " +
"clients");
final long startTime = System.currentTimeMillis();
ThreadLocalCopies copies;
long transactionId;
outBytesLock.lock();
try {
final long sizeLocation = writeEventAnSkip(GET);
copies = writeKey((K) key);
transactionId = send(sizeLocation, startTime);
} finally {
outBytesLock.unlock();
}
if (eventReturnsNull(GET))
return null;
return (V) readValue(transactionId, startTime, copies, usingValue);
}
@NotNull
@@ -1403,7 +1424,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
}
private V readValue(long transactionId, long startTime, ThreadLocalCopies copies) {
private V readValue(long transactionId, long startTime, ThreadLocalCopies copies, V usingValue) {
assert !outBytesLock.isHeldByCurrentThread();
long timeoutTime = startTime + this.config.timeoutMs();
@@ -1411,7 +1432,12 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
// receive
inBytesLock.lock();
try {
return readValue(copies, blockingFetchReadOnly(timeoutTime, transactionId));
if (usingValue != null)
return readValue(copies, blockingFetchReadOnly(timeoutTime, transactionId), usingValue);
else
return readValue(copies, blockingFetchReadOnly(timeoutTime, transactionId));
} finally {
inBytesLock.unlock();
}
@@ -1481,7 +1507,13 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
private V readValue(ThreadLocalCopies copies, Bytes in) {
assert !outBytesLock.isHeldByCurrentThread();
assert inBytesLock.isHeldByCurrentThread();
return valueReaderWithSize.readNullable(in, copies);
return valueReaderWithSize.readNullable(in, copies, null);
}
private V readValue(ThreadLocalCopies copies, Bytes in, V using) {
assert !outBytesLock.isHeldByCurrentThread();
assert inBytesLock.isHeldByCurrentThread();
return valueReaderWithSize.readNullable(in, copies, using);
}
@SuppressWarnings("SameParameterValue")
@@ -1703,7 +1735,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
return null;
if (rClass == vClass)
return (R) readValue(transactionId, startTime, copies);
return (R) readValue(transactionId, startTime, copies, null);
else
throw new UnsupportedOperationException("class of type class=" + rClass + " is not " +
"supported");
@@ -1732,7 +1764,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
return null;
if (rClass == vClass)
return (R) readValue(transactionId, startTime, copies);
return (R) readValue(transactionId, startTime, copies, null);
else
throw new UnsupportedOperationException("class of type class=" + rClass + " is not " +
"supported");
@@ -1565,7 +1565,7 @@ class StatelessServerConnector<K, V> {
@Nullable
public Work mapForKey(@NotNull ByteBufferBytes reader, @NotNull Bytes writer,
long sizeLocation) {
final K key = keyReaderWithSize.read(reader, null);
final K key = keyReaderWithSize.read(reader, null, null);
final Function<V, ?> function = (Function<V, ?>) reader.readObject();
try {
Object result = map.getMapped(key, function);
@@ -1582,7 +1582,7 @@ class StatelessServerConnector<K, V> {
@Nullable
public Work putMapped(@NotNull ByteBufferBytes reader, @NotNull Bytes writer,
long sizeLocation) {
final K key = keyReaderWithSize.read(reader, null);
final K key = keyReaderWithSize.read(reader, null, null);
final UnaryOperator<V> unaryOperator = (UnaryOperator<V>) reader.readObject();
try {
Object result = map.putMapped(key, unaryOperator);
@@ -1744,7 +1744,7 @@ class StatelessServerConnector<K, V> {
@Nullable
private Work containsValue(Bytes reader, @NotNull Bytes writer, final long sizeLocation) {
// todo optimize -- eliminate
final V v = valueReaderWithSize.read(reader, null);
final V v = valueReaderWithSize.read(reader, null, null);
try {
writer.writeBoolean(map.containsValue(v));