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;
|
2015-07-02 12:12:34 +02:00
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
2022-11-12 10:21:24 +01:00
|
|
|
import org.apache.pekko.cluster.ddata.AbstractReplicatedData;
|
|
|
|
|
import org.apache.pekko.cluster.ddata.GSet;
|
2015-07-02 12:12:34 +02:00
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #twophaseset
|
2015-07-02 12:12:34 +02:00
|
|
|
public class TwoPhaseSet extends AbstractReplicatedData<TwoPhaseSet> {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
public final GSet<String> adds;
|
|
|
|
|
public final GSet<String> removals;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
public TwoPhaseSet(GSet<String> adds, GSet<String> removals) {
|
|
|
|
|
this.adds = adds;
|
|
|
|
|
this.removals = removals;
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
public static TwoPhaseSet create() {
|
|
|
|
|
return new TwoPhaseSet(GSet.create(), GSet.create());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TwoPhaseSet add(String element) {
|
|
|
|
|
return new TwoPhaseSet(adds.add(element), removals);
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2015-07-02 12:12:34 +02:00
|
|
|
public TwoPhaseSet remove(String element) {
|
|
|
|
|
return new TwoPhaseSet(adds, removals.add(element));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Set<String> getElements() {
|
|
|
|
|
Set<String> result = new HashSet<>(adds.getElements());
|
|
|
|
|
result.removeAll(removals.getElements());
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public TwoPhaseSet mergeData(TwoPhaseSet that) {
|
2019-01-12 04:00:53 +08:00
|
|
|
return new TwoPhaseSet(this.adds.merge(that.adds), this.removals.merge(that.removals));
|
2015-07-02 12:12:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
// #twophaseset
|