Skip to content

Commit 315b36a

Browse files
anatennisArtDu
authored andcommitted
Add datetime converter
Closes #214
1 parent 663403c commit 315b36a

File tree

12 files changed

+258
-29
lines changed

12 files changed

+258
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Split retrying into more detailed modules ([#341](https://github.com/tarantool/cartridge-java/issues/341))
88
- Add deep copy instead of shallow copy in default message pack mapper ([#166](https://github.com/tarantool/cartridge-java/issues/166))
99
- Add a factory builder for constructing mapper hierarchies
10+
- Support Datetime type ([#293](https://github.com/tarantool/cartridge-java/pull/293))
1011

1112
## [0.10.1] - 2023-01-13
1213

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
*
@@ -17,7 +18,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
1718
/**
1819
* Get a tuple field by its position
1920
*
20-
* @param fieldPosition the field position from the the tuple start, starting from 0
21+
* @param fieldPosition the field position from the tuple start, starting from 0
2122
* @return field or empty optional if the field position is out of tuple length
2223
*/
2324
Optional<TarantoolField> getField(int fieldPosition);
@@ -40,7 +41,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
4041
/**
4142
* Get a tuple field value by its position specifying the target value type
4243
*
43-
* @param fieldPosition field position from the the tuple start, starting from 0
44+
* @param fieldPosition field position from the tuple start, starting from 0
4445
* @param objectClass target value type class
4546
* @param <O> target value type
4647
* @return nullable value of a field wrapped in Optional, possibly converted to a Java type
@@ -50,7 +51,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
5051
/**
5152
* Check if a tuple field exists and can be converted to the target value type
5253
*
53-
* @param fieldPosition field position from the the tuple start, starting from 0
54+
* @param fieldPosition field position from the tuple start, starting from 0
5455
* @param objectClass target value type class
5556
* @return true, if the field exists and can be converted to the given type, false otherwise
5657
*/
@@ -78,7 +79,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
7879
/**
7980
* Get a tuple field value as a raw object
8081
*
81-
* @param fieldPosition field position from the the tuple start, starting from 0
82+
* @param fieldPosition field position from the tuple start, starting from 0
8283
* @return nullable value of a field wrapped in Optional
8384
*/
8485
Optional<?> getObject(int fieldPosition);
@@ -101,7 +102,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
101102
/**
102103
* Set a tuple field by field position
103104
*
104-
* @param fieldPosition the field position from the the tuple start, starting from 0
105+
* @param fieldPosition the field position from the tuple start, starting from 0
105106
* @param field new field
106107
*/
107108
void setField(int fieldPosition, TarantoolField field);
@@ -117,7 +118,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
117118
/**
118119
* Set a tuple field value from an object by field position
119120
*
120-
* @param fieldPosition the field position from the the tuple start, starting from 0
121+
* @param fieldPosition the field position from the tuple start, starting from 0
121122
* @param value new field value
122123
*/
123124
void putObject(int fieldPosition, Object value);
@@ -133,7 +134,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
133134
/**
134135
* Get the field value converted to {@code byte[]}
135136
*
136-
* @param fieldPosition the field position from the the tuple start, starting from 0
137+
* @param fieldPosition the field position from the tuple start, starting from 0
137138
* @return value
138139
*/
139140
byte[] getByteArray(int fieldPosition);
@@ -149,7 +150,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
149150
/**
150151
* Get the field value converted to {@code Boolean}
151152
*
152-
* @param fieldPosition the field position from the the tuple start, starting from 0
153+
* @param fieldPosition the field position from the tuple start, starting from 0
153154
* @return value
154155
*/
155156
Boolean getBoolean(int fieldPosition);
@@ -165,7 +166,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
165166
/**
166167
* Get the field value converted to {@code Double}
167168
*
168-
* @param fieldPosition the field position from the the tuple start, starting from 0
169+
* @param fieldPosition the field position from the tuple start, starting from 0
169170
* @return value
170171
*/
171172
Double getDouble(int fieldPosition);
@@ -181,7 +182,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
181182
/**
182183
* Get the field value converted to {@code Float}
183184
*
184-
* @param fieldPosition the field position from the the tuple start, starting from 0
185+
* @param fieldPosition the field position from the tuple start, starting from 0
185186
* @return value
186187
*/
187188
Float getFloat(int fieldPosition);
@@ -197,7 +198,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
197198
/**
198199
* Get the field value converted to {@code Integer}
199200
*
200-
* @param fieldPosition the field position from the the tuple start, starting from 0
201+
* @param fieldPosition the field position from the tuple start, starting from 0
201202
* @return value
202203
*/
203204
Integer getInteger(int fieldPosition);
@@ -213,7 +214,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
213214
/**
214215
* Get the field value converted to {@code Long}
215216
*
216-
* @param fieldPosition the field position from the the tuple start, starting from 0
217+
* @param fieldPosition the field position from the tuple start, starting from 0
217218
* @return value
218219
*/
219220
Long getLong(int fieldPosition);
@@ -229,7 +230,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
229230
/**
230231
* Get the field value converted to {@code String}
231232
*
232-
* @param fieldPosition the field position from the the tuple start, starting from 0
233+
* @param fieldPosition the field position from the tuple start, starting from 0
233234
* @return value
234235
*/
235236
String getString(int fieldPosition);
@@ -245,7 +246,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
245246
/**
246247
* Get the field value converted to {@code Character}
247248
*
248-
* @param fieldPosition the field position from the the tuple start, starting from 0
249+
* @param fieldPosition the field position from the tuple start, starting from 0
249250
* @return value
250251
*/
251252
Character getCharacter(int fieldPosition);
@@ -261,7 +262,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
261262
/**
262263
* Get the field value converted to {@link UUID}
263264
*
264-
* @param fieldPosition the field position from the the tuple start, starting from 0
265+
* @param fieldPosition the field position from the tuple start, starting from 0
265266
* @return value
266267
*/
267268
UUID getUUID(int fieldPosition);
@@ -274,10 +275,26 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
274275
*/
275276
UUID getUUID(String fieldName);
276277

278+
/**
279+
* Get the field value converted to {@link Instant}
280+
*
281+
* @param fieldPosition the field position from the tuple start, starting from 0
282+
* @return value
283+
*/
284+
Instant getInstant(int fieldPosition);
285+
286+
/**
287+
* Get the field value converted to {@link Instant}
288+
*
289+
* @param fieldName the field name, must not be null
290+
* @return value
291+
*/
292+
Instant getInstant(String fieldName);
293+
277294
/**
278295
* Get the field value converted to {@link BigDecimal}
279296
*
280-
* @param fieldPosition the field position from the the tuple start, starting from 0
297+
* @param fieldPosition the field position from the tuple start, starting from 0
281298
* @return value
282299
*/
283300
BigDecimal getDecimal(int fieldPosition);
@@ -293,7 +310,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
293310
/**
294311
* Get the field value converted to {@link List}
295312
*
296-
* @param fieldPosition the field position from the the tuple start, starting from 0
313+
* @param fieldPosition the field position from the tuple start, starting from 0
297314
* @return value
298315
*/
299316
List<?> getList(int fieldPosition);
@@ -309,7 +326,7 @@ public interface TarantoolTuple extends Iterable<TarantoolField>, Packable {
309326
/**
310327
* Get the field value converted to {@link Map}
311328
*
312-
* @param fieldPosition the field position from the the tuple start, starting from 0
329+
* @param fieldPosition the field position from the tuple start, starting from 0
313330
* @return value
314331
*/
315332
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;
@@ -362,6 +363,16 @@ public UUID getUUID(String fieldName) {
362363
return getObject(fieldName, UUID.class).orElse(null);
363364
}
364365

366+
@Override
367+
public Instant getInstant(int fieldPosition) {
368+
return getObject(fieldPosition, Instant.class).orElse(null);
369+
}
370+
371+
@Override
372+
public Instant getInstant(String fieldName) {
373+
return getObject(fieldName, Instant.class).orElse(null);
374+
}
375+
365376
@Override
366377
public BigDecimal getDecimal(int fieldPosition) {
367378
return getObject(fieldPosition, BigDecimal.class).orElse(null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
*/
19+
public class DefaultInstantToExtensionValueConverter implements ObjectConverter<Instant, ExtensionValue> {
20+
21+
private static final long serialVersionUID = 20221025L;
22+
23+
private static final byte DATETIME_TYPE = 0x04;
24+
25+
private byte[] toBytes(Instant value) {
26+
ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
27+
buffer.order(ByteOrder.LITTLE_ENDIAN);
28+
buffer.putLong(value.getEpochSecond());
29+
buffer.putInt(value.getNano());
30+
return buffer.array();
31+
}
32+
33+
@Override
34+
public ExtensionValue toValue(Instant object) {
35+
return ValueFactory.newExtension(DATETIME_TYPE, toBytes(object));
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
*/
15+
public class DefaultExtensionValueToInstantConverter implements ValueConverter<ExtensionValue, Instant> {
16+
17+
private static final long serialVersionUID = 20221025L;
18+
private static final byte DATETIME_TYPE = 0x04;
19+
20+
private Instant fromBytes(byte[] bytes) {
21+
ByteBuffer buffer = ByteBuffer.wrap(bytes);
22+
buffer.order(ByteOrder.LITTLE_ENDIAN);
23+
return Instant.ofEpochSecond(buffer.getLong()).plusNanos(buffer.getInt());
24+
}
25+
26+
@Override
27+
public Instant fromValue(ExtensionValue value) {
28+
return fromBytes(value.getData());
29+
}
30+
31+
@Override
32+
public boolean canConvertValue(ExtensionValue value) {
33+
return value.getType() == DATETIME_TYPE;
34+
}
35+
}

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

src/test/java/io/tarantool/driver/TarantoolUtils.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@ public final class TarantoolUtils {
1515
private TarantoolUtils() {
1616
}
1717

18-
public static boolean versionGreaterThen(String tarantoolVersion) {
18+
public static boolean versionGreaterOrEqualThen(String tarantoolVersion) {
1919
Assert.notNull(tarantoolVersion, "tarantoolVersion must not be null");
2020
String tarantoolCiVersion = java.lang.System.getenv(TARANTOOL_VERSION);
2121
if (StringUtils.isEmpty(tarantoolCiVersion)) {
2222
return true;
2323
}
2424
TarantoolVersion ciVersion = new TarantoolVersion(tarantoolCiVersion);
2525
TarantoolVersion version = new TarantoolVersion(tarantoolVersion);
26-
return ciVersion.getMajor() > version.getMajor() &&
27-
ciVersion.getMinor() > version.getMinor();
26+
return ciVersion.getMajor() >= version.getMajor() &&
27+
ciVersion.getMinor() >= version.getMinor();
2828
}
2929

3030
public static boolean versionWithUUID() {
31-
return versionGreaterThen("2.4");
31+
return versionGreaterOrEqualThen("2.4");
32+
}
33+
34+
public static boolean versionWithInstant() {
35+
return versionGreaterOrEqualThen("2.10");
3236
}
3337

3438
public static boolean versionWithVarbinary() {
35-
return versionGreaterThen("2.2.1");
39+
return versionGreaterOrEqualThen("2.2.1");
3640
}
3741

3842
public static class TarantoolVersion {
@@ -45,7 +49,7 @@ public TarantoolVersion(String stringVersion) {
4549
.map(Integer::parseInt)
4650
.collect(Collectors.toList());
4751
major = majorMinor.size() >= 1 ? majorMinor.get(0) : 0;
48-
minor = majorMinor.size() >= 2 ? majorMinor.get(1) : 0;
52+
minor = majorMinor.size() >= 2 ? majorMinor.get(1) : 999;
4953
}
5054

5155
public Integer getMajor() {

0 commit comments

Comments
 (0)