From 50dd5352df683aeff5f7aedbcd44266d8ebfc103 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 2 Sep 2023 19:16:26 +0200 Subject: [PATCH] WIP --- .../persistence/inmemory/ShardedChatHome.java | 7 ++- .../persistence/inmemory/SimpleChatHome.java | 44 ++++++++++++------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHome.java index ac7a9808..ab3969f5 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/ShardedChatHome.java @@ -3,6 +3,7 @@ package de.juplo.kafka.chat.backend.persistence.inmemory; import de.juplo.kafka.chat.backend.domain.ChatHome; import de.juplo.kafka.chat.backend.domain.ChatRoom; import de.juplo.kafka.chat.backend.domain.ShardNotOwnedException; +import de.juplo.kafka.chat.backend.domain.UnknownChatroomException; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -46,7 +47,11 @@ public class ShardedChatHome implements ChatHome int shard = selectShard(id); return chatHomes[shard] == null ? Mono.error(new ShardNotOwnedException(shard)) - : chatHomes[shard].getChatRoom(id); + : chatHomes[shard] + .getChatRoom(id) + .onErrorMap(throwable -> throwable instanceof UnknownChatroomException + ? Mono.error(new UnknownChatroomException(id, shard, ownedShards)) + : Mono.error(throwable)); } @Override diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHome.java index f99bc9d8..3048aa50 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/SimpleChatHome.java @@ -13,37 +13,51 @@ import java.util.*; @Slf4j public class SimpleChatHome implements ChatHome { - private final InMemoryChatHomeService service; private final int shard; + private final Map chatrooms; - public SimpleChatHome(InMemoryChatHomeService service, int shard) + + public SimpleChatHome( + int shard, + Flux chatroomFlux) { log.info("Created SimpleChatHome for shard {}", shard); - this.service = service; - this.shard = shard; - } - public SimpleChatHome(InMemoryChatHomeService service) - { - this(service, 0); + this.chatrooms = new HashMap<>(); + chatroomFlux + .filter(chatRoom -> + { + if (shard > -1 && chatRoom.getShard() == shard) + { + return true; + } + else + { + log.info( + "SimpleChatHome for shard {} ignores not owned chat-room {}", + shard, + chatRoom); + return false; + } + }) + .toStream() + .forEach(chatroom -> chatrooms.put(chatroom.getId(), chatroom)); + this.shard = shard; } @Override public Mono getChatRoom(UUID id) { - return service - .getChatRoom(shard, id) - .switchIfEmpty(Mono.error(() -> new UnknownChatroomException( - id, - shard, - service.getOwnedShards()))); + return Mono + .justOrEmpty(chatrooms.get(id)) + .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id))); } @Override public Flux getChatRooms() { - return service.getChatRooms(shard); + return Flux.fromIterable(chatrooms.values()); } } -- 2.20.1