HDecrBy decrements the integer value of a hash field by the given number. It takes a string key 'k', a field 'f', and a decrement value 'decrement'. It returns the updated value after decrement and any possible error. Parameters: k: The key of the hash table. f: The field whose value needs to be
(key string, field interface{}, decrement int64)
| 813 | // int64: The updated value of the field after decrement. |
| 814 | // error: An error if occurred during the operation, or nil on success. |
| 815 | func (hs *HashStructure) HDecrBy(key string, field interface{}, decrement int64) (int64, error) { |
| 816 | // Convert the parameters to bytes |
| 817 | k := stringToBytesWithKey(key) |
| 818 | |
| 819 | // Convert the parameters to bytes |
| 820 | f, err, _ := interfaceToBytes(field) |
| 821 | if err != nil { |
| 822 | return 0, err |
| 823 | } |
| 824 | |
| 825 | // Check the parameters |
| 826 | if len(k) == 0 || len(f) == 0 { |
| 827 | return 0, _const.ErrKeyIsEmpty |
| 828 | } |
| 829 | |
| 830 | // Find the hash metadata by the given key |
| 831 | hashMeta, err := hs.findHashMeta(key, Hash) |
| 832 | if err != nil { |
| 833 | return 0, err |
| 834 | } |
| 835 | |
| 836 | // If the counter is 0, return 0 |
| 837 | if hashMeta.counter == 0 { |
| 838 | return 0, nil |
| 839 | } |
| 840 | |
| 841 | // Create a new HashField |
| 842 | hf := &HashField{ |
| 843 | field: f, |
| 844 | key: k, |
| 845 | version: hashMeta.version, |
| 846 | } |
| 847 | |
| 848 | // Encode the HashField |
| 849 | hfBuf := hf.encodeHashField() |
| 850 | |
| 851 | // Get the field from the database |
| 852 | value, err := hs.db.Get(hfBuf) |
| 853 | if err != nil && err == _const.ErrKeyNotFound { |
| 854 | return 0, nil |
| 855 | } |
| 856 | |
| 857 | // Convert the value to int64 |
| 858 | val, err := strconv.ParseInt(string(value), 10, 64) |
| 859 | if err != nil { |
| 860 | return 0, err |
| 861 | } |
| 862 | |
| 863 | // Subtract the decrement from the value |
| 864 | val -= decrement |
| 865 | |
| 866 | // Convert the value to string |
| 867 | value = []byte(strconv.FormatInt(val, 10)) |
| 868 | |
| 869 | // Create a new write batch |
| 870 | batch := hs.db.NewWriteBatch(config.DefaultWriteBatchOptions) |
| 871 | |
| 872 | // Put the field to the database |
nothing calls this directly
no test coverage detected