From 1470772a00faad4a5b4130e0fe47513e5618e31d Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 11 Jul 2026 11:57:44 +0200 Subject: refactored some more commands to work with the new command registry --- pkg/commands/command-registry.go | 20 ++++++++++++++++++++ pkg/commands/decr.go | 33 +++++++++++++++++++++++++++++++++ pkg/commands/exists.go | 13 +++++++++++++ pkg/commands/incr.go | 33 +++++++++++++++++++++++++++++++++ pkg/commands/rename.go | 19 +++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 pkg/commands/decr.go create mode 100644 pkg/commands/exists.go create mode 100644 pkg/commands/incr.go create mode 100644 pkg/commands/rename.go (limited to 'pkg') diff --git a/pkg/commands/command-registry.go b/pkg/commands/command-registry.go index 0193da1..eaefc65 100644 --- a/pkg/commands/command-registry.go +++ b/pkg/commands/command-registry.go @@ -55,4 +55,24 @@ var Registry = map[string]CommandDef{ ExtractKeys: AllSubsequentKeys, Execute: Del, }, + "EXISTS": { + MinArgs: 2, + ExtractKeys: SingleKey, + Execute: Exists, + }, + "INCR": { + MinArgs: 2, + ExtractKeys: SingleKey, + Execute: Incr, + }, + "DECR": { + MinArgs: 2, + ExtractKeys: SingleKey, + Execute: Decr, + }, + "RENAME": { + MinArgs: 3, + ExtractKeys: SingleKey, + Execute: Rename, + }, } \ No newline at end of file diff --git a/pkg/commands/decr.go b/pkg/commands/decr.go new file mode 100644 index 0000000..c598b46 --- /dev/null +++ b/pkg/commands/decr.go @@ -0,0 +1,33 @@ +package commands + +import ( + "errors" + "redisClone/pkg/core" + "strconv" +) + +func Decr(args []string, getShard func(k string) *core.Shard) interface{}{ + key := args[1] + shard := getShard(key) + item, exists := shard.Data[key] + if exists { + strValue, ok := item.Value.(string) + if !ok { + err := errors.New("value is not an integer or out of range") + return err + } + + currentInt, err := strconv.Atoi(strValue) + if err != nil { + err := errors.New("value is not an integer or out of range") + return err + } + + newValueStr := strconv.Itoa(currentInt - 1) + shard.Data[key] = core.Item{Value: newValueStr} + return currentInt - 1 + } else { + shard.Data[key] = core.Item{Value: "-1"} + return -1 + } +} \ No newline at end of file diff --git a/pkg/commands/exists.go b/pkg/commands/exists.go new file mode 100644 index 0000000..42a0f01 --- /dev/null +++ b/pkg/commands/exists.go @@ -0,0 +1,13 @@ +package commands + +import "redisClone/pkg/core" + +func Exists(args []string, getShard func(k string) *core.Shard) interface{}{ + _, exists := getShard(args[1]).Data[args[1]] + if exists { + return 1 + } else { + return 0 + } + +} \ No newline at end of file diff --git a/pkg/commands/incr.go b/pkg/commands/incr.go new file mode 100644 index 0000000..1b9a493 --- /dev/null +++ b/pkg/commands/incr.go @@ -0,0 +1,33 @@ +package commands + +import ( + "errors" + "redisClone/pkg/core" + "strconv" +) + +func Incr(args []string, getShard func(k string) *core.Shard) interface{}{ + key := args[1] + shard := getShard(key) + item, exists := shard.Data[key] + if exists { + strValue, ok := item.Value.(string) + if !ok { + err := errors.New("value is not an integer or out of range") + return err + } + + currentInt, err := strconv.Atoi(strValue) + if err != nil { + err := errors.New("value is not an integer or out of range") + return err + } + + newValueStr := strconv.Itoa(currentInt + 1) + shard.Data[key] = core.Item{Value: newValueStr} + return currentInt + 1 + } else { + shard.Data[key] = core.Item{Value: "1"} + return 1 + } +} \ No newline at end of file diff --git a/pkg/commands/rename.go b/pkg/commands/rename.go new file mode 100644 index 0000000..6325fd4 --- /dev/null +++ b/pkg/commands/rename.go @@ -0,0 +1,19 @@ +package commands + +import ( + "errors" + "redisClone/pkg/core" +) + +func Rename(args []string, getShard func(k string) *core.Shard) interface{}{ + item, exists := getShard(args[1]).Data[args[1]] + if !exists { + + return errors.New("no such key") + } + + getShard(args[2]).Data[args[2]] = item + delete(getShard(args[1]).Data, args[1]) + + return core.SimpleString("OK") +} -- cgit v1.2.3