From 5e0a716e62d5ab3820573c59a5fa5db7355844a4 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 13 Jan 2023 20:37:36 +0100 Subject: [PATCH] =?utf8?q?refactor:=20DRY=20f=C3=BCr=20shard-selection?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../backend/api/ChatBackendController.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) 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 1c0224bc..9be7fa16 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 @@ -45,8 +45,7 @@ public class ChatBackendController @GetMapping("{chatroomId}/list") public Flux list(@PathVariable UUID chatroomId) { - int shard = selectionStrategy.selectShard(chatroomId); - return chatHomes[shard] + return chatHomes[selectShard(chatroomId)] .getChatRoom(chatroomId) .flatMapMany(chatroom -> chatroom .getMessages() @@ -56,8 +55,7 @@ public class ChatBackendController @GetMapping("{chatroomId}") public Mono get(@PathVariable UUID chatroomId) { - int shard = selectionStrategy.selectShard(chatroomId); - return chatHomes[shard] + return chatHomes[selectShard(chatroomId)] .getChatRoom(chatroomId) .map(chatroom -> ChatRoomTo.from(chatroom)); } @@ -69,9 +67,8 @@ public class ChatBackendController @PathVariable Long messageId, @RequestBody String text) { - int shard = selectionStrategy.selectShard(chatroomId); return - chatHomes[shard] + chatHomes[selectShard(chatroomId)] .getChatRoom(chatroomId) .flatMap(chatroom -> put(chatroom, username, messageId, text)); } @@ -97,9 +94,8 @@ public class ChatBackendController @PathVariable String username, @PathVariable Long messageId) { - int shard = selectionStrategy.selectShard(chatroomId); return - chatHomes[shard] + chatHomes[selectShard(chatroomId)] .getChatRoom(chatroomId) .flatMap(chatroom -> get(chatroom, username, messageId)); } @@ -118,8 +114,7 @@ public class ChatBackendController @GetMapping(path = "{chatroomId}/listen") public Flux> listen(@PathVariable UUID chatroomId) { - int shard = selectionStrategy.selectShard(chatroomId); - return chatHomes[shard] + return chatHomes[selectShard(chatroomId)] .getChatRoom(chatroomId) .flatMapMany(chatroom -> listen(chatroom)); } @@ -144,4 +139,9 @@ public class ChatBackendController for (int shard = 0; shard < chatHomes.length; shard++) storageStrategy.write(chatHomes[shard].getChatRooms()); } + + private int selectShard(UUID chatroomId) + { + return selectionStrategy.selectShard(chatroomId); + } } -- 2.20.1