feat: Implemented new Default-`StorageStrategy` `none'
authorKai Moritz <kai@juplo.de>
Sat, 19 Aug 2023 15:33:03 +0000 (17:33 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 20 Aug 2023 08:44:31 +0000 (10:44 +0200)
* If `none` is selected as storage strategy, an empty implementation of
  `Storage-Strategy` instanciated.
* The owned shardes are derived from the according configuration property.
* Before, they were derived from the stored data.

src/main/java/de/juplo/kafka/chat/backend/ChatBackendProperties.java
src/main/java/de/juplo/kafka/chat/backend/persistence/inmemory/InMemoryServicesConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/files/FilesStorageConfiguration.java
src/main/java/de/juplo/kafka/chat/backend/persistence/storage/nostorage/NoStorageStorageConfiguration.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesAndShardingConfigurationIT.java
src/test/java/de/juplo/kafka/chat/backend/InMemoryWithFilesConfigurationIT.java

index 177d4f5..def9de1 100644 (file)
@@ -25,11 +25,11 @@ public class ChatBackendProperties
     private ShardingStrategyType shardingStrategy = ShardingStrategyType.none;
     private int numShards = 1;
     private int[] ownedShards = new int[] { 0 };
-    private StorageStrategyType storageStrategy = StorageStrategyType.files;
+    private StorageStrategyType storageStrategy = StorageStrategyType.none;
     private String storageDirectory = Paths.get(System.getProperty("java.io.tmpdir"),"chat", "backend").toString();
   }
 
   public enum ServiceType { inmemory }
-  public enum StorageStrategyType { files, mongodb }
+  public enum StorageStrategyType { none, files, mongodb }
   public enum ShardingStrategyType { none, kafkalike }
 }
index de50448..175f714 100644 (file)
@@ -13,6 +13,7 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 import java.time.Clock;
+import java.util.stream.IntStream;
 
 
 @ConditionalOnProperty(
@@ -41,19 +42,13 @@ public class InMemoryServicesConfiguration
       havingValue = "kafkalike")
   ChatHome kafkalikeShardingChatHome(
       ChatBackendProperties properties,
-      InMemoryChatHomeService chatHomeService,
-      StorageStrategy storageStrategy)
+      InMemoryChatHomeService chatHomeService)
   {
     int numShards = properties.getInmemory().getNumShards();
     SimpleChatHome[] chatHomes = new SimpleChatHome[numShards];
-    storageStrategy
-        .read()
-        .subscribe(chatRoom ->
-        {
-          int shard = chatRoom.getShard();
-          if (chatHomes[shard] == null)
-            chatHomes[shard] = new SimpleChatHome(chatHomeService, shard);
-        });
+    IntStream
+        .of(properties.getInmemory().getOwnedShards())
+        .forEach(shard -> chatHomes[shard] = new SimpleChatHome(chatHomeService, shard));
     ShardingStrategy strategy = new KafkaLikeShardingStrategy(numShards);
     return new ShardedChatHome(chatHomes, strategy);
   }
index df730aa..702d1d5 100644 (file)
@@ -19,8 +19,7 @@ import java.time.Clock;
 @ConditionalOnProperty(
     prefix = "chat.backend.inmemory",
     name = "storage-strategy",
-    havingValue = "files",
-    matchIfMissing = true)
+    havingValue = "files")
 @Configuration
 @EnableAutoConfiguration(
     exclude = {
diff --git a/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/nostorage/NoStorageStorageConfiguration.java b/src/main/java/de/juplo/kafka/chat/backend/persistence/storage/nostorage/NoStorageStorageConfiguration.java
new file mode 100644 (file)
index 0000000..824c6f2
--- /dev/null
@@ -0,0 +1,48 @@
+package de.juplo.kafka.chat.backend.persistence.storage.nostorage;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import de.juplo.kafka.chat.backend.ChatBackendProperties;
+import de.juplo.kafka.chat.backend.domain.ChatRoom;
+import de.juplo.kafka.chat.backend.domain.ShardingStrategy;
+import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
+import de.juplo.kafka.chat.backend.persistence.inmemory.InMemoryChatRoomService;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration;
+import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import reactor.core.publisher.Flux;
+
+import java.nio.file.Paths;
+import java.time.Clock;
+
+
+@ConditionalOnProperty(
+    prefix = "chat.backend.inmemory",
+    name = "storage-strategy",
+    havingValue = "none",
+    matchIfMissing = true)
+@Configuration
+@EnableAutoConfiguration(
+    exclude = {
+        MongoRepositoriesAutoConfiguration.class,
+        MongoAutoConfiguration.class })
+public class NoStorageStorageConfiguration
+{
+  @Bean
+  public StorageStrategy storageStrategy()
+  {
+    return new StorageStrategy()
+    {
+      @Override
+      public void write(Flux<ChatRoom> chatroomFlux) {}
+
+      @Override
+      public Flux<ChatRoom> read()
+      {
+        return Flux.empty();
+      }
+    };
+  }
+}
index d792d25..72230e9 100644 (file)
@@ -6,6 +6,7 @@ import org.springframework.boot.test.context.SpringBootTest;
 @SpringBootTest(
     webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
     properties = {
+        "chat.backend.inmemory.storage-strategy=files",
         "chat.backend.inmemory.storage-directory=target/test-classes/data/files",
         "chat.backend.inmemory.sharding-strategy=kafkalike",
         "chat.backend.inmemory.num-shards=10",
index 151a833..2ff9e11 100644 (file)
@@ -7,6 +7,7 @@ import org.springframework.boot.test.context.SpringBootTest;
                webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
                properties = {
                                "chat.backend.inmemory.sharding-strategy=none",
+                               "chat.backend.inmemory.storage-strategy=files",
                                "chat.backend.inmemory.storage-directory=target/test-classes/data/files" })
 class InMemoryWithFilesConfigurationIT extends AbstractConfigurationIT
 {