-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom stored procedures with variable number of parameters (#369)
* Support custom commands (stored procedures) with a variable number of parameters. This can be used to implement commands such as a custom MSET with expiration. Example of MSETPX is included in the PR. * Add unit test * Add sample for multi-get where values match a specified prefix.
- Loading branch information
Showing
8 changed files
with
270 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Garnet.common; | ||
using Garnet.server; | ||
|
||
namespace Garnet | ||
{ | ||
/// <summary> | ||
/// Functions to implement custom transaction MGETIFPM - get multiple keys whose values match with the given prefix | ||
/// | ||
/// Format: MGETIFPM prefix key1 key2 ... | ||
/// Output: array of matching key-value pairs | ||
/// | ||
/// Description: Perform a non-transactional multi-get with value condition (prefix match) for the given set of keys | ||
/// </summary> | ||
sealed class MGetIfPM : CustomTransactionProcedure | ||
{ | ||
/// <summary> | ||
/// No transactional phase, skip Prepare | ||
/// </summary> | ||
public override bool Prepare<TGarnetReadApi>(TGarnetReadApi api, ArgSlice input) | ||
=> false; | ||
|
||
/// <summary> | ||
/// Main will not be called because Prepare returns false | ||
/// </summary> | ||
public override void Main<TGarnetApi>(TGarnetApi api, ArgSlice input, ref MemoryResult<byte> output) | ||
=> throw new InvalidOperationException(); | ||
|
||
/// <summary> | ||
/// Perform the MGETIFPM operation | ||
/// </summary> | ||
public override void Finalize<TGarnetApi>(TGarnetApi api, ArgSlice input, ref MemoryResult<byte> output) | ||
{ | ||
int offset = 0; | ||
|
||
// Read prefix | ||
var prefix = GetNextArg(input, ref offset); | ||
|
||
// Read key, check condition, add to output | ||
ArgSlice key; | ||
List<ArgSlice> values = []; | ||
while ((key = GetNextArg(input, ref offset)).Length > 0) | ||
{ | ||
if (api.GET(key, out var value) == GarnetStatus.OK) | ||
{ | ||
if (value.ReadOnlySpan.StartsWith(prefix.ReadOnlySpan)) | ||
{ | ||
values.Add(key); | ||
values.Add(value); | ||
} | ||
} | ||
} | ||
|
||
// Return the matching key-value pairs as an array of bulk strings | ||
WriteBulkStringArray(ref output, values); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
using System; | ||
using Garnet.common; | ||
using Garnet.server; | ||
|
||
namespace Garnet | ||
{ | ||
/// <summary> | ||
/// Functions to implement custom transaction MSETPX - set multiple keys with given expiration in milliseconds | ||
/// | ||
/// Format: MSETPX 60000 key1 value1 key2 value2 ... | ||
/// | ||
/// Description: Perform a non-transactional multi-set with expiry for the given set of key-value pairs | ||
/// </summary> | ||
sealed class MSetPxTxn : CustomTransactionProcedure | ||
{ | ||
/// <summary> | ||
/// No transactional phase, skip Prepare | ||
/// </summary> | ||
public override bool Prepare<TGarnetReadApi>(TGarnetReadApi api, ArgSlice input) | ||
=> false; | ||
|
||
/// <summary> | ||
/// Main will not be called because Prepare returns false | ||
/// </summary> | ||
public override void Main<TGarnetApi>(TGarnetApi api, ArgSlice input, ref MemoryResult<byte> output) | ||
=> throw new InvalidOperationException(); | ||
|
||
/// <summary> | ||
/// Perform the MSETPX operation | ||
/// </summary> | ||
public override void Finalize<TGarnetApi>(TGarnetApi api, ArgSlice input, ref MemoryResult<byte> output) | ||
{ | ||
int offset = 0; | ||
|
||
// Read expiry | ||
var expiryMs = GetNextArg(input, ref offset); | ||
|
||
// Read and set key-value pairs with expiry | ||
ArgSlice key, value; | ||
while ((key = GetNextArg(input, ref offset)).Length > 0) | ||
{ | ||
value = GetNextArg(input, ref offset); | ||
api.SETEX(key, value, expiryMs); | ||
} | ||
WriteSimpleString(ref output, "OK"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters