NG
authorKai Moritz <kai@juplo.de>
Fri, 18 Aug 2023 14:10:45 +0000 (16:10 +0200)
committerKai Moritz <kai@juplo.de>
Fri, 18 Aug 2023 15:18:41 +0000 (17:18 +0200)
src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/ChatMessageChannel.java
src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHome.java
src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatRoomFactory.java

index b473a41..7f885e6 100644 (file)
@@ -362,4 +362,12 @@ public class ChatMessageChannel implements Runnable, ConsumerRebalanceListener
   {
     return Mono.justOrEmpty(chatrooms[shard].get(id));
   }
+
+  Flux<ChatRoom> getChatRooms()
+  {
+    return Flux
+        .fromStream(IntStream.range(0, numShards).mapToObj(i -> Integer.valueOf(i)))
+        .filter(shard -> isShardOwned[shard])
+        .flatMap(shard -> Flux.fromIterable(chatrooms[shard].values()));
+  }
 }
index 2bccc3a..7189362 100644 (file)
@@ -4,6 +4,7 @@ import de.juplo.kafka.chat.backend.domain.ChatHome;
 import de.juplo.kafka.chat.backend.domain.ChatRoom;
 import de.juplo.kafka.chat.backend.domain.ChatRoomInfo;
 import de.juplo.kafka.chat.backend.domain.ShardingStrategy;
+import de.juplo.kafka.chat.backend.persistence.KafkaLikeShardingStrategy;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import reactor.core.publisher.Flux;
@@ -16,11 +17,19 @@ import java.util.*;
 @Slf4j
 public class KafkaChatHome implements ChatHome
 {
-  private final ShardingStrategy shardingStrategy;
-  private final ChatRoomChannel chatRoomChannel;
+  private final KafkaLikeShardingStrategy shardingStrategy;
   private final ChatMessageChannel chatMessageChanel;
 
 
+  public KafkaChatHome(
+      int numPartitions,
+      ChatMessageChannel chatMessageChannel)
+  {
+    this.shardingStrategy = new KafkaLikeShardingStrategy(numPartitions);
+    this.chatMessageChanel = chatMessageChannel;
+  }
+
+
   @Override
   public Mono<ChatRoom> getChatRoom(UUID id)
   {
@@ -38,6 +47,6 @@ public class KafkaChatHome implements ChatHome
   @Override
   public Flux<ChatRoom> getChatRooms()
   {
-      return chatRoomChannel.getChatRooms();
+      return chatMessageChanel.getChatRooms();
   }
 }
index 825f16e..c46529d 100644 (file)
@@ -13,12 +13,12 @@ import java.util.UUID;
 @Slf4j
 public class KafkaChatRoomFactory implements ChatRoomFactory
 {
-  private final ChatRoomChannel chatRoomChannel;
+  private final ChatMessageChannel chatMessageChannel;
 
   @Override
   public Mono<ChatRoomInfo> createChatRoom(UUID id, String name)
   {
     log.info("Sending create-request for chat rooom: id={}, name={}");
-    return chatRoomChannel.sendCreateChatRoomRequest(id, name);
+    return chatMessageChannel.sendCreateChatRoomRequest(id, name);
   }
 }