aboutsummaryrefslogtreecommitdiff
path: root/pkg/commands/lrange.go
blob: d2b6ff3376d30a1e714ef2f0d87f6136a18e9fa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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]
}