aboutsummaryrefslogtreecommitdiff
path: root/pkg/commands
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-11 13:18:25 +0200
committeralex <[email protected]>2026-07-11 13:18:25 +0200
commit56bf3fc1b36e7301727e9d8d94a6239a88c4e208 (patch)
treee08f45dc1eefbfc435cc6984970e75facdcb55e9 /pkg/commands
parent5f9649fe9000e4e499cffb7a1f034d8ab4bd247c (diff)
downloadredis-clone-56bf3fc1b36e7301727e9d8d94a6239a88c4e208.tar.xz
redis-clone-56bf3fc1b36e7301727e9d8d94a6239a88c4e208.zip
refactroed LPUSH and RPUSH for command registry
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/command-registry.go10
-rw-r--r--pkg/commands/lpush.go26
-rw-r--r--pkg/commands/rpush.go26
3 files changed, 62 insertions, 0 deletions
diff --git a/pkg/commands/command-registry.go b/pkg/commands/command-registry.go
index 7db4923..6e34ff7 100644
--- a/pkg/commands/command-registry.go
+++ b/pkg/commands/command-registry.go
@@ -75,6 +75,16 @@ var Registry = map[string]CommandDef{
ExtractKeys: SingleKey,
Execute: Rename,
},
+ "RPUSH": {
+ MinArgs: 3,
+ ExtractKeys: SingleKey,
+ Execute: Rpush,
+ },
+ "LPUSH": {
+ MinArgs: 3,
+ ExtractKeys: SingleKey,
+ Execute: Lpush,
+ },
"RPOP": {
MinArgs: 2,
ExtractKeys: SingleKey,
diff --git a/pkg/commands/lpush.go b/pkg/commands/lpush.go
new file mode 100644
index 0000000..dcd67ee
--- /dev/null
+++ b/pkg/commands/lpush.go
@@ -0,0 +1,26 @@
+package commands
+
+import (
+ "errors"
+ "redisClone/pkg/core"
+)
+
+func Lpush(args []string, getShard func(k string) *core.Shard) interface{}{
+ key := args[1]
+ newItems := args[2:]
+ s := getShard(key)
+ var list []string
+ existingItem, exists := s.Data[key]
+ if exists {
+ var ok bool
+ list, ok = existingItem.Value.([]string)
+ if !ok {
+ return errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ }
+ } else {
+ list = []string{}
+ }
+ list = append(newItems, list...)
+ s.Data[key] = core.Item{Value: list}
+ return len(list)
+} \ No newline at end of file
diff --git a/pkg/commands/rpush.go b/pkg/commands/rpush.go
new file mode 100644
index 0000000..ea7e691
--- /dev/null
+++ b/pkg/commands/rpush.go
@@ -0,0 +1,26 @@
+package commands
+
+import (
+ "errors"
+ "redisClone/pkg/core"
+)
+
+func Rpush(args []string, getShard func(k string) *core.Shard) interface{}{
+ key := args[1]
+ newItems := args[2:]
+ s := getShard(key)
+ var list []string
+ existingItem, exists := s.Data[key]
+ if exists {
+ var ok bool
+ list, ok = existingItem.Value.([]string)
+ if !ok {
+ return errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ }
+ } else {
+ list = []string{}
+ }
+ list = append(list, newItems...)
+ s.Data[key] = core.Item{Value: list}
+ return len(list)
+} \ No newline at end of file