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;
21 import static java.nio.file.StandardOpenOption.CREATE;
22 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
25 @RequiredArgsConstructor
27 public class FilesStorageStrategy implements StorageStrategy
29 public static final String CHATROOMS_FILENAME = "chatrooms.json";
32 private final Path storagePath;
33 private final ShardingStrategy shardingStrategy;
34 private final ObjectMapper mapper;
38 public Flux<ChatRoomInfo> writeChatRoomInfo(Flux<ChatRoomInfo> chatRoomInfoFlux)
40 Path path = chatroomsPath();
41 log.info("Writing chatrooms to {}", path);
44 Files.createDirectories(storagePath);
46 JsonGenerator generator =
49 .createGenerator(Files.newBufferedWriter(path, CREATE, TRUNCATE_EXISTING));
51 return chatRoomInfoFlux
57 generator.useDefaultPrettyPrinter();
58 generator.writeStartArray();
62 throw new RuntimeException(e);
69 generator.writeEndArray();
74 throw new RuntimeException(e);
81 ChatRoomInfoTo chatRoomInfoTo = ChatRoomInfoTo.from(chatRoomInfo);
82 generator.writeObject(chatRoomInfoTo);
87 throw new RuntimeException(e);
93 throw new RuntimeException(e);
98 public Flux<ChatRoomInfo> readChatRoomInfo()
100 JavaType type = mapper.getTypeFactory().constructType(ChatRoomInfoTo.class);
102 .from(new JsonFilePublisher<ChatRoomInfoTo>(chatroomsPath(), mapper, type))
104 .map(chatRoomInfoTo ->
106 UUID chatRoomId = chatRoomInfoTo.getId();
107 int shard = shardingStrategy.selectShard(chatRoomId);
110 "{} - old shard: {}, new shard: {}",
112 chatRoomInfoTo.getShard(),
115 return new ChatRoomInfo(
117 chatRoomInfoTo.getName(),
123 public Flux<Message> writeChatRoomData(
125 Flux<Message> messageFlux)
127 Path path = chatroomPath(chatRoomId);
128 log.info("Writing messages for {} to {}", chatRoomId, path);
131 Files.createDirectories(storagePath);
133 JsonGenerator generator =
136 .createGenerator(Files.newBufferedWriter(path, CREATE, TRUNCATE_EXISTING));
144 generator.useDefaultPrettyPrinter();
145 generator.writeStartArray();
147 catch (IOException e)
149 throw new RuntimeException(e);
156 generator.writeEndArray();
159 catch (IOException e)
161 throw new RuntimeException(e);
168 MessageTo messageTo = MessageTo.from(message);
169 generator.writeObject(messageTo);
172 catch (IOException e)
174 throw new RuntimeException(e);
178 catch (IOException e)
180 throw new RuntimeException(e);
185 public Flux<Message> readChatRoomData(UUID chatRoomId)
187 JavaType type = mapper.getTypeFactory().constructType(MessageTo.class);
189 .from(new JsonFilePublisher<MessageTo>(chatroomPath(chatRoomId), mapper, type))
191 .map(MessageTo::toMessage);
196 return storagePath.resolve(Path.of(CHATROOMS_FILENAME));
199 Path chatroomPath(UUID id)
201 return storagePath.resolve(Path.of(id.toString() + ".json"));