Skip to content

Commit

Permalink
Merge branch 'master' into gh-107
Browse files Browse the repository at this point in the history
  • Loading branch information
nickkkccc committed Sep 5, 2023
2 parents 80b2b79 + 770b59f commit 4005d79
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### API changes
- Add "mode" option for crud select operation ([#107](https://github.com/tarantool/cartridge-java/issues/107))

### Bugfixes
- Fix Instant converter to parse 8 bytes datetime ([#408](https://github.com/tarantool/cartridge-java/issues/408))

## [0.12.1] - 2023-08-04

### Bugfixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,21 @@ public class DefaultInstantToExtensionValueConverter implements ObjectConverter<
private static final byte DATETIME_TYPE = 0x04;

private byte[] toBytes(Instant value) {
ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
long seconds = value.getEpochSecond();
Integer nano = value.getNano();

int size = 8;
boolean withOptionalFields = !nano.equals(0);
if (withOptionalFields) {
size = 16;
}

ByteBuffer buffer = ByteBuffer.wrap(new byte[size]);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putLong(value.getEpochSecond());
buffer.putInt(value.getNano());
buffer.putLong(seconds);
if (withOptionalFields) {
buffer.putInt(nano);
}
return buffer.array();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ public class DefaultExtensionValueToInstantConverter implements ValueConverter<E
private static final byte DATETIME_TYPE = 0x04;

private Instant fromBytes(byte[] bytes) {
int size = bytes.length;
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.order(ByteOrder.LITTLE_ENDIAN);
return Instant.ofEpochSecond(buffer.getLong()).plusNanos(buffer.getInt());
long seconds = buffer.getLong();
int nsec = 0;
if (size == 16) {
nsec = buffer.getInt();
}

return Instant.ofEpochSecond(seconds, nsec);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.junit.jupiter.api.condition.EnabledIf;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.UUID;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -80,6 +82,15 @@ public void test_boxSelect_shouldReturnTupleWithInstant() throws Exception {
Assertions.assertEquals(instant, fields.getInstant("instant_field"));
}

@Test
@EnabledIf("io.tarantool.driver.TarantoolUtils#versionWithInstant")
public void test_eval_shouldReturnDatetimeWithoutNsec() throws Exception {
Instant expected = LocalDateTime.of(1, 1, 1, 0, 0).toInstant(ZoneOffset.UTC);
List<?> result = client.eval("return require('datetime').new({year = 1})").get();

Assertions.assertEquals(expected, result.get(0));
}

@Test
@EnabledIf("io.tarantool.driver.TarantoolUtils#versionWithVarbinary")
public void test_boxOperations_shouldWorkWithVarbinary() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.msgpack.core.MessageBufferPacker;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessagePacker;
import org.msgpack.core.MessageUnpacker;
import org.msgpack.value.ExtensionValue;
import org.msgpack.value.ImmutableExtensionValue;
import org.msgpack.value.ValueFactory;
Expand All @@ -30,7 +31,8 @@ void toValue() throws IOException {
Base64.Encoder encoder = Base64.getEncoder();
Instant instant = LocalDateTime.parse("2022-10-25T12:03:58").toInstant(ZoneOffset.UTC);
byte[] result = ((MessageBufferPacker) packer.packValue(converter.toValue(instant))).toByteArray();
assertEquals("2ASu0FdjAAAAAAAAAAAAAAAA", encoder.encodeToString(result));
assertEquals("1wSu0FdjAAAAAA==", encoder.encodeToString(result));
packer.close();
}

@Test
Expand All @@ -39,8 +41,10 @@ void fromValue() throws IOException {
Base64.Decoder base64decoder = Base64.getDecoder();
Instant instant = LocalDateTime.parse("2022-10-25T12:03:58").toInstant(ZoneOffset.UTC);
byte[] packed = base64decoder.decode("2ASu0FdjAAAAAAAAAAAAAAAA");
ExtensionValue value = MessagePack.newDefaultUnpacker(packed).unpackValue().asExtensionValue();
MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(packed);
ExtensionValue value = unpacker.unpackValue().asExtensionValue();
assertEquals(instant, converter.fromValue(value));
unpacker.close();
}

@Test
Expand Down

0 comments on commit 4005d79

Please sign in to comment.