2023-01-08 17:13:31 +08:00
|
|
|
/*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* license agreements; and to You under the Apache License, version 2.0:
|
|
|
|
|
*
|
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
2023-06-22 14:19:26 +01:00
|
|
|
* This file is part of the Apache Pekko project, which was derived from Akka.
|
2023-01-08 17:13:31 +08:00
|
|
|
*/
|
|
|
|
|
|
2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2015-2022 Lightbend Inc. <https://www.lightbend.com>
|
2015-07-02 12:12:34 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.ddata.protobuf;
|
2015-07-02 12:12:34 +02:00
|
|
|
|
|
|
|
|
// #serializer
|
|
|
|
|
import docs.ddata.protobuf.msg.TwoPhaseSetMessages;
|
|
|
|
|
import docs.ddata.protobuf.msg.TwoPhaseSetMessages.TwoPhaseSet.Builder;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.ddata.TwoPhaseSet;
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.actor.ExtendedActorSystem;
|
|
|
|
|
import org.apache.pekko.cluster.ddata.GSet;
|
|
|
|
|
import org.apache.pekko.cluster.ddata.protobuf.AbstractSerializationSupport;
|
2015-07-02 12:12:34 +02:00
|
|
|
|
|
|
|
|
public class TwoPhaseSetSerializer extends AbstractSerializationSupport {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
private final ExtendedActorSystem system;
|
|
|
|
|
|
|
|
|
|
public TwoPhaseSetSerializer(ExtendedActorSystem system) {
|
|
|
|
|
this.system = system;
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
@Override
|
|
|
|
|
public ExtendedActorSystem system() {
|
|
|
|
|
return this.system;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean includeManifest() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int identifier() {
|
|
|
|
|
return 99998;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public byte[] toBinary(Object obj) {
|
2025-08-25 13:45:13 +01:00
|
|
|
if (obj instanceof TwoPhaseSet tps) {
|
|
|
|
|
return twoPhaseSetToProto(tps).toByteArray();
|
2015-07-02 12:12:34 +02:00
|
|
|
} else {
|
|
|
|
|
throw new IllegalArgumentException("Can't serialize object of type " + obj.getClass());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Object fromBinaryJava(byte[] bytes, Class<?> manifest) {
|
|
|
|
|
return twoPhaseSetFromBinary(bytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected TwoPhaseSetMessages.TwoPhaseSet twoPhaseSetToProto(TwoPhaseSet twoPhaseSet) {
|
|
|
|
|
Builder b = TwoPhaseSetMessages.TwoPhaseSet.newBuilder();
|
|
|
|
|
ArrayList<String> adds = new ArrayList<>(twoPhaseSet.adds.getElements());
|
|
|
|
|
if (!adds.isEmpty()) {
|
|
|
|
|
Collections.sort(adds);
|
|
|
|
|
b.addAllAdds(adds);
|
|
|
|
|
}
|
|
|
|
|
ArrayList<String> removals = new ArrayList<>(twoPhaseSet.removals.getElements());
|
|
|
|
|
if (!removals.isEmpty()) {
|
|
|
|
|
Collections.sort(removals);
|
|
|
|
|
b.addAllRemovals(removals);
|
|
|
|
|
}
|
|
|
|
|
return b.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected TwoPhaseSet twoPhaseSetFromBinary(byte[] bytes) {
|
|
|
|
|
try {
|
|
|
|
|
TwoPhaseSetMessages.TwoPhaseSet msg = TwoPhaseSetMessages.TwoPhaseSet.parseFrom(bytes);
|
|
|
|
|
GSet<String> adds = GSet.create();
|
|
|
|
|
for (String elem : msg.getAddsList()) {
|
|
|
|
|
adds = adds.add(elem);
|
|
|
|
|
}
|
|
|
|
|
GSet<String> removals = GSet.create();
|
|
|
|
|
for (String elem : msg.getRemovalsList()) {
|
|
|
|
|
removals = removals.add(elem);
|
|
|
|
|
}
|
2017-01-26 14:26:25 +01:00
|
|
|
// GSet will accumulate deltas when adding elements,
|
|
|
|
|
// but those are not of interest in the result of the deserialization
|
|
|
|
|
return new TwoPhaseSet(adds.resetDelta(), removals.resetDelta());
|
2015-07-02 12:12:34 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// #serializer
|