Skip to content

Commit 3e29443

Browse files
committed
Adding more commands, Tests for TimeSeries and arrangements
1 parent d29286f commit 3e29443

21 files changed

+1027
-42
lines changed

src/NRedisStack.Core/RedisStackCommands/Bloom.cs renamed to src/NRedisStack.Core/Bloom/BloomCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using StackExchange.Redis;
2-
namespace NRedisStack.Core.RedisStackCommands
2+
namespace NRedisStack.Core
33
{
44

55
public class BloomCommands

src/NRedisStack.Core/RedisStackCommands/Json.cs renamed to src/NRedisStack.Core/Json/JsonCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Text.Json;
33
using System.Text.Json.Serialization;
44

5-
namespace NRedisStack.Core.RedisStackCommands;
5+
namespace NRedisStack.Core;
66

77
public class JsonCommands
88
{

src/NRedisStack.Core/RedisStackCommands/Search.cs renamed to src/NRedisStack.Core/Search/SearchCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using StackExchange.Redis;
2-
namespace NRedisStack.Core.RedisStackCommands
2+
namespace NRedisStack.Core
33
{
44
public class SearchCommands
55
{

src/NRedisStack.Core/TimeSeries/Commands/CommandArgs.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace NRedisStack.Core.TimeSeries.Commands
1+
namespace NRedisStack.Core.Commands
22
{
33
internal class CommandArgs
44
{
@@ -19,5 +19,6 @@ internal class CommandArgs
1919
public static string REDUCE => "REDUCE";
2020
public static string FILTER_BY_TS => "FILTER_BY_TS";
2121
public static string FILTER_BY_VALUE => "FILTER_BY_VALUE";
22+
2223
}
23-
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
using StackExchange.Redis;
2-
namespace NRedisStack.Core.RedisStackCommands
1+
namespace NRedisStack.Core.Commands
32
{
4-
public class TimeSeriesCommands
3+
internal class TS
54
{
6-
IDatabase _db;
7-
public TimeSeriesCommands(IDatabase db)
8-
{
9-
_db = db;
10-
}
11-
//TODO: INFO, CREATE, ALTER
12-
/*public static string CREATE => "TS.CREATE";
5+
public static string CREATE => "TS.CREATE";
136
public static string ALTER => "TS.ALTER";
147
public static string ADD => "TS.ADD";
158
public static string MADD => "TS.MADD";
169
public static string INCRBY => "TS.INCRBY";
1710
public static string DECRBY => "TS.DECRBY";
11+
public static string DEL => "TS.DEL";
1812
public static string CREATERULE => "TS.CREATERULE";
1913
public static string DELETERULE => "TS.DELETERULE";
2014
public static string RANGE => "TS.RANGE";
@@ -24,6 +18,6 @@ public TimeSeriesCommands(IDatabase db)
2418
public static string GET => "TS.GET";
2519
public static string MGET => "TS.MGET";
2620
public static string INFO => "TS.INFO";
27-
public static string QUERYINDEX => "TS.QUERYINDEX";*/
21+
public static string QUERYINDEX => "TS.QUERYINDEX";
2822
}
29-
}
23+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
namespace NRedisStack.Core.Commands.Enums
2+
{
3+
/// <summary>
4+
/// An aggregation type to be used with a time bucket.
5+
/// </summary>
6+
public enum TsAggregation
7+
{
8+
/// <summary>
9+
/// The average of all samples in the aggregation
10+
/// </summary>
11+
Avg,
12+
13+
/// <summary>
14+
/// A sum of all samples in the aggregation
15+
/// </summary>
16+
Sum,
17+
18+
/// <summary>
19+
/// A minimum sample of all samples in the aggregation
20+
/// </summary>
21+
Min,
22+
23+
/// <summary>
24+
/// A maximum sample of all samples in the aggregation
25+
/// </summary>
26+
Max,
27+
28+
/// <summary>
29+
/// A range of the min and max sample of all samples in the aggregation (range r = max-min)
30+
/// For example if the min sample was 100 and the max was 400, the range aggregation would return 300
31+
/// </summary>
32+
Range,
33+
34+
/// <summary>
35+
/// The total number of all samples in the aggregation
36+
/// </summary>
37+
Count,
38+
39+
/// <summary>
40+
/// The first sample in the aggregation
41+
/// </summary>
42+
First,
43+
44+
/// <summary>
45+
/// The last sample in the aggregation
46+
/// </summary>
47+
Last,
48+
49+
/// <summary>
50+
/// The standard deviation based on the entire population
51+
/// The standard deviation is a measure of how widely values are dispersed from the average sample in the aggregation
52+
/// </summary>
53+
StdP,
54+
55+
/// <summary>
56+
/// The standard deviation based on a sample of the population
57+
/// The standard deviation is a measure of how widely values are dispersed from the average sample in the aggregation
58+
/// </summary>
59+
StdS,
60+
61+
/// <summary>
62+
/// The variance based on the entire population
63+
/// The variance is the average of the squared differences from the mean
64+
/// </summary>
65+
VarP,
66+
67+
/// <summary>
68+
/// The variance based on a sample of the population
69+
/// The variance is the average of the squared differences from the mean
70+
/// </summary>
71+
VarS,
72+
}
73+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace NRedisStack.Core.Commands.Enums
2+
{
3+
/// <summary>
4+
/// Policy to handle duplicate samples.
5+
/// The default policy for database-wide is BLOCK.
6+
/// </summary>
7+
public enum TsDuplicatePolicy
8+
{
9+
/// <summary>
10+
/// An error will occur for any out of order sample.
11+
/// </summary>
12+
BLOCK,
13+
14+
/// <summary>
15+
/// Ignore the new value.
16+
/// </summary>
17+
FIRST,
18+
19+
/// <summary>
20+
/// Override with latest value.
21+
/// </summary>
22+
LAST,
23+
24+
/// <summary>
25+
/// Only override if the value is lower than the existing value.
26+
/// </summary>
27+
MIN,
28+
29+
/// <summary>
30+
/// Only override if the value is higher than the existing value.
31+
/// </summary>
32+
MAX,
33+
34+
/// <summary>
35+
/// If a previous sample exists, add the new sample to it so that the updated value is equal to (previous + new).
36+
/// If no previous sample exists, set the updated value equal to the new value.
37+
/// </summary>
38+
SUM
39+
}
40+
}
41+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace NRedisStack.Core.Commands.Enums
2+
{
3+
/// <summary>
4+
/// TODO: Add description
5+
/// </summary>
6+
public enum TsReduce
7+
{
8+
/// <summary>
9+
/// A sum of all samples in the group
10+
/// </summary>
11+
Sum,
12+
13+
/// <summary>
14+
/// A minimum sample of all samples in the group
15+
/// </summary>
16+
Min,
17+
18+
/// <summary>
19+
/// A maximum sample of all samples in the group
20+
/// </summary>
21+
Max,
22+
}
23+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Collections.Generic;
4+
using NRedisStack.Core.Commands.Enums;
5+
6+
7+
namespace NRedisStack.Core.DataTypes
8+
{
9+
/// <summary>
10+
/// This class represents the response for TS.INFO command.
11+
/// This object has Read-only properties and cannot be generated outside a TS.INFO response.
12+
/// </summary>
13+
public class TimeSeriesInformation
14+
{
15+
/// <summary>
16+
/// Total samples in the time-series.
17+
/// </summary>
18+
public long TotalSamples { get; private set; }
19+
20+
/// <summary>
21+
/// Total number of bytes allocated for the time-series.
22+
/// </summary>
23+
public long MemoryUsage { get; private set; }
24+
25+
/// <summary>
26+
/// First timestamp present in the time-series.
27+
/// </summary>
28+
public TimeStamp FirstTimeStamp { get; private set; }
29+
30+
/// <summary>
31+
/// Last timestamp present in the time-series.
32+
/// </summary>
33+
public TimeStamp LastTimeStamp { get; private set; }
34+
35+
/// <summary>
36+
/// Retention time, in milliseconds, for the time-series.
37+
/// </summary>
38+
public long RetentionTime { get; private set; }
39+
40+
/// <summary>
41+
/// Number of Memory Chunks used for the time-series.
42+
/// </summary>
43+
public long ChunkCount { get; private set; }
44+
45+
/// <summary>
46+
/// Maximum Number of samples per Memory Chunk.
47+
/// </summary>
48+
[ObsoleteAttribute("This method has been deprecated. Use ChunkSize instead.")]
49+
public long MaxSamplesPerChunk { get; private set; }
50+
51+
/// <summary>
52+
/// Memory Chunk size in Bytes.
53+
/// </summary>
54+
public long ChunkSize { get; private set; }
55+
56+
/// <summary>
57+
/// A readonly list of TimeSeriesLabel that represent metadata labels of the time-series.
58+
/// </summary>
59+
public IReadOnlyList<TimeSeriesLabel> Labels { get; private set; }
60+
61+
/// <summary>
62+
/// Source key for the queries time series key.
63+
/// </summary>
64+
public string SourceKey { get; private set; }
65+
66+
/// <summary>
67+
/// A readonly list of TimeSeriesRules that represent compaction Rules of the time-series.
68+
/// </summary>
69+
public IReadOnlyList<TimeSeriesRule> Rules { get; private set; }
70+
71+
/// <summary>
72+
/// The policy will define handling of duplicate samples.
73+
/// </summary>
74+
public TsDuplicatePolicy? DuplicatePolicy { get; private set; }
75+
76+
internal TimeSeriesInformation(long totalSamples, long memoryUsage, TimeStamp firstTimeStamp, TimeStamp lastTimeStamp, long retentionTime, long chunkCount, long chunkSize, IReadOnlyList<TimeSeriesLabel> labels, string sourceKey, IReadOnlyList<TimeSeriesRule> rules, TsDuplicatePolicy? policy)
77+
{
78+
TotalSamples = totalSamples;
79+
MemoryUsage = memoryUsage;
80+
FirstTimeStamp = firstTimeStamp;
81+
LastTimeStamp = lastTimeStamp;
82+
RetentionTime = retentionTime;
83+
ChunkCount = chunkCount;
84+
Labels = labels;
85+
SourceKey = sourceKey;
86+
Rules = rules;
87+
// backwards compatible with RedisTimeSeries < v1.4
88+
MaxSamplesPerChunk = chunkSize/16;
89+
ChunkSize = chunkSize;
90+
// configure what to do on duplicate sample > v1.4
91+
DuplicatePolicy = policy;
92+
}
93+
94+
/// <summary>
95+
/// Implicit cast from TimeSeriesInformation to string.
96+
/// </summary>
97+
/// <param name="info">TimeSeriesInformation</param>
98+
public static implicit operator string(TimeSeriesInformation info) => JsonSerializer.Serialize(info);
99+
}
100+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections.Generic;
2+
3+
namespace NRedisStack.Core.DataTypes
4+
{
5+
/// <summary>
6+
/// A key-value pair class represetns metadata label of time-series.
7+
/// </summary>
8+
public class TimeSeriesLabel
9+
{
10+
/// <summary>
11+
/// Label key
12+
/// </summary>
13+
public string Key { get; }
14+
15+
/// <summary>
16+
/// Label value
17+
/// </summary>
18+
public string Value { get; }
19+
20+
/// <summary>
21+
/// Create a new TimeSeriesLabel out of key and value strings.
22+
/// </summary>
23+
/// <param name="key">Key string</param>
24+
/// <param name="value">Value string</param>
25+
public TimeSeriesLabel(string key, string value) => (Key, Value) = (key, value);
26+
27+
/// <summary>
28+
/// Equality of TimeSeriesLabel objects
29+
/// </summary>
30+
/// <param name="obj">Object to compare</param>
31+
/// <returns>If two TimeSeriesLabel objects are equal</returns>
32+
public override bool Equals(object obj) =>
33+
obj is TimeSeriesLabel label &&
34+
Key == label.Key &&
35+
Value == label.Value;
36+
37+
/// <summary>
38+
/// Implicit cast from TimeSeriesLabel to string.
39+
/// </summary>
40+
/// <param name="tsl">TimeSeriesLabel</param>
41+
public static implicit operator string(TimeSeriesLabel tsl) => string.Format("Key: {0}, Val:{1}", tsl.Key, tsl.Value);
42+
43+
/// <summary>
44+
/// TimeSeriesLabel object hash code.
45+
/// </summary>
46+
/// <returns>TimeSeriesLabel object hash code.</returns>
47+
public override int GetHashCode()
48+
{
49+
var hashCode = 206514262;
50+
hashCode = (hashCode * -1521134295) + EqualityComparer<string>.Default.GetHashCode(Key);
51+
hashCode = (hashCode * -1521134295) + EqualityComparer<string>.Default.GetHashCode(Value);
52+
return hashCode;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)