WIP
authorKai Moritz <kai@juplo.de>
Fri, 24 Feb 2023 10:12:00 +0000 (11:12 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 24 Feb 2023 10:12:00 +0000 (11:12 +0100)
src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java
src/main/java/de/juplo/kafka/chat/backend/api/ChatRoomsTo.java [new file with mode: 0644]

index 8e1ff9e..4bcc542 100644 (file)
@@ -33,7 +33,7 @@ public class ChatBackendApplication implements WebFluxConfigurer
        public void onExit()
        {
                for (int shard = 0; shard < chatHomes.length; shard++)
-                       storageStrategy.write(chatHomes[shard].getChatRooms());
+                       storageStrategy.write(chatHomes[shard].getChatRooms(shard));
        }
 
        public static void main(String[] args)
index 4db77ee..b17050a 100644 (file)
@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.*;
 import reactor.core.publisher.Flux;
 import reactor.core.publisher.Mono;
 
+import java.util.Arrays;
 import java.util.UUID;
 
 
@@ -20,6 +21,7 @@ public class ChatBackendController
   private final ChatHome chatHome;
   private final ChatRoomFactory factory;
   private final StorageStrategy storageStrategy;
+  private final int[] ownedShards;
 
 
   @PostMapping("create")
@@ -31,11 +33,21 @@ public class ChatBackendController
         .map(ChatRoomInfoTo::from);
   }
 
-  @GetMapping("list")
-  public Flux<ChatRoomInfoTo> list()
+
+  @GetMapping("list/{shard}")
+  public Flux<ChatRoomInfoTo> list(@PathVariable int shard)
   {
     return chatHome
-        .getChatRooms()
+        .getChatRooms(shard)
+        .map(chatroom -> ChatRoomInfoTo.from(chatroom));
+  }
+
+  @GetMapping("list")
+  public Mono<ChatRoomsTo> list()
+  {
+    return Arrays
+        .stream(ownedShards)
+        .mapToObj(shard -> chatHome.getChatRooms(shard))
         .map(chatroom -> ChatRoomInfoTo.from(chatroom));
   }
 
@@ -133,6 +145,8 @@ public class ChatBackendController
   @PostMapping("/store")
   public void store()
   {
-    storageStrategy.write(chatHome.getChatRooms());
+    Arrays
+        .stream(ownedShards)
+        .forEach(i -> storageStrategy.write(chatHome.getChatRooms(i))); // TODO: Sharded Storage
   }
 }
diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatRoomsTo.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatRoomsTo.java
new file mode 100644 (file)
index 0000000..6de0fbd
--- /dev/null
@@ -0,0 +1,13 @@
+package de.juplo.kafka.chat.backend.api;
+
+import lombok.Value;
+
+import java.util.List;
+import java.util.Map;
+
+
+@Value
+public class ChatRoomsTo
+{
+  Map<Integer, List<ChatRoomInfoTo>> chatRooms;
+}