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=e16112254a04f78cdeb1a106245de692e56c8324;hb=refs%2Ftags%2Frebase--2023-08-18;hp=24c6a01edd1356f32d3caffd5870e4bbb8c55aca;hpb=ff98b068a91fc9e60e51bd4a95065633bb8ed2db;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 24c6a01e..e1611225 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 @@ -3,9 +3,10 @@ package de.juplo.kafka.chat.backend.persistence.storage.files; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; -import de.juplo.kafka.chat.backend.api.ChatRoomTo; +import de.juplo.kafka.chat.backend.api.ChatRoomInfoTo; import de.juplo.kafka.chat.backend.api.MessageTo; -import de.juplo.kafka.chat.backend.api.ShardingStrategy; +import de.juplo.kafka.chat.backend.domain.ChatRoomInfo; +import de.juplo.kafka.chat.backend.domain.ShardingStrategy; import de.juplo.kafka.chat.backend.domain.ChatRoom; import de.juplo.kafka.chat.backend.domain.Message; import de.juplo.kafka.chat.backend.persistence.StorageStrategy; @@ -39,7 +40,7 @@ public class FilesStorageStrategy implements StorageStrategy @Override - public void write(Flux chatroomFlux) + public void write(Flux chatroomFlux) { Path path = chatroomsPath(); log.info("Writing chatrooms to {}", path); @@ -82,9 +83,9 @@ public class FilesStorageStrategy implements StorageStrategy { try { - ChatRoomTo chatroomTo = ChatRoomTo.from(chatroom); - generator.writeObject(chatroomTo); - writeMessages(chatroomTo, chatroom.getMessages()); + ChatRoomInfoTo infoTo = ChatRoomInfoTo.from(chatroom); + generator.writeObject(infoTo); + writeMessages(infoTo, chatroom.getMessages()); } catch (IOException e) { @@ -101,28 +102,28 @@ public class FilesStorageStrategy implements StorageStrategy @Override public Flux read() { - JavaType type = mapper.getTypeFactory().constructType(ChatRoomTo.class); + JavaType type = mapper.getTypeFactory().constructType(ChatRoomInfoTo.class); return Flux - .from(new JsonFilePublisher(chatroomsPath(), mapper, type)) + .from(new JsonFilePublisher(chatroomsPath(), mapper, type)) .log() - .map(chatRoomTo -> + .map(infoTo -> { - UUID chatRoomId = chatRoomTo.getId(); + UUID chatRoomId = infoTo.getId(); int shard = shardingStrategy.selectShard(chatRoomId); return new ChatRoom( - chatRoomTo.getId(), - chatRoomTo.getName(), + infoTo.getId(), + infoTo.getName(), shard, clock, - factory.create(readMessages(chatRoomTo)), + factory.create(readMessages(infoTo)), bufferSize); }); } - public void writeMessages(ChatRoomTo chatroomTo, Flux messageFlux) + public void writeMessages(ChatRoomInfoTo infoTo, Flux messageFlux) { - Path path = chatroomPath(chatroomTo); - log.info("Writing messages for {} to {}", chatroomTo, path); + Path path = chatroomPath(infoTo); + log.info("Writing messages for {} to {}", infoTo, path); try { Files.createDirectories(storagePath); @@ -177,11 +178,11 @@ public class FilesStorageStrategy implements StorageStrategy } } - public Flux readMessages(ChatRoomTo chatroomTo) + public Flux readMessages(ChatRoomInfoTo infoTo) { JavaType type = mapper.getTypeFactory().constructType(MessageTo.class); return Flux - .from(new JsonFilePublisher(chatroomPath(chatroomTo), mapper, type)) + .from(new JsonFilePublisher(chatroomPath(infoTo), mapper, type)) .log() .map(MessageTo::toMessage); } @@ -191,8 +192,8 @@ public class FilesStorageStrategy implements StorageStrategy return storagePath.resolve(Path.of(CHATROOMS_FILENAME)); } - Path chatroomPath(ChatRoomTo chatroomTo) + Path chatroomPath(ChatRoomInfoTo infoTo) { - return storagePath.resolve(Path.of(chatroomTo.getId().toString() + ".json")); + return storagePath.resolve(Path.of(infoTo.getId().toString() + ".json")); } }