Skip to content

Backport OffsetDateTime serializers in GLVs to enable deserialization from server #3168

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

Open
wants to merge 1 commit into
base: 3.7-dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
* Support hot reloading of SSL certificates.
* Increase default `max_content_length`/`max_msg_size` in `gremlin-python` from 4MB to 10MB.
* Added the `PopContaining` interface designed to get label and `Pop` combinations held in a `PopInstruction` object.
* Backport `OffsetDateTime` serializers from 4.0.0-beta for deserialization. Note `Date` remains the default serializer for GLV native date types.
* Fixed bug preventing a vertex from being dropped and then re-added in the same `TinkerTransaction`

[[release-3-7-3]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class DataType : IEquatable<DataType>
// TODO: Support metrics and traversal metrics
public static readonly DataType Char = new DataType(0x80);
public static readonly DataType Duration = new DataType(0x81);
public static readonly DataType OffsetDateTime = new DataType(0x88);
#pragma warning restore 1591

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public class TypeSerializerRegistry
{DataType.BulkSet, new BulkSetSerializer<List<object>>()},
{DataType.Char, new CharSerializer()},
{DataType.Duration, new DurationSerializer()},
{DataType.OffsetDateTime, new OffsetDateTimeSerializer()},
};

private readonly Dictionary<string, CustomTypeSerializer> _serializerByCustomTypeName =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#region License

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#endregion

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Gremlin.Net.Structure.IO.GraphBinary.Types
{
/// <summary>
/// A serializer for the GraphBinary type OffsetDateTime, represented as <see cref="DateTimeOffset"/>
/// in .NET.
/// </summary>
public class OffsetDateTimeSerializer : SimpleTypeSerializer<DateTimeOffset>
{

/// <summary>
/// Initializes a new instance of the <see cref="OffsetDateTimeSerializer" /> class.
/// </summary>
public OffsetDateTimeSerializer() : base(DataType.OffsetDateTime)
{
}

/// <inheritdoc />
protected override async Task WriteValueAsync(DateTimeOffset value, Stream stream, GraphBinaryWriter writer,
CancellationToken cancellationToken = default)
{
await stream.WriteIntAsync(value.Year, cancellationToken).ConfigureAwait(false);
await stream.WriteByteAsync(Convert.ToByte(value.Month), cancellationToken).ConfigureAwait(false);
await stream.WriteByteAsync(Convert.ToByte(value.Day), cancellationToken).ConfigureAwait(false);
// Note that nanosecond precisions were added after .NET 7
// Get the time of day as TimeSpan
var timeOfDay = value.TimeOfDay;
// Convert ticks to nanoseconds (1 tick = 100 nanoseconds)
var ns = timeOfDay.Ticks * 100;
await stream.WriteLongAsync(Convert.ToInt64(ns), cancellationToken).ConfigureAwait(false);

var offset = value.Offset;
var os = offset.Hours * 60 * 60 + offset.Minutes * 60 + offset.Seconds;
await stream.WriteIntAsync(os, cancellationToken).ConfigureAwait(false);
}

/// <inheritdoc />
protected override async Task<DateTimeOffset> ReadValueAsync(Stream stream, GraphBinaryReader reader,
CancellationToken cancellationToken = default)
{
var year = await stream.ReadIntAsync(cancellationToken).ConfigureAwait(false);
var month = await stream.ReadByteAsync(cancellationToken).ConfigureAwait(false);
var day = await stream.ReadByteAsync(cancellationToken).ConfigureAwait(false);
var ns = await stream.ReadLongAsync(cancellationToken).ConfigureAwait(false);
var timeDelta = TimeSpan.FromMilliseconds(ns / 1e6);

var os = await stream.ReadIntAsync(cancellationToken).ConfigureAwait(false);
var offset = TimeSpan.FromSeconds(os);

return new DateTimeOffset(year, Convert.ToInt32(month), Convert.ToInt32(day), 0, 0, 0, offset).Add(timeDelta);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public abstract class GraphSONReader
{ "g:T", new TDeserializer() },

//Extended
{ "gx:OffsetDateTime", new OffsetDateTimeDeserializer() },
{ "gx:BigDecimal", new DecimalConverter() },
{ "gx:Duration", new DurationDeserializer() },
{ "gx:BigInteger", new BigIntegerDeserializer() },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#region License

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
#endregion

using System;
using System.IO;
using System.Text.Json;

namespace Gremlin.Net.Structure.IO.GraphSON
{
internal class OffsetDateTimeDeserializer : IGraphSONDeserializer
{
public dynamic Objectify(JsonElement graphsonObject, GraphSONReader reader)
{
return DateTimeOffset.Parse(graphsonObject.GetString() ??
throw new IOException("Read null but expected a OffsetDateTime value"));
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I just want to clarify that I'm understanding this correctly. This OffsetDateTimeSerializer will not be used until 3.8 and is not being used in 3.7.4. This was not added to the GraphSONWriter.cs logic because for 3.7.4 we are still serializing DateTimeOffset as Date and there is no way to write OffsetDateTime to a request.

Is my understanding correct? If so, is there any value with adding this OffsetDateTimeSerializer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that is correct. This is more of a connivence back-port for the ability to deserialize potential OffsetDateTime from server in the GLVs (Java has the support already). I don't quite expect this to be used per se, but the difference would be a deserialization error vs successful deserialization.
Potential downside is that OffsetDateTime will not be able to round-trip, given GLVs still use Date as default type for native date supports.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#region License

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#endregion

using System;
using System.Collections.Generic;

namespace Gremlin.Net.Structure.IO.GraphSON
{
internal class OffsetDateTimeSerializer : IGraphSONSerializer
{
public Dictionary<string, dynamic> Dictify(dynamic objectData, GraphSONWriter writer)
{
DateTimeOffset value = objectData;
return GraphSONUtil.ToTypedValue("OffsetDateTime", value.ToString("O"), "gx");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ public void ShouldDeserializeDateToDateTimeOffset(int version)
Assert.Equal(expectedDateTimeOffset, deserializedValue);
}

[Theory, MemberData(nameof(Versions))]
public void ShouldDeserializeOffsetDateTimeToDateTimeOffset(int version)
{
const string graphSon = "{\"@type\":\"gx:OffsetDateTime\",\"@value\":\"2016-10-04T12:17:22.5520000+00:00\"}";
var reader = CreateStandardGraphSONReader(version);

var jsonElement = JsonSerializer.Deserialize<JsonElement>(graphSon);
var deserializedValue = reader.ToObject(jsonElement);

var expectedDateTimeOffset = TestUtils.FromJavaTime(1475583442552);
Comment on lines +114 to +120
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const string graphSon = "{\"@type\":\"gx:OffsetDateTime\",\"@value\":\"2016-10-04T12:17:22.5520000+00:00\"}";
var reader = CreateStandardGraphSONReader(version);
var jsonElement = JsonSerializer.Deserialize<JsonElement>(graphSon);
var deserializedValue = reader.ToObject(jsonElement);
var expectedDateTimeOffset = TestUtils.FromJavaTime(1475583442552);
const string dateTimeString = "2016-10-04T12:17:22.5520000+00:00";
const string graphSon = "{\"@type\":\"gx:OffsetDateTime\",\"@value\":\"" + dateTimeString + "\"}";
var reader = CreateStandardGraphSONReader(version);
var jsonElement = JsonSerializer.Deserialize<JsonElement>(graphSon);
var deserializedValue = reader.ToObject(jsonElement);
var expectedDateTimeOffset = DateTimeOffset.Parse(dateTimeString);

Assert.Equal(expectedDateTimeOffset, deserializedValue);
}

[Theory, MemberData(nameof(Versions))]
public void ShouldDeserializeDictionary(int version)
{
Expand Down
72 changes: 72 additions & 0 deletions gremlin-go/driver/graphBinary.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
metricsType dataType = 0x2c
traversalMetricsType dataType = 0x2d
durationType dataType = 0x81
offsetDateTimeType dataType = 0x88
nullType dataType = 0xFE
)

Expand Down Expand Up @@ -509,6 +510,40 @@ func timeWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSeria
return buffer.Bytes(), nil
}

// Datetime remains serialized as Date by default, real use is the ability to deserialize OffsetDateTime
func offsetDateTimeWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
t := value.(time.Time)
err := binary.Write(buffer, binary.BigEndian, int32(t.Year()))
if err != nil {
return nil, err
}

err = binary.Write(buffer, binary.BigEndian, byte(t.Month()))
if err != nil {
return nil, err
}
err = binary.Write(buffer, binary.BigEndian, byte(t.Day()))
if err != nil {
return nil, err
}
// construct time of day in nanoseconds
h := int64(t.Hour())
m := int64(t.Minute())
s := int64(t.Second())
ns := (h * 60 * 60 * 1e9) + (m * 60 * 1e9) + (s * 1e9) + int64(t.Nanosecond())
err = binary.Write(buffer, binary.BigEndian, ns)
if err != nil {
return nil, err
}
_, os := t.Zone()
err = binary.Write(buffer, binary.BigEndian, int32(os))
if err != nil {
return nil, err
}

return buffer.Bytes(), nil
}

func durationWriter(value interface{}, buffer *bytes.Buffer, _ *graphBinaryTypeSerializer) ([]byte, error) {
t := value.(time.Duration)
sec := int64(t / time.Second)
Expand Down Expand Up @@ -1044,6 +1079,43 @@ func timeReader(data *[]byte, i *int) (interface{}, error) {
return time.UnixMilli(readLongSafe(data, i)), nil
}

func offsetDateTimeReader(data *[]byte, i *int) (interface{}, error) {
year := readIntSafe(data, i)
month := readByteSafe(data, i)
day := readByteSafe(data, i)
totalNS := readLongSafe(data, i)
// calculate hour, minute, second, and ns from totalNS (int64) to prevent int overflow in the nanoseconds arg
ns := totalNS % 1e9
totalS := totalNS / 1e9
s := totalS % 60
totalM := totalS / 60
m := totalM % 60
h := totalM / 60

offset := readIntSafe(data, i)
datetime := time.Date(int(year), time.Month(month), int(day), int(h), int(m), int(s), int(ns), GetTimezoneFromOffset(int(offset)))
return datetime, nil
}

// GetTimezoneFromOffset is a helper function to convert an offset in seconds to a time.Location
func GetTimezoneFromOffset(offsetSeconds int) *time.Location {
// calculate hours and minutes from seconds
hours := offsetSeconds / 3600
minutes := (offsetSeconds % 3600) / 60

// format the timezone name in the format that go expects
// for example: "UTC+01:00" or "UTC-05:30"
sign := "+"
if hours < 0 {
sign = "-"
hours = -hours
minutes = -minutes
}
tzName := fmt.Sprintf("UTC%s%02d:%02d", sign, hours, minutes)

return time.FixedZone(tzName, offsetSeconds)
}

func durationReader(data *[]byte, i *int) (interface{}, error) {
return time.Duration(readLongSafe(data, i)*int64(time.Second) + int64(readIntSafe(data, i))), nil
}
Expand Down
33 changes: 33 additions & 0 deletions gremlin-go/driver/graphBinary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,39 @@ func TestGraphBinaryV1(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, source, res)
})
t.Run("read-write local datetime", func(t *testing.T) {
pos := 0
var buffer bytes.Buffer
source := time.Date(2022, 5, 10, 9, 51, 0, 0, time.Local)
buf, err := offsetDateTimeWriter(source, &buffer, nil)
assert.Nil(t, err)
res, err := offsetDateTimeReader(&buf, &pos)
assert.Nil(t, err)
// ISO format
assert.Equal(t, source.Format(time.RFC3339Nano), res.(time.Time).Format(time.RFC3339Nano))
})
t.Run("read-write UTC datetime", func(t *testing.T) {
pos := 0
var buffer bytes.Buffer
source := time.Date(2022, 5, 10, 9, 51, 0, 0, time.UTC)
buf, err := offsetDateTimeWriter(source, &buffer, nil)
assert.Nil(t, err)
res, err := offsetDateTimeReader(&buf, &pos)
assert.Nil(t, err)
// ISO format
assert.Equal(t, source.Format(time.RFC3339Nano), res.(time.Time).Format(time.RFC3339Nano))
})
t.Run("read-write HST datetime", func(t *testing.T) {
pos := 0
var buffer bytes.Buffer
source := time.Date(2022, 5, 10, 9, 51, 34, 123456789, GetTimezoneFromOffset(-36000))
buf, err := offsetDateTimeWriter(source, &buffer, nil)
assert.Nil(t, err)
res, err := offsetDateTimeReader(&buf, &pos)
assert.Nil(t, err)
// ISO format
assert.Equal(t, source.Format(time.RFC3339Nano), res.(time.Time).Format(time.RFC3339Nano))
})
})

t.Run("error handle tests", func(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions gremlin-go/driver/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func initSerializers() {
setType: setWriter,
dateType: timeWriter,
durationType: durationWriter,
offsetDateTimeType: offsetDateTimeWriter,
cardinalityType: enumWriter,
columnType: enumWriter,
directionType: enumWriter,
Expand Down Expand Up @@ -310,9 +311,10 @@ func initDeserializers() {
classType: readClass,

// Date Time
dateType: timeReader,
timestampType: timeReader,
durationType: durationReader,
dateType: timeReader,
timestampType: timeReader,
offsetDateTimeType: offsetDateTimeReader,
durationType: durationReader,

// Graph
traverserType: traverserReader,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ ioc.longSerializer = new (require('./internals/LongSerializer'))(io
ioc.longSerializerNg = new (require('./internals/LongSerializerNg'))(ioc);
ioc.stringSerializer = new (require('./internals/StringSerializer'))(ioc, ioc.DataType.STRING);
ioc.dateSerializer = new (require('./internals/DateSerializer'))(ioc, ioc.DataType.DATE);
ioc.offsetDateTimeSerializer = new (require('./internals/OffsetDateTimeSerializer'))(ioc, ioc.DataType.OFFSETDATETIME);
ioc.timestampSerializer = new (require('./internals/DateSerializer'))(ioc, ioc.DataType.TIMESTAMP);
ioc.classSerializer = new (require('./internals/StringSerializer'))(ioc, ioc.DataType.CLASS);
ioc.doubleSerializer = new (require('./internals/DoubleSerializer'))(ioc);
Expand Down
Loading
Loading