From 11f43f0154e680f06dfe75adf865fc12cb124a23 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 24 Feb 2023 11:12:00 +0100 Subject: [PATCH] WIP --- .../chat/backend/ChatBackendApplication.java | 2 +- .../backend/api/ChatBackendController.java | 22 +++++++++++++++---- .../kafka/chat/backend/api/ChatRoomsTo.java | 13 +++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/juplo/kafka/chat/backend/api/ChatRoomsTo.java diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java index 8e1ff9e5..4bcc542c 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java @@ -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) diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java index 4db77ee2..b17050ad 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java @@ -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 list() + + @GetMapping("list/{shard}") + public Flux list(@PathVariable int shard) { return chatHome - .getChatRooms() + .getChatRooms(shard) + .map(chatroom -> ChatRoomInfoTo.from(chatroom)); + } + + @GetMapping("list") + public Mono 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 index 00000000..6de0fbdf --- /dev/null +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatRoomsTo.java @@ -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> chatRooms; +} -- 2.20.1