aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-11 12:58:05 +0200
committeralex <[email protected]>2026-07-11 12:58:05 +0200
commit5f9649fe9000e4e499cffb7a1f034d8ab4bd247c (patch)
tree91af7e472f7fa0a9e41ec88307f99aad807c6721
parent1470772a00faad4a5b4130e0fe47513e5618e31d (diff)
downloadredis-clone-5f9649fe9000e4e499cffb7a1f034d8ab4bd247c.tar.xz
redis-clone-5f9649fe9000e4e499cffb7a1f034d8ab4bd247c.zip
refactored more commands to work with command registry
-rw-r--r--pkg/commands/command-registry.go10
-rw-r--r--pkg/commands/lpop.go30
-rw-r--r--pkg/commands/rpop.go30
3 files changed, 70 insertions, 0 deletions
diff --git a/pkg/commands/command-registry.go b/pkg/commands/command-registry.go
index eaefc65..7db4923 100644
--- a/pkg/commands/command-registry.go
+++ b/pkg/commands/command-registry.go
@@ -75,4 +75,14 @@ var Registry = map[string]CommandDef{
ExtractKeys: SingleKey,
Execute: Rename,
},
+ "RPOP": {
+ MinArgs: 2,
+ ExtractKeys: SingleKey,
+ Execute: Rpop,
+ },
+ "LPOP": {
+ MinArgs: 2,
+ ExtractKeys: SingleKey,
+ Execute: Lpop,
+ },
} \ No newline at end of file
diff --git a/pkg/commands/lpop.go b/pkg/commands/lpop.go
new file mode 100644
index 0000000..d1e1ccb
--- /dev/null
+++ b/pkg/commands/lpop.go
@@ -0,0 +1,30 @@
+package commands
+
+import (
+ "errors"
+ "redisClone/pkg/core"
+)
+
+func Lpop(args []string, getShard func(k string) *core.Shard) interface{}{
+ key := args[1]
+ s := getShard(key)
+ item, exists := s.Data[key]
+ if !exists {
+ return nil
+ }
+ typedList, ok := item.Value.([]string)
+ if !ok {
+ return errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ }
+ if len(typedList) == 0 {
+ return nil
+ }
+ newItem := typedList[0]
+ newList := typedList[1:]
+ if len(newList) <= 0 {
+ delete(s.Data, key)
+ } else {
+ s.Data[key] = core.Item{Value: newList}
+ }
+ return newItem
+} \ No newline at end of file
diff --git a/pkg/commands/rpop.go b/pkg/commands/rpop.go
new file mode 100644
index 0000000..4ef4a1d
--- /dev/null
+++ b/pkg/commands/rpop.go
@@ -0,0 +1,30 @@
+package commands
+
+import (
+ "errors"
+ "redisClone/pkg/core"
+)
+
+func Rpop(args []string, getShard func(k string) *core.Shard) interface{}{
+ key := args[1]
+ s := getShard(key)
+ item, exists := s.Data[key]
+ if !exists {
+ return nil
+ }
+ typedList, ok := item.Value.([]string)
+ if !ok {
+ return errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ }
+ if len(typedList) == 0 {
+ return nil
+ }
+ newItem := typedList[len(typedList)-1]
+ newList := typedList[:len(typedList)-1]
+ if len(newList) <= 0 {
+ delete(s.Data, key)
+ } else {
+ s.Data[key] = core.Item{Value: newList}
+ }
+ return newItem
+} \ No newline at end of file