Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Hash method HMGet (#136) #184

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions structure/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,65 @@ func (hs *HashStructure) HGet(k string, f interface{}) (interface{}, error) {
return valueToInterface, nil
}

// HMGet gets the string value of a hash field.
func (hs *HashStructure) HMGet(k string, f ...interface{}) ([]interface{}, error) {
// Convert the parameters to bytes
key := stringToBytesWithKey(k)
var interfaces []interface{}

for _, fi := range f {
// Convert the parameters to bytes
field, err, _ := interfaceToBytes(fi)
if err != nil {
return nil, err
}

// Check the parameters
if len(key) == 0 || len(field) == 0 {
return nil, _const.ErrKeyIsEmpty
}

// Find the hash metadata by the given key
hashMeta, err := hs.findHashMeta(k, Hash)
if err != nil {
return nil, err
}

// If the counter is 0, return nil
if hashMeta.counter == 0 {
return nil, nil
}

// Create a new HashField
hf := &HashField{
field: field,
key: key,
version: hashMeta.version,
}

// Encode the HashField
hfBuf := hf.encodeHashField()

// Get the field from the database
value, err := hs.db.Get(hfBuf)
if err != nil {
return nil, err
}

// Get the value type from the hashValueTypes
valueType := hs.hashValueType

// Values of different types need to be converted to corresponding types
valueToInterface, err := byteToInterface(value, valueType)
if err != nil {
return nil, err
}
interfaces = append(interfaces, valueToInterface)
}

return interfaces, nil
}

// HDel deletes one field from a hash.
func (hs *HashStructure) HDel(k string, f interface{}) (bool, error) {
// Convert the parameters to bytes
Expand Down
27 changes: 27 additions & 0 deletions structure/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ func TestHashStructure_HGet(t *testing.T) {

}

func TestHashStructure_HMGet(t *testing.T) {
hash := initHashDB()
defer hash.db.Close()

ok1, err := hash.HSet("1", []byte("field1"), randkv.RandomValue(10))
assert.Nil(t, err)
assert.True(t, ok1)

v1 := randkv.RandomValue(10)
ok2, err := hash.HSet("1", []byte("field1"), v1)
assert.Nil(t, err)
assert.False(t, ok2)

v2 := randkv.RandomValue(10)
ok3, err := hash.HSet("1", []byte("field2"), v2)
assert.Nil(t, err)
assert.True(t, ok3)

mulVal, err := hash.HMGet("1", []byte("field1"), []byte("field2"))
assert.Equal(t, v1, mulVal[0])
assert.Equal(t, v2, mulVal[1])

_, err = hash.HGet("1", []byte("field3"))
assert.Equal(t, err, _const.ErrKeyNotFound)

}

func TestHashStructure_HDel(t *testing.T) {
hash := initHashDB()
defer hash.db.Close()
Expand Down