Skip to content

Add Datetime to Instant converter #376

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

Merged
merged 4 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.x", "2.10.6"]
fail-fast: false
steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Add deep copy instead of shallow copy in default message pack mapper ([#166](https://github.com/tarantool/cartridge-java/issues/166))
- Add a factory builder for constructing mapper hierarchies
- Add "can convert value" check to TupleResultConverter
- Support Datetime type ([#214](https://github.com/tarantool/cartridge-java/pull/214))

## [0.10.1] - 2023-01-13

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
<dependency>
<groupId>io.tarantool</groupId>
<artifactId>testcontainers-java-tarantool</artifactId>
<version>0.5.3</version>
<version>0.5.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down
57 changes: 37 additions & 20 deletions src/main/java/io/tarantool/driver/api/tuple/TarantoolTuple.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.tarantool.driver.api.tuple;

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;
import java.util.UUID;

import io.tarantool.driver.protocol.Packable;

/**
* Basic Tarantool atom of data
*
Expand All @@ -24,7 +25,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 @@ -47,7 +48,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 @@ -57,7 +58,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 Down Expand Up @@ -85,7 +86,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 @@ -108,7 +109,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 @@ -124,7 +125,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 @@ -140,7 +141,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 @@ -156,7 +157,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 @@ -172,7 +173,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 @@ -188,7 +189,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 @@ -204,7 +205,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 @@ -220,7 +221,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 @@ -236,7 +237,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 @@ -252,7 +253,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 @@ -268,7 +269,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 @@ -281,10 +282,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 @@ -300,7 +317,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 @@ -316,7 +333,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 @@ -368,6 +369,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
@@ -0,0 +1,38 @@
package io.tarantool.driver.mappers.converters.object;

import io.tarantool.driver.mappers.converters.ObjectConverter;

import org.msgpack.core.MessageBufferPacker;
import org.msgpack.core.MessagePack;
import org.msgpack.value.ExtensionValue;
import org.msgpack.value.ValueFactory;

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

/**
* Default {@link java.time.Instant} to {@link ExtensionValue} converter
*
* @author Anastasiia Romanova
* @author Artyom Dubinin
*/
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[16]);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putLong(value.getEpochSecond());
buffer.putInt(value.getNano());
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,36 @@
package io.tarantool.driver.mappers.converters.value.defaults;

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

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

/**
* Default {@link ExtensionValue} to {@link java.time.Instant} converter
*
* @author Anastasiia Romanova
* @author Artyom Dubinin
*/
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);
buffer.order(ByteOrder.LITTLE_ENDIAN);
return Instant.ofEpochSecond(buffer.getLong()).plusNanos(buffer.getInt());
}

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

@Override
public boolean canConvertValue(ExtensionValue value) {
return value.getType() == DATETIME_TYPE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.tarantool.driver.mappers.converters.object.DefaultCharacterToStringValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultDoubleToFloatValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultFloatToFloatValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultInstantToExtensionValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultIntegerToIntegerValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultLongArrayToArrayValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultLongToIntegerValueConverter;
Expand All @@ -15,6 +16,7 @@
import io.tarantool.driver.mappers.converters.object.DefaultShortToIntegerValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultStringToStringValueConverter;
import io.tarantool.driver.mappers.converters.object.DefaultUUIDToExtensionValueConverter;
import io.tarantool.driver.mappers.converters.value.defaults.DefaultExtensionValueToInstantConverter;
import io.tarantool.driver.mappers.converters.value.defaults.DefaultArrayValueToLongArrayConverter;
import io.tarantool.driver.mappers.converters.value.defaults.DefaultBinaryValueToByteArrayConverter;
import io.tarantool.driver.mappers.converters.value.defaults.DefaultBooleanValueToBooleanConverter;
Expand Down Expand Up @@ -43,6 +45,7 @@
import org.msgpack.value.ValueType;

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

/**
Expand Down Expand Up @@ -82,6 +85,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 @@ -97,6 +101,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
Loading