Skip to content

Commit ede27d1

Browse files
anatennisArtDu
authored andcommitted
Add datetime converter
Closes #214
1 parent 7af0378 commit ede27d1

File tree

15 files changed

+293
-36
lines changed

15 files changed

+293
-36
lines changed

.github/workflows/tests-ce.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
tarantool-version: [ "1.10", "2.8" ]
17+
tarantool-version: [ "1.x", "2.10.6", "2.x" ]
1818
fail-fast: false
1919
steps:
2020
- uses: actions/checkout@v2

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Add deep copy instead of shallow copy in default message pack mapper ([#166](https://github.com/tarantool/cartridge-java/issues/166))
1010
- Add a factory builder for constructing mapper hierarchies
1111
- Add "can convert value" check to TupleResultConverter
12+
- Support Datetime type ([#293](https://github.com/tarantool/cartridge-java/pull/293))
1213

1314
## [0.10.1] - 2023-01-13
1415

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@
264264
<dependency>
265265
<groupId>io.tarantool</groupId>
266266
<artifactId>testcontainers-java-tarantool</artifactId>
267-
<version>0.5.3</version>
267+
<version>0.5.4</version>
268268
<scope>test</scope>
269269
<exclusions>
270270
<exclusion>

src/main/java/io/tarantool/driver/api/tuple/TarantoolTuple.java

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package io.tarantool.driver.api.tuple;
22

3-
import io.tarantool.driver.protocol.Packable;
4-
53
import java.math.BigDecimal;
4+
import java.time.Instant;
65
import java.util.List;
76
import java.util.Map;
87
import java.util.Optional;
98
import java.util.UUID;
109

10+
import io.tarantool.driver.protocol.Packable;
11+
1112
/**
1213
* Basic Tarantool atom of data
1314
*
@@ -24,7 +25,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
2425
/**
2526
* Get a tuple field by its position
2627
*
27-
* @param fieldPosition the field position from the the tuple start, starting from 0
28+
* @param fieldPosition the field position from the tuple start, starting from 0
2829
* @return field or empty optional if the field position is out of tuple length
2930
*/
3031
Optional<TarantoolField> getField(int fieldPosition);
@@ -47,7 +48,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
4748
/**
4849
* Get a tuple field value by its position specifying the target value type
4950
*
50-
* @param fieldPosition field position from the the tuple start, starting from 0
51+
* @param fieldPosition field position from the tuple start, starting from 0
5152
* @param objectClass target value type class
5253
* @param <O> target value type
5354
* @return nullable value of a field wrapped in Optional, possibly converted to a Java type
@@ -57,7 +58,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
5758
/**
5859
* Check if a tuple field exists and can be converted to the target value type
5960
*
60-
* @param fieldPosition field position from the the tuple start, starting from 0
61+
* @param fieldPosition field position from the tuple start, starting from 0
6162
* @param objectClass target value type class
6263
* @return true, if the field exists and can be converted to the given type, false otherwise
6364
*/
@@ -85,7 +86,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
8586
/**
8687
* Get a tuple field value as a raw object
8788
*
88-
* @param fieldPosition field position from the the tuple start, starting from 0
89+
* @param fieldPosition field position from the tuple start, starting from 0
8990
* @return nullable value of a field wrapped in Optional
9091
*/
9192
Optional<?> getObject(int fieldPosition);
@@ -108,7 +109,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
108109
/**
109110
* Set a tuple field by field position
110111
*
111-
* @param fieldPosition the field position from the the tuple start, starting from 0
112+
* @param fieldPosition the field position from the tuple start, starting from 0
112113
* @param field new field
113114
*/
114115
void setField(int fieldPosition, TarantoolField field);
@@ -124,7 +125,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
124125
/**
125126
* Set a tuple field value from an object by field position
126127
*
127-
* @param fieldPosition the field position from the the tuple start, starting from 0
128+
* @param fieldPosition the field position from the tuple start, starting from 0
128129
* @param value new field value
129130
*/
130131
void putObject(int fieldPosition, Object value);
@@ -140,7 +141,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
140141
/**
141142
* Get the field value converted to {@code byte[]}
142143
*
143-
* @param fieldPosition the field position from the the tuple start, starting from 0
144+
* @param fieldPosition the field position from the tuple start, starting from 0
144145
* @return value
145146
*/
146147
byte[] getByteArray(int fieldPosition);
@@ -156,7 +157,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
156157
/**
157158
* Get the field value converted to {@code Boolean}
158159
*
159-
* @param fieldPosition the field position from the the tuple start, starting from 0
160+
* @param fieldPosition the field position from the tuple start, starting from 0
160161
* @return value
161162
*/
162163
Boolean getBoolean(int fieldPosition);
@@ -172,7 +173,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
172173
/**
173174
* Get the field value converted to {@code Double}
174175
*
175-
* @param fieldPosition the field position from the the tuple start, starting from 0
176+
* @param fieldPosition the field position from the tuple start, starting from 0
176177
* @return value
177178
*/
178179
Double getDouble(int fieldPosition);
@@ -188,7 +189,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
188189
/**
189190
* Get the field value converted to {@code Float}
190191
*
191-
* @param fieldPosition the field position from the the tuple start, starting from 0
192+
* @param fieldPosition the field position from the tuple start, starting from 0
192193
* @return value
193194
*/
194195
Float getFloat(int fieldPosition);
@@ -204,7 +205,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
204205
/**
205206
* Get the field value converted to {@code Integer}
206207
*
207-
* @param fieldPosition the field position from the the tuple start, starting from 0
208+
* @param fieldPosition the field position from the tuple start, starting from 0
208209
* @return value
209210
*/
210211
Integer getInteger(int fieldPosition);
@@ -220,7 +221,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
220221
/**
221222
* Get the field value converted to {@code Long}
222223
*
223-
* @param fieldPosition the field position from the the tuple start, starting from 0
224+
* @param fieldPosition the field position from the tuple start, starting from 0
224225
* @return value
225226
*/
226227
Long getLong(int fieldPosition);
@@ -236,7 +237,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
236237
/**
237238
* Get the field value converted to {@code String}
238239
*
239-
* @param fieldPosition the field position from the the tuple start, starting from 0
240+
* @param fieldPosition the field position from the tuple start, starting from 0
240241
* @return value
241242
*/
242243
String getString(int fieldPosition);
@@ -252,7 +253,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
252253
/**
253254
* Get the field value converted to {@code Character}
254255
*
255-
* @param fieldPosition the field position from the the tuple start, starting from 0
256+
* @param fieldPosition the field position from the tuple start, starting from 0
256257
* @return value
257258
*/
258259
Character getCharacter(int fieldPosition);
@@ -268,7 +269,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
268269
/**
269270
* Get the field value converted to {@link UUID}
270271
*
271-
* @param fieldPosition the field position from the the tuple start, starting from 0
272+
* @param fieldPosition the field position from the tuple start, starting from 0
272273
* @return value
273274
*/
274275
UUID getUUID(int fieldPosition);
@@ -281,10 +282,26 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
281282
*/
282283
UUID getUUID(String fieldName);
283284

285+
/**
286+
* Get the field value converted to {@link Instant}
287+
*
288+
* @param fieldPosition the field position from the tuple start, starting from 0
289+
* @return value
290+
*/
291+
Instant getInstant(int fieldPosition);
292+
293+
/**
294+
* Get the field value converted to {@link Instant}
295+
*
296+
* @param fieldName the field name, must not be null
297+
* @return value
298+
*/
299+
Instant getInstant(String fieldName);
300+
284301
/**
285302
* Get the field value converted to {@link BigDecimal}
286303
*
287-
* @param fieldPosition the field position from the the tuple start, starting from 0
304+
* @param fieldPosition the field position from the tuple start, starting from 0
288305
* @return value
289306
*/
290307
BigDecimal getDecimal(int fieldPosition);
@@ -300,7 +317,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
300317
/**
301318
* Get the field value converted to {@link List}
302319
*
303-
* @param fieldPosition the field position from the the tuple start, starting from 0
320+
* @param fieldPosition the field position from the tuple start, starting from 0
304321
* @return value
305322
*/
306323
List<?> getList(int fieldPosition);
@@ -316,7 +333,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
316333
/**
317334
* Get the field value converted to {@link Map}
318335
*
319-
* @param fieldPosition the field position from the the tuple start, starting from 0
336+
* @param fieldPosition the field position from the tuple start, starting from 0
320337
* @return value
321338
*/
322339
Map<?, ?> getMap(int fieldPosition);

src/main/java/io/tarantool/driver/core/tuple/TarantoolTupleImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.math.BigDecimal;
21+
import java.time.Instant;
2122
import java.util.ArrayList;
2223
import java.util.Collection;
2324
import java.util.Collections;
@@ -368,6 +369,16 @@ public UUID getUUID(String fieldName) {
368369
return getObject(fieldName, UUID.class).orElse(null);
369370
}
370371

372+
@Override
373+
public Instant getInstant(int fieldPosition) {
374+
return getObject(fieldPosition, Instant.class).orElse(null);
375+
}
376+
377+
@Override
378+
public Instant getInstant(String fieldName) {
379+
return getObject(fieldName, Instant.class).orElse(null);
380+
}
381+
371382
@Override
372383
public BigDecimal getDecimal(int fieldPosition) {
373384
return getObject(fieldPosition, BigDecimal.class).orElse(null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.tarantool.driver.mappers.converters.object;
2+
3+
import io.tarantool.driver.mappers.converters.ObjectConverter;
4+
5+
import org.msgpack.core.MessageBufferPacker;
6+
import org.msgpack.core.MessagePack;
7+
import org.msgpack.value.ExtensionValue;
8+
import org.msgpack.value.ValueFactory;
9+
10+
import java.nio.ByteBuffer;
11+
import java.nio.ByteOrder;
12+
import java.time.Instant;
13+
14+
/**
15+
* Default {@link java.time.Instant} to {@link ExtensionValue} converter
16+
*
17+
* @author Anastasiia Romanova
18+
* @author Artyom Dubinin
19+
*/
20+
public class DefaultInstantToExtensionValueConverter implements ObjectConverter<Instant, ExtensionValue> {
21+
22+
private static final long serialVersionUID = 20221025L;
23+
24+
private static final byte DATETIME_TYPE = 0x04;
25+
26+
private byte[] toBytes(Instant value) {
27+
ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
28+
buffer.order(ByteOrder.LITTLE_ENDIAN);
29+
buffer.putLong(value.getEpochSecond());
30+
buffer.putInt(value.getNano());
31+
return buffer.array();
32+
}
33+
34+
@Override
35+
public ExtensionValue toValue(Instant object) {
36+
return ValueFactory.newExtension(DATETIME_TYPE, toBytes(object));
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.tarantool.driver.mappers.converters.value.defaults;
2+
3+
import io.tarantool.driver.mappers.converters.ValueConverter;
4+
import org.msgpack.value.ExtensionValue;
5+
6+
import java.nio.ByteBuffer;
7+
import java.nio.ByteOrder;
8+
import java.time.Instant;
9+
10+
/**
11+
* Default {@link ExtensionValue} to {@link java.time.Instant} converter
12+
*
13+
* @author Anastasiia Romanova
14+
* @author Artyom Dubinin
15+
*/
16+
public class DefaultExtensionValueToInstantConverter implements ValueConverter<ExtensionValue, Instant> {
17+
18+
private static final long serialVersionUID = 20221025L;
19+
private static final byte DATETIME_TYPE = 0x04;
20+
21+
private Instant fromBytes(byte[] bytes) {
22+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
23+
buffer.order(ByteOrder.LITTLE_ENDIAN);
24+
return Instant.ofEpochSecond(buffer.getLong()).plusNanos(buffer.getInt());
25+
}
26+
27+
@Override
28+
public Instant fromValue(ExtensionValue value) {
29+
return fromBytes(value.getData());
30+
}
31+
32+
@Override
33+
public boolean canConvertValue(ExtensionValue value) {
34+
return value.getType() == DATETIME_TYPE;
35+
}
36+
}

src/main/java/io/tarantool/driver/mappers/factories/DefaultMessagePackMapperFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.tarantool.driver.mappers.converters.object.DefaultCharacterToStringValueConverter;
88
import io.tarantool.driver.mappers.converters.object.DefaultDoubleToFloatValueConverter;
99
import io.tarantool.driver.mappers.converters.object.DefaultFloatToFloatValueConverter;
10+
import io.tarantool.driver.mappers.converters.object.DefaultInstantToExtensionValueConverter;
1011
import io.tarantool.driver.mappers.converters.object.DefaultIntegerToIntegerValueConverter;
1112
import io.tarantool.driver.mappers.converters.object.DefaultLongArrayToArrayValueConverter;
1213
import io.tarantool.driver.mappers.converters.object.DefaultLongToIntegerValueConverter;
@@ -15,6 +16,7 @@
1516
import io.tarantool.driver.mappers.converters.object.DefaultShortToIntegerValueConverter;
1617
import io.tarantool.driver.mappers.converters.object.DefaultStringToStringValueConverter;
1718
import io.tarantool.driver.mappers.converters.object.DefaultUUIDToExtensionValueConverter;
19+
import io.tarantool.driver.mappers.converters.value.defaults.DefaultExtensionValueToInstantConverter;
1820
import io.tarantool.driver.mappers.converters.value.defaults.DefaultArrayValueToLongArrayConverter;
1921
import io.tarantool.driver.mappers.converters.value.defaults.DefaultBinaryValueToByteArrayConverter;
2022
import io.tarantool.driver.mappers.converters.value.defaults.DefaultBooleanValueToBooleanConverter;
@@ -43,6 +45,7 @@
4345
import org.msgpack.value.ValueType;
4446

4547
import java.math.BigDecimal;
48+
import java.time.Instant;
4649
import java.util.UUID;
4750

4851
/**
@@ -82,6 +85,7 @@ private DefaultMessagePackMapperFactory() {
8285
.withValueConverter(ValueType.EXTENSION, UUID.class, new DefaultExtensionValueToUUIDConverter())
8386
.withValueConverter(ValueType.EXTENSION, BigDecimal.class,
8487
new DefaultExtensionValueToBigDecimalConverter())
88+
.withValueConverter(ValueType.EXTENSION, Instant.class, new DefaultExtensionValueToInstantConverter())
8589
.withValueConverter(ValueType.NIL, Object.class, new DefaultNilValueToNullConverter())
8690
//TODO: Potential issue https://github.com/tarantool/cartridge-java/issues/118
8791
.withObjectConverter(Character.class, StringValue.class, new DefaultCharacterToStringValueConverter())
@@ -97,6 +101,7 @@ private DefaultMessagePackMapperFactory() {
97101
.withObjectConverter(UUID.class, ExtensionValue.class, new DefaultUUIDToExtensionValueConverter())
98102
.withObjectConverter(BigDecimal.class, ExtensionValue.class,
99103
new DefaultBigDecimalToExtensionValueConverter())
104+
.withObjectConverter(Instant.class, ExtensionValue.class, new DefaultInstantToExtensionValueConverter())
100105
.build();
101106
}
102107

0 commit comments

Comments
 (0)