MCPcopy Index your code
hub / github.com/ByteStorage/FlyDB / HUpdate

Method HUpdate

structure/hash.go:573–633  ·  view source on GitHub ↗

HUpdate updates the string value of a hash field. It takes a string key 'k', a field 'f', and a value 'v' to update the field's value. It returns a boolean indicating the success of the update and any possible error. Parameters: k: The key of the hash table. f: The field to be updated. v: The n

(key string, field, value interface{})

Source from the content-addressed store, hash-verified

571// bool: True if the update was successful, false otherwise.
572// error: An error if occurred during the operation, or nil on success.
573func (hs *HashStructure) HUpdate(key string, field, value interface{}) (bool, error) {
574 // Convert the parameters to bytes
575 k := stringToBytesWithKey(key)
576
577 // Convert the parameters to bytes
578 f, err, _ := interfaceToBytes(field)
579 if err != nil {
580 return false, err
581 }
582
583 // Convert the parameters to bytes
584 v, err, _ := interfaceToBytes(value)
585 if err != nil {
586 return false, err
587 }
588 // Check the parameters
589 if len(k) == 0 || len(f) == 0 || len(v) == 0 {
590 return false, _const.ErrKeyIsEmpty
591 }
592
593 // Find the hash metadata by the given key
594 hashMeta, err := hs.findHashMeta(key, Hash)
595 if err != nil {
596 return false, err
597 }
598
599 // If the counter is 0, return false
600 if hashMeta.counter == 0 {
601 return false, nil
602 }
603
604 // Create a new HashField
605 hf := &HashField{
606 field: f,
607 key: k,
608 version: hashMeta.version,
609 }
610
611 // Encode the HashField
612 hfBuf := hf.encodeHashField()
613
614 // Get the field from the database
615 _, err = hs.db.Get(hfBuf)
616 if err != nil && err == _const.ErrKeyNotFound {
617 return false, nil
618 }
619
620 // Create a new write batch
621 batch := hs.db.NewWriteBatch(config.DefaultWriteBatchOptions)
622
623 // Put the field to the database
624 _ = batch.Put(hfBuf, v)
625
626 // Commit the write batch
627 err = batch.Commit()
628 if err != nil {
629 return false, err
630 }

Callers

nothing calls this directly

Calls 8

findHashMetaMethod · 0.95
encodeHashFieldMethod · 0.95
PutMethod · 0.95
CommitMethod · 0.95
stringToBytesWithKeyFunction · 0.85
interfaceToBytesFunction · 0.85
NewWriteBatchMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected