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
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the Apache Pekko project, derived from Akka.
|
|
|
|
|
*/
|
|
|
|
|
|
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
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.ddata.TwoPhaseSet;
|
2015-07-02 12:12:34 +02:00
|
|
|
import docs.ddata.protobuf.msg.TwoPhaseSetMessages;
|
|
|
|
|
import docs.ddata.protobuf.msg.TwoPhaseSetMessages.TwoPhaseSet2.Builder;
|
|
|
|
|
|
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;
|
|
|
|
|
import org.apache.pekko.cluster.ddata.protobuf.ReplicatedDataSerializer;
|
2015-07-02 12:12:34 +02:00
|
|
|
|
|
|
|
|
public class TwoPhaseSetSerializer2 extends AbstractSerializationSupport {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
private final ExtendedActorSystem system;
|
|
|
|
|
private final ReplicatedDataSerializer replicatedDataSerializer;
|
|
|
|
|
|
|
|
|
|
public TwoPhaseSetSerializer2(ExtendedActorSystem system) {
|
|
|
|
|
this.system = system;
|
|
|
|
|
this.replicatedDataSerializer = new ReplicatedDataSerializer(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) {
|
|
|
|
|
if (obj instanceof TwoPhaseSet) {
|
|
|
|
|
return twoPhaseSetToProto((TwoPhaseSet) obj).toByteArray();
|
|
|
|
|
} 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.TwoPhaseSet2 twoPhaseSetToProto(TwoPhaseSet twoPhaseSet) {
|
|
|
|
|
Builder b = TwoPhaseSetMessages.TwoPhaseSet2.newBuilder();
|
|
|
|
|
if (!twoPhaseSet.adds.isEmpty())
|
|
|
|
|
b.setAdds(otherMessageToProto(twoPhaseSet.adds).toByteString());
|
|
|
|
|
if (!twoPhaseSet.removals.isEmpty())
|
|
|
|
|
b.setRemovals(otherMessageToProto(twoPhaseSet.removals).toByteString());
|
|
|
|
|
return b.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
protected TwoPhaseSet twoPhaseSetFromBinary(byte[] bytes) {
|
|
|
|
|
try {
|
|
|
|
|
TwoPhaseSetMessages.TwoPhaseSet2 msg = TwoPhaseSetMessages.TwoPhaseSet2.parseFrom(bytes);
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
GSet<String> adds = GSet.create();
|
|
|
|
|
if (msg.hasAdds()) adds = (GSet<String>) otherMessageFromBinary(msg.getAdds().toByteArray());
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
GSet<String> removals = GSet.create();
|
|
|
|
|
if (msg.hasRemovals())
|
|
|
|
|
adds = (GSet<String>) otherMessageFromBinary(msg.getRemovals().toByteArray());
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
return new TwoPhaseSet(adds, removals);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// #serializer
|