Updated HSET commands handler to be more thread-safe

This commit is contained in:
Kelvin Clement Mwinuka
2024-07-11 11:21:06 +08:00
parent d3252e8c04
commit 718b7f270f
3 changed files with 24 additions and 9 deletions
+1
View File
@@ -6,3 +6,4 @@ dist/
dump.rdb
**/*/testdata
echovault/aof
aof
+1 -1
View File
@@ -14,7 +14,7 @@
package constants
const Version = "0.10.0" // Next EchoVault version. Update this before each release.
const Version = "0.10.1" // Next EchoVault version. Update this before each release.
const (
ACLModule = "acl"
+22 -8
View File
@@ -55,22 +55,36 @@ func handleHSET(params internal.HandlerFuncParams) ([]byte, error) {
hash, ok := params.GetValues(params.Context, []string{key})[key].(map[string]interface{})
if !ok {
hash = make(map[string]interface{})
// Not hash, save the entries map directly.
if err = params.SetValues(params.Context, map[string]interface{}{key: entries}); err != nil {
return nil, err
}
return []byte(fmt.Sprintf(":%d\r\n", len(entries))), nil
}
count := 0
for field, value := range entries {
if strings.EqualFold(params.Command[0], "hsetnx") {
switch strings.ToLower(params.Command[0]) {
case "hsetnx":
// Handle HSETNX
for field, _ := range entries {
if hash[field] == nil {
hash[field] = value
count += 1
}
continue
}
hash[field] = value
count += 1
for field, value := range hash {
entries[field] = value
}
default:
// Handle HSET
for field, value := range hash {
if entries[field] == nil {
entries[field] = value
}
}
count = len(entries)
}
if err = params.SetValues(params.Context, map[string]interface{}{key: hash}); err != nil {
if err = params.SetValues(params.Context, map[string]interface{}{key: entries}); err != nil {
return nil, err
}