Skip to content

Commit

Permalink
Added method withTarantoolClientConfig, withDefaultMessagePackMapperC…
Browse files Browse the repository at this point in the history
…onfiguration and created interface for builder of MessagePackMapper
  • Loading branch information
wey1and committed Feb 17, 2022
1 parent 5ed7e3e commit 5adf1b0
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 132 deletions.
136 changes: 136 additions & 0 deletions src/main/java/io/tarantool/driver/api/MessagePackMapperBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package io.tarantool.driver.api;

import io.tarantool.driver.mappers.MessagePackMapper;
import io.tarantool.driver.mappers.ObjectConverter;
import io.tarantool.driver.mappers.ValueConverter;
import org.msgpack.value.Value;

import java.util.List;
import java.util.Map;

/**
* Builder for {@link MessagePackMapper}
*/
public interface MessagePackMapperBuilder {
/**
* Configure the mapper with default {@code MP_MAP} entity to {@link Map} converter
*
* @return builder
*/
MessagePackMapperBuilder withDefaultMapValueConverter();

/**
* Configure the mapper with default {@link Map} to {@code MP_MAP} entity converter
*
* @return builder
*/
MessagePackMapperBuilder withDefaultMapObjectConverter();

/**
* Configure the mapper with default {@code MP_ARRAY} entity to {@link List} converter
*
* @return builder
*/
MessagePackMapperBuilder withDefaultArrayValueConverter();

/**
* Configure the mapper with default {@link List} to {@code MP_ARRAY} entity converter
*
* @return builder
*/
MessagePackMapperBuilder withDefaultListObjectConverter();

/**
* Configure the mapper with a specified MessagePack entity-to-object and object-to-entity converter
*
* @param valueClass MessagePack entity class
* @param objectClass object class
* @param converter MessagePack entity-to-object and object-to-entity converter
* @param <V> MessagePack entity type
* @param <O> object type
* @param <T> converter type
* @return builder
*/
<V extends Value, O, T extends ValueConverter<V, O> & ObjectConverter<O, V>> MessagePackMapperBuilder withConverter(
Class<V> valueClass, Class<O> objectClass, T converter);

/**
* Configure the mapper with a specified MessagePack entity-to-object converter
*
* @param converter MessagePack entity-to-object and object-to-entity converter
* @param <V> MessagePack entity type
* @param <O> object type
* @return builder
* @see io.tarantool.driver.mappers.DefaultMessagePackMapper#registerValueConverter(ValueConverter)
*/
<V extends Value, O> MessagePackMapperBuilder withValueConverter(ValueConverter<V, O> converter);

/**
* Configure the mapper with a specified MessagePack entity-to-object converter
*
* @param valueClass source entity class
* @param converter MessagePack entity-to-object and object-to-entity converter
* @param <V> MessagePack entity type
* @param <O> object type
* @return builder
* @see io.tarantool.driver.mappers.DefaultMessagePackMapper#registerValueConverter(Class, ValueConverter)
*/
<V extends Value, O> MessagePackMapperBuilder
withValueConverter(Class<V> valueClass, ValueConverter<V, O> converter);

/**
* Configure the mapper with a specified MessagePack entity-to-object converter
*
* @param valueClass source entity class
* @param objectClass target object class
* @param converter MessagePack entity-to-object and object-to-entity converter
* @param <V> MessagePack entity type
* @param <O> object type
* @return builder
* @see io.tarantool.driver.mappers.DefaultMessagePackMapper#registerValueConverter(Class, Class, ValueConverter)
*/
<V extends Value, O> MessagePackMapperBuilder withValueConverter(Class<V> valueClass, Class<O> objectClass,
ValueConverter<V, O> converter);

/**
* Configure the mapper with a specified MessagePack object-to-entity converter
*
* @param converter MessagePack entity-to-object and object-to-entity converter
* @param <V> MessagePack entity type
* @param <O> object type
* @return builder
*/
<V extends Value, O> MessagePackMapperBuilder withObjectConverter(ObjectConverter<O, V> converter);

/**
* Configure the mapper with a specified MessagePack object-to-entity converter
*
* @param objectClass source object class
* @param converter MessagePack entity-to-object and object-to-entity converter
* @param <V> MessagePack entity type
* @param <O> object type
* @return builder
*/
<V extends Value, O> MessagePackMapperBuilder
withObjectConverter(Class<O> objectClass, ObjectConverter<O, V> converter);

/**
* Configure the mapper with a specified MessagePack object-to-entity converter
*
* @param objectClass source object class
* @param valueClass target object class
* @param converter MessagePack entity-to-object and object-to-entity converter
* @param <V> MessagePack entity type
* @param <O> object type
* @return builder
*/
<V extends Value, O> MessagePackMapperBuilder withObjectConverter(Class<O> objectClass, Class<V> valueClass,
ObjectConverter<O, V> converter);

/**
* Build the mapper instance
*
* @return a new mapper instance
*/
MessagePackMapper build();
}
28 changes: 28 additions & 0 deletions src/main/java/io/tarantool/driver/api/TarantoolClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import io.tarantool.driver.api.connection.TarantoolConnectionSelectionStrategyType;
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.auth.TarantoolCredentials;
import io.tarantool.driver.mappers.DefaultMessagePackMapper;
import io.tarantool.driver.mappers.MessagePackMapper;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.function.UnaryOperator;

