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

[WIP] Add hash expiration - HEXPIRE and family #674

Closed
wants to merge 9 commits into from
Closed
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
8 changes: 8 additions & 0 deletions libs/server/API/GarnetApiObjectCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,14 @@ public GarnetStatus HashIncrement(byte[] key, ref ObjectInput input, ref GarnetO
public GarnetStatus HashScan(ArgSlice key, long cursor, string match, int count, out ArgSlice[] items)
=> storageSession.ObjectScan(GarnetObjectType.Hash, key, cursor, match, count, out items, ref objectContext);

/// <inheritdoc />
public GarnetStatus HashExpire(byte[] key, ref ObjectInput input, ref GarnetObjectStoreOutput outputFooter)
=> storageSession.HashExpire(key, ref input, ref outputFooter, ref objectContext);

/// <inheritdoc />
public GarnetStatus HashTtl(byte[] key, ref ObjectInput input, ref GarnetObjectStoreOutput outputFooter)
=> storageSession.HashTtl(key, ref input, ref outputFooter, ref objectContext);

#endregion
}

Expand Down
17 changes: 17 additions & 0 deletions libs/server/API/IGarnetApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,23 @@ public interface IGarnetApi : IGarnetReadApi, IGarnetAdvancedApi
/// <returns></returns>
GarnetStatus HashIncrement(byte[] key, ref ObjectInput input, ref GarnetObjectStoreOutput outputFooter);

/// <summary>
/// Sets the expiration time for a hash field
/// </summary>
/// <param name="key"></param>
/// <param name="input"></param>
/// <param name="outputFooter"></param>
/// <returns></returns>
GarnetStatus HashExpire(byte[] key, ref ObjectInput input, ref GarnetObjectStoreOutput outputFooter);

/// <summary>
/// Gets the time to live for a hash field
/// </summary>
/// <param name="key"></param>
/// <param name="input"></param>
/// <param name="outputFooter"></param>
/// <returns></returns>
GarnetStatus HashTtl(byte[] key, ref ObjectInput input, ref GarnetObjectStoreOutput outputFooter);
#endregion

#region BitMaps Methods
Expand Down
51 changes: 41 additions & 10 deletions libs/server/Objects/Hash/HashObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public enum HashOperation : byte
HINCRBYFLOAT,
HRANDFIELD,
HSCAN,
HSTRLEN
HSTRLEN,
HEXPIRE,
HPEXPIRE,
HEXPIREAT,
HPEXPIREAT,
HTTL,
HPTTL
}


Expand All @@ -41,15 +47,30 @@ public enum HashOperation : byte
/// </summary>
public unsafe partial class HashObject : GarnetObjectBase
{
readonly Dictionary<byte[], byte[]> hash;
public class HashValue
{
public byte[] Value { get; set; }
public long Expiration { get; set; }
public HashValue(byte[] value, long expiration)
{
this.Value = value;
this.Expiration = expiration;
}

public HashValue(byte[] value) : this(value, 0) { }

public bool IsExpired() { return Expiration > 0 && DateTimeOffset.UtcNow.Ticks > Expiration; }
}

readonly Dictionary<byte[], HashValue> hash;

/// <summary>
/// Constructor
/// </summary>
public HashObject(long expiration = 0)
: base(expiration, MemoryUtils.DictionaryOverhead)
{
hash = new Dictionary<byte[], byte[]>(ByteArrayComparer.Instance);
hash = new Dictionary<byte[], HashValue>(ByteArrayComparer.Instance);
}

/// <summary>
Expand All @@ -58,14 +79,14 @@ public HashObject(long expiration = 0)
public HashObject(BinaryReader reader)
: base(reader, MemoryUtils.DictionaryOverhead)
{
hash = new Dictionary<byte[], byte[]>(ByteArrayComparer.Instance);
hash = new Dictionary<byte[], HashValue>(ByteArrayComparer.Instance);

int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
var item = reader.ReadBytes(reader.ReadInt32());
var value = reader.ReadBytes(reader.ReadInt32());
hash.Add(item, value);
hash.Add(item, new HashValue(value));

this.UpdateSize(item, value);
}
Expand All @@ -74,7 +95,7 @@ public HashObject(BinaryReader reader)
/// <summary>
/// Copy constructor
/// </summary>
public HashObject(Dictionary<byte[], byte[]> hash, long expiration, long size)
public HashObject(Dictionary<byte[], HashValue> hash, long expiration, long size)
: base(expiration, size)
{
this.hash = hash;
Expand All @@ -94,8 +115,8 @@ public override void DoSerialize(BinaryWriter writer)
{
writer.Write(kvp.Key.Length);
writer.Write(kvp.Key);
writer.Write(kvp.Value.Length);
writer.Write(kvp.Value);
writer.Write(kvp.Value.Value.Length);
writer.Write(kvp.Value.Value);
count--;
}
Debug.Assert(count == 0);
Expand Down Expand Up @@ -183,6 +204,16 @@ public override unsafe bool Operate(ref ObjectInput input, ref SpanByteAndMemory
ObjectUtils.WriteScanError(error, ref output);
}
break;
case HashOperation.HEXPIRE:
case HashOperation.HPEXPIRE:
case HashOperation.HEXPIREAT:
case HashOperation.HPEXPIREAT:
HashExpire(ref input, ref output);
break;
case HashOperation.HTTL:
case HashOperation.HPTTL:
HashTtl(ref input, ref output);
break;
default:
throw new GarnetException($"Unsupported operation {input.header.HashOp} in HashObject.Operate");
}
Expand Down Expand Up @@ -228,7 +259,7 @@ public override unsafe void Scan(long start, out List<byte[]> items, out long cu
if (patternLength == 0)
{
items.Add(item.Key);
items.Add(item.Value);
items.Add(item.Value.Value);
}
else
{
Expand All @@ -237,7 +268,7 @@ public override unsafe void Scan(long start, out List<byte[]> items, out long cu
if (GlobUtils.Match(pattern, patternLength, keyPtr, item.Key.Length))
{
items.Add(item.Key);
items.Add(item.Value);
items.Add(item.Value.Value);
}
}
}
Expand Down
Loading
Loading