diff options
| author | alex <[email protected]> | 2026-07-11 13:22:16 +0200 |
|---|---|---|
| committer | alex <[email protected]> | 2026-07-11 13:22:16 +0200 |
| commit | 90778a710319efc44754d4b1a4e7e18ddad6661a (patch) | |
| tree | dd50aa27a4da706aaef33504193ad62b21049489 /pkg/commands/lrange.go | |
| parent | 56bf3fc1b36e7301727e9d8d94a6239a88c4e208 (diff) | |
| download | redis-clone-90778a710319efc44754d4b1a4e7e18ddad6661a.tar.xz redis-clone-90778a710319efc44754d4b1a4e7e18ddad6661a.zip | |
refactored LRANGE
Diffstat (limited to 'pkg/commands/lrange.go')
| -rw-r--r-- | pkg/commands/lrange.go | 52 |
1 files changed, 52 insertions, 0 deletions
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 |
