HDel deletes one field from a hash. It takes a string key 'k' and a field 'f' to be deleted from the hash. It returns a boolean indicating the success of the operation and any possible error. Parameters: k: The key of the hash table. f: The field to be deleted. Returns: bool: True if the fiel
(key string, field interface{})
| 306 | // bool: True if the field was deleted successfully, false otherwise. |
| 307 | // error: An error if occurred during the operation, or nil on success. |
| 308 | func (hs *HashStructure) HDel(key string, field interface{}) (bool, error) { |
| 309 | // Convert the parameters to bytes |
| 310 | k := stringToBytesWithKey(key) |
| 311 | |
| 312 | // Convert the parameters to bytes |
| 313 | f, err, _ := interfaceToBytes(field) |
| 314 | if err != nil { |
| 315 | return false, err |
| 316 | } |
| 317 | |
| 318 | // Check the parameters |
| 319 | if len(k) == 0 || len(f) == 0 { |
| 320 | return false, _const.ErrKeyIsEmpty |
| 321 | } |
| 322 | |
| 323 | // Find the hash metadata by the given key |
| 324 | hashMeta, err := hs.findHashMeta(key, Hash) |
| 325 | if err != nil { |
| 326 | return false, err |
| 327 | } |
| 328 | |
| 329 | // If the counter is 0, return false |
| 330 | if hashMeta.counter == 0 { |
| 331 | return false, nil |
| 332 | } |
| 333 | |
| 334 | // Create a new HashField |
| 335 | hf := &HashField{ |
| 336 | field: f, |
| 337 | key: k, |
| 338 | version: hashMeta.version, |
| 339 | } |
| 340 | |
| 341 | // Encode the HashField |
| 342 | hfBuf := hf.encodeHashField() |
| 343 | |
| 344 | // Get the field from the database |
| 345 | _, err = hs.db.Get(hfBuf) |
| 346 | if err != nil && err == _const.ErrKeyNotFound { |
| 347 | return false, nil |
| 348 | } |
| 349 | |
| 350 | // Create a new write batch |
| 351 | batch := hs.db.NewWriteBatch(config.DefaultWriteBatchOptions) |
| 352 | |
| 353 | // Delete the field from the database |
| 354 | _ = batch.Delete(hfBuf) |
| 355 | |
| 356 | // Decrease the counter |
| 357 | hashMeta.counter-- |
| 358 | |
| 359 | // Put the updated hash metadata to the database |
| 360 | _ = batch.Put(k, hashMeta.encodeHashMeta()) |
| 361 | |
| 362 | // Commit the write batch |
| 363 | err = batch.Commit() |
| 364 | if err != nil { |
| 365 | return false, err |
nothing calls this directly
no test coverage detected