Skip to content
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d5254a1
IGNITE-12824: WIP
nizhikov Dec 11, 2020
ca3cc3a
IGNITE-12824: WIP
nizhikov Dec 11, 2020
063b6f0
IGNITE-12824: Fix of writing dates to the cache from the .Net
nizhikov Dec 14, 2020
24a89ed
IGNITE-12824: Fix of writing dates to the cache from the .Net
nizhikov Dec 14, 2020
17ffb01
IGNITE-12824: Fix of writing dates to the cache from the .Net
nizhikov Dec 14, 2020
317a666
IGNITE-12824: Fix of writing dates to the cache from the .Net
nizhikov Dec 14, 2020
2086fbf
IGNITE-12824: Fix of writing dates to the cache from the .Net
nizhikov Dec 14, 2020
d0514d3
IGNITE-12824: Fix of writing dates to the cache from the .Net
nizhikov Dec 14, 2020
03469e9
IGNITE-12824: Tests fix.
nizhikov Dec 14, 2020
58254af
Merge branch 'master' into IGNITE-12824
nizhikov Dec 16, 2020
75ee2ce
IGNITE-12824: Test update
nizhikov Dec 16, 2020
5b196fe
IGNITE-13865: Support of DateTime as a key or value in .Net and Java
nizhikov Dec 16, 2020
a06e4a3
IGNITE-13865: Support of DateTime as a key or value in .Net and Java
nizhikov Dec 16, 2020
1879594
IGNITE-13865: Support of DateTime as a key or value in .Net and Java
nizhikov Dec 16, 2020
e08e09a
IGNITE-13865: Support of DateTime as a key or value in .Net and Java
nizhikov Dec 16, 2020
ef420da
IGNITE-13865: Support of DateTime as a key or value in .Net and Java
nizhikov Dec 16, 2020
7ac33e7
IGNITE-13865: Support of DateTime as a key or value in .Net and Java
nizhikov Dec 17, 2020
af92eb4
IGNITE-13865: Support of DateTime as a key or value in .Net and Java
nizhikov Dec 17, 2020
e99e251
Merge branch 'master' into IGNITE-12824
nizhikov Dec 18, 2020
3fb26e1
IGNITE-12824: Test update
nizhikov Dec 18, 2020
d84249a
IGNITE-12824: Update of the date conversion(works on Mac)
nizhikov Dec 19, 2020
0b0af6f
IGNITE-12824: Allows local dates only for `ForceTimestamp` mode.
nizhikov Dec 20, 2020
e3ecbaa
IGNITE-12824: Allows local dates only for `ForceTimestamp` mode.
nizhikov Dec 20, 2020
332eb06
IGNITE-12824: Allows local dates only for `ForceTimestamp` mode.
nizhikov Dec 20, 2020
1d1dc86
IGNITE-12824: Allows local dates only for `ForceTimestamp` mode.
nizhikov Dec 20, 2020
c846297
IGNITE-12824: Allows usages of Local dates but only for .NetCore appl…
nizhikov Dec 20, 2020
2e5edb3
IGNITE-12824: Allows usages of Local dates but only for .NetCore appl…
nizhikov Dec 20, 2020
078b658
IGNITE-12824: IDateTimeConverter implemented
nizhikov Dec 21, 2020
969e5c5
IGNITE-12824: IDateTimeConverter implemented
nizhikov Dec 21, 2020
14b4456
IGNITE-12824: IDateTimeConverter implemented
nizhikov Dec 21, 2020
a93dc9f
IGNITE-12824: License fix.
nizhikov Dec 22, 2020
8a481da
IGNITE-12824: Code review fixes.
nizhikov Dec 22, 2020
e635495
IGNITE-12824: Code review fixes.
nizhikov Dec 22, 2020
f7fe8cd
Merge branch 'master' into IGNITE-12824
nizhikov Dec 23, 2020
c66a2f1
IGNITE-12824: Code review fixes.
nizhikov Dec 23, 2020
be9a7df
IGNITE-12824: Code review fixes.
nizhikov Dec 23, 2020
05a244b
IGNITE-12824: Code review fixes.
nizhikov Dec 24, 2020
9500998
IGNITE-12824: Code review fixes.
nizhikov Dec 24, 2020
fc2b7b1
Add TestAddYearTimestampConverter
ptupitsyn Dec 24, 2020
ae70be9
Add TestAddYearTimestampConverter
ptupitsyn Dec 24, 2020
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
Expand Up @@ -18,6 +18,8 @@
package org.apache.ignite.platform;

