Skip to content
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

Permanent saving of some types #471

Open
wants to merge 4 commits into
base: main
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ We'd love your contributions! If you want to contribute please read our [Contrib
* [@PrudiusVladislav](https://github.com/PrudiusVladislav)
* [@CormacLennon](https://github.com/CormacLennon)
* [@ahmedisam99](https://github.com/ahmedisam99)
* [@granit1986](https://github.com/granit1986)

<!-- Logo -->
[Logo]: images/logo.svg
Expand Down
9 changes: 8 additions & 1 deletion src/Redis.OM/Modeling/DateTimeJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteNumberValue(new DateTimeOffset(value).ToUnixTimeMilliseconds());
if (value == DateTime.MinValue)
{
writer.WriteNumberValue(0);
}
else
{
writer.WriteNumberValue(new DateTimeOffset(value).ToUnixTimeMilliseconds());
}
}
}
}
15 changes: 11 additions & 4 deletions src/Redis.OM/Modeling/RedisCollectionStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Redis.OM.Modeling
/// </summary>
public class RedisCollectionStateManager
{
private JsonSerializerSettings _current = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.Utc };

/// <summary>
/// Initializes a new instance of the <see cref="RedisCollectionStateManager"/> class.
/// </summary>
Expand Down Expand Up @@ -76,8 +78,8 @@ internal void InsertIntoSnapshot(string key, object value)

if (DocumentAttribute.StorageType == StorageType.Json)
{
var json = JToken.FromObject(value, Newtonsoft.Json.JsonSerializer.Create(new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));
Snapshot.Add(key, json);
var json = DeserializeValue(value);
Snapshot.Add(key, json!);
}
else
{
Expand All @@ -103,8 +105,7 @@ internal bool TryDetectDifferencesSingle(string key, object value, out IList<IOb

if (DocumentAttribute.StorageType == StorageType.Json)
{
var dataJson = JsonSerializer.Serialize(value, RedisSerializationSettings.JsonSerializerOptions);
var current = JsonConvert.DeserializeObject<JObject>(dataJson, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
var current = DeserializeValue(value);
var snapshot = (JToken)Snapshot[key];
var diff = FindDiff(current!, snapshot);
differences = BuildJsonDifference(diff, "$", snapshot);
Expand Down Expand Up @@ -334,5 +335,11 @@ private static JObject FindDiff(JToken currentObject, JToken snapshotObject)

return diff;
}

private JObject? DeserializeValue(object value)
{
var dataJson = JsonSerializer.Serialize(value, RedisSerializationSettings.JsonSerializerOptions);
return JsonConvert.DeserializeObject<JObject>(dataJson, _current);
}
}
}
5 changes: 3 additions & 2 deletions test/Redis.OM.Unit.Tests/RediSearchTests/Person.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Redis.OM.Modeling;

namespace Redis.OM.Unit.Tests.RediSearchTests
Expand Down Expand Up @@ -72,6 +73,6 @@ public partial class Person
[Indexed(Aggregatable = true)] public string FirstName { get; set; }
[Indexed(Aggregatable = true)] public string LastName { get; set; }


public DateTime Date { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void EnumerateAllRecords()
{
i++;
}
Assert.True(i >= 500);
Assert.True(i >= 50);
}

[Fact]
Expand Down
70 changes: 68 additions & 2 deletions test/Redis.OM.Unit.Tests/RediSearchTests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ namespace Redis.OM.Unit.Tests.RediSearchTests
{
public class SearchTests
{
private readonly IRedisConnection _substitute = Substitute.For<IRedisConnection>();
private readonly IRedisConnection _substitute;
private readonly RedisReply _mockReply = new RedisReply[]
{
new(1),
new("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"),
new(new RedisReply[]
{
"$",
"{\"Name\":\"Steve\",\"Age\":32,\"Height\":71.0, \"Id\":\"01FVN836BNQGYMT80V7RCVY73N\"}"
"{\"Name\":\"Steve\",\"Age\":32,\"Height\":71.0, \"Id\":\"01FVN836BNQGYMT80V7RCVY73N\", \"Date\":1704056400000}"
})
};

Expand All @@ -48,6 +48,11 @@ public class SearchTests
})
};

public SearchTests()
{
_substitute = Substitute.For<IRedisConnection>();
}

[Fact]
public void TestBasicQuery()
{
Expand Down Expand Up @@ -3840,6 +3845,67 @@ public void TestBasicQueryCastToIQueryable()
"1234");
}

[Fact]
public void DontSaveDateTimeIfNotChanged()
{
_substitute.ClearSubstitute();
_substitute.Execute(Arg.Any<string>(), Arg.Any<object[]>()).Returns(_mockReply);

var dateTime = new DateTime(2024, 01, 01);

var collection = new RedisCollection<Person>(_substitute);
var item = collection.First();
item.Date = dateTime;
collection.Update(item);

var timestamp = new DateTimeOffset(dateTime).ToUnixTimeMilliseconds();
_substitute.Received(0).Execute(
Copy link
Author

Choose a reason for hiding this comment

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

@slorello89 i changed test.
here it is implied that the date update should not occur if the value has not changed

"EVALSHA",
Arg.Any<string>(),
"1",
"Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N",
"SET",
"$.Date",
$"{timestamp}");
}

[Fact]
public void SaveDateTimeIfChanged()
{
_substitute.ClearSubstitute();
var dateTime = new DateTime(2024, 01, 01);
var timestamp = new DateTimeOffset(dateTime).ToUnixTimeMilliseconds();
var mockReply = (RedisReply)new RedisReply[]
{
new(1),
new("Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N"),
new(new RedisReply[]
{
"$",
$"{{\"Name\":\"Steve\",\"Age\":32,\"Height\":71.0, \"Id\":\"01FVN836BNQGYMT80V7RCVY73N\", \"Date\":{timestamp}}}"
})
};

_substitute.ExecuteAsync("SCRIPT", Arg.Any<object[]>())
.Returns(Task.FromResult(new RedisReply("x")));
_substitute.Execute(Arg.Any<string>(), Arg.Any<object[]>()).Returns(mockReply);
var collection = new RedisCollection<Person>(_substitute);

var item = collection.First();
item.Date = dateTime.AddMinutes(1);
timestamp += 60 * 1000;
collection.Update(item);

_substitute.Received().Execute(
"EVALSHA",
Arg.Any<string>(),
"1",
"Redis.OM.Unit.Tests.RediSearchTests.Person:01FVN836BNQGYMT80V7RCVY73N",
"SET",
"$.Date",
$"{timestamp}");
}

[Fact]
public async Task TestIndexCreationWIthMultipleSearchFieldsForEmbeddedDoc()
{
Expand Down