Skip to content

Commit

Permalink
support getHosts() and nameSpace() methods along with application.pro…
Browse files Browse the repository at this point in the history
…perties (has precedence)
  • Loading branch information
agrgr committed Jan 2, 2024
1 parent 0ff2585 commit 1976133
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import java.util.List;
import java.util.Set;

import static org.springframework.data.aerospike.utility.Utils.parseHosts;

/**
* @author Taras Danylchuk
*/
Expand Down Expand Up @@ -108,7 +110,8 @@ public AerospikeExceptionTranslator aerospikeExceptionTranslator() {

@Bean(name = "aerospikeClient", destroyMethod = "close")
public AerospikeClient aerospikeClient(AerospikeSettings settings) {
Collection<Host> hosts = settings.getHosts();
Collection<Host> hosts;
if ((hosts = parseHosts(settings.getHosts())) == null) hosts = getHosts();
return new AerospikeClient(getClientPolicy(), hosts.toArray(new Host[0]));
}

Expand Down Expand Up @@ -170,6 +173,28 @@ protected FieldNamingStrategy fieldNamingStrategy() {
return PropertyNameFieldNamingStrategy.INSTANCE;
}

/**
* Override this method to define the hosts to be used.
* <p>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<Host> getHosts() {
return null;
}

/**
* Override this method to define the namespace to be used.
* <p>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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <address>:<port>
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 <N> seconds
@Getter
int indexCacheRefreshSeconds;
// Automatically refresh cached server version every <N> 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<Host> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -104,4 +107,18 @@ public static Optional<Integer> getIntegerProperty(String property) {
public static boolean allArrayElementsAreNull(Object[] array) {
return Arrays.stream(array).allMatch(Objects::isNull);
}

public static Collection<Host> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +28,6 @@
"indexSuffix: index1"
}
)
@ActiveProfiles("test")
public abstract class BaseBlockingIntegrationTests extends BaseIntegrationTests {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +26,6 @@
"indexSuffix: index1"
}
)
@ActiveProfiles("test")
public abstract class BaseReactiveIntegrationTests extends BaseIntegrationTests {

@Autowired
Expand Down

0 comments on commit 1976133

Please sign in to comment.