import java.sql.Timestamp;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
Expand Down Expand Up @@ -530,6 +532,24 @@ public void testUTCDateFromCache() {
assertEquals(new Timestamp(new Date(91, Calendar.OCTOBER, 1, 0, 0, 0).getTime()), cache.get(2));
}

/** */
public void testLocalDateFromCache() {
IgniteCache<Integer, Timestamp> cache = ignite.cache("net-dates");

ZoneId msk = ZoneId.of("Europe/Moscow");

//This Date in Europe/Moscow have offset +4.
Timestamp ts1 = new Timestamp(ZonedDateTime.of(1982, 4, 1, 1, 0, 0, 0, msk).toInstant().toEpochMilli());
//This Date in Europe/Moscow have offset +3.
Timestamp ts2 = new Timestamp(ZonedDateTime.of(1982, 3, 31, 22, 0, 0, 0, msk).toInstant().toEpochMilli());

assertEquals(ts1, cache.get(5));
assertEquals(ts2, cache.get(6));

cache.put(7, ts1);
cache.put(8, ts2);
}

/** */
public void sleep(long delayMs) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Apache.Ignite.Core.Tests.Binary
using System.Linq;
using Apache.Ignite.Core.Binary;
using Apache.Ignite.Core.Cache.Configuration;
using Apache.Ignite.Core.Impl.Binary;
using NUnit.Framework;

/// <summary>
Expand Down Expand Up @@ -80,7 +81,7 @@ public void TestSerializerForceTimestamp()
.Select(x => x.Serializer)
.OfType<BinaryReflectiveSerializer>()
.Single();

Assert.IsTrue(ser.ForceTimestamp);

AssertTimestampField<DateTimeObj2>((o, d) => o.Value = d, o => o.Value, "Value");
Expand Down Expand Up @@ -110,6 +111,45 @@ public void TestClassAttributes()
AssertTimestampField<DateTimeClassAttribute2>((o, d) => o.Value = d, o => o.Value, "Value");
}

/// <summary>
/// Tests custom timestamp converter.
/// </summary>
[Test]
public void TestCustomTimestampConverter()
{
var cfg = new IgniteConfiguration(TestUtils.GetTestConfiguration(name: "ignite-1"))
{
BinaryConfiguration = new BinaryConfiguration()
{
ForceTimestamp = true,
TimestampConverter = new TimestampConverter()
}
};
var ignite = Ignition.Start(cfg);
var binary = ignite.GetBinary();

// Check config.
Assert.NotNull(ignite.GetConfiguration().BinaryConfiguration.TimestampConverter);

AssertTimestampField<DateTimeObj2>((o, d) => o.Value = d, o => o.Value, "Value");

var dt1 = new DateTime(1997, 8, 29, 0, 0, 0, DateTimeKind.Utc);

var ex = Assert.Throws<BinaryObjectException>(() => binary.ToBinary<DateTime>(dt1));
Assert.AreEqual("ToJavaTicks Error!", ex.Message);

ex = Assert.Throws<BinaryObjectException>(() => binary.ToBinary<DateTime?[]>(new DateTime?[] {dt1}));
Assert.AreEqual("ToJavaTicks Error!", ex.Message);

var dt2 = new DateTime(1997, 8, 4, 0, 0, 0, DateTimeKind.Utc);

ex = Assert.Throws<BinaryObjectException>(() => binary.ToBinary<DateTime>(dt2));
Assert.AreEqual("FromJavaTicks Error!", ex.Message);

ex = Assert.Throws<BinaryObjectException>(() => binary.ToBinary<DateTime?[]>(new DateTime?[] {dt2}));
Assert.AreEqual("FromJavaTicks Error!", ex.Message);
}

/// <summary>
/// Asserts that specified field is serialized as DateTime object.
/// </summary>
Expand Down Expand Up @@ -201,4 +241,31 @@ private class DateTimeClassAttribute2
public DateTime Value;
}
}

