mirror of
https://github.com/EchoVault/SugarDB.git
synced 2026-04-22 23:47:09 +08:00
Updated HSET commands handler to be more thread-safe
This commit is contained in:
@@ -6,3 +6,4 @@ dist/
|
||||
dump.rdb
|
||||
**/*/testdata
|
||||
echovault/aof
|
||||
aof
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user