Skip to content

Instant converter #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
2 changes: 1 addition & 1 deletion .github/workflows/tests-ce.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
tarantool-version: [ "1.10", "2.8" ]
tarantool-version: [ "1.10", "2.8", "2.10.3" ]
fail-fast: false
steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## [Unreleased]

- Adding default mapper for long arrays ([#290](https://github.com/tarantool/cartridge-java/pull/290))
- Support Datetime type ([#293](https://github.com/tarantool/cartridge-java/pull/293))


## [0.9.1] - 2022-10-13

Expand Down
53 changes: 35 additions & 18 deletions src/main/java/io/tarantool/driver/api/tuple/TarantoolTuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.tarantool.driver.protocol.Packable;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -16,7 +17,7 @@
public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get a tuple field by its position
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return field or empty optional if the field position is out of tuple length
*/
Optional<TarantoolField> getField(int fieldPosition);
Expand All @@ -38,7 +39,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {

/**
* Get a tuple field value by its position specifying the target value type
* @param fieldPosition field position from the the tuple start, starting from 0
* @param fieldPosition field position from the tuple start, starting from 0
* @param objectClass target value type class
* @param <O> target value type
* @return nullable value of a field wrapped in Optional, possibly converted to a Java type
Expand All @@ -47,7 +48,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {

/**
* Check if a tuple field exists and can be converted to the target value type
* @param fieldPosition field position from the the tuple start, starting from 0
* @param fieldPosition field position from the tuple start, starting from 0
* @param objectClass target value type class
* @return true, if the field exists and can be converted to the given type, false otherwise
*/
Expand All @@ -72,7 +73,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {

/**
* Get a tuple field value as a raw object
* @param fieldPosition field position from the the tuple start, starting from 0
* @param fieldPosition field position from the tuple start, starting from 0
* @return nullable value of a field wrapped in Optional
*/
Optional<?> getObject(int fieldPosition);
Expand All @@ -94,7 +95,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Set a tuple field by field position
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @param field new field
*/
void setField(int fieldPosition, TarantoolField field);
Expand All @@ -110,7 +111,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Set a tuple field value from an object by field position
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @param value new field value
*/
void putObject(int fieldPosition, Object value);
Expand All @@ -126,7 +127,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@code byte[]}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
byte[] getByteArray(int fieldPosition);
Expand All @@ -142,7 +143,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@code Boolean}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
Boolean getBoolean(int fieldPosition);
Expand All @@ -158,7 +159,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@code Double}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
Double getDouble(int fieldPosition);
Expand All @@ -174,7 +175,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@code Float}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
Float getFloat(int fieldPosition);
Expand All @@ -190,7 +191,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@code Integer}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
Integer getInteger(int fieldPosition);
Expand All @@ -206,7 +207,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@code Long}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
Long getLong(int fieldPosition);
Expand All @@ -222,7 +223,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@code String}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
String getString(int fieldPosition);
Expand All @@ -238,7 +239,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@code Character}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
Character getCharacter(int fieldPosition);
Expand All @@ -254,7 +255,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@link UUID}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
UUID getUUID(int fieldPosition);
Expand All @@ -267,10 +268,26 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
*/
UUID getUUID(String fieldName);

/**
* Get the field value converted to {@link Instant}
*
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
Instant getInstant(int fieldPosition);

/**
* Get the field value converted to {@link Instant}
*
* @param fieldName the field name, must not be null
* @return value
*/
Instant getInstant(String fieldName);

