
io.activej.crdt.util.CrdtDataBinarySerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of activej-crdt Show documentation
Show all versions of activej-crdt Show documentation
Conflict-free replicated data type implementation
The newest version!
/*
* Copyright (C) 2020 ActiveJ LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.activej.crdt.util;
import io.activej.crdt.CrdtData;
import io.activej.crdt.CrdtTombstone;
import io.activej.serializer.*;
public final class CrdtDataBinarySerializer, S> implements BinarySerializer> {
public static final BinarySerializer TIMESTAMP_SERIALIZER = BinarySerializers.LONG_SERIALIZER;
private final BinarySerializer keySerializer;
private final BinarySerializer stateSerializer;
private final BinarySerializer> tombstoneSerializer;
public CrdtDataBinarySerializer(BinarySerializer keySerializer, BinarySerializer stateSerializer) {
this.keySerializer = keySerializer;
this.stateSerializer = stateSerializer;
this.tombstoneSerializer = createTombstoneSerializer(keySerializer);
}
public BinarySerializer getKeySerializer() {
return keySerializer;
}
public BinarySerializer getStateSerializer() {
return stateSerializer;
}
public BinarySerializer> getTombstoneSerializer() {
return tombstoneSerializer;
}
@Override
public void encode(BinaryOutput out, CrdtData item) {
keySerializer.encode(out, item.getKey());
TIMESTAMP_SERIALIZER.encode(out, item.getTimestamp());
stateSerializer.encode(out, item.getState());
}
@Override
public CrdtData decode(BinaryInput in) {
return new CrdtData<>(keySerializer.decode(in), TIMESTAMP_SERIALIZER.decode(in), stateSerializer.decode(in));
}
private static > BinarySerializer> createTombstoneSerializer(BinarySerializer keySerializer) {
return new BinarySerializer<>() {
@Override
public void encode(BinaryOutput out, CrdtTombstone item) {
keySerializer.encode(out, item.getKey());
TIMESTAMP_SERIALIZER.encode(out, item.getTimestamp());
}
@Override
public CrdtTombstone decode(BinaryInput in) throws CorruptedDataException {
return new CrdtTombstone<>(keySerializer.decode(in), TIMESTAMP_SERIALIZER.decode(in));
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy