WIP
authorKai Moritz <kai@juplo.de>
Sun, 20 Aug 2023 08:47:41 +0000 (10:47 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 20 Aug 2023 08:47:41 +0000 (10:47 +0200)
src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/chat/backend/KafkaConfigurationIT.java

diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java
new file mode 100644 (file)
index 0000000..a67d629
--- /dev/null
@@ -0,0 +1,80 @@
+package de.juplo.kafka.chat.backend.persistence.kafka;
+
+import de.juplo.kafka.chat.backend.domain.ChatHomeService;
+import de.juplo.kafka.chat.backend.domain.ChatRoom;
+import lombok.extern.slf4j.Slf4j;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+import java.util.*;
+import java.util.stream.IntStream;
+
+
+@Slf4j
+public class KafkaChatHomeService implements ChatHomeService
+{
+  private final Map<UUID, ChatRoom>[] chatrooms;
+
+
+  public KafkaChatHomeService(
+      int numShards,
+      int[] ownedShards,
+      Flux<ChatRoom> chatroomFlux)
+  {
+    log.debug("Creating InMemoryChatHomeService");
+    this.chatrooms = new Map[numShards];
+    Set<Integer> owned = Arrays
+        .stream(ownedShards)
+        .collect(
+            () -> new HashSet<>(),
+            (set, i) -> set.add(i),
+            (a, b) -> a.addAll(b));
+    for (int shard = 0; shard < numShards; shard++)
+    {
+      chatrooms[shard] = owned.contains(shard)
+          ? new HashMap<>()
+          : null;
+    }
+    chatroomFlux
+        .filter(chatRoom ->
+        {
+          if (owned.contains(chatRoom.getShard()))
+          {
+            return true;
+          }
+          else
+          {
+            log.info("Ignoring not owned chat-room {}", chatRoom);
+            return false;
+          }
+        })
+        .toStream()
+        .forEach(chatroom -> chatrooms[chatroom.getShard()].put(chatroom.getId(), chatroom));
+  }
+
+  public void putChatRoom(ChatRoom chatRoom)
+  {
+    chatrooms[chatRoom.getShard()].put(chatRoom.getId(), chatRoom);
+  }
+
+  @Override
+  public Mono<ChatRoom> getChatRoom(int shard, UUID id)
+  {
+    return Mono.justOrEmpty(chatrooms[shard].get(id));
+  }
+
+  @Override
+  public int[] getOwnedShards()
+  {
+    return IntStream
+        .range(0, chatrooms.length)
+        .filter(i -> chatrooms[i] != null)
+        .toArray();
+  }
+
+  @Override
+  public Flux<ChatRoom> getChatRooms(int shard)
+  {
+    return Flux.fromStream(chatrooms[shard].values().stream());
+  }
+}
index 38c170e..e56c660 100644 (file)
@@ -25,7 +25,7 @@ import static de.juplo.kafka.chat.backend.KafkaConfigurationIT.MESSAGES_TOPIC;
         "chat.backend.kafka.message-channel-topic=" + MESSAGES_TOPIC,
         "chat.backend.kafka.num-partitions=3" })
 @EmbeddedKafka(topics = { CHATROOMS_TOPIC, MESSAGES_TOPIC }, partitions = 3)
-class KafkaConfigurationIT extends AbstractConfigurationIT
+class KafkaConfigurationIT extends AbstractConfigurationWithShardingIT
 {
   final static String CHATROOMS_TOPIC = "TEST_CHATROOM_CHANNEL";
   final static String MESSAGES_TOPIC = "TEST_MESSAGE_CHANNEL";