HIncrByFloat increments the float value of a hash field by the given number. It takes a string key 'k', a field 'f', and an increment value 'increment'. It returns the updated value after increment and any possible error. Parameters: k: The key of the hash table. f: The field whose value needs t
(key string, field interface{}, increment float64)
| 730 | // float64: The updated value of the field after increment. |
| 731 | // error: An error if occurred during the operation, or nil on success. |
| 732 | func (hs *HashStructure) HIncrByFloat(key string, field interface{}, increment float64) (float64, error) { |
| 733 | // Convert the parameters to bytes |
| 734 | k := stringToBytesWithKey(key) |
| 735 | |
| 736 | // Convert the parameters to bytes |
| 737 | f, err, _ := interfaceToBytes(field) |
| 738 | if err != nil { |
| 739 | return 0, err |
| 740 | } |
| 741 | |
| 742 | // Check the parameters |
| 743 | if len(k) == 0 || len(f) == 0 { |
| 744 | return 0, _const.ErrKeyIsEmpty |
| 745 | } |
| 746 | |
| 747 | // Find the hash metadata by the given key |
| 748 | hashMeta, err := hs.findHashMeta(key, Hash) |
| 749 | if err != nil { |
| 750 | return 0, err |
| 751 | } |
| 752 | |
| 753 | // If the counter is 0, return 0 |
| 754 | if hashMeta.counter == 0 { |
| 755 | return 0, nil |
| 756 | } |
| 757 | |
| 758 | // Create a new HashField |
| 759 | hf := &HashField{ |
| 760 | field: f, |
| 761 | key: k, |
| 762 | version: hashMeta.version, |
| 763 | } |
| 764 | |
| 765 | // Encode the HashField |
| 766 | hfBuf := hf.encodeHashField() |
| 767 | |
| 768 | // Get the field from the database |
| 769 | value, err := hs.db.Get(hfBuf) |
| 770 | if err != nil && err == _const.ErrKeyNotFound { |
| 771 | return 0, nil |
| 772 | } |
| 773 | |
| 774 | // Convert the value to float64 |
| 775 | val, err := strconv.ParseFloat(string(value), 64) |
| 776 | if err != nil { |
| 777 | return 0, err |
| 778 | } |
| 779 | |
| 780 | // Add the increment to the value |
| 781 | val += increment |
| 782 | |
| 783 | // Convert the value to string |
| 784 | value = []byte(strconv.FormatFloat(val, 'f', -1, 64)) |
| 785 | |
| 786 | // Create a new write batch |
| 787 | batch := hs.db.NewWriteBatch(config.DefaultWriteBatchOptions) |
| 788 | |
| 789 | // Put the field to the database |
nothing calls this directly
no test coverage detected