1 package de.juplo.kafka.chat.backend.persistence.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.ChatRoomTo;
7 import de.juplo.kafka.chat.backend.api.MessageTo;
8 import de.juplo.kafka.chat.backend.domain.ChatRoom;
9 import de.juplo.kafka.chat.backend.domain.Message;
10 import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
11 import lombok.RequiredArgsConstructor;
12 import lombok.extern.slf4j.Slf4j;
13 import reactor.core.publisher.Flux;
15 import java.io.IOException;
16 import java.nio.file.Files;
17 import java.nio.file.Path;
18 import java.time.Clock;
20 import static java.nio.file.StandardOpenOption.CREATE;
21 import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
24 @RequiredArgsConstructor
26 public class FilesStorageStrategy implements StorageStrategy
28 public static final String CHATROOMS_FILENAME = "chatrooms.json";
31 private final Path storagePath;
32 private final Clock clock;
33 private final int bufferSize;
34 private final ChatRoomServiceFactory factory;
35 private final ObjectMapper mapper;
39 public void write(Flux<ChatRoom> chatroomFlux)
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));
58 generator.useDefaultPrettyPrinter();
59 generator.writeStartArray();
63 throw new RuntimeException(e);
70 generator.writeEndArray();
75 throw new RuntimeException(e);
78 .subscribe(chatroom ->
82 ChatRoomTo chatroomTo = ChatRoomTo.from(chatroom);
83 generator.writeObject(chatroomTo);
84 writeMessages(chatroomTo, chatroom.getMessages());
88 throw new RuntimeException(e);
94 throw new RuntimeException(e);
99 public Flux<ChatRoom> read()
101 JavaType type = mapper.getTypeFactory().constructType(ChatRoomTo.class);
103 .from(new JsonFilePublisher<ChatRoomTo>(chatroomsPath(), mapper, type))
105 .map(chatRoomTo -> new ChatRoom(
107 chatRoomTo.getName(),
108 chatRoomTo.getShard(),
110 factory.create(readMessages(chatRoomTo)),
114 public void writeMessages(ChatRoomTo chatroomTo, Flux<Message> messageFlux)
116 Path path = chatroomPath(chatroomTo);
117 log.info("Writing messages for {} to {}", chatroomTo, path);
120 Files.createDirectories(storagePath);
122 JsonGenerator generator =
125 .createGenerator(Files.newBufferedWriter(path, CREATE, TRUNCATE_EXISTING));
133 generator.useDefaultPrettyPrinter();
134 generator.writeStartArray();
136 catch (IOException e)
138 throw new RuntimeException(e);
145 generator.writeEndArray();
148 catch (IOException e)
150 throw new RuntimeException(e);
153 .subscribe(message ->
157 MessageTo messageTo = MessageTo.from(message);
158 generator.writeObject(messageTo);
160 catch (IOException e)
162 throw new RuntimeException(e);
166 catch (IOException e)
168 throw new RuntimeException(e);
172 public Flux<Message> readMessages(ChatRoomTo chatroomTo)
174 JavaType type = mapper.getTypeFactory().constructType(MessageTo.class);
176 .from(new JsonFilePublisher<MessageTo>(chatroomPath(chatroomTo), mapper, type))
178 .map(MessageTo::toMessage);
183 return storagePath.resolve(Path.of(CHATROOMS_FILENAME));
186 Path chatroomPath(ChatRoomTo chatroomTo)
188 return storagePath.resolve(Path.of(chatroomTo.getId().toString() + ".json"));