1 package de.juplo.kafka.chat.backend.implementation.inmemory;
3 import de.juplo.kafka.chat.backend.domain.*;
4 import de.juplo.kafka.chat.backend.domain.exceptions.UnknownChatroomException;
5 import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
6 import lombok.extern.slf4j.Slf4j;
7 import reactor.core.publisher.Flux;
8 import reactor.core.publisher.Mono;
10 import java.time.Clock;
15 public class SimpleChatHomeService implements ChatHomeService
17 private final Integer shard;
18 private final Map<UUID, ChatRoomInfo> chatRoomInfo;
19 private final Map<UUID, ChatRoomData> chatRoomData;
20 private final Clock clock;
21 private final int bufferSize;
25 public SimpleChatHomeService(
26 StorageStrategy storageStrategy,
37 public SimpleChatHomeService(
39 StorageStrategy storageStrategy,
43 log.info("Created SimpleChatHome for shard {}", shard);
46 this.chatRoomInfo = new HashMap<>();
47 this.chatRoomData = new HashMap<>();
52 if (shard == null || info.getShard() == shard)
59 "SimpleChatHome for shard {} ignores not owned chat-room {}",
68 UUID chatRoomId = info.getId();
69 chatRoomInfo.put(chatRoomId, info);
70 Flux<Message> messageFlux =
71 storageStrategy.readChatRoomData(chatRoomId);
76 new InMemoryChatMessageService(messageFlux),
80 this.bufferSize = bufferSize;
85 public Mono<ChatRoomInfo> createChatRoom(UUID id, String name)
87 log.info("Creating ChatRoom with buffer-size {}", bufferSize);
88 ChatMessageService service = new InMemoryChatMessageService(Flux.empty());
89 ChatRoomInfo chatRoomInfo = new ChatRoomInfo(id, name, shard);
90 this.chatRoomInfo.put(id, chatRoomInfo);
91 ChatRoomData chatRoomData = new ChatRoomData(clock, service, bufferSize);
92 this.chatRoomData.put(id, chatRoomData);
93 return Mono.just(chatRoomInfo);
97 public Mono<ChatRoomInfo> getChatRoomInfo(UUID id)
100 .justOrEmpty(chatRoomInfo.get(id))
101 .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id)));
105 public Flux<ChatRoomInfo> getChatRoomInfo()
107 return Flux.fromIterable(chatRoomInfo.values());
111 public Mono<ChatRoomData> getChatRoomData(UUID id)
114 .justOrEmpty(chatRoomData.get(id))
115 .switchIfEmpty(Mono.error(() -> new UnknownChatroomException(id)));