From 1976133e258050aef979f4d836c2d81630f09e4e Mon Sep 17 00:00:00 2001 From: agrgr Date: Tue, 2 Jan 2024 16:52:29 +0200 Subject: [PATCH] support getHosts() and nameSpace() methods along with application.properties (has precedence) --- .../AbstractAerospikeDataConfiguration.java | 7 +++-- ...actReactiveAerospikeDataConfiguration.java | 9 ++++--- .../AerospikeDataConfigurationSupport.java | 27 ++++++++++++++++++- .../aerospike/config/AerospikeSettings.java | 24 +---------------- .../data/aerospike/utility/Utils.java | 17 ++++++++++++ .../BaseBlockingIntegrationTests.java | 2 -- .../BaseReactiveIntegrationTests.java | 2 -- 7 files changed, 55 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/springframework/data/aerospike/config/AbstractAerospikeDataConfiguration.java b/src/main/java/org/springframework/data/aerospike/config/AbstractAerospikeDataConfiguration.java index 0268d4089..9d08e8cce 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AbstractAerospikeDataConfiguration.java +++ b/src/main/java/org/springframework/data/aerospike/config/AbstractAerospikeDataConfiguration.java @@ -35,6 +35,8 @@ import org.springframework.data.aerospike.query.cache.InternalIndexOperations; import org.springframework.data.aerospike.server.version.ServerVersionSupport; +import static org.springframework.data.aerospike.utility.Utils.getNamespace; + @Slf4j @Configuration public abstract class AbstractAerospikeDataConfiguration extends AerospikeDataConfigurationSupport { @@ -46,8 +48,9 @@ public AerospikeTemplate aerospikeTemplate(IAerospikeClient aerospikeClient, AerospikeExceptionTranslator aerospikeExceptionTranslator, QueryEngine queryEngine, IndexRefresher indexRefresher, ServerVersionSupport serverVersionSupport, AerospikeSettings settings) { - return new AerospikeTemplate(aerospikeClient, settings.getNamespace(), mappingAerospikeConverter, - aerospikeMappingContext, aerospikeExceptionTranslator, queryEngine, indexRefresher, serverVersionSupport); + return new AerospikeTemplate(aerospikeClient, getNamespace(settings.getNamespace(), nameSpace()), + mappingAerospikeConverter, aerospikeMappingContext, aerospikeExceptionTranslator, queryEngine, + indexRefresher, serverVersionSupport); } @Bean(name = "aerospikeQueryEngine") diff --git a/src/main/java/org/springframework/data/aerospike/config/AbstractReactiveAerospikeDataConfiguration.java b/src/main/java/org/springframework/data/aerospike/config/AbstractReactiveAerospikeDataConfiguration.java index 9c3cdcf4d..b007d00b1 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AbstractReactiveAerospikeDataConfiguration.java +++ b/src/main/java/org/springframework/data/aerospike/config/AbstractReactiveAerospikeDataConfiguration.java @@ -39,8 +39,10 @@ import org.springframework.data.aerospike.query.cache.ReactorIndexRefresher; import org.springframework.data.aerospike.server.version.ServerVersionSupport; +import static org.springframework.data.aerospike.utility.Utils.getNamespace; + /** - * Configuration with beans needed for reactive stuff + * Configuration with beans needed for reactive flow * * @author Igor Ermolenko */ @@ -57,8 +59,9 @@ public ReactiveAerospikeTemplate reactiveAerospikeTemplate(MappingAerospikeConve ReactorIndexRefresher reactorIndexRefresher, ServerVersionSupport serverVersionSupport, AerospikeSettings settings) { - return new ReactiveAerospikeTemplate(aerospikeReactorClient, settings.getNamespace(), mappingAerospikeConverter, - aerospikeMappingContext, aerospikeExceptionTranslator, reactorQueryEngine, reactorIndexRefresher, + return new ReactiveAerospikeTemplate(aerospikeReactorClient, getNamespace(settings.getNamespace(), + nameSpace()), mappingAerospikeConverter, aerospikeMappingContext, aerospikeExceptionTranslator, + reactorQueryEngine, reactorIndexRefresher, serverVersionSupport); } diff --git a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataConfigurationSupport.java b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataConfigurationSupport.java index b3a6b2dd0..0965e15be 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataConfigurationSupport.java +++ b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataConfigurationSupport.java @@ -52,6 +52,8 @@ import java.util.List; import java.util.Set; +import static org.springframework.data.aerospike.utility.Utils.parseHosts; + /** * @author Taras Danylchuk */ @@ -108,7 +110,8 @@ public AerospikeExceptionTranslator aerospikeExceptionTranslator() { @Bean(name = "aerospikeClient", destroyMethod = "close") public AerospikeClient aerospikeClient(AerospikeSettings settings) { - Collection hosts = settings.getHosts(); + Collection hosts; + if ((hosts = parseHosts(settings.getHosts())) == null) hosts = getHosts(); return new AerospikeClient(getClientPolicy(), hosts.toArray(new Host[0])); } @@ -170,6 +173,28 @@ protected FieldNamingStrategy fieldNamingStrategy() { return PropertyNameFieldNamingStrategy.INSTANCE; } + /** + * Override this method to define the hosts to be used. + *

The value of 'spring-data-aerospike.hosts' parameter in application.properties has precedence over this + * method's return value. + * + * @return Collection of Host objects for Aerospike client to connect + */ + protected Collection getHosts() { + return null; + } + + /** + * Override this method to define the namespace to be used. + *

The value of 'spring-data-aerospike.namespace' parameter in application.properties has precedence over this + * method's return value. + * + * @return Collection of Host objects for Aerospike client to connect + */ + protected String nameSpace() { + return null; + } + /** * Return {@link ClientPolicy} object that contains all client policies. * diff --git a/src/main/java/org/springframework/data/aerospike/config/AerospikeSettings.java b/src/main/java/org/springframework/data/aerospike/config/AerospikeSettings.java index d523dc0af..c32d85238 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AerospikeSettings.java +++ b/src/main/java/org/springframework/data/aerospike/config/AerospikeSettings.java @@ -1,53 +1,31 @@ package org.springframework.data.aerospike.config; -import com.aerospike.client.Host; import lombok.Getter; import lombok.Setter; -import org.springframework.util.StringUtils; - -import java.util.Arrays; -import java.util.Collection; -import java.util.stream.Collectors; @Setter +@Getter public class AerospikeSettings { // Hosts separated by ',' in form of

: String hosts; // Namespace - @Getter String namespace; // Enable scan operation - @Getter boolean scansEnabled; // Send user defined key in addition to hash digest on both reads and writes - @Getter boolean sendKey; // Create secondary indexes specified using `@Indexed` annotation on startup - @Getter boolean createIndexesOnStartup; // Automatically refresh indexes cache every seconds - @Getter int indexCacheRefreshSeconds; // Automatically refresh cached server version every seconds - @Getter int serverVersionRefreshSeconds; // Limit amount of results returned by server. Non-positive value means no limit - @Getter long queryMaxRecords; // Maximum batch size for batch write operations - @Getter int batchWriteSize; // Define how @Id fields (primary keys) and Map keys are stored: false - always as String, // true - preserve original type if supported - @Getter boolean keepOriginalKeyTypes; - - public Collection getHosts() { - if (StringUtils.hasText(hosts)) return Arrays.stream(hosts.split(",")) - .map(host -> host.split(":")) - .map(hostArr -> new Host(hostArr[0], Integer.parseInt(hostArr[1]))) - .collect(Collectors.toList()); - return null; - } } diff --git a/src/main/java/org/springframework/data/aerospike/utility/Utils.java b/src/main/java/org/springframework/data/aerospike/utility/Utils.java index 76e7c122b..caf784d9c 100644 --- a/src/main/java/org/springframework/data/aerospike/utility/Utils.java +++ b/src/main/java/org/springframework/data/aerospike/utility/Utils.java @@ -16,6 +16,7 @@ package org.springframework.data.aerospike.utility; import com.aerospike.client.AerospikeException; +import com.aerospike.client.Host; import com.aerospike.client.IAerospikeClient; import com.aerospike.client.Info; import com.aerospike.client.ResultCode; @@ -25,9 +26,11 @@ import org.springframework.util.StringUtils; import java.util.Arrays; +import java.util.Collection; import java.util.Objects; import java.util.Optional; import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; /** * Utility class containing useful methods for interacting with Aerospike across the entire implementation @@ -104,4 +107,18 @@ public static Optional getIntegerProperty(String property) { public static boolean allArrayElementsAreNull(Object[] array) { return Arrays.stream(array).allMatch(Objects::isNull); } + + public static Collection parseHosts(String hostsString) { + if (StringUtils.hasText(hostsString)) return Arrays.stream(hostsString.split(",")) + .map(host -> host.split(":")) + .map(hostArr -> new Host(hostArr[0], Integer.parseInt(hostArr[1]))) + .collect(Collectors.toList()); + return null; + } + + public static String getNamespace(String prioritizedNamespace, String fallbackNamespace) { + String namespace; + if ((namespace = prioritizedNamespace) == null) namespace = fallbackNamespace; + return namespace; + } } diff --git a/src/test/java/org/springframework/data/aerospike/BaseBlockingIntegrationTests.java b/src/test/java/org/springframework/data/aerospike/BaseBlockingIntegrationTests.java index 976c82ddb..9563f98bc 100644 --- a/src/test/java/org/springframework/data/aerospike/BaseBlockingIntegrationTests.java +++ b/src/test/java/org/springframework/data/aerospike/BaseBlockingIntegrationTests.java @@ -14,7 +14,6 @@ import org.springframework.data.aerospike.query.cache.IndexesCache; import org.springframework.data.aerospike.repository.query.Query; import org.springframework.data.aerospike.server.version.ServerVersionSupport; -import org.springframework.test.context.ActiveProfiles; import java.util.Collection; import java.util.List; @@ -29,7 +28,6 @@ "indexSuffix: index1" } ) -@ActiveProfiles("test") public abstract class BaseBlockingIntegrationTests extends BaseIntegrationTests { @Autowired diff --git a/src/test/java/org/springframework/data/aerospike/BaseReactiveIntegrationTests.java b/src/test/java/org/springframework/data/aerospike/BaseReactiveIntegrationTests.java index 28e2d3dac..be4c795a3 100644 --- a/src/test/java/org/springframework/data/aerospike/BaseReactiveIntegrationTests.java +++ b/src/test/java/org/springframework/data/aerospike/BaseReactiveIntegrationTests.java @@ -11,7 +11,6 @@ import org.springframework.data.aerospike.query.cache.ReactorIndexRefresher; import org.springframework.data.aerospike.repository.query.Query; import org.springframework.data.aerospike.server.version.ServerVersionSupport; -import org.springframework.test.context.ActiveProfiles; import reactor.core.publisher.Flux; import java.io.Serializable; @@ -27,7 +26,6 @@ "indexSuffix: index1" } ) -@ActiveProfiles("test") public abstract class BaseReactiveIntegrationTests extends BaseIntegrationTests { @Autowired