aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/commands/command-registry.go5
-rw-r--r--pkg/commands/lrange.go52
2 files changed, 57 insertions, 0 deletions
diff --git a/pkg/commands/command-registry.go b/pkg/commands/command-registry.go
index 6e34ff7..baf7133 100644
--- a/pkg/commands/command-registry.go
+++ b/pkg/commands/command-registry.go
@@ -95,4 +95,9 @@ var Registry = map[string]CommandDef{
ExtractKeys: SingleKey,
Execute: Lpop,
},
+ "LRANGE": {
+ MinArgs: 4,
+ ExtractKeys: SingleKey,
+ Execute: Lrange,
+ },
} \ No newline at end of file
diff --git a/pkg/commands/lrange.go b/pkg/commands/lrange.go
new file mode 100644
index 0000000..d2b6ff3
--- /dev/null
+++ b/pkg/commands/lrange.go
@@ -0,0 +1,52 @@
+package commands
+
+import (
+ "errors"
+ "redisClone/pkg/core"
+ "strconv"
+ "time"
+)
+
+func Lrange(args []string, getShard func(k string) *core.Shard) interface{}{
+ start, err1 := strconv.Atoi(args[2])
+ stop, err2 := strconv.Atoi(args[3])
+ if err1 != nil || err2 != nil {
+ err := errors.New("value is not an integer or out of range")
+ return err
+ }
+ key := args[1]
+ s := getShard(key)
+
+ item, exists := s.Data[key]
+ if !exists {
+ return []string{}
+ }
+ if item.ExpiresAt != nil && time.Now().After(*item.ExpiresAt) {
+ delete(s.Data, key)
+ return nil
+ }
+ typedList, ok := item.Value.([]string)
+ if !ok {
+ return errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
+ }
+ size := len(typedList)
+ if start < 0 {
+ start += size
+ if start < 0 {
+ start = 0
+ }
+ }
+ if stop < 0 {
+ stop += size
+ if stop < 0 {
+ stop = 0
+ }
+ }
+ if stop >= size {
+ stop = size - 1
+ }
+ if start > stop || start >= size {
+ return []string{}
+ }
+ return typedList[start : stop+1]
+} \ No newline at end of file