Skip to content

[WIP] Add add support for Timestamp type #431

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 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// MessagePack for Java
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.core;

/**
* Thrown when format of an extension type is invalid
*/
public class MessageExtensionFormatException
extends MessageFormatException
{
public MessageExtensionFormatException(Throwable e)
{
super(e);
}

public MessageExtensionFormatException(String message)
{
super(message);
}

public MessageExtensionFormatException(String message, Throwable cause)
{
super(message, cause);
}
}
2 changes: 2 additions & 0 deletions msgpack-core/src/main/java/org/msgpack/core/MessagePack.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ public static final boolean isFixedRaw(byte b)
public static final byte MAP32 = (byte) 0xdf;

public static final byte NEGFIXINT_PREFIX = (byte) 0xe0;

public static final byte EXT_TIMESTAMP = (byte) -1;
}

private MessagePack()
Expand Down
118 changes: 118 additions & 0 deletions msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.time.Instant;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop Java 7 support ?

msgpack-java/.travis.yml

Lines 12 to 13 in 81d540d

- openjdk7
- oraclejdk7

Copy link
Member

@xerial xerial Oct 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frsyuki has confirmed it works in Java6 and Java7 even if Instant class is in the import statement. Only programs that use Instance related methods will fail to compile.

import java.util.Date;

import static org.msgpack.core.MessagePack.Code.ARRAY16;
import static org.msgpack.core.MessagePack.Code.ARRAY32;
Expand All @@ -41,6 +43,7 @@
import static org.msgpack.core.MessagePack.Code.EXT16;
import static org.msgpack.core.MessagePack.Code.EXT32;
import static org.msgpack.core.MessagePack.Code.EXT8;
import static org.msgpack.core.MessagePack.Code.EXT_TIMESTAMP;
import static org.msgpack.core.MessagePack.Code.FALSE;
import static org.msgpack.core.MessagePack.Code.FIXARRAY_PREFIX;
import static org.msgpack.core.MessagePack.Code.FIXEXT1;
Expand Down Expand Up @@ -790,6 +793,121 @@ else if (s.length() < (1 << 16)) {
return this;
}

/**
* Writes a Timestamp value.
*
* <p>
* This method writes a timestamp value using timestamp format family.
*
* @param date the timestamp to be written
* @return this
* @throws IOException when underlying output throws IOException
*/
public MessagePacker packTimestamp(Date date)
throws IOException
{
long epochMilli = date.getTime();
long sec = Math.floorDiv(epochMilli, 1000L);
int nsec = ((int) (epochMilli - sec * 1000L)) * 1000; // 0 <= nsec < 1,000,000,000 < 2^30
return packTimestampImpl(sec, nsec);
}

/**
* Writes a Timestamp value.
*
* <p>
* This method writes a timestamp value using timestamp format family.
*
* @param instant the timestamp to be written
* @return this
* @throws IOException when underlying output throws IOException
*/
public MessagePacker packTimestamp(Instant instant)
throws IOException
{
return packTimestampImpl(instant.getEpochSecond(), instant.getNano());
}

/**
* Writes a Timestamp value.
*
* <p>
* This method writes a timestamp value using timestamp format family.
*
* @param epochSecond the number of seconds from 1970-01-01T00:00:00Z
* @param nanoAdjustment the nanosecond adjustment to the number of seconds, positive or negative
* @return this
* @throws IOException when underlying output throws IOException
* @throws ArithmeticException when epochSecond plus nanoAdjustment in seconds exceeds the range of long
*/
public MessagePacker packTimestampEpochSecond(long epochSecond, int nanoAdjustment)
throws IOException, ArithmeticException
{
long sec = Math.addExact(epochSecond, Math.floorDiv(nanoAdjustment, 1000000000L));
int nsec = (int) Math.floorMod(nanoAdjustment, 1000000000L);
return packTimestampImpl(sec, nsec);
}

private MessagePacker packTimestampImpl(long sec, int nsec)
throws IOException
{
if (sec >>> 34 == 0) {
// sec can be serialized in 34 bits.
long data64 = (nsec << 34) | sec;
if ((data64 & 0xffffffff00000000L) == 0L) {
// sec can be serialized in 32 bits and nsec is 0.
// use timestamp 32
writeTimestamp32((int) sec);
}
else {
// sec exceeded 32 bits or nsec is not 0.
// use timestamp 64
writeTimestamp64(data64);
}
}
else {
// use timestamp 96 format
writeTimestamp96(sec, nsec);
}
return this;
}

private void writeTimestamp32(int sec)
throws IOException
{
// timestamp 32 in fixext 4
ensureCapacity(6);
buffer.putByte(position++, FIXEXT4);
buffer.putByte(position++, EXT_TIMESTAMP);
buffer.putInt(position, sec);
position += 4;
}

private void writeTimestamp64(long data64)
throws IOException
{
// timestamp 64 in fixext 8
ensureCapacity(10);
buffer.putByte(position++, FIXEXT8);
buffer.putByte(position++, EXT_TIMESTAMP);
buffer.putLong(position, data64);
position += 8;
}

private void writeTimestamp96(long sec, int nsec)
throws IOException
{
// timestamp 96 in ext 8
ensureCapacity(15);
buffer.putByte(position++, EXT8);
buffer.putByte(position++, (byte) 12); // length of nsec and sec
buffer.putByte(position++, EXT_TIMESTAMP);
buffer.putInt(position, nsec);
position += 4;
buffer.putLong(position, sec);
position += 8;
}

