HSet sets the string value of a hash field in the HashStructure. It takes the key, field, and value as input and stores the value in the specified hash field. Parameters: k: The key under which the hash is stored. f: The field within the hash where the value will be set. v: The value to be set
(key string, field, value interface{})
| 61 | // - The function uses a write batch to efficiently commit changes to the database. |
| 62 | // - It returns a boolean indicating whether the field was newly created or updated. |
| 63 | func (hs *HashStructure) HSet(key string, field, value interface{}) (bool, error) { |
| 64 | // Convert the parameters to bytes |
| 65 | k := stringToBytesWithKey(key) |
| 66 | |
| 67 | // Convert the parameters to bytes |
| 68 | f, err, _ := interfaceToBytes(field) |
| 69 | if err != nil { |
| 70 | return false, err |
| 71 | } |
| 72 | |
| 73 | // Convert the parameters to bytes |
| 74 | v, err, valueType := interfaceToBytes(value) |
| 75 | |
| 76 | if err != nil { |
| 77 | return false, err |
| 78 | } |
| 79 | |
| 80 | // Set the hash value type |
| 81 | hs.hashValueType = valueType |
| 82 | |
| 83 | // Set the hash field type |
| 84 | _, _, fieldType := interfaceToBytes(v) |
| 85 | hs.HashFieldType = fieldType |
| 86 | |
| 87 | // Check the parameters |
| 88 | if len(k) == 0 || len(f) == 0 || len(v) == 0 { |
| 89 | return false, _const.ErrKeyIsEmpty |
| 90 | } |
| 91 | |
| 92 | // Find the hash metadata by the given key |
| 93 | hashMeta, err := hs.findHashMeta(key, Hash) |
| 94 | if err != nil { |
| 95 | return false, err |
| 96 | } |
| 97 | |
| 98 | // Create a new HashField |
| 99 | hf := &HashField{ |
| 100 | key: k, |
| 101 | field: f, |
| 102 | version: hashMeta.version, |
| 103 | } |
| 104 | |
| 105 | // Encode the HashField |
| 106 | hfBuf := hf.encodeHashField() |
| 107 | |
| 108 | var exist = true |
| 109 | |
| 110 | // Get the field from the database |
| 111 | _, err = hs.db.Get(hfBuf) |
| 112 | if err != nil && err == _const.ErrKeyNotFound { |
| 113 | exist = false |
| 114 | } |
| 115 | |
| 116 | // new a write batch |
| 117 | batch := hs.db.NewWriteBatch(config.DefaultWriteBatchOptions) |
| 118 | |
| 119 | // If the field is not found, increase the counter |
| 120 | if !exist { |
no test coverage detected