/
TestPubSubFactory.java
88 lines (75 loc) · 3.6 KB
/
TestPubSubFactory.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
/*
* 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.services.utils.pubsub;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.eclipse.ditto.services.utils.pubsub.config.PubSubConfig;
import org.eclipse.ditto.services.utils.pubsub.ddata.DDataReader;
import org.eclipse.ditto.services.utils.pubsub.ddata.Hashes;
import org.eclipse.ditto.services.utils.pubsub.extractors.AckExtractor;
import org.eclipse.ditto.services.utils.pubsub.extractors.PubSubTopicExtractor;
import akka.actor.ActorContext;
import akka.actor.ActorRef;
/**
* Pub-sub factory for tests. Messages are strings. Topics of a message are its prefixes.
*/
public final class TestPubSubFactory extends AbstractPubSubFactory<String> implements Hashes {
private static final DDataProvider PROVIDER = DDataProvider.of("dc-default");
private static final LiteralDDataProvider ACKS_PROVIDER = LiteralDDataProvider.of("dc-default", "acks");
private final Collection<Integer> seeds;
private TestPubSubFactory(final ActorContext context, final Class<String> messageClass,
final PubSubTopicExtractor<String> topicExtractor,
final AckExtractor<String> ackExtractor,
final DistributedAcks distributedAcks) {
super(context, messageClass, topicExtractor, PROVIDER, ackExtractor, distributedAcks);
final PubSubConfig config = PubSubConfig.of(context.system().settings().config().getConfig("ditto.pubsub"));
seeds = Hashes.digestStringsToIntegers(config.getSeed(), config.getHashFamilySize());
}
static DistributedAcks startDistributedAcks(final ActorContext context) {
return DistributedAcksImpl.create(context, "dc-default", ACKS_PROVIDER);
}
static TestPubSubFactory of(final ActorContext context, final AckExtractor<String> ackExtractor,
final DistributedAcks distributedAcks) {
return new TestPubSubFactory(context, String.class, TestPubSubFactory::getPrefixes, ackExtractor,
distributedAcks);
}
/**
* @return subscribers of a topic in the distributed data.
*/
CompletionStage<Collection<ActorRef>> getSubscribers(final String topic) {
return getSubscribers(Collections.singleton(topic), ddata.getReader());
}
/**
* Retrieve subscribers of a collection of topics from the distributed data.
* Useful for circumventing lackluster existential type implementation when the reader type parameter isn't known.
*
* @param topics the topics.
* @return subscribers of those topics in the distributed data.
*/
private static <T> CompletionStage<Collection<ActorRef>> getSubscribers(
final Collection<String> topics, final DDataReader<ActorRef, T> reader) {
return reader.getSubscribers(topics.stream().map(reader::approximate).collect(Collectors.toSet()));
}
@Override
public Collection<Integer> getSeeds() {
return seeds;
}
private static Collection<String> getPrefixes(final String string) {
return IntStream.range(0, string.length())
.mapToObj(i -> string.substring(0, i + 1))
.collect(Collectors.toList());
}
}