X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fpersistence%2Fstorage%2Ffiles%2FFilesStorageStrategy.java;h=9c791977135170d2024ebef87c026a62f33ff227;hb=e8d8cb2aba9988608ee98b0a7dfc1053b6429040;hp=025e3aefb44df5e8360b0988e5283e86719ede3d;hpb=d7689f059964f8c6105b65bb3ee5a097fd106cb7;p=demos%2Fkafka%2Fchat diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageStrategy.java index 025e3aef..9c791977 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageStrategy.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageStrategy.java @@ -5,10 +5,10 @@ import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo; import de.juplo.kafka.chat.backend.api.MessageTo; -import de.juplo.kafka.chat.backend.persistence.inmemory.ShardingStrategy; -import de.juplo.kafka.chat.backend.domain.ChatRoom; +import de.juplo.kafka.chat.backend.domain.ChatRoomInfo; import de.juplo.kafka.chat.backend.domain.Message; import de.juplo.kafka.chat.backend.persistence.StorageStrategy; +import de.juplo.kafka.chat.backend.persistence.ShardingStrategy; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import reactor.core.publisher.Flux; @@ -16,7 +16,6 @@ import reactor.core.publisher.Flux; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.time.Clock; import java.util.UUID; import static java.nio.file.StandardOpenOption.CREATE; @@ -31,15 +30,12 @@ public class FilesStorageStrategy implements StorageStrategy private final Path storagePath; - private final Clock clock; - private final int bufferSize; private final ShardingStrategy shardingStrategy; - private final ChatRoomServiceFactory factory; private final ObjectMapper mapper; @Override - public void write(Flux chatroomFlux) + public void writeChatRoomInfo(Flux chatRoomInfoFlux) { Path path = chatroomsPath(); log.info("Writing chatrooms to {}", path); @@ -52,7 +48,7 @@ public class FilesStorageStrategy implements StorageStrategy .getFactory() .createGenerator(Files.newBufferedWriter(path, CREATE, TRUNCATE_EXISTING)); - chatroomFlux + chatRoomInfoFlux .log() .doFirst(() -> { @@ -78,13 +74,12 @@ public class FilesStorageStrategy implements StorageStrategy throw new RuntimeException(e); } }) - .subscribe(chatroom -> + .subscribe(chatRoomInfo -> { try { - ChatRoomInfoTo infoTo = ChatRoomInfoTo.from(chatroom); - generator.writeObject(infoTo); - writeMessages(infoTo, chatroom.getMessages()); + ChatRoomInfoTo chatRoomInfoTo = ChatRoomInfoTo.from(chatRoomInfo); + generator.writeObject(chatRoomInfoTo); } catch (IOException e) { @@ -99,30 +94,37 @@ public class FilesStorageStrategy implements StorageStrategy } @Override - public Flux read() + public Flux readChatRoomInfo() { JavaType type = mapper.getTypeFactory().constructType(ChatRoomInfoTo.class); return Flux .from(new JsonFilePublisher(chatroomsPath(), mapper, type)) .log() - .map(infoTo -> + .map(chatRoomInfoTo -> { - UUID chatRoomId = infoTo.getId(); + UUID chatRoomId = chatRoomInfoTo.getId(); int shard = shardingStrategy.selectShard(chatRoomId); - return new ChatRoom( - infoTo.getId(), - infoTo.getName(), - shard, - clock, - factory.create(readMessages(infoTo)), - bufferSize); + + log.info( + "{} - old shard: {}, new shard: {}", + chatRoomId, + chatRoomInfoTo.getShard(), + shard); + + return new ChatRoomInfo( + chatRoomId, + chatRoomInfoTo.getName(), + shard); }); } - public void writeMessages(ChatRoomInfoTo infoTo, Flux messageFlux) + @Override + public void writeChatRoomData( + UUID chatRoomId, + Flux messageFlux) { - Path path = chatroomPath(infoTo); - log.info("Writing messages for {} to {}", infoTo, path); + Path path = chatroomPath(chatRoomId); + log.info("Writing messages for {} to {}", chatRoomId, path); try { Files.createDirectories(storagePath); @@ -177,11 +179,12 @@ public class FilesStorageStrategy implements StorageStrategy } } - public Flux readMessages(ChatRoomInfoTo infoTo) + @Override + public Flux readChatRoomData(UUID chatRoomId) { JavaType type = mapper.getTypeFactory().constructType(MessageTo.class); return Flux - .from(new JsonFilePublisher(chatroomPath(infoTo), mapper, type)) + .from(new JsonFilePublisher(chatroomPath(chatRoomId), mapper, type)) .log() .map(MessageTo::toMessage); } @@ -191,8 +194,8 @@ public class FilesStorageStrategy implements StorageStrategy return storagePath.resolve(Path.of(CHATROOMS_FILENAME)); } - Path chatroomPath(ChatRoomInfoTo infoTo) + Path chatroomPath(UUID id) { - return storagePath.resolve(Path.of(infoTo.getId().toString() + ".json")); + return storagePath.resolve(Path.of(id.toString() + ".json")); } }