X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fpersistence%2Fstorage%2Ffiles%2FFilesStorageStrategy.java;h=f0ee1dfb0e1ee449653fe7fe67343250f17af09b;hb=1d4b90c15b1571bce48389e2c34e7b15c1697b89;hp=d043696c451fe5998892289577b620d029df484b;hpb=9d61871563f4be4850cebca4fad0545d504522c3;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 d043696c..f0ee1dfb 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,8 +3,9 @@ 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.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; @@ -16,6 +17,7 @@ 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; import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; @@ -31,6 +33,7 @@ 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; @@ -79,9 +82,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) { @@ -98,23 +101,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 -> new ChatRoom( - chatRoomTo.getId(), - chatRoomTo.getName(), - chatRoomTo.getShard(), - clock, - factory.create(readMessages(chatRoomTo)), - bufferSize)); + .map(infoTo -> + { + UUID chatRoomId = infoTo.getId(); + int shard = shardingStrategy.selectShard(chatRoomId); + return new ChatRoom( + infoTo.getId(), + infoTo.getName(), + shard, + clock, + 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); @@ -169,11 +177,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); } @@ -183,8 +191,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")); } }