Skip to content

Commit

Permalink
feat: new method--HIncrByFloat(#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
qishenonly committed Jul 5, 2023
1 parent 892311d commit 45cd930
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
61 changes: 61 additions & 0 deletions structure/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,64 @@ func (hs *HashStructure) HIncrBy(key, field []byte, increment int64) (int64, err

return val, nil
}

// HIncrByFloat increments the float value of a hash field by the given number.
func (hs *HashStructure) HIncrByFloat(key, field []byte, increment float64) (float64, error) {
// Check the parameters
if len(key) == 0 || len(field) == 0 {
return 0, _const.ErrKeyIsEmpty
}

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

// If the counter is 0, return 0
if hashMeta.counter == 0 {
return 0, 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 && err == _const.ErrKeyNotFound {
return 0, nil
}

// Convert the value to float64
val, err := strconv.ParseFloat(string(value), 64)
if err != nil {
return 0, err
}

// Add the increment to the value
val += increment

// Convert the value to string
value = []byte(strconv.FormatFloat(val, 'f', -1, 64))

// new a write batch
batch := hs.db.NewWriteBatch(config.DefaultWriteBatchOptions)

// Put the field to the database
_ = batch.Put(hfBuf, value)

// Commit the write batch
err = batch.Commit()
if err != nil {
return 0, err
}

return val, nil
}
33 changes: 33 additions & 0 deletions structure/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,36 @@ func TestHashStructure_HIncrBy(t *testing.T) {
assert.Equal(t, v4, int64(0))

}

func TestHashStructure_HIncrByFloat(t *testing.T) {
hash := initHashDB()

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

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

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

v1, err := hash.HIncrByFloat(randkv.GetTestKey(1), []byte("field1"), 1.1)
assert.Nil(t, err)
assert.Equal(t, v1, float64(11.1))

v2, err := hash.HIncrByFloat(randkv.GetTestKey(1), []byte("field2"), -1.1)
assert.Nil(t, err)
assert.Equal(t, v2, float64(8.9))

v3, err := hash.HIncrByFloat(randkv.GetTestKey(1), []byte("field3"), 0)
assert.Nil(t, err)
assert.Equal(t, v3, float64(10))

v4, err := hash.HIncrByFloat(randkv.GetTestKey(1), []byte("field4"), 1.1)
assert.Nil(t, err)
assert.Equal(t, v4, float64(0))

}

0 comments on commit 45cd930

Please sign in to comment.