From 5d81a85879e90e4b3d8b254a40df92fe0f879980 Mon Sep 17 00:00:00 2001 From: Vasileios Zois Date: Fri, 9 Aug 2024 11:57:22 -0700 Subject: [PATCH] generalized keyspecs use --- .../Resp/RespServerSessionSlotVerify.cs | 92 +++++++++++-------- .../Garnet.test.cluster/ClusterTestContext.cs | 2 +- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/libs/server/Resp/RespServerSessionSlotVerify.cs b/libs/server/Resp/RespServerSessionSlotVerify.cs index a73ebaf0abc..ff46de65425 100644 --- a/libs/server/Resp/RespServerSessionSlotVerify.cs +++ b/libs/server/Resp/RespServerSessionSlotVerify.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System; +using Garnet.common; namespace Garnet.server { @@ -51,51 +52,62 @@ bool CanServeSlot(RespCommand cmd) return true; var specs = commandInfo.KeySpecifications; - switch (cmd) + switch (specs.Length) { - case RespCommand.ZDIFF: - var beginSearch = (BeginSearchIndex)specs[0].BeginSearch; - var findKeysKeyNum = (FindKeysKeyNum)specs[0].FindKeys; - csvi.firstKey = beginSearch.Index; - csvi.lastKey = beginSearch.Index + parseState.GetInt(0); - csvi.step = findKeysKeyNum.KeyStep; - break; - case RespCommand.BITOP: - beginSearch = (BeginSearchIndex)specs[0].BeginSearch; - var findKeys = (FindKeysRange)specs[1].FindKeys; - csvi.firstKey = beginSearch.Index - 2; - csvi.lastKey = findKeys.LastKey < 0 ? findKeys.LastKey + parseState.count + 1 : findKeys.LastKey - beginSearch.Index + 1; - csvi.step = findKeys.KeyStep; - break; - case RespCommand.PFMERGE: - case RespCommand.SDIFFSTORE: - case RespCommand.SUNIONSTORE: - case RespCommand.SINTERSTORE: - beginSearch = (BeginSearchIndex)specs[0].BeginSearch; - findKeys = (FindKeysRange)specs[1].FindKeys; - csvi.firstKey = beginSearch.Index - 1; - csvi.lastKey = findKeys.LastKey < 0 ? findKeys.LastKey + parseState.count + 1 : findKeys.LastKey - beginSearch.Index + 1; - csvi.step = findKeys.KeyStep; + case 1: + var searchIndex = (BeginSearchIndex)specs[0].BeginSearch; + + switch (specs[0].FindKeys) + { + case FindKeysRange: + var findRange = (FindKeysRange)specs[0].FindKeys; + csvi.firstKey = searchIndex.Index - 1; + csvi.lastKey = findRange.LastKey < 0 ? findRange.LastKey + parseState.count + 1 : findRange.LastKey - searchIndex.Index + 1; + csvi.step = findRange.KeyStep; + break; + case FindKeysKeyNum: + var findKeysKeyNum = (FindKeysKeyNum)specs[0].FindKeys; + csvi.firstKey = searchIndex.Index + findKeysKeyNum.FirstKey - 1; + csvi.lastKey = searchIndex.Index + parseState.GetInt(0); + csvi.step = findKeysKeyNum.KeyStep; + break; + case FindKeysUnknown: + default: + throw new GarnetException("FindKeysUnknown range"); + } + break; - case RespCommand.RENAME: - case RespCommand.SMOVE: - case RespCommand.LMOVE: - beginSearch = (BeginSearchIndex)specs[0].BeginSearch; - var beginSearch1 = (BeginSearchIndex)specs[1].BeginSearch; - findKeys = (FindKeysRange)specs[1].FindKeys; - csvi.firstKey = beginSearch.Index - 1; - csvi.lastKey = beginSearch1.Index - beginSearch.Index + 1; - csvi.step = findKeys.KeyStep; + case 2: + searchIndex = (BeginSearchIndex)specs[0].BeginSearch; + switch (specs[0].FindKeys) + { + case FindKeysRange: + csvi.firstKey = RespCommand.BITOP == cmd ? searchIndex.Index - 2 : searchIndex.Index - 1; + break; + case FindKeysKeyNum: + case FindKeysUnknown: + default: + throw new GarnetException("FindKeysUnknown range"); + } + + switch (specs[1].FindKeys) + { + case FindKeysRange: + var searchIndex1 = (BeginSearchIndex)specs[1].BeginSearch; + var findRange = (FindKeysRange)specs[1].FindKeys; + csvi.lastKey = findRange.LastKey < 0 ? findRange.LastKey + parseState.count + 1 : findRange.LastKey + searchIndex1.Index - searchIndex.Index + 1; + csvi.step = findRange.KeyStep; + break; + case FindKeysKeyNum: + case FindKeysUnknown: + default: + throw new GarnetException("FindKeysUnknown range"); + } + break; default: - beginSearch = (BeginSearchIndex)specs[0].BeginSearch; - findKeys = (FindKeysRange)specs[0].FindKeys; - csvi.firstKey = beginSearch.Index - 1; - csvi.lastKey = findKeys.LastKey < 0 ? findKeys.LastKey + parseState.count + 1 : findKeys.LastKey - beginSearch.Index + 1; - csvi.step = findKeys.KeyStep; - break; + throw new GarnetException("KeySpecification unknown count"); } - csvi.readOnly = cmd.IsReadOnly(); csvi.sessionAsking = SessionAsking; diff --git a/test/Garnet.test.cluster/ClusterTestContext.cs b/test/Garnet.test.cluster/ClusterTestContext.cs index b3f38ef27f6..a06d263e944 100644 --- a/test/Garnet.test.cluster/ClusterTestContext.cs +++ b/test/Garnet.test.cluster/ClusterTestContext.cs @@ -39,7 +39,7 @@ internal class ClusterTestContext public void Setup(Dictionary monitorTests) { TestFolder = TestUtils.UnitTestWorkingDir() + "\\"; - var logLevel = !string.IsNullOrEmpty(TestContext.CurrentContext.Test.MethodName) && monitorTests.Contains(TestContext.CurrentContext.Test.MethodName) ? LogLevel.Trace : LogLevel.Error; + var logLevel = !string.IsNullOrEmpty(TestContext.CurrentContext.Test.MethodName) && monitorTests.ContainsKey(TestContext.CurrentContext.Test.MethodName) ? LogLevel.Trace : LogLevel.Error; loggerFactory = TestUtils.CreateLoggerFactoryInstance(logTextWriter, logLevel, scope: TestContext.CurrentContext.Test.Name); logger = loggerFactory.CreateLogger(TestContext.CurrentContext.Test.Name); logger.LogDebug("0. Setup >>>>>>>>>>>>");