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

Fix timestamp cast to datetime #226

Merged
merged 7 commits into from
Dec 17, 2023
Next Next commit
Change TimeStamp to a readonly struct record in order to avoid unnece…
…ssary allocations.
AmirShitrit committed Dec 16, 2023
commit c5506397ab61cde24e06d00a96dfa4d47e35fce8
14 changes: 3 additions & 11 deletions src/NRedisStack/TimeSeries/DataTypes/TimeStamp.cs
Original file line number Diff line number Diff line change
@@ -4,14 +4,14 @@
/// A class represents timestamp.
/// Value can be either primitive long, DateTime or one of the strings "-", "+", "*".
/// </summary>
public class TimeStamp
public readonly record struct TimeStamp
{
private static readonly string[] constants = { "-", "+", "*" };

/// <summary>
/// TimeStamp value.
/// </summary>
public object Value { get; private set; }
public object Value { get; }

/// <summary>
/// Build a TimeStamp from primitive long.
@@ -34,7 +34,7 @@ public TimeStamp(string timestamp)
{
if (Array.IndexOf(constants, timestamp) == -1)
{
throw new NotSupportedException(string.Format("The string {0} cannot be used", timestamp));
throw new NotSupportedException($"The string {timestamp} cannot be used");
}
Value = timestamp;
}
@@ -78,14 +78,6 @@ public static implicit operator long(TimeStamp ts) =>
/// <param name="timeStamp">TimeStamp</param>
public static implicit operator DateTime(TimeStamp timeStamp) => new DateTime(timeStamp);

/// <summary>
/// Equality of TimeSeriesTuple objects
/// </summary>
/// <param name="obj">Object to compare</param>
/// <returns>If two TimeStamp objects are equal</returns>
public override bool Equals(object? obj) =>
obj is TimeStamp stamp && EqualityComparer<object>.Default.Equals(Value, stamp.Value);

/// <summary>
/// TimeStamp object hash code.
/// </summary>
4 changes: 2 additions & 2 deletions src/NRedisStack/TimeSeries/TimeSeriesAux.cs
Original file line number Diff line number Diff line change
@@ -250,11 +250,11 @@ public static List<object> BuildTsAddArgs(string key, TimeStamp timestamp, doubl
return args;
}

public static List<object> BuildTsIncrDecrByArgs(string key, double value, TimeStamp? timestamp, long? retentionTime,
public static List<object> BuildTsIncrDecrByArgs(string key, double value, TimeStamp? timestampMaybe, long? retentionTime,
IReadOnlyCollection<TimeSeriesLabel>? labels, bool? uncompressed, long? chunkSizeBytes)
{
var args = new List<object> { key, value };
if (timestamp != null) args.AddTimeStamp(timestamp);
if (timestampMaybe is {} timestamp) args.AddTimeStamp(timestamp);
args.AddRetentionTime(retentionTime);
args.AddChunkSize(chunkSizeBytes);
if (labels != null) args.AddLabels(labels);