refactor: DRY für shard-selection
authorKai Moritz <kai@juplo.de>
Fri, 13 Jan 2023 19:37:36 +0000 (20:37 +0100)
committerKai Moritz <kai@juplo.de>
Wed, 25 Jan 2023 21:00:58 +0000 (22:00 +0100)
src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java

index 1c0224b..9be7fa1 100644 (file)
@@ -45,8 +45,7 @@ public class ChatBackendController
   @GetMapping("{chatroomId}/list")
   public Flux<MessageTo> 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<ChatRoomTo> 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<ServerSentEvent<MessageTo>> 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);
+  }
 }