aboutsummaryrefslogtreecommitdiff
path: root/pkg/commands/hset.go
blob: 33f992f8b4b4b6ee94ece8d9a8418bb979a34c1e (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
package commands

import (
	"errors"
	"redisClone/pkg/core"
)

func Hset(args []string, getShard func(k string) *core.Shard) interface{} {
	if (len(args)-2)%2 != 0 {
		return errors.New("wrong number of arguments for 'HSET'")
	}
	key := args[1]
	s := getShard(key)
	item, exists := s.Data[key]
	var hash map[string]string
	if exists {
		var ok bool
		hash, ok = item.Value.(map[string]string)
		if !ok {
			return errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
		}
	} else {
		hash = make(map[string]string)
	}
	count := 0
	for i := 2; i < len(args); i += 2 {
		hash[args[i]] = args[i+1]
		count++
	}
	s.Data[key] = core.Item{Value: hash}
	return count
}