/// <summary>
/// Adds support of the local dates to the Ignite timestamp serialization.
/// </summary>
class TimestampConverter : ITimestampConverter
{
/** <inheritdoc /> */
public void ToJavaTicks(DateTime date, out long high, out int low)
{
if (date.Year == 1997 && date.Month == 8 && date.Day == 29)
throw new BinaryObjectException("ToJavaTicks Error!");

BinaryUtils.ToJavaDate(date, out high, out low);
}

/** <inheritdoc /> */
public DateTime FromJavaTicks(long high, int low)
{
var date = new DateTime(BinaryUtils.JavaDateTicks + high * TimeSpan.TicksPerMillisecond + low / 100,
DateTimeKind.Utc);

if (date.Year == 1997 && date.Month == 8 && date.Day == 4)
throw new BinaryObjectException("FromJavaTicks Error!");

return date;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ public interface IJavaService
/** */
void testUTCDateFromCache();

/** */
void testLocalDateFromCache();

/** */
void sleep(long delayMs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ public void testUTCDateFromCache()
_svc.testDateFromCache();
}

/** <inheritDoc /> */
public void testLocalDateFromCache()
{
_svc.testLocalDateFromCache();
}

/** <inheritDoc /> */
public void sleep(long delayMs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Apache.Ignite.Core.Tests.Services
using Apache.Ignite.Core.Cluster;
using Apache.Ignite.Core.Common;
using Apache.Ignite.Core.Impl;
using Apache.Ignite.Core.Impl.Binary;
using Apache.Ignite.Core.Resource;
using Apache.Ignite.Core.Services;
using NUnit.Framework;
Expand Down Expand Up @@ -973,6 +974,28 @@ public void TestCallJavaService()
Assert.AreEqual(dt1, cache.Get(3));
Assert.AreEqual(dt2, cache.Get(4));

#if NETCOREAPP
//This Date in Europe/Moscow have offset +4.
DateTime dt3 = new DateTime(1982, 4, 1, 1, 0, 0, 0, DateTimeKind.Local);
//This Date in Europe/Moscow have offset +3.
DateTime dt4 = new DateTime(1982, 3, 31, 22, 0, 0, 0, DateTimeKind.Local);

cache.Put(5, dt3);
cache.Put(6, dt4);

Assert.AreEqual(dt3.ToUniversalTime(), cache.Get(5).ToUniversalTime());
Assert.AreEqual(dt4.ToUniversalTime(), cache.Get(6).ToUniversalTime());

svc.testLocalDateFromCache();

Assert.AreEqual(dt3, cache.Get(7).ToLocalTime());
Assert.AreEqual(dt4, cache.Get(8).ToLocalTime());

var now = DateTime.Now;
cache.Put(9, now);
Assert.AreEqual(now.ToUniversalTime(), cache.Get(9).ToUniversalTime());
#endif

Services.Cancel(javaSvcName);
}

Expand Down Expand Up @@ -1157,6 +1180,9 @@ private IgniteConfiguration GetConfiguration(string springConfigUrl)
{
NameMapper = BinaryBasicNameMapper.SimpleNameInstance,
ForceTimestamp = true
#if NETCOREAPP
, TimestampConverter = new TimestampConverter()
#endif
}
};
}
Expand Down Expand Up @@ -1539,5 +1565,28 @@ public class PlatformComputeBinarizable2
/** */
public int Field { get; set; }
}

