package org.ehcache.clustered.common.internal.store.operations;
import org.ehcache.clustered.common.internal.store.operations.codecs.CodecException;
import org.ehcache.spi.serialization.Serializer;
import java.nio.ByteBuffer;
abstract class BaseKeyValueOperation<K, V> implements Operation<K, V> {
private final K key;
private final LazyValueHolder<V> valueHolder;
private final long timeStamp;
BaseKeyValueOperation(K key, V value, long timeStamp) {
if(key == null) {
throw new NullPointerException("Key can not be null");
}
if(value == null) {
throw new NullPointerException("Value can not be null");
}
this.key = key;
this.valueHolder = new LazyValueHolder<>(value);
this.timeStamp = timeStamp;
}
BaseKeyValueOperation(BaseKeyValueOperation<K, V> copy, long timeStamp) {
this.key = copy.key;
this.valueHolder = copy.valueHolder;
this.timeStamp = timeStamp;
}
BaseKeyValueOperation(ByteBuffer buffer, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
OperationCode opCode = OperationCode.valueOf(buffer.get());
if (opCode != getOpCode()) {
throw new IllegalArgumentException("Invalid operation: " + opCode);
}
this.timeStamp = buffer.getLong();
int keySize = buffer.getInt();
int maxLimit = buffer.limit();
buffer.limit(buffer.position() + keySize);
ByteBuffer keyBlob = buffer.slice();
buffer.position(buffer.limit());
buffer.limit(maxLimit);
try {
this.key = keySerializer.read(keyBlob);
} catch (ClassNotFoundException e) {
throw new CodecException(e);
}
this.valueHolder = new LazyValueHolder<>(buffer.slice(), valueSerializer);
}
public K getKey() {
return key;
}
public V getValue() {
return valueHolder.getValue();
}
@Override
public ByteBuffer encode(final Serializer<K> keySerializer, final Serializer<V> valueSerializer) {
ByteBuffer keyBuf = keySerializer.serialize(key);
ByteBuffer valueBuf = valueHolder.encode(valueSerializer);
int size = BYTE_SIZE_BYTES +
INT_SIZE_BYTES +
LONG_SIZE_BYTES +
keyBuf.remaining() +
valueBuf.remaining();
ByteBuffer buffer = ByteBuffer.allocate(size);
buffer.put(getOpCode().getValue());
buffer.putLong(this.timeStamp);
buffer.putInt(keyBuf.remaining());
buffer.put(keyBuf);
buffer.put(valueBuf);
buffer.flip();
return buffer;
}
@Override
public String toString() {
return "{" + getOpCode() + "# key: " + key + ", value: " + getValue() + "}";
}
@Override
public boolean equals(final Object obj) {
if(obj == null) {
return false;
}
if(!(obj instanceof BaseKeyValueOperation)) {
return false;
}
BaseKeyValueOperation<?, ?> other = (BaseKeyValueOperation<?, ?>) obj;
if(this.getOpCode() != other.getOpCode()) {
return false;
}
if(!this.getKey().equals(other.getKey())) {
return false;
}
if(!this.getValue().equals(other.getValue())) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = getOpCode().hashCode();
hash = hash * 31 + key.hashCode();
hash = hash * 31 + getValue().hashCode();
return hash;
}
@Override
public long timeStamp() {
if (!isExpiryAvailable()) {
return this.timeStamp;
} else {
throw new RuntimeException("Timestamp not available");
}
}
@Override
public boolean isExpiryAvailable() {
return timeStamp < 0;
}
@Override
public long expirationTime() {
if (isExpiryAvailable()) {
return - this.timeStamp;
} else {
throw new RuntimeException("Expiry not available");
}
}
}