Skip to content

Commit

Permalink
Merge pull request ByteStorage#184 from saeid-a/feat/hash-HMGet
Browse files Browse the repository at this point in the history
Implement Hash method HMGet (ByteStorage#136)
  • Loading branch information
sjcsjc123 authored Jul 10, 2023
2 parents 84911f8 + 2cf669b commit 3e1c45d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
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

0 comments on commit 3e1c45d

Please sign in to comment.