#if NETCOREAPP
/// <summary>
/// Adds support of the local dates to the Ignite timestamp serialization.
/// </summary>
class TimestampConverter : ITimestampConverter
{
/** <inheritdoc /> */
public void ToJavaTicks(DateTime date, out long high, out int low)
{
if (date.Kind == DateTimeKind.Local)
date = date.ToUniversalTime();

BinaryUtils.ToJavaDate(date, out high, out low);
}

/** <inheritdoc /> */
public DateTime FromJavaTicks(long high, int low)
{
return new DateTime(BinaryUtils.JavaDateTicks + high * TimeSpan.TicksPerMillisecond + low / 100, DateTimeKind.Utc);
}
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<ItemGroup>
<Compile Include="Binary\BinaryBasicNameMapper.cs" />
<Compile Include="Binary\TimestampAttribute.cs" />
<Compile Include="Binary\ITimestampConverter.cs" />
<Compile Include="Cache\Affinity\IAffinityBackupFilter.cs" />
<Compile Include="Cache\Affinity\Rendezvous\ClusterNodeAttributeAffinityBackupFilter.cs" />
<Compile Include="Cache\Configuration\CacheKeyConfiguration.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ internal void CopyLocalProperties(BinaryConfiguration cfg)
NameMapper = cfg.NameMapper;
KeepDeserialized = cfg.KeepDeserialized;
ForceTimestamp = cfg.ForceTimestamp;
TimestampConverter = cfg.TimestampConverter;

if (cfg.Serializer != null)
{
Expand Down Expand Up @@ -136,6 +137,15 @@ public BinaryConfiguration(params Type[] binaryTypes)
/// </summary>
public IBinarySerializer Serializer { get; set; }

/// <summary>
/// Gets or sets a converter between <see cref="DateTime"/> and Java Timestamp.
/// Called from <see cref="IBinaryWriter.WriteTimestamp"/>, <see cref="IBinaryWriter.WriteTimestampArray"/>,
/// <see cref="IBinaryReader.ReadTimestamp"/>, <see cref="IBinaryReader.ReadTimestampArray"/>.
/// <para />
/// See also <see cref="ForceTimestamp"/>.
/// </summary>
public ITimestampConverter TimestampConverter { get; set; }

/// <summary>
/// Default keep deserialized flag.
/// </summary>
Expand Down Expand Up @@ -164,7 +174,7 @@ public bool CompactFooter
/// should be written as a Timestamp.
/// <para />
/// Timestamp format is required for values used in SQL and for interoperation with other platforms.
/// Only UTC values are supported in Timestamp format. Other values will cause an exception on write.
/// Only UTC values are supported in Timestamp format. Other values will cause an exception on write, unless <see cref="TimestampConverter"/> is provided.
/// <para />
/// Normally Ignite serializer uses <see cref="IBinaryWriter.WriteObject{T}"/> for DateTime fields,
/// keys and values.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

namespace Apache.Ignite.Core.Binary
{
using System;

/// <summary>
/// Converts <see cref="DateTime"/> values to Java Timestamp and back.
/// </summary>
public interface ITimestampConverter
{
/// <summary>Converts date to Java ticks.</summary>
/// <param name="date">Date</param>
/// <param name="high">High part (milliseconds).</param>
/// <param name="low">Low part (nanoseconds)</param>
void ToJavaTicks(DateTime date, out long high, out int low);

/// <summary>Converts date from Java ticks.</summary>
/// <param name="high">High part (milliseconds).</param>
/// <param name="low">Low part (nanoseconds)</param>
DateTime FromJavaTicks(long high, int low);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="timestampConverter" minOccurs="0">
<xs:annotation>
<xs:documentation>Default date time converter.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:attribute name="type" type="xs:string" use="required">
<xs:annotation>
<xs:documentation>Assembly-qualified type name.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="keepDeserialized" type="xs:boolean">
<xs:annotation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="timestampConverter" minOccurs="0">
<xs:annotation>
<xs:documentation>Default date time converter.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:attribute name="type" type="xs:string" use="required">
<xs:annotation>
<xs:documentation>Assembly-qualified type name.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:all>
<xs:attribute name="keepDeserialized" type="xs:boolean">
<xs:annotation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,25 +315,25 @@ public double[] ReadDoubleArray()
/** <inheritdoc /> */
public DateTime? ReadTimestamp(string fieldName)
{
return ReadField(fieldName, BinaryUtils.ReadTimestamp, BinaryTypeId.Timestamp);
return ReadField(fieldName, stream => BinaryUtils.ReadTimestamp(stream, _marsh.TimestampConverter), BinaryTypeId.Timestamp);
}

/** <inheritdoc /> */
public DateTime? ReadTimestamp()
{
return Read(BinaryUtils.ReadTimestamp, BinaryTypeId.Timestamp);
return Read(stream => BinaryUtils.ReadTimestamp(stream, _marsh.TimestampConverter), BinaryTypeId.Timestamp);
}

/** <inheritdoc /> */
public DateTime?[] ReadTimestampArray(string fieldName)
{
return ReadField(fieldName, BinaryUtils.ReadTimestampArray, BinaryTypeId.ArrayTimestamp);
return ReadField(fieldName, stream => BinaryUtils.ReadTimestampArray(stream, _marsh.TimestampConverter), BinaryTypeId.ArrayTimestamp);
}

/** <inheritdoc /> */
public DateTime?[] ReadTimestampArray()
{
return Read(BinaryUtils.ReadTimestampArray, BinaryTypeId.ArrayTimestamp);
return Read(stream => BinaryUtils.ReadTimestampArray(stream, _marsh.TimestampConverter), BinaryTypeId.ArrayTimestamp);
}

/** <inheritdoc /> */
Expand Down
Loading