Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,13 +21,24 @@ 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>
/// DateTime binary serialization tests.
/// </summary>
public class BinaryDateTimeTest
{
/** */
internal const String FromErrMsg = "FromJavaTicks Error!";

/** */
internal const String ToErrMsg = "ToJavaTicks Error!";

/** */
private const String NotUtcDate =
"DateTime is not UTC. Only UTC DateTime can be used for interop with other platforms.";

/// <summary>
/// Sets up the test fixture.
/// </summary>
Expand Down Expand Up @@ -80,7 +91,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 +121,152 @@ public void TestClassAttributes()
AssertTimestampField<DateTimeClassAttribute2>((o, d) => o.Value = d, o => o.Value, "Value");
}

/// <summary>
/// Tests custom timestamp converter that modifies the values by adding one year on write and read.
/// This test verifies that actual converted values are used by Ignite.
/// </summary>
[Test]
public void TestAddYearTimestampConverter()
{
var cfg = new IgniteConfiguration(TestUtils.GetTestConfiguration())
{
AutoGenerateIgniteInstanceName = true,
BinaryConfiguration = new BinaryConfiguration
{
ForceTimestamp = true,
TimestampConverter = new AddYearTimestampConverter()
}
};

var ignite = Ignition.Start(cfg);

var dt = DateTime.UtcNow;
var expected = dt.AddYears(2);

// Key & value.
var cache = ignite.GetOrCreateCache<DateTime, DateTime>(TestUtils.TestName);
cache[dt] = dt;

var resEntry = cache.Single();

Assert.AreEqual(expected, resEntry.Key);
Assert.AreEqual(expected, resEntry.Value);

// Key & value array.

// Object field.
var cache2 = ignite.GetOrCreateCache<DateTimePropertyAttribute, DateTimePropertyAttribute>(
TestUtils.TestName);

cache2.RemoveAll();

var obj = new DateTimePropertyAttribute {Value = dt};
cache2[obj] = obj;

var resEntry2 = cache2.Single();
Assert.AreEqual(expected, resEntry2.Key.Value);
Assert.AreEqual(expected, resEntry2.Value.Value);

// Object array field.
var cache3 = ignite.GetOrCreateCache<DateTimeArr, DateTimeArr>(TestUtils.TestName);
cache3.RemoveAll();

var obj2 = new DateTimeArr {Value = new[]{dt}};
cache3[obj2] = obj2;
cache3[obj2] = obj2;

var resEntry3 = cache3.Single();
Assert.AreEqual(expected, resEntry3.Key.Value.Single());
Assert.AreEqual(expected, resEntry3.Value.Value.Single());
}

/// <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", ignite);

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

var ex = Assert.Throws<BinaryObjectException>(() => binary.ToBinary<DateTime>(dt1));
Assert.AreEqual(ToErrMsg, ex.Message);

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

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

ex = Assert.Throws<BinaryObjectException>(() => binary.ToBinary<DateTime>(dt2));
Assert.AreEqual(FromErrMsg, ex.Message);

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

var datesCache = ignite.CreateCache<DateTime, DateTime>("dates");

var check = new Action<DateTime, DateTime, String>((date1, date2, errMsg) =>
{
ex = Assert.Throws<BinaryObjectException>(() => datesCache.Put(date1, date2), "Timestamp fields should throw an error on non-UTC values");

Assert.AreEqual(errMsg, ex.Message);
});

check.Invoke(DateTime.Now, DateTime.UtcNow, NotUtcDate);
check.Invoke(DateTime.UtcNow, DateTime.Now, NotUtcDate);
check.Invoke(dt1, DateTime.UtcNow, ToErrMsg);
check.Invoke(DateTime.UtcNow, dt1, ToErrMsg);

var now = DateTime.UtcNow;

datesCache.Put(now, dt2);
ex = Assert.Throws<BinaryObjectException>(() => datesCache.Get(now), "Timestamp fields should throw an error on non-UTC values");
Assert.AreEqual(FromErrMsg, ex.Message);

datesCache.Put(now, now);
Assert.AreEqual(now, datesCache.Get(now));

var datesObjCache = ignite.CreateCache<DateTimeObj, DateTimeObj>("datesObj");

check = (date1, date2, errMsg) =>
{
ex = Assert.Throws<BinaryObjectException>(() => datesObjCache.Put(new DateTimeObj {Value = date1}, new DateTimeObj {Value = date2}),
"Timestamp fields should throw an error on non-UTC values");

Assert.AreEqual(errMsg, ex.Message);
};

check.Invoke(DateTime.Now, DateTime.UtcNow, NotUtcDate);
check.Invoke(DateTime.UtcNow, DateTime.Now, NotUtcDate);
check.Invoke(dt1, DateTime.UtcNow, ToErrMsg);
check.Invoke(DateTime.UtcNow, dt1, ToErrMsg);

var nowObj = new DateTimeObj {Value = now};

datesObjCache.Put(nowObj, new DateTimeObj {Value = dt2});
ex = Assert.Throws<BinaryObjectException>(() => datesObjCache.Get(nowObj), "Timestamp fields should throw an error on non-UTC values");
Assert.AreEqual(FromErrMsg, ex.Message);

datesObjCache.Put(nowObj, nowObj);
Assert.AreEqual(nowObj.Value, datesObjCache.Get(nowObj).Value);
}

/// <summary>
/// Asserts that specified field is serialized as DateTime object.
/// </summary>
Expand All @@ -136,10 +293,10 @@ private static void AssertDateTimeField<T>(Action<T, DateTime> setValue,
/// Asserts that specified field is serialized as Timestamp.
/// </summary>
private static void AssertTimestampField<T>(Action<T, DateTime> setValue,
Func<T, DateTime> getValue, string fieldName) where T : new()
Func<T, DateTime> getValue, string fieldName, IIgnite ignite = null) where T : new()
{
// Non-UTC DateTime throws.
var binary = Ignition.GetIgnite().GetBinary();
var binary = ignite != null ? ignite.GetBinary() : Ignition.GetIgnite().GetBinary();

var obj = new T();

Expand All @@ -148,8 +305,7 @@ private static void AssertTimestampField<T>(Action<T, DateTime> setValue,
var ex = Assert.Throws<BinaryObjectException>(() => binary.ToBinary<IBinaryObject>(obj),
"Timestamp fields should throw an error on non-UTC values");

Assert.AreEqual("DateTime is not UTC. Only UTC DateTime can be used for interop with other platforms.",
ex.Message);
Assert.AreEqual(NotUtcDate, ex.Message);

// UTC DateTime works.
setValue(obj, DateTime.UtcNow);
Expand All @@ -171,6 +327,11 @@ private class DateTimeObj2
public DateTime Value { get; set; }
}

private class DateTimeArr
{
public DateTime[] Value { get; set; }
}

private class DateTimePropertyAttribute
{
[Timestamp]
Expand Down Expand Up @@ -200,5 +361,47 @@ private class DateTimeClassAttribute2
{
public DateTime Value;
}

private 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(BinaryDateTimeTest.ToErrMsg);

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(BinaryDateTimeTest.FromErrMsg);

return date;
}
}

private class AddYearTimestampConverter : ITimestampConverter
{
/** <inheritdoc /> */
public void ToJavaTicks(DateTime date, out long high, out int low)
{
BinaryUtils.ToJavaDate(date.AddYears(1), 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);

return date.AddYears(1);
}
}
}
}
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
Loading