-
Notifications
You must be signed in to change notification settings - Fork 214
/
CompressedDDataHandler.java
100 lines (87 loc) · 3.64 KB
/
CompressedDDataHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.pubsub.ddata.compressed;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import org.eclipse.ditto.internal.utils.ddata.DistributedDataConfig;
import org.eclipse.ditto.internal.utils.pubsub.config.PubSubConfig;
import org.eclipse.ditto.internal.utils.pubsub.ddata.AbstractDDataHandler;
import org.eclipse.ditto.internal.utils.pubsub.ddata.Hashes;
import org.eclipse.ditto.internal.utils.pubsub.ddata.literal.LiteralUpdate;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorRefFactory;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Address;
import org.apache.pekko.cluster.ddata.ORMultiMap;
import org.apache.pekko.cluster.ddata.Replicator;
/**
* A distributed collection of hashes of strings indexed by ActorRef.
* The hash functions for all filter should be identical.
*/
public final class CompressedDDataHandler extends AbstractDDataHandler<ActorRef, String, LiteralUpdate>
implements Hashes {
private final List<Integer> seeds;
private CompressedDDataHandler(final DistributedDataConfig config,
final ActorRefFactory actorRefFactory,
final ActorSystem actorSystem,
final Executor ddataExecutor,
final String topicType,
final List<Integer> seeds) {
super(config, actorRefFactory, actorSystem, ddataExecutor, topicType);
this.seeds = seeds;
}
/**
* Start distributed-data replicator for compressed topics under an actor system's user guardian using the default
* dispatcher.
*
* @param system the actor system.
* @param ddataConfig the distributed data config.
* @param topicType the type of messages, typically "message-type-name-aware".
* @param pubSubConfig the pub-sub config.
* @return access to the distributed data.
*/
public static CompressedDDataHandler create(final ActorSystem system, final DistributedDataConfig ddataConfig,
final String topicType, final PubSubConfig pubSubConfig) {
final List<Integer> seeds =
Hashes.digestStringsToIntegers(pubSubConfig.getSeed(), HASH_FAMILY_SIZE);
return new CompressedDDataHandler(ddataConfig, system, system, system.dispatcher(), topicType, seeds);
}
@Override
public List<Integer> getSeeds() {
return seeds;
}
/**
* Lossy-compress a topic into a ByteString consisting of hash codes from the family of hash functions.
*
* @param topic the topic.
* @return the compressed topic.
*/
@Override
public long approximate(final String topic) {
return hashAsLong(topic);
}
@Override
public CompletionStage<Void> removeAddress(final Address address,
final Replicator.WriteConsistency writeConsistency) {
return update(getKey(address), writeConsistency, mmap -> {
ORMultiMap<ActorRef, String> result = mmap;
for (final ActorRef subscriber : mmap.getEntries().keySet()) {
if (subscriber.path().address().equals(address)) {
result = result.remove(selfUniqueAddress, subscriber);
}
}
return result;
});
}
}