mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-410 map event listener expose the identifer, timestamp and hasValueChanged
This commit is contained in:
+8
@@ -63,6 +63,14 @@ public abstract class BasicCopyingMetaBytesInterop<E, W> implements MetaBytesInt
|
||||
return h;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqual(W write, Bytes bytes, E e, long size) {
|
||||
if (buffer.buffer.remaining() != size)
|
||||
return false;
|
||||
|
||||
return bytes.compare(bytes.position(), buffer.buffer, buffer.buffer.position(), size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(W writer, Bytes bytes, E e) {
|
||||
bytes.write(buffer.buffer, buffer.buffer.position(), buffer.buffer.remaining());
|
||||
|
||||
+4
-4
@@ -31,22 +31,22 @@ public enum ByteBufferMarshaller implements BytesInterop<ByteBuffer>, BytesReade
|
||||
@Override
|
||||
public boolean startsWith(Bytes bytes, ByteBuffer bb) {
|
||||
int inputRemaining = bb.remaining();
|
||||
if(bytes.capacity() - bytes.position() < (long) inputRemaining)
|
||||
if (bytes.capacity() - bytes.position() < (long) inputRemaining)
|
||||
return false;
|
||||
|
||||
long pos = bytes.position();
|
||||
int inputPos = bb.position();
|
||||
|
||||
int i;
|
||||
for(i = 0; i < inputRemaining - 7; i += 8) {
|
||||
for (i = 0; i < inputRemaining - 7; i += 8) {
|
||||
if (bytes.readLong(pos + (long) i) != bb.getLong(inputPos + i))
|
||||
return false;
|
||||
}
|
||||
for(; i < inputRemaining - 3; i += 4) {
|
||||
for (; i < inputRemaining - 3; i += 4) {
|
||||
if (bytes.readInt(pos + (long) i) != bb.getInt(inputPos + i))
|
||||
return false;
|
||||
}
|
||||
for(; i < inputRemaining; i++) {
|
||||
for (; i < inputRemaining; i++) {
|
||||
if (bytes.readByte(pos + (long) i) != bb.get(inputPos + i))
|
||||
return false;
|
||||
}
|
||||
|
||||
+15
-1
@@ -192,6 +192,13 @@ public abstract class ByteableMarshaller<E extends Byteable>
|
||||
super(tClass);
|
||||
initSize();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isEqual(Object write, Bytes bytes, E e, long size) {
|
||||
// todo
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static class WithCustomFactory<E extends Byteable> extends ByteableMarshaller<E> {
|
||||
@@ -201,7 +208,7 @@ public abstract class ByteableMarshaller<E extends Byteable>
|
||||
private final ObjectFactory<E> factory;
|
||||
|
||||
WithCustomFactory(@NotNull Class<E> tClass,
|
||||
@NotNull ObjectFactory<E> factory) {
|
||||
@NotNull ObjectFactory<E> factory) {
|
||||
super(tClass);
|
||||
this.factory = factory;
|
||||
initSize();
|
||||
@@ -212,5 +219,12 @@ public abstract class ByteableMarshaller<E extends Byteable>
|
||||
E getInstance() throws Exception {
|
||||
return factory.create();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isEqual(Object write, Bytes bytes, E e, long size) {
|
||||
// todo
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -52,6 +52,12 @@ public final class DelegatingMetaBytesInterop<E, I extends BytesInterop<E>>
|
||||
interop.write(bytes, e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqual(I interop, Bytes bytes, E e, long size) {
|
||||
// todo
|
||||
return false;
|
||||
}
|
||||
|
||||
private Object readResolve() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@@ -136,5 +136,10 @@ class DirectBytesBuffer
|
||||
private ForDataValueWriter() {
|
||||
super(DirectBytesBuffer.this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqual(BytesWriter<E> write, Bytes bytes, E e, long size) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,4 +24,16 @@ public interface MetaBytesWriter<E, W> extends Serializable {
|
||||
long size(W writer, E e);
|
||||
|
||||
void write(W writer, Bytes bytes, E e);
|
||||
|
||||
|
||||
/**
|
||||
* returns true if the {@code bytes} equals {@code e}
|
||||
*
|
||||
* @param write
|
||||
* @param bytes
|
||||
* @param e
|
||||
* @param size
|
||||
* @return
|
||||
*/
|
||||
boolean isEqual(W write, Bytes bytes, E e, long size);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package net.openhft.chronicle.map;
|
||||
|
||||
import net.openhft.chronicle.hash.ChronicleHashBuilder;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -64,10 +63,43 @@ public abstract class MapEventListener<K, V> implements Serializable {
|
||||
return new LoggingMapEventListener(prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called whenever a new value is put for the key in the map during calls of such
|
||||
* methods as {@link ChronicleMap#put put}, {@link ChronicleMap#putIfAbsent putIfAbsent}, {@link
|
||||
* ChronicleMap#replace(Object, Object, Object) replace}, etc. When a new value is {@linkplain
|
||||
* ChronicleMapBuilder#defaultValue(Object) default} for the map or obtained during {@link
|
||||
* ChronicleMap#acquireUsing acquireUsing} call is put for the key, this method is called as
|
||||
* well.
|
||||
*
|
||||
* <p>This method is called when put is already happened.
|
||||
*
|
||||
* @param key the key the given value is put for
|
||||
* @param newValue the value which is now associated with the given key
|
||||
* @param replacedValue the value which was replaced by {@code newValue}, {@code null} if
|
||||
* the key was absent in the map before current {@code ChronicleMap}
|
||||
* @param hasValueChanged {@code true} if the {@code newValue } equals {@code replacedValue
|
||||
* }
|
||||
* @param identifier the identifer used for replicaiton or zero if not definded
|
||||
* @param replacedIdentifier the replaced identifer used for replicaiton or zero if not
|
||||
* definded
|
||||
* @param timeStamp the timestamp used for replicaiton or zero if not definded
|
||||
* @param replacedTimeStamp the replaced timestamp used for replicaiton or zero if not
|
||||
* definded
|
||||
*/
|
||||
public void onPut(K key,
|
||||
V newValue,
|
||||
@Nullable V replacedValue,
|
||||
boolean replicationEvent,
|
||||
boolean added,
|
||||
boolean hasValueChanged,
|
||||
byte identifier,
|
||||
byte replacedIdentifier, long timeStamp, long replacedTimeStamp) {
|
||||
|
||||
}
|
||||
|
||||
private static class LoggingMapEventListener extends MapEventListener {
|
||||
private static final long serialVersionUID = 0L;
|
||||
public final static Logger LOGGER = LoggerFactory.getLogger(LoggingMapEventListener.class);
|
||||
|
||||
private final String prefix;
|
||||
|
||||
private LoggingMapEventListener(String prefix) {
|
||||
@@ -81,8 +113,8 @@ public abstract class MapEventListener<K, V> implements Serializable {
|
||||
|
||||
@Override
|
||||
public void onPut(Object key, Object value, Object replacedValue, boolean
|
||||
replicationEvent,
|
||||
@NotNull boolean updateResult) {
|
||||
replicationEvent, boolean updateResult, boolean hasValueChanged, byte identifier,
|
||||
byte replacedIdentifier, long timeStamp, long replacedTimeStamp) {
|
||||
LOGGER.info("{} put {} => {}", prefix, key, value);
|
||||
}
|
||||
|
||||
@@ -114,18 +146,24 @@ public abstract class MapEventListener<K, V> implements Serializable {
|
||||
*
|
||||
* <p>This method is called when put is already happened.
|
||||
*
|
||||
* @param key the key the given value is put for
|
||||
* @param newValue the value which is now associated with the given key
|
||||
* @param replacedValue the value which was replaced by {@code newValue}, {@code null} if the
|
||||
* key was absent in the map before current {@code ChronicleMap}
|
||||
* @param key the key the given value is put for
|
||||
* @param newValue the value which is now associated with the given key
|
||||
* @param replacedValue the value which was replaced by {@code newValue}, {@code null}
|
||||
* if the key was absent in the map before current {@code
|
||||
* ChronicleMap}
|
||||
* @param replicationEvent
|
||||
* @param lastModifiedTimeStamp
|
||||
* @param lastId
|
||||
* @param timestamp
|
||||
* @param identifier
|
||||
*/
|
||||
public void onPut(K key,
|
||||
V newValue,
|
||||
@Nullable V replacedValue,
|
||||
boolean replicationEvent,
|
||||
boolean added) {
|
||||
// do nothing
|
||||
boolean added, long lastModifiedTimeStamp, byte lastId, long timestamp,
|
||||
byte identifier, boolean hasValueChanged) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -53,4 +53,10 @@ final class PrepareValueBytesAsWriter<K>
|
||||
public void write(PrepareValueBytesAsWriter<K> writer, Bytes bytes, K k) {
|
||||
write(bytes, k);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqual(PrepareValueBytesAsWriter<K> write, Bytes bytes, K k, long size) {
|
||||
// todo
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,6 @@ import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReferenceArray;
|
||||
|
||||
@@ -901,7 +900,10 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
// because locking might take time > 1 ms. but making current time a param
|
||||
// of acquireWithoutLock would complicate code much, requiring putting
|
||||
// lookupUsing() back into ReplicatedChMap, and other methods
|
||||
entry.writeLong(currentTime());
|
||||
final long timestamp = currentTime();
|
||||
final long replacedTimestamp = entry.readLong(entry.position());
|
||||
entry.writeLong(timestamp);
|
||||
byte replacedIdentifier = entry.readByte(entry.position());
|
||||
entry.writeByte(localIdentifier);
|
||||
// deleted flag
|
||||
entry.writeBoolean(false);
|
||||
@@ -931,7 +933,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
} else {
|
||||
throw defaultValueOrPrepareBytesShouldBeSpecified();
|
||||
}
|
||||
putValue(pos, offset, entry, valueSizePos, entryEndAddr, isDeleted,
|
||||
boolean hasValueChanged = putValue(pos, offset, entry, valueSizePos,
|
||||
entryEndAddr, isDeleted,
|
||||
segmentState,
|
||||
metaElemWriter, elemWriter, elem, metaElemWriter.size(elemWriter, elem),
|
||||
hashLookup, sizeOfEverythingBeforeValue);
|
||||
@@ -952,7 +955,9 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
}
|
||||
if (eventListener != null) {
|
||||
V valueInstance = toValue.toInstance(copies, v, valueSize);
|
||||
eventListener.onPut(keyInstance, valueInstance, null, false, true);
|
||||
eventListener.onPut(keyInstance, valueInstance, null, false, true,
|
||||
hasValueChanged, localIdentifier, replacedIdentifier, timestamp,
|
||||
replacedTimestamp);
|
||||
}
|
||||
entryCreated(lock);
|
||||
return v;
|
||||
@@ -1109,7 +1114,10 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
boolean isDeleted = entry.readBoolean();
|
||||
|
||||
entry.positionAddr(timeStampPosAddr);
|
||||
final long replacedTimestamp = entry.readLong(entry.position());
|
||||
entry.writeLong(timeStamp);
|
||||
|
||||
byte replacedIdentifier = entry.readByte(entry.position());
|
||||
entry.writeByte(identifier);
|
||||
// deleted flag
|
||||
entry.writeBoolean(false);
|
||||
@@ -1123,6 +1131,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
long prevValueSize = valueSizeMarshaller.readSize(entry);
|
||||
long sizeOfEverythingBeforeValue = entry.position();
|
||||
alignment.alignPositionAddr(entry);
|
||||
boolean hasValueChanged = false;
|
||||
try {
|
||||
if (!isDeleted && prevValueSize == valueSize &&
|
||||
metaValueInterop.startsWith(valueInterop, entry, value)) {
|
||||
@@ -1132,7 +1141,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
long entryEndAddr = valueAddr + prevValueSize;
|
||||
|
||||
// putValue may relocate entry and change offset
|
||||
putValue(pos, offset, entry, valueSizePos, entryEndAddr, isDeleted,
|
||||
hasValueChanged = putValue(pos, offset, entry, valueSizePos,
|
||||
entryEndAddr, isDeleted,
|
||||
segmentState,
|
||||
metaValueInterop, valueInterop, value, valueSize, hashLookup,
|
||||
sizeOfEverythingBeforeValue);
|
||||
@@ -1147,7 +1157,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
if (eventListener != null) {
|
||||
eventListener.onPut(toKey.toInstance(copies, key, keySize),
|
||||
toValue.toInstance(copies, value, valueSize), null, false,
|
||||
isDeleted);
|
||||
isDeleted, hasValueChanged, identifier, replacedIdentifier, timeStamp, replacedTimestamp);
|
||||
}
|
||||
|
||||
// for DRY (reusing replaceValueAndNotifyPut() method),
|
||||
@@ -1171,10 +1181,12 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
MVBI metaValueInterop =
|
||||
getValueInterops.getMetaValueInterop(copies, valueInterop, value);
|
||||
long valueSize = metaValueInterop.size(valueInterop, value);
|
||||
putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
|
||||
final long l = putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
|
||||
metaValueInterop, valueInterop, value, entry, false);
|
||||
entry.position(segmentState.valueSizePos - ADDITIONAL_ENTRY_BYTES);
|
||||
final long replacedTimestamp = entry.readLong(entry.position());
|
||||
entry.writeLong(timeStamp);
|
||||
byte replacedIdentifier = entry.readByte(entry.position());
|
||||
entry.writeByte(identifier);
|
||||
entry.writeBoolean(false);
|
||||
|
||||
@@ -1185,7 +1197,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
segmentState.valueSizePos, true, false);
|
||||
if (eventListener != null)
|
||||
eventListener.onPut(toKey.toInstance(copies, key, keySize),
|
||||
toValue.toInstance(copies, value, valueSize), null, false, true);
|
||||
toValue.toInstance(copies, value, valueSize), null, false, true,
|
||||
l < 0, identifier, replacedIdentifier, timeStamp, replacedTimestamp);
|
||||
|
||||
return UpdateResult.INSERT;
|
||||
} finally {
|
||||
@@ -1203,7 +1216,9 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
InstanceOrBytesToInstance<KB, K> toKey,
|
||||
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
|
||||
InstanceOrBytesToInstance<? super VB, V> toValue, long hash2,
|
||||
boolean replaceIfPresent, ReadValue<RV> readValue, boolean resultUnused) {
|
||||
boolean replaceIfPresent, ReadValue<RV> readValue, boolean resultUnused,
|
||||
final byte identifier, final byte replacedIdentifier,
|
||||
final long timestamp, final long replacedTimestamp) {
|
||||
return putWithoutLock(copies, segmentState, metaKeyInterop, keyInterop, key, keySize,
|
||||
toKey, getValueInterops, value, toValue, hash2, replaceIfPresent, readValue,
|
||||
resultUnused, localIdentifier, currentTime(), false);
|
||||
@@ -1249,7 +1264,9 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
|
||||
if (replaceIfPresent || isDeleted) {
|
||||
entry.positionAddr(timeStampPosAddr);
|
||||
long replacedTimestamp = entry.readLong(entry.position());
|
||||
entry.writeLong(timestamp);
|
||||
final byte replacedIdentifier = entry.readByte(entry.position());
|
||||
entry.writeByte(identifier);
|
||||
// deleted flag
|
||||
entry.writeBoolean(false);
|
||||
@@ -1258,7 +1275,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
key, keySize, toKey,
|
||||
getValueInterops, value, toValue,
|
||||
entry, pos, offset, hashLookup, readValue,
|
||||
resultUnused, isDeleted, remote);
|
||||
resultUnused, isDeleted, remote, identifier,
|
||||
replacedIdentifier, timestamp, replacedTimestamp);
|
||||
// for DRY (reusing replaceValueAndNotifyPut() method),
|
||||
// size is updated AFTER callbacks are called.
|
||||
// however this shouldn't be an issue because exclusive segment lock
|
||||
@@ -1287,10 +1305,13 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
MVBI metaValueInterop =
|
||||
getValueInterops.getMetaValueInterop(copies, valueInterop, value);
|
||||
long valueSize = metaValueInterop.size(valueInterop, value);
|
||||
putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
|
||||
final long l = putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
|
||||
metaValueInterop, valueInterop, value, entry, false);
|
||||
boolean hasValueChanged = (l < 0);
|
||||
entry.position(segmentState.valueSizePos - ADDITIONAL_ENTRY_BYTES);
|
||||
final long replacedTimestamp = entry.readLong(entry.position());
|
||||
entry.writeLong(timestamp);
|
||||
final byte replacedIdentifier = entry.readByte(entry.position());
|
||||
entry.writeByte(identifier);
|
||||
entry.writeBoolean(false);
|
||||
|
||||
@@ -1301,7 +1322,9 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
segmentState.valueSizePos, true, remote);
|
||||
if (eventListener != null)
|
||||
eventListener.onPut(toKey.toInstance(copies, key, keySize),
|
||||
toValue.toInstance(copies, value, valueSize), null, remote, true);
|
||||
toValue.toInstance(copies, value, valueSize), null, remote, true,
|
||||
hasValueChanged, identifier, replacedIdentifier, timestamp,
|
||||
replacedTimestamp);
|
||||
|
||||
return resultUnused ? null : readValue.readNull();
|
||||
}
|
||||
@@ -1787,9 +1810,10 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
|
||||
long timeStamp = segment.timeStamp(pos);
|
||||
|
||||
assert timeStamp > currentTime() - TimeUnit.SECONDS.toMillis(1) &&
|
||||
timeStamp <= currentTime() : "timeStamp=" + timeStamp + ", " +
|
||||
"currentTime=" + currentTime();
|
||||
// this is not true for bootstraping of replicaiton events
|
||||
// assert timeStamp > currentTime() - TimeUnit.SECONDS.toMillis(1) &&
|
||||
// timeStamp <= currentTime() : "timeStamp=" + timeStamp + ", " +
|
||||
// "currentTime=" + currentTime();
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,173 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2015 higherfrequencytrading.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.openhft.chronicle.map;
|
||||
|
||||
import net.openhft.chronicle.hash.ChronicleHash;
|
||||
import net.openhft.chronicle.hash.ChronicleHashBuilder;
|
||||
import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder;
|
||||
import net.openhft.chronicle.hash.replication.ReplicationChannel;
|
||||
import net.openhft.chronicle.hash.replication.ReplicationHub;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @author Rob Austin.
|
||||
*/
|
||||
class ReplicationHubFindByName<K> implements FindByName {
|
||||
public static final Logger LOG = LoggerFactory.getLogger(ReplicationHubFindByName.class.getName());
|
||||
|
||||
public static final int MAP_BY_NAME_CHANNEL = 1;
|
||||
|
||||
private final AtomicInteger nextFreeChannel = new AtomicInteger(2);
|
||||
private final Map<String, MapInstanceBuilder> map;
|
||||
private final ReplicationHub replicationHub;
|
||||
|
||||
/**
|
||||
* @throws IOException
|
||||
*/
|
||||
public ReplicationHubFindByName(ReplicationHub replicationHub) throws IOException {
|
||||
|
||||
LOG.info("connecting to replicationHub=" + replicationHub);
|
||||
|
||||
this.replicationHub = replicationHub;
|
||||
ReplicationChannel channel = replicationHub.createChannel((short) MAP_BY_NAME_CHANNEL);
|
||||
|
||||
final MapEventListener<CharSequence, MapInstanceBuilder> listener =
|
||||
new MapEventListener<CharSequence, MapInstanceBuilder>() {
|
||||
// creates a map based on the details that are sent to the map of builders
|
||||
@Override
|
||||
public void onPut(CharSequence key, MapInstanceBuilder value,
|
||||
MapInstanceBuilder replacedValue, boolean replicationEvent,
|
||||
boolean added0) {
|
||||
super.onPut(key, value, replacedValue, replicationEvent, added0);
|
||||
boolean added = replacedValue == null;
|
||||
if (!added || value == null)
|
||||
return;
|
||||
|
||||
// establish the new map based on this channel ID
|
||||
LOG.info("create new map for name=" + value.name
|
||||
+ ",channelId=" + value.channel.channelId());
|
||||
|
||||
try {
|
||||
toReplicatedViaChannel(value.mapBuilder, value.channel.channelId()).create();
|
||||
} catch (IllegalStateException e) {
|
||||
// channel is already created
|
||||
LOG.debug("while creating channel for name=" + value.name
|
||||
+ ",channelId=" + value.channel.channelId(), e);
|
||||
} catch (IOException e) {
|
||||
LOG.error("", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.map = (Map) ChronicleMapBuilder
|
||||
.of(CharSequence.class, MapInstanceBuilder.class)
|
||||
.averageKeySize(10)
|
||||
.averageValueSize(4000)
|
||||
.entries(128)
|
||||
.eventListener(listener)
|
||||
.instance()
|
||||
.replicatedViaChannel(channel)
|
||||
.create();
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("map=" + map);
|
||||
}
|
||||
|
||||
public <T extends ChronicleHash> T create(
|
||||
MapInstanceBuilder<CharSequence, CharSequence> config)
|
||||
throws TimeoutException, InterruptedException {
|
||||
|
||||
int withChannelId = nextFreeChannel.incrementAndGet();
|
||||
|
||||
map.put(config.name,
|
||||
config.replicatedViaChannel(replicationHub.createChannel(withChannelId)));
|
||||
|
||||
return (T) get(config.name).mapBuilder.create();
|
||||
}
|
||||
|
||||
public <T extends ChronicleHash> T from(String name) throws IllegalArgumentException,
|
||||
IOException, TimeoutException, InterruptedException {
|
||||
return (T) get(name).create();
|
||||
}
|
||||
|
||||
MapInstanceBuilder get(String name) throws IllegalArgumentException,
|
||||
TimeoutException,
|
||||
InterruptedException {
|
||||
|
||||
MapInstanceBuilder config = waitTillEntryReceived(5000, name);
|
||||
if (config == null)
|
||||
throw new IllegalArgumentException("A map name=" + name + " can not be found.");
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* * waits until map1 and map2 show the same value
|
||||
*
|
||||
* @param timeOutMs timeout in milliseconds
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
private MapInstanceBuilder waitTillEntryReceived(final int timeOutMs, String name)
|
||||
throws TimeoutException, InterruptedException {
|
||||
int t = 0;
|
||||
for (; t < timeOutMs; t++) {
|
||||
MapInstanceBuilder config = map.get(name);
|
||||
|
||||
if (config != null)
|
||||
return config;
|
||||
Thread.sleep(1);
|
||||
}
|
||||
throw new TimeoutException("timed out wait for map name=" + name);
|
||||
}
|
||||
|
||||
/**
|
||||
* similar to {@link ChronicleMapBuilder#createPersistedTo(File file) }
|
||||
*
|
||||
* @param name the name of the map to be created
|
||||
* @param file the file which will be used as shared off heap storage
|
||||
* @param <T>
|
||||
* @return a new instance of ChronicleMap
|
||||
* @throws IllegalArgumentException
|
||||
* @throws IOException
|
||||
* @see ChronicleMapBuilder#createPersistedTo(File file)
|
||||
*/
|
||||
public <T extends ChronicleHash> T createPersistedTo(String name, File file)
|
||||
throws IllegalArgumentException,
|
||||
IOException, TimeoutException, InterruptedException {
|
||||
return (T) get(name).persistedTo(file).create();
|
||||
}
|
||||
|
||||
private ChronicleHashInstanceBuilder toReplicatedViaChannel(ChronicleMapBuilder builder) {
|
||||
int channelId = nextFreeChannel.incrementAndGet();
|
||||
if (channelId > replicationHub.maxNumberOfChannels())
|
||||
throw new IllegalStateException("There are no more free channels, you can increase the number " +
|
||||
"of changes in the replicationHub by calling replicationHub.maxNumberOfChannels(..);");
|
||||
return builder.instance().replicatedViaChannel(replicationHub.createChannel((short) channelId));
|
||||
}
|
||||
|
||||
private ChronicleHashInstanceBuilder toReplicatedViaChannel(ChronicleHashBuilder<K, ?, ?> builder, int
|
||||
withChannelId) {
|
||||
return builder.instance().replicatedViaChannel(replicationHub.createChannel((short) withChannelId));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1376,6 +1376,15 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
public void write(Void writer, Bytes bytes, Bytes value) {
|
||||
bytes.write(value, value.position(), value.remaining());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEqual(Void write, Bytes bytes, Bytes bytes2, long size) {
|
||||
|
||||
if (size != bytes2.remaining())
|
||||
return false;
|
||||
|
||||
return bytes.compare(bytes.position(), bytes2, bytes2.position(), bytes2.remaining());
|
||||
}
|
||||
}
|
||||
|
||||
public static final class SegmentState implements StatefulCopyable<SegmentState>,
|
||||
@@ -2091,6 +2100,12 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
throw defaultValueOrPrepareBytesShouldBeSpecified();
|
||||
}
|
||||
|
||||
|
||||
boolean hasValueChanged = valueSize < 0;
|
||||
|
||||
if (valueSize < 0)
|
||||
valueSize = valueSize * -1;
|
||||
|
||||
entry.positionAddr(entry.positionAddr() - valueSize);
|
||||
RV v = readValue.readValue(copies, entry, usingValue, valueSize);
|
||||
|
||||
@@ -2102,7 +2117,9 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
}
|
||||
if (eventListener != null) {
|
||||
eventListener.onPut(toKey.toInstance(copies, key, keySize),
|
||||
toValue.toInstance(copies, v, valueSize), null, false, true);
|
||||
toValue.toInstance(copies, v, valueSize), null, false, true, hasValueChanged,
|
||||
(byte) 0,
|
||||
(byte) 0, 0, 0);
|
||||
}
|
||||
|
||||
return v;
|
||||
@@ -2171,6 +2188,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
long prevValueSize = valueSizeMarshaller.readSize(entry);
|
||||
long sizeOfEverythingBeforeValue = entry.position();
|
||||
alignment.alignPositionAddr(entry);
|
||||
|
||||
boolean hasValueChanged = false;
|
||||
try {
|
||||
if (prevValueSize == valueSize &&
|
||||
metaValueInterop.startsWith(valueInterop, entry, value)) {
|
||||
@@ -2193,7 +2212,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
if (eventListener != null) {
|
||||
eventListener.onPut(toKey.toInstance(copies, key, keySize),
|
||||
toValue.toInstance(copies, value, valueSize), null, false,
|
||||
false);
|
||||
false, hasValueChanged, (byte) 0, (byte) 0, 0,
|
||||
0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2202,7 +2222,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
MVBI metaValueInterop =
|
||||
getValueInterops.getMetaValueInterop(copies, valueInterop, value);
|
||||
long valueSize = metaValueInterop.size(valueInterop, value);
|
||||
putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
|
||||
long l = putEntry(segmentState, metaKeyInterop, keyInterop, key,
|
||||
keySize,
|
||||
metaValueInterop, valueInterop, value, entry, false);
|
||||
|
||||
// put callbacks
|
||||
@@ -2212,7 +2233,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
segmentState.valueSizePos, true, false);
|
||||
if (eventListener != null)
|
||||
eventListener.onPut(toKey.toInstance(copies, key, keySize),
|
||||
toValue.toInstance(copies, value, valueSize), null, false, true);
|
||||
toValue.toInstance(copies, value, valueSize), null, false, true, l <
|
||||
0, (byte) 0, (byte) 0, 0, 0);
|
||||
|
||||
return UpdateResult.INSERT;
|
||||
} finally {
|
||||
@@ -2241,7 +2263,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
return putWithoutLock(copies, segmentState,
|
||||
metaKeyInterop, keyInterop, key, keySize, toKey,
|
||||
getValueInterops, value, toValue,
|
||||
hash2, replaceIfPresent, readValue, resultUnused);
|
||||
hash2, replaceIfPresent, readValue, resultUnused, (byte) 0, (byte) 0, 0,
|
||||
0);
|
||||
} finally {
|
||||
segmentState.close();
|
||||
writeUnlock();
|
||||
@@ -2257,7 +2280,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
|
||||
InstanceOrBytesToInstance<? super VB, V> toValue,
|
||||
long hash2, boolean replaceIfPresent,
|
||||
ReadValue<RV> readValue, boolean resultUnused) {
|
||||
ReadValue<RV> readValue, boolean resultUnused, final byte identifier,
|
||||
final byte replacedIdentifier, final long timestamp, final long replacedTimestamp) {
|
||||
|
||||
SearchState searchState = segmentState.searchState;
|
||||
hashLookup.startSearch(hash2, searchState);
|
||||
@@ -2274,7 +2298,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
key, keySize, toKey,
|
||||
getValueInterops, value, toValue,
|
||||
entry, pos, offset, hashLookup, readValue, resultUnused,
|
||||
false, false);
|
||||
false, false, identifier, replacedIdentifier, timestamp, replacedTimestamp);
|
||||
} else {
|
||||
long valueSize = readValueSize(entry);
|
||||
return resultUnused ? null :
|
||||
@@ -2286,7 +2310,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
MVBI metaValueInterop =
|
||||
getValueInterops.getMetaValueInterop(copies, valueInterop, value);
|
||||
long valueSize = metaValueInterop.size(valueInterop, value);
|
||||
putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
|
||||
long l = putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize,
|
||||
metaValueInterop, valueInterop, value, entry, false);
|
||||
|
||||
// put callbacks
|
||||
@@ -2296,7 +2320,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
segmentState.valueSizePos, true, false);
|
||||
if (eventListener != null)
|
||||
eventListener.onPut(toKey.toInstance(copies, key, keySize),
|
||||
toValue.toInstance(copies, value, valueSize), null, false, true);
|
||||
toValue.toInstance(copies, value, valueSize), null, false, true, l < 0,
|
||||
identifier, (byte) 0, 0, 0);
|
||||
|
||||
return resultUnused ? null : readValue.readNull();
|
||||
}
|
||||
@@ -2309,7 +2334,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
InstanceOrBytesToInstance<? super VB, V> toValue,
|
||||
MultiStoreBytes entry, long pos, long offset, MultiMap searchedHashLookup,
|
||||
ReadValue<RV> readValue, boolean resultUnused,
|
||||
boolean entryIsDeleted, boolean remote) {
|
||||
boolean entryIsDeleted, boolean remote, final byte identifier, byte replacedIdentifier, final long timestamp, final long replacedTimestamp) {
|
||||
VBI valueInterop = getValueInterops.getValueInterop(copies);
|
||||
MVBI metaValueInterop = getValueInterops.getMetaValueInterop(
|
||||
copies, valueInterop, value);
|
||||
@@ -2335,7 +2360,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
}
|
||||
|
||||
// putValue may relocate entry and change offset
|
||||
putValue(pos, offset, entry, valueSizePos, entryEndAddr, entryIsDeleted, segmentState,
|
||||
boolean hasValueChanged = putValue(pos, offset, entry, valueSizePos, entryEndAddr,
|
||||
entryIsDeleted, segmentState,
|
||||
metaValueInterop, valueInterop, value, valueSize, searchedHashLookup,
|
||||
sizeOfEverythingBeforeValue);
|
||||
|
||||
@@ -2346,7 +2372,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
if (eventListener != null) {
|
||||
eventListener.onPut(toKey.toInstance(copies, key, keySize),
|
||||
toValue.toInstance(copies, value, valueSize), prevValueInstance, remote,
|
||||
entryIsDeleted);
|
||||
entryIsDeleted, hasValueChanged, identifier, replacedIdentifier,
|
||||
timestamp, replacedTimestamp);
|
||||
}
|
||||
|
||||
return resultUnused ? null : prevValue;
|
||||
@@ -2364,6 +2391,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
* Returns value size, writes the entry (key, value, sizes) to the entry, after this method
|
||||
* call entry positioned after value bytes written (i. e. at the end of entry), sets entry
|
||||
* position (in segment) and value size position in the given segmentState
|
||||
*
|
||||
* @return the size or negivtive size if the value has changed.
|
||||
*/
|
||||
final <KB, KBI, MKBI extends MetaBytesInterop<KB, ? super KBI>, E, EW>
|
||||
long putEntry(SegmentState segmentState,
|
||||
@@ -2379,14 +2408,18 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
clearMetaData(offset);
|
||||
reuse(entry, offset);
|
||||
|
||||
|
||||
keySizeMarshaller.writeSize(entry, keySize);
|
||||
metaKeyInterop.write(keyInterop, entry, key);
|
||||
|
||||
manageReplicationBytes(entry, writeDefaultInitialReplicationValues, false);
|
||||
|
||||
segmentState.valueSizePos = entry.position();
|
||||
final long size = valueSizeMarshaller.readSize(entry);
|
||||
entry.position(segmentState.valueSizePos);
|
||||
valueSizeMarshaller.writeSize(entry, valueSize);
|
||||
alignment.alignPositionAddr(entry);
|
||||
boolean hasValueChanged = !metaElemWriter.isEqual(elemWriter, entry, elem, size);
|
||||
metaElemWriter.write(elemWriter, entry, elem);
|
||||
|
||||
freeExtraAllocatedChunks(pos, allocatedChunks, entry);
|
||||
@@ -2394,7 +2427,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
hashLookup.putAfterFailedSearch(segmentState.searchState, pos);
|
||||
incrementSize();
|
||||
|
||||
return valueSize;
|
||||
return hasValueChanged ? -valueSize : valueSize;
|
||||
}
|
||||
|
||||
final void freeExtraAllocatedChunks(long pos, int allocatedChunks, Bytes entry) {
|
||||
@@ -2714,7 +2747,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
copies, valueInterop, newValue);
|
||||
long newValueSize = metaValueInterop.size(valueInterop, newValue);
|
||||
boolean entryIsDeleted = false; // couldn't replace deleted entry
|
||||
putValue(pos, offset, entry, valueSizePos, entryEndAddr, entryIsDeleted, segmentState,
|
||||
boolean hasValueChanged = putValue(pos, offset, entry, valueSizePos, entryEndAddr, entryIsDeleted, segmentState,
|
||||
metaValueInterop, valueInterop, newValue, newValueSize, searchedHashLookup,
|
||||
sizeOfEverythingBeforeValue);
|
||||
|
||||
@@ -2727,7 +2760,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
if (eventListener != null)
|
||||
eventListener.onPut(toKey.toInstance(copies, key, keySize),
|
||||
toValue.toInstance(copies, newValue, newValueSize),
|
||||
toValue.toInstance(copies, prevValue, valueSize), false, true);
|
||||
toValue.toInstance(copies, prevValue, valueSize), false, true,
|
||||
hasValueChanged, (byte) 0, (byte) 0, 0, 0);
|
||||
|
||||
return expectedValue == null ? prevValue : Boolean.TRUE;
|
||||
}
|
||||
@@ -2742,16 +2776,16 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
* @param entry relative pointer in Segment bytes
|
||||
* @param valueSizePos relative position of value size in entry
|
||||
* @param entryEndAddr absolute address of the entry end
|
||||
* @return relative offset of the entry in Segment bytes after putting value (that may cause
|
||||
* entry relocation)
|
||||
* @return true if the value has change
|
||||
*/
|
||||
final <E, EW> long putValue(
|
||||
final <E, EW> boolean putValue(
|
||||
long pos, long offset, MultiStoreBytes entry, long valueSizePos, long entryEndAddr,
|
||||
boolean entryIsDeleted,
|
||||
SegmentState segmentState,
|
||||
MetaBytesWriter<E, ? super EW> metaElemWriter, EW elemWriter, E newElem,
|
||||
long newElemSize,
|
||||
MultiMap searchedHashLookup, long sizeOfEverythingBeforeValue) {
|
||||
final boolean hasValueChanged;
|
||||
long entryStartAddr = entry.address();
|
||||
long valueSizeAddr = entryStartAddr + valueSizePos;
|
||||
long newValueAddr = alignment.alignAddr(
|
||||
@@ -2789,14 +2823,20 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
NativeBytes.UNSAFE.copyMemory(entryStartAddr,
|
||||
newEntryStartAddr, valueSizeAddr - entryStartAddr);
|
||||
entry.position(valueSizePos);
|
||||
final long originalSize = valueSizeMarshaller.readSize(entry);
|
||||
entry.position(valueSizePos);
|
||||
valueSizeMarshaller.writeSize(entry, newElemSize);
|
||||
alignment.alignPositionAddr(entry);
|
||||
metaElemWriter.write(elemWriter, entry, newElem);
|
||||
|
||||
hasValueChanged = !metaElemWriter.isEqual(elemWriter, entry, newElem,
|
||||
originalSize);
|
||||
if (hasValueChanged)
|
||||
metaElemWriter.write(elemWriter, entry, newElem);
|
||||
|
||||
freeExtraAllocatedChunks(newPos, allocatedChunks, entry);
|
||||
|
||||
segmentState.pos = newPos;
|
||||
return offset;
|
||||
return hasValueChanged;
|
||||
// END OF RELOCATION
|
||||
} else if (newSizeInChunks < oldSizeInChunks) {
|
||||
// Freeing extra chunks
|
||||
@@ -2809,11 +2849,15 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
}
|
||||
// Common code for all cases
|
||||
entry.position(valueSizePos);
|
||||
final long originalSize = valueSizeMarshaller.readSize(entry);
|
||||
entry.position(valueSizePos);
|
||||
valueSizeMarshaller.writeSize(entry, newElemSize);
|
||||
alignment.alignPositionAddr(entry);
|
||||
|
||||
hasValueChanged = !metaElemWriter.isEqual(elemWriter, entry, newElem, originalSize);
|
||||
metaElemWriter.write(elemWriter, entry, newElem);
|
||||
segmentState.pos = pos;
|
||||
return offset;
|
||||
return hasValueChanged;
|
||||
}
|
||||
|
||||
private void clear() {
|
||||
|
||||
Reference in New Issue
Block a user