cmd_hset.go
cmd_hset.go - Overview
This file implements the HSET
command, which sets field-value pairs in a string-string map (SSMap) stored at a given key in the DiceDB store.
Detailed Documentation
SSMap
Type
type SSMap map[string]string
- Purpose: Represents a string-string map.
cHSET
Variable
var cHSET = &CommandMeta{
Name: "HSET",
Syntax: "HSET key field value [field value ...]",
HelpShort: "HSET sets field value in the string-string map stored at key",
HelpLong: `
HSET sets the field and value for the key in the string-string map.
Returns "OK" if the field value was set or updated in the map. Returns (nil) if the
field value was not set or updated.
`,
Examples: `
localhost:7379> HSET k1 f1 v1
OK 1
localhost:7379> HSET k1 f1 v1 f2 v2 f3 v3
OK 2
localhost:7379> HGET k1 f1
OK v1
localhost:7379> HGET k2 f1
OK (nil)
`,
Eval: evalHSET,
Execute: executeHSET,
}
- Purpose: Defines the metadata for the
HSET
command, including its name, syntax, help messages, example usages, and the functions to evaluate and execute the command.
init
Function
func init() {
CommandRegistry.AddCommand(cHSET)
}
- Purpose: Registers the
HSET
command with theCommandRegistry
during package initialization.
(h SSMap) Get(k string) (string, bool)
Function
func (h SSMap) Get(k string) (string, bool) {
value, ok := h[k]
if !ok {
return "", false
}
return value, true
}
- Purpose: Retrieves the value associated with a given key from the
SSMap
. - Parameters:
k string
: The key to retrieve.
- Returns:
string
: The value associated with the key. Returns an empty string if the key is not found.bool
: Indicates whether the key was found in the map.
(h SSMap) Set(k, v string) (string, bool)
Function
func (h SSMap) Set(k, v string) (string, bool) {
value, ok := h[k]
if ok {
oldValue := value
h[k] = v
return oldValue, true
}
h[k] = v
return "", false
}
- Purpose: Sets the value for a given key in the
SSMap
. - Parameters:
k string
: The key to set.v string
: The value to associate with the key.
- Returns:
string
: The old value associated with the key, if it existed. Returns an empty string if the key was not previously present.bool
: Indicates whether the key was already present in the map.
evalHSET(c *Cmd, s *dstore.Store) (*CmdRes, error)
Function
func evalHSET(c *Cmd, s *dstore.Store) (*CmdRes, error) {
key := c.C.Args[0]
var m SSMap
var newFields int64
obj := s.Get(key)
if obj != nil {
if err := object.AssertType(obj.Type, object.ObjTypeSSMap); err != nil {
return cmdResNil, errors.ErrWrongTypeOperation
}
m = obj.Value.(SSMap)
} else {
m = make(SSMap)
}
// kvs is the list of key-value pairs to set in the SSMap
// key and value are alternating elements in the list
kvs := c.C.Args[1:]
if (len(kvs) & 1) == 1 {
return cmdResNil, errors.ErrWrongArgumentCount("HSET")
}
for i := 0; i < len(kvs); i += 2 {
k, v := kvs[i], kvs[i+1]
if _, ok := m[k]; !ok {
newFields++
}
m[k] = v
}
obj = s.NewObj(m, -1, object.ObjTypeSSMap)
s.Put(key, obj)
return cmdResInt(newFields), nil
}
- Purpose: Evaluates the
HSET
command, setting field-value pairs in theSSMap
stored at the given key. - Parameters:
c *Cmd
: The command to evaluate.s *dstore.Store
: The data store.
- Returns:
*CmdRes
: The result of the command, indicating the number of new fields added.error
: An error if one occurred, such as an incorrect argument count or type.
executeHSET(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error)
Function
func executeHSET(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) < 3 {
return cmdResNil, errors.ErrWrongArgumentCount("HSET")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalHSET(c, shard.Thread.Store())
}
- Purpose: Executes the
HSET
command by retrieving the appropriate shard and callingevalHSET
. - Parameters:
c *Cmd
: The command to execute.sm *shardmanager.ShardManager
: The shard manager.
- Returns:
*CmdRes
: The result of the command.error
: An error if one occurred, such as an incorrect argument count.
Code Examples
None.