io.datakernel.crdt.primitives.LWWObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-crdt Show documentation
Show all versions of datakernel-crdt Show documentation
Conflict-free replicated data type implementation for DataKernel Framework.
package io.datakernel.crdt.primitives;
import io.datakernel.serializer.BinarySerializer;
import io.datakernel.serializer.util.BinaryInput;
import io.datakernel.serializer.util.BinaryOutput;
import org.jetbrains.annotations.Nullable;
public class LWWObject implements CrdtType> {
private final long timestamp;
private final T object;
public LWWObject(long timestamp, T object) {
this.timestamp = timestamp;
this.object = object;
}
public T getObject() {
return object;
}
public long getTimestamp() {
return timestamp;
}
@Override
public LWWObject merge(LWWObject other) {
if (timestamp < other.timestamp) {
return other;
}
return this;
}
@Override
@Nullable
public LWWObject extract(long timestamp) {
if (timestamp >= this.timestamp) {
return null;
}
return this;
}
public static class Serializer implements BinarySerializer> {
private final BinarySerializer valueSerializer;
public Serializer(BinarySerializer valueSerializer) {
this.valueSerializer = valueSerializer;
}
@Override
public void encode(BinaryOutput out, LWWObject item) {
out.writeLong(item.timestamp);
valueSerializer.encode(out, item.object);
}
@Override
public LWWObject decode(BinaryInput in) {
return new LWWObject<>(in.readLong(), valueSerializer.decode(in));
}
}
}