1 package de.juplo.kafka.chat.backend.storage.files;
3 import com.fasterxml.jackson.core.JsonGenerator;
4 import com.fasterxml.jackson.databind.JavaType;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo;
7 import de.juplo.kafka.chat.backend.api.MessageTo;
8 import de.juplo.kafka.chat.backend.domain.ChatRoomInfo;
9 import de.juplo.kafka.chat.backend.domain.Message;
10 import de.juplo.kafka.chat.backend.implementation.StorageStrategy;
11 import de.juplo.kafka.chat.backend.implementation.ShardingStrategy;
12 import lombok.RequiredArgsConstructor;
13 import lombok.extern.slf4j.Slf4j;
14 import reactor.core.publisher.Flux;
16 import java.io.IOException;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19 import java.util.UUID;
20 import java.util.logging.Level;
22 import static java.nio.file.StandardOpenOption.CREATE;
23 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
26 @RequiredArgsConstructor
28 public class FilesStorageStrategy implements StorageStrategy
30 public static final String CHATROOMS_FILENAME = "chatrooms.json";
33 private final Path storagePath;
34 private final ShardingStrategy shardingStrategy;
35 private final ObjectMapper mapper;
39 public Flux<ChatRoomInfo> writeChatRoomInfo(Flux<ChatRoomInfo> chatRoomInfoFlux)
41 Path path = chatroomsPath();
42 log.info("Writing chatrooms to {}", path);
45 Files.createDirectories(storagePath);
47 JsonGenerator generator =
50 .createGenerator(Files.newBufferedWriter(path, CREATE, TRUNCATE_EXISTING));
52 return chatRoomInfoFlux
54 FilesStorageStrategy.class.getSimpleName(),
61 generator.useDefaultPrettyPrinter();
62 generator.writeStartArray();
66 throw new RuntimeException(e);
73 generator.writeEndArray();
78 throw new RuntimeException(e);
85 ChatRoomInfoTo chatRoomInfoTo = ChatRoomInfoTo.from(chatRoomInfo);
86 generator.writeObject(chatRoomInfoTo);
91 throw new RuntimeException(e);
97 throw new RuntimeException(e);
102 public Flux<ChatRoomInfo> readChatRoomInfo()
104 JavaType type = mapper.getTypeFactory().constructType(ChatRoomInfoTo.class);
106 .from(new JsonFilePublisher<ChatRoomInfoTo>(chatroomsPath(), mapper, type))
108 FilesStorageStrategy.class.getSimpleName(),
111 .map(chatRoomInfoTo ->
113 UUID chatRoomId = chatRoomInfoTo.getId();
114 int shard = shardingStrategy.selectShard(chatRoomId);
117 "{} - old shard: {}, new shard: {}",
119 chatRoomInfoTo.getShard(),
122 return new ChatRoomInfo(
124 chatRoomInfoTo.getName(),
130 public Flux<Message> writeChatRoomData(
132 Flux<Message> messageFlux)
134 Path path = chatroomPath(chatRoomId);
135 log.info("Writing messages for {} to {}", chatRoomId, path);
138 Files.createDirectories(storagePath);
140 JsonGenerator generator =
143 .createGenerator(Files.newBufferedWriter(path, CREATE, TRUNCATE_EXISTING));
147 FilesStorageStrategy.class.getSimpleName(),
154 generator.useDefaultPrettyPrinter();
155 generator.writeStartArray();
157 catch (IOException e)
159 throw new RuntimeException(e);
166 generator.writeEndArray();
169 catch (IOException e)
171 throw new RuntimeException(e);
178 MessageTo messageTo = MessageTo.from(message);
179 generator.writeObject(messageTo);
182 catch (IOException e)
184 throw new RuntimeException(e);
188 catch (IOException e)
190 throw new RuntimeException(e);
195 public Flux<Message> readChatRoomData(UUID chatRoomId)
197 JavaType type = mapper.getTypeFactory().constructType(MessageTo.class);
199 .from(new JsonFilePublisher<MessageTo>(chatroomPath(chatRoomId), mapper, type))
201 FilesStorageStrategy.class.getSimpleName(),
204 .map(MessageTo::toMessage);
209 return storagePath.resolve(Path.of(CHATROOMS_FILENAME));
212 Path chatroomPath(UUID id)
214 return storagePath.resolve(Path.of(id.toString() + ".json"));