/**
* Get the field value converted to {@link BigDecimal}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
BigDecimal getDecimal(int fieldPosition);
Expand All @@ -286,7 +303,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@link List}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
List<?> getList(int fieldPosition);
Expand All @@ -302,7 +319,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
/**
* Get the field value converted to {@link Map}
*
* @param fieldPosition the field position from the the tuple start, starting from 0
* @param fieldPosition the field position from the tuple start, starting from 0
* @return value
*/
Map<?, ?> getMap(int fieldPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -361,6 +362,16 @@ public UUID getUUID(String fieldName) {
return getObject(fieldName, UUID.class).orElse(null);
}

@Override
public Instant getInstant(int fieldPosition) {
return getObject(fieldPosition, Instant.class).orElse(null);
}

@Override
public Instant getInstant(String fieldName) {
return getObject(fieldName, Instant.class).orElse(null);
}

@Override
public BigDecimal getDecimal(int fieldPosition) {
return getObject(fieldPosition, BigDecimal.class).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.tarantool.driver.mappers;

import io.tarantool.driver.mappers.converters.object.DefaultInstantToExtensionValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultNilValueToNullConverter;
import io.tarantool.driver.mappers.converters.object.DefaultBigDecimalToExtensionValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultLongArrayToArrayValueConverter;
Expand All @@ -18,6 +19,7 @@
import io.tarantool.driver.mappers.converters.value.DefaultBinaryValueToByteArrayConverter;
import io.tarantool.driver.mappers.converters.value.DefaultBooleanValueToBooleanConverter;
import io.tarantool.driver.mappers.converters.value.DefaultExtensionValueToBigDecimalConverter;
import io.tarantool.driver.mappers.converters.value.DefaultExtensionValueToInstantConverter;
import io.tarantool.driver.mappers.converters.value.DefaultExtensionValueToUUIDConverter;
import io.tarantool.driver.mappers.converters.value.DefaultFloatValueToDoubleConverter;
import io.tarantool.driver.mappers.converters.value.DefaultFloatValueToFloatConverter;
Expand All @@ -42,6 +44,7 @@
import org.msgpack.value.ValueType;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.UUID;

/**
Expand Down Expand Up @@ -81,6 +84,7 @@ private DefaultMessagePackMapperFactory() {
.withValueConverter(ValueType.EXTENSION, UUID.class, new DefaultExtensionValueToUUIDConverter())
.withValueConverter(ValueType.EXTENSION, BigDecimal.class,
new DefaultExtensionValueToBigDecimalConverter())
.withValueConverter(ValueType.EXTENSION, Instant.class, new DefaultExtensionValueToInstantConverter())
.withValueConverter(ValueType.NIL, Object.class, new DefaultNilValueToNullConverter())
//TODO: Potential issue https://github.com/tarantool/cartridge-java/issues/118
.withObjectConverter(Character.class, StringValue.class, new DefaultCharacterToStringValueConverter())
Expand All @@ -96,6 +100,7 @@ private DefaultMessagePackMapperFactory() {
.withObjectConverter(UUID.class, ExtensionValue.class, new DefaultUUIDToExtensionValueConverter())
.withObjectConverter(BigDecimal.class, ExtensionValue.class,
new DefaultBigDecimalToExtensionValueConverter())
.withObjectConverter(Instant.class, ExtensionValue.class, new DefaultInstantToExtensionValueConverter())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.tarantool.driver.mappers.converters.object;

import io.tarantool.driver.mappers.converters.ObjectConverter;
import org.msgpack.value.ExtensionValue;
import org.msgpack.value.ValueFactory;

import java.nio.ByteBuffer;
import java.time.Instant;

/**
* Default {@link java.time.Instant} to {@link ExtensionValue} converter
*
* @author Anastasiia Romanova
*/
public class DefaultInstantToExtensionValueConverter implements ObjectConverter<Instant, ExtensionValue> {

private static final long serialVersionUID = 20221025L;

private static final byte DATETIME_TYPE = 0x04;

private byte[] toBytes(Instant value) {
ByteBuffer buffer = ByteBuffer.wrap(new byte[8]);
buffer.putLong(0, value.getEpochSecond());
return buffer.array();
}

@Override
public ExtensionValue toValue(Instant object) {
return ValueFactory.newExtension(DATETIME_TYPE, toBytes(object));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.tarantool.driver.mappers.converters.value;

import io.tarantool.driver.mappers.converters.ValueConverter;
import org.msgpack.value.ExtensionValue;

import java.nio.ByteBuffer;
import java.time.Instant;

/**
* Default {@link ExtensionValue} to {@link java.time.Instant} converter
*
* @author Anastasiia Romanova
*/
public class DefaultExtensionValueToInstantConverter implements ValueConverter<ExtensionValue, Instant> {

private static final long serialVersionUID = 20221025L;
private static final byte DATETIME_TYPE = 0x04;

private Instant fromBytes(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.wrap(bytes);
return Instant.ofEpochSecond(buffer.getLong());
}

@Override
public Instant fromValue(ExtensionValue value) {
return fromBytes(value.getData());
}

@Override
public boolean canConvertValue(ExtensionValue value) {
return value.getType() == DATETIME_TYPE;
}
}
4 changes: 4 additions & 0 deletions src/test/java/io/tarantool/driver/TarantoolUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static boolean versionWithUUID() {
return versionGreaterThen("2.4");
}

public static boolean versionWithInstant() {
return versionGreaterThen("2.10");
}

public static boolean versionWithVarbinary() {
return versionGreaterThen("2.2.1");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;

import java.time.Instant;
import java.util.UUID;
import org.testcontainers.shaded.org.apache.commons.lang.ArrayUtils;

Expand Down Expand Up @@ -65,6 +66,23 @@ public void test_boxSelect_shouldReturnTupleWithUUID() throws Exception {
Assertions.assertEquals(uuid, fields.getUUID("uuid_field"));
}

@Test
@EnabledIf("io.tarantool.driver.TarantoolUtils#versionWithInstant")
public void test_boxSelect_shouldReturnTupleWithInstant() throws Exception {
//given
Instant instant = Instant.now();
client.space("space_with_instant")
.insert(tupleFactory.create(1, instant)).get();

//when
TarantoolTuple fields = client
.space("space_with_instant")
.select(Conditions.equals("id", 1)).get().get(0);

//then
Assertions.assertEquals(instant, fields.getInstant("instant_field"));
}

@Test
@EnabledIf("io.tarantool.driver.TarantoolUtils#versionWithVarbinary")
public void test_boxOperations_shouldWorkWithVarbinary() throws Exception {
Expand Down
Loading