/**
* Writes header of an Array value.
* <p>
Expand Down
80 changes: 79 additions & 1 deletion msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.msgpack.core.MessagePack.Code.EXT_TIMESTAMP;
import static org.msgpack.core.Preconditions.checkNotNull;

/**
Expand Down Expand Up @@ -596,6 +599,12 @@ private static MessagePackException unexpected(String expected, byte b)
}
}

private static MessagePackException unexpectedExtension(String expected, int expectedType, int actualType)
{
return new MessageTypeException(String.format("Expected extension type %s (%d), but got extension type %d",
expected, expectedType, actualType));
}

public ImmutableValue unpackValue()
throws IOException
{
Expand Down Expand Up @@ -644,7 +653,12 @@ public ImmutableValue unpackValue()
}
case EXTENSION: {
ExtensionTypeHeader extHeader = unpackExtensionTypeHeader();
return ValueFactory.newExtension(extHeader.getType(), readPayload(extHeader.getLength()));
switch (extHeader.getType()) {
case EXT_TIMESTAMP:
return ValueFactory.newTimestamp(readPayload(extHeader.getLength()));
default:
return ValueFactory.newExtension(extHeader.getType(), readPayload(extHeader.getLength()));
}
}
default:
throw new MessageNeverUsedFormatException("Unknown value type");
Expand Down Expand Up @@ -1232,6 +1246,70 @@ private String decodeStringFastPath(int length)
}
}

public Instant unpackInstant()
throws IOException
{
ExtensionTypeHeader ext = unpackExtensionTypeHeader();
if (ext.getType() != EXT_TIMESTAMP) {
throw unexpectedExtension("Timestamp", EXT_TIMESTAMP, ext.getType());
}
switch (ext.getLength()) {
case 4: {
int u32 = readInt();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frsyuki Need to convert to uint32 by applying 0xFFFFFFFFL mask

return Instant.ofEpochSecond(u32);
}
case 8: {
long data64 = readLong();
int nsec = (int) (data64 >>> 34);
long sec = data64 & 0x00000003ffffffffL;
return Instant.ofEpochSecond(sec, nsec);
}
case 12: {
int nsec = readInt();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also needs to be converted to uint32 with 0xFFFFFFFFL mask

long sec = readLong();
return Instant.ofEpochSecond(sec, nsec);
}
default:
throw new MessageExtensionFormatException(String.format("Timestamp extension type (%d) expects 4, 8, or 12 bytes of payload but got %d bytes",
EXT_TIMESTAMP, ext.getLength()));
}
}

public Date unpackDate()
throws IOException
{
return new Date(unpackTimestampMillis());
}

public long unpackTimestampMillis()
throws IOException
{
ExtensionTypeHeader ext = unpackExtensionTypeHeader();
if (ext.getType() != EXT_TIMESTAMP) {
throw unexpectedExtension("Timestamp", EXT_TIMESTAMP, ext.getType());
}
switch (ext.getLength()) {
case 4: {
int u32 = readInt();
return (u32 & 0xffffffffL) * 1000L;
}
case 8: {
long data64 = readLong();
int nsec = (int) (data64 >>> 34);
long sec = data64 & 0x00000003ffffffffL;
return sec * 1000L + nsec / 1000L;
}
case 12: {
int nsec = readInt();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@frsyuki nsec is uint32, so &xffffffffL mask is necessary to convert negative int32 to uint32

long sec = readLong();
return sec * 1000L + nsec / 1000L;
}
default:
throw new MessageExtensionFormatException(String.format("Timestamp extension type (%d) expects 4, 8, or 12 bytes of payload but got %d bytes",
EXT_TIMESTAMP, ext.getLength()));
}
}

/**
* Reads header of an array.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// MessagePack for Java
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.value;

/**
* Immutable representation of MessagePack's Timestamp type.
*
* @see org.msgpack.value.TimestampValue
*/
public interface ImmutableTimestampValue
extends TimestampValue, ImmutableValue
{
}
38 changes: 38 additions & 0 deletions msgpack-core/src/main/java/org/msgpack/value/TimestampValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// MessagePack for Java
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.value;

import java.time.Instant;
import java.util.Date;

/**
* Representation of MessagePack's Timestamp type.
*
* MessagePack's Timestamp type can represent a timestamp.
*/
public interface TimestampValue
extends ExtensionValue
{
long getEpochSecond();

int getNano();

long toEpochMilli();

Instant toInstant();

Date toDate();
}
1 change: 1 addition & 0 deletions msgpack-core/src/main/java/org/msgpack/value/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.msgpack.value;

import org.msgpack.core.MessagePacker;
import org.msgpack.core.MessageTypeCastException;

import java.io.IOException;

Expand Down
11 changes: 11 additions & 0 deletions msgpack-core/src/main/java/org/msgpack/value/ValueFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.msgpack.value.impl.ImmutableMapValueImpl;
import org.msgpack.value.impl.ImmutableNilValueImpl;
import org.msgpack.value.impl.ImmutableStringValueImpl;
import org.msgpack.value.impl.ImmutableTimestampValueImpl;

import java.math.BigInteger;
import java.util.AbstractMap;
Expand Down Expand Up @@ -294,4 +295,14 @@ public static ImmutableExtensionValue newExtension(byte type, byte[] data)
{
return new ImmutableExtensionValueImpl(type, data);
}

public static ImmutableTimestampValue newTimestamp(byte[] data)
{
return new ImmutableTimestampValueImpl(data);
}

public static ImmutableTimestampValue newTimestamp(long epochSecond, int nanoAdjustment)
{
return new ImmutableTimestampValueImpl(epochSecond, nanoAdjustment);
}
}
Loading