From 272aaac28826436bc6aa17ef9d0c1e733399c221 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 23 Jan 2023 17:20:35 +0100 Subject: [PATCH] WIP --- .../chat/backend/domain/SimpleChatHome.java | 1 - .../inmemory/InMemoryChatHomeService.java | 2 +- .../kafka/KafkaChatHomeService.java | 50 +++++++++++++++++-- .../kafka/KafkaChatRoomService.java | 32 ++++++++++-- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java b/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java index bb2e158f..46802c69 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java +++ b/src/main/java/de/juplo/kafka/chat/backend/domain/SimpleChatHome.java @@ -1,6 +1,5 @@ package de.juplo.kafka.chat.backend.domain; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeService.java index 0f39fba3..fd54d34c 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryChatHomeService.java @@ -20,7 +20,7 @@ public class InMemoryChatHomeService implements ChatHomeService int[] ownedShards, Flux chatroomFlux) { - log.debug("Creating ChatHomeService"); + log.debug("Creating InMemoryChatHomeService"); this.chatrooms = new Map[numShards]; Set owned = Arrays .stream(ownedShards) diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java index 24cd0219..0e18f685 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatHomeService.java @@ -2,29 +2,71 @@ package de.juplo.kafka.chat.backend.persistence.kafka; import de.juplo.kafka.chat.backend.domain.ChatHomeService; import de.juplo.kafka.chat.backend.domain.ChatRoom; +import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.util.UUID; +import java.util.*; +@Slf4j public class KafkaChatHomeService implements ChatHomeService { + private final Map[] chatrooms; + + + public KafkaChatHomeService( + int numShards, + int[] ownedShards, + Flux chatroomFlux) + { + log.debug("Creating ChatHomeService"); + this.chatrooms = new Map[numShards]; + Set owned = Arrays + .stream(ownedShards) + .collect( + () -> new HashSet<>(), + (set, i) -> set.add(i), + (a, b) -> a.addAll(b)); + for (int shard = 0; shard < numShards; shard++) + { + chatrooms[shard] = owned.contains(shard) + ? new HashMap<>() + : null; + } + chatroomFlux + .filter(chatRoom -> + { + if (owned.contains(chatRoom.getShard())) + { + return true; + } + else + { + log.info("Ignoring not owned chat-room {}", chatRoom); + return false; + } + }) + .toStream() + .forEach(chatroom -> chatrooms[chatroom.getShard()].put(chatroom.getId(), chatroom)); + } + @Override public Mono putChatRoom(ChatRoom chatRoom) { - return null; + chatrooms[chatRoom.getShard()].put(chatRoom.getId(), chatRoom); + return Mono.just(chatRoom); } @Override public Mono getChatRoom(int shard, UUID id) { - return null; + return Mono.justOrEmpty(chatrooms[shard].get(id)); } @Override public Flux getChatRooms(int shard) { - return null; + return Flux.fromStream(chatrooms[shard].values().stream()); } } diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatRoomService.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatRoomService.java index 5ab23fe9..024f9aae 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatRoomService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/kafka/KafkaChatRoomService.java @@ -2,6 +2,7 @@ package de.juplo.kafka.chat.backend.persistence.kafka; import de.juplo.kafka.chat.backend.domain.ChatRoomService; import de.juplo.kafka.chat.backend.domain.Message; +import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -9,25 +10,46 @@ import java.time.LocalDateTime; import java.util.LinkedHashMap; +@Slf4j public class KafkaChatRoomService implements ChatRoomService { - private final LinkedHashMap messages = null; + private final LinkedHashMap messages; + + + public KafkaChatRoomService(Flux messageFlux) + { + log.debug("Creating KafkaChatRoomService"); + messages = new LinkedHashMap<>(); + messageFlux.subscribe(message -> messages.put(message.getKey(), message)); + } @Override - public Message persistMessage(Message.MessageKey key, LocalDateTime timestamp, String text) + public Message persistMessage( + Message.MessageKey key, + LocalDateTime timestamp, + String text) { - return null; + Message message = new Message(key, (long)messages.size(), timestamp, text); + messages.put(message.getKey(), message); + return message; } @Override public Mono getMessage(Message.MessageKey key) { - return null; + return Mono.fromSupplier(() -> messages.get(key)); } @Override public Flux getMessages(long first, long last) { - return null; + return Flux.fromStream(messages + .values() + .stream() + .filter(message -> + { + long serial = message.getSerialNumber(); + return serial >= first && serial <= last; + })); } } -- 2.20.1