HExists determines whether a hash field exists or not. It takes a string key 'k' and a field 'f' to check for existence. It returns a boolean indicating whether the field exists and any possible error. Parameters: k: The key of the hash table. f: The field to check for existence. Returns: boo
(key string, field interface{})
| 478 | // bool: True if the field exists, false otherwise. |
| 479 | // error: An error if occurred during the operation, or nil on success. |
| 480 | func (hs *HashStructure) HExists(key string, field interface{}) (bool, error) { |
| 481 | // Convert the parameters to bytes |
| 482 | k := stringToBytesWithKey(key) |
| 483 | |
| 484 | // Convert the parameters to bytes |
| 485 | f, err, _ := interfaceToBytes(field) |
| 486 | if err != nil { |
| 487 | return false, err |
| 488 | } |
| 489 | |
| 490 | // Check the parameters |
| 491 | if len(k) == 0 || len(f) == 0 { |
| 492 | return false, _const.ErrKeyIsEmpty |
| 493 | } |
| 494 | |
| 495 | // Find the hash metadata by the given key |
| 496 | hashMeta, err := hs.findHashMeta(key, Hash) |
| 497 | if err != nil { |
| 498 | return false, err |
| 499 | } |
| 500 | |
| 501 | // If the counter is 0, return false |
| 502 | if hashMeta.counter == 0 { |
| 503 | return false, nil |
| 504 | } |
| 505 | |
| 506 | // Create a new HashField |
| 507 | hf := &HashField{ |
| 508 | field: f, |
| 509 | key: k, |
| 510 | version: hashMeta.version, |
| 511 | } |
| 512 | |
| 513 | // Encode the HashField |
| 514 | hfBuf := hf.encodeHashField() |
| 515 | |
| 516 | // Get the field from the database |
| 517 | _, err = hs.db.Get(hfBuf) |
| 518 | if err != nil && err == _const.ErrKeyNotFound { |
| 519 | return false, nil |
| 520 | } |
| 521 | |
| 522 | return true, nil |
| 523 | } |
| 524 | |
| 525 | // HLen gets the number of fields contained in a hash. |
| 526 | // It takes a string key 'k' and returns the number of fields in the hash. |
nothing calls this directly
no test coverage detected