From 9b7107025293a0e585d1850c9a5f11f6de74c3b0 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 9 Jan 2023 22:46:42 +0100 Subject: [PATCH] refactor: Refined (simplified) `StorageStrategy` --- .../juplo/kafka/chat/backend/ChatBackendApplication.java | 2 +- .../kafka/chat/backend/ChatBackendConfiguration.java | 2 +- .../kafka/chat/backend/api/ChatBackendController.java | 2 +- .../kafka/chat/backend/persistence/StorageStrategy.java | 8 ++------ .../persistence/storage/files/FilesStorageStrategy.java | 6 ++---- .../backend/persistence/AbstractStorageStrategyIT.java | 2 +- .../persistence/InMemoryWithFilesStorageStrategyIT.java | 2 +- 7 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java index 80a46c00..c61f8488 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendApplication.java @@ -32,7 +32,7 @@ public class ChatBackendApplication implements WebFluxConfigurer @PreDestroy public void onExit() { - storageStrategy.writeChatrooms(chatHome.getChatRooms()); + storageStrategy.write(chatHome.getChatRooms()); } public static void main(String[] args) diff --git a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java index dcf2d9f3..87965ab9 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java +++ b/src/main/java/de/juplo/kafka/chat/backend/ChatBackendConfiguration.java @@ -32,7 +32,7 @@ public class ChatBackendConfiguration ChatBackendProperties properties) { return new InMemoryChatHomeService( - storageStrategy.readChatrooms(), + storageStrategy.read(), clock, properties.getChatroomBufferSize()); } diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java index e7a6f1a5..3b8fc842 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendController.java @@ -124,6 +124,6 @@ public class ChatBackendController @PostMapping("/store") public void store() { - storageStrategy.writeChatrooms(chatHome.getChatRooms()); + storageStrategy.write(chatHome.getChatRooms()); } } diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/StorageStrategy.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/StorageStrategy.java index 4355f9fc..bedd0aac 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/persistence/StorageStrategy.java +++ b/src/main/java/de/juplo/kafka/chat/backend/persistence/StorageStrategy.java @@ -1,15 +1,11 @@ package de.juplo.kafka.chat.backend.persistence; -import de.juplo.kafka.chat.backend.api.ChatRoomTo; import de.juplo.kafka.chat.backend.domain.ChatRoom; -import de.juplo.kafka.chat.backend.domain.Message; import reactor.core.publisher.Flux; public interface StorageStrategy { - void writeChatrooms(Flux chatroomFlux); - Flux readChatrooms(); - void writeMessages(ChatRoomTo chatroomTo, Flux messageFlux); - Flux readMessages(ChatRoomTo chatroomTo); + void write(Flux chatroomFlux); + Flux read(); } 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 e6709709..1fd307f5 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 @@ -36,7 +36,7 @@ public class FilesStorageStrategy implements StorageStrategy @Override - public void writeChatrooms(Flux chatroomFlux) + public void write(Flux chatroomFlux) { Path path = chatroomsPath(); log.info("Writing chatrooms to {}", path); @@ -96,7 +96,7 @@ public class FilesStorageStrategy implements StorageStrategy } @Override - public Flux readChatrooms() + public Flux read() { JavaType type = mapper.getTypeFactory().constructType(ChatRoomTo.class); return Flux @@ -110,7 +110,6 @@ public class FilesStorageStrategy implements StorageStrategy bufferSize)); } - @Override public void writeMessages(ChatRoomTo chatroomTo, Flux messageFlux) { Path path = chatroomPath(chatroomTo); @@ -169,7 +168,6 @@ public class FilesStorageStrategy implements StorageStrategy } } - @Override public Flux readMessages(ChatRoomTo chatroomTo) { JavaType type = mapper.getTypeFactory().constructType(MessageTo.class); diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java index b2cbde2b..549faada 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/AbstractStorageStrategyIT.java @@ -29,7 +29,7 @@ public abstract class AbstractStorageStrategyIT protected void stop() { - getStorageStrategy().writeChatrooms(chathome.getChatRooms()); + getStorageStrategy().write(chathome.getChatRooms()); } @Test diff --git a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageStrategyIT.java b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageStrategyIT.java index 4df59441..218b1864 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageStrategyIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/persistence/InMemoryWithFilesStorageStrategyIT.java @@ -53,7 +53,7 @@ public class InMemoryWithFilesStorageStrategyIT extends AbstractStorageStrategyI @Override protected Supplier chatHomeServiceSupplier() { - return () -> new InMemoryChatHomeService(getStorageStrategy().readChatrooms(), clock, 8); + return () -> new InMemoryChatHomeService(getStorageStrategy().read(), clock, 8); } @BeforeEach -- 2.20.1