/**
* Tarantool client builder interface.
Expand Down Expand Up @@ -104,6 +106,21 @@ public interface TarantoolClientBuilder extends TarantoolClientConfigurator<Tara
*/
TarantoolClientBuilder withConnections(int connections);

/**
* Specify a configuration for mapping between Java objects and MessagePack entities.
* <p>
* This method takes a lambda as an argument, where the mapperBuilder is {@link DefaultMessagePackMapper.Builder}.
* </p>
* see {@link io.tarantool.driver.mappers.DefaultMessagePackMapperFactory}.
*
* @param mapperBuilder builder provider instance, e.g. a lambda function taking the builder
* for {@link MessagePackMapper} instance
* @return this instance of builder {@link TarantoolClientBuilder}
* @see TarantoolClientConfig#setMessagePackMapper(MessagePackMapper)
*/
TarantoolClientBuilder
withDefaultMessagePackMapperConfiguration(UnaryOperator<MessagePackMapperBuilder> mapperBuilder);

/**
* Specify a mapper between Java objects and MessagePack entities.
* The mapper contains converters for simple and complex tuple field types and for the entire tuples into custom
Expand Down Expand Up @@ -165,6 +182,17 @@ TarantoolClientBuilder withConnectionSelectionStrategy(
TarantoolClientBuilder withConnectionSelectionStrategy(
TarantoolConnectionSelectionStrategyType connectionSelectionStrategyType);

/**
* Specify a tarantool client config
* <p>
* It overrides previous settings for config
* </p>
*
* @param config tarantool client config
* @return this instance of builder {@link TarantoolClientBuilder}
*/
TarantoolClientBuilder withTarantoolClientConfig(TarantoolClientConfig config);

/**
* Build the configured Tarantool client instance. Call this when you have specified all necessary settings.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.tarantool.driver.core;

import io.tarantool.driver.api.MessagePackMapperBuilder;
import io.tarantool.driver.api.TarantoolClient;
import io.tarantool.driver.api.TarantoolClientBuilder;
import io.tarantool.driver.api.TarantoolClientConfig;
Expand All @@ -11,12 +12,14 @@
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.auth.SimpleTarantoolCredentials;
import io.tarantool.driver.auth.TarantoolCredentials;
import io.tarantool.driver.mappers.DefaultMessagePackMapper;
import io.tarantool.driver.mappers.MessagePackMapper;

import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.UnaryOperator;

/**
* Tarantool client builder implementation.
Expand All @@ -27,6 +30,8 @@ public class TarantoolClientBuilderImpl extends TarantoolClientConfiguratorImpl<
implements TarantoolClientBuilder {

private final TarantoolClientConfig.Builder configBuilder;

private TarantoolClientConfig config;
private TarantoolClusterAddressProvider addressProvider;

public TarantoolClientBuilderImpl() {
Expand Down Expand Up @@ -82,6 +87,12 @@ public TarantoolClientBuilder withConnections(int numberOfConnections) {
return this;
}

@Override
public TarantoolClientBuilder
withDefaultMessagePackMapperConfiguration(UnaryOperator<MessagePackMapperBuilder> mapperBuilder) {
return withMessagePackMapper(mapperBuilder.apply(new DefaultMessagePackMapper.Builder()).build());
}

@Override
public TarantoolClientBuilder withMessagePackMapper(MessagePackMapper mapper) {
this.configBuilder.withMessagePackMapper(mapper);
Expand Down Expand Up @@ -119,8 +130,16 @@ public TarantoolClientBuilder withConnectionSelectionStrategy(
return this;
}

@Override
public TarantoolClientBuilder withTarantoolClientConfig(TarantoolClientConfig config) {
this.config = config;
return this;
}

@Override
public TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> build() {
return super.decorate(new ClusterTarantoolTupleClient(this.configBuilder.build(), this.addressProvider));
TarantoolClientConfig config = this.config != null ? this.config : this.configBuilder.build();

return super.decorate(new ClusterTarantoolTupleClient(config, this.addressProvider));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.tarantool.driver.api.retry.RequestRetryPolicyFactory;
import io.tarantool.driver.api.retry.TarantoolRequestRetryPolicies;
import io.tarantool.driver.api.tuple.TarantoolTuple;
import io.tarantool.driver.utils.Assert;

import java.util.function.Predicate;
import java.util.function.UnaryOperator;
Expand All @@ -31,7 +32,7 @@ public TarantoolClientConfiguratorImpl(TarantoolClient<TarantoolTuple, Tarantool
}

protected TarantoolClientConfiguratorImpl() {
this.client = new ClusterTarantoolTupleClient();
this.client = null;
}

@Override
Expand Down Expand Up @@ -99,6 +100,7 @@ public TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> build()
*/
protected TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>>
decorate(TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client) {
Assert.notNull(client, "Tarantool client must not be null!");

if (this.mappingConfig != null) {
client = new ProxyTarantoolTupleClient(client, this.mappingConfig);
Expand Down
Loading

0 comments on commit 5adf1b0

Please sign in to comment.