From: Kai Moritz Date: Fri, 13 Jan 2023 19:37:36 +0000 (+0100) Subject: refactor: DRY für shard-selection X-Git-Tag: wip-sharding~32 X-Git-Url: https://juplo.de/gitweb/?a=commitdiff_plain;ds=sidebyside;h=5e0a716e62d5ab3820573c59a5fa5db7355844a4;p=demos%2Fkafka%2Fchat refactor: DRY für shard-selection --- 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); + } }