Add ChronicleMap.readKey() and readValue() for convenient work with BytesMapEventListener

This commit is contained in:
leventov
2015-07-28 08:15:12 +03:00
parent c1a630f129
commit 7e9daf73f3
6 changed files with 66 additions and 0 deletions
@@ -36,6 +36,9 @@ import java.io.Serializable;
* alignment (see {@link ChronicleMapBuilder#entryAndValueAlignment(Alignment)}
* ), followed by the serialized value itself.</li>
* </ul>
*
* <p>There are helper methods {@link ChronicleMap#readKey(Bytes, long)} and
* {@link ChronicleMap#readValue(Bytes, long)} that help to deal with this.
*/
public abstract class BytesMapEventListener implements Serializable {
private static final long serialVersionUID = 0L;
@@ -294,6 +294,10 @@ public interface ChronicleMap<K, V> extends ConcurrentMap<K, V>, ChronicleHash {
*/
K newKeyInstance();
K readKey(Bytes entry, long keyPos);
V readValue(Bytes entry, long valuePos);
/**
* @return the class of {@code <K>}
*/
@@ -217,6 +217,16 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
return VanillaChronicleMap.newInstance(kClass, true);
}
@Override
public K readKey(Bytes entry, long keyPos) {
throw new UnsupportedOperationException();
}
@Override
public V readValue(Bytes entry, long valuePos) {
throw new UnsupportedOperationException();
}
@Override
public Class<K> keyClass() {
return kClass;
@@ -233,6 +233,33 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
}
}
@Override
public K readKey(Bytes entry, long keyPos) {
long initialPos = entry.position();
try {
entry.position(keyPos);
long keySize = keySizeMarshaller.readSize(entry);
ThreadLocalCopies copies = keyReaderProvider.getCopies(null);
return keyReaderProvider.get(copies, originalKeyReader).read(entry, keySize);
} finally {
entry.position(initialPos);
}
}
@Override
public V readValue(Bytes entry, long valuePos) {
long initialPos = entry.position();
try {
entry.position(valuePos);
long valueSize = valueSizeMarshaller.readSize(entry);
alignment.alignPositionAddr(entry);
ThreadLocalCopies copies = valueReaderProvider.getCopies(null);
return valueReaderProvider.get(copies, originalValueReader).read(entry, valueSize);
} finally {
entry.position(initialPos);
}
}
static void segmentStateNotNullImpliesCopiesNotNull(
@Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState) {
assert copies != null || segmentState == null; // segmentState != null -> copies != null
@@ -16,6 +16,7 @@
package net.openhft.chronicle.map;
import net.openhft.lang.io.Bytes;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
@@ -245,6 +246,16 @@ public class ReplicationCheckingMap<K, V> implements ChronicleMap<K, V> {
throw new UnsupportedOperationException();
}
@Override
public K readKey(Bytes entry, long keyPos) {
return map1.readKey(entry, keyPos);
}
@Override
public V readValue(Bytes entry, long valuePos) {
return map1.readValue(entry, valuePos);
}
@Override
public Class<K> keyClass() {
return map1.keyClass();
@@ -19,6 +19,7 @@ package net.openhft.chronicle.map.jsr166.map;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.map.*;
import net.openhft.chronicle.map.jsr166.JSR166TestCase;
import net.openhft.lang.io.Bytes;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
@@ -198,6 +199,16 @@ public class StatelessChronicleMapTest extends JSR166TestCase {
throw new UnsupportedOperationException();
}
@Override
public K readKey(Bytes entry, long keyPos) {
return d.readKey(entry, keyPos);
}
@Override
public V readValue(Bytes entry, long valuePos) {
return d.readValue(entry, valuePos);
}
@Override
public Class<K> keyClass() {
return d.keyClass();