MCPcopy Create free account
hub / github.com/ByteStorage/FlyDB / HIncrBy

Method HIncrBy

structure/hash.go:649–716  ·  view source on GitHub ↗

HIncrBy increments the integer value of a hash field by the given number. It takes a string key 'k', a field 'f', and an increment value 'increment'. It returns the updated value after increment and any possible error. Parameters: k: The key of the hash table. f: The field whose value needs to b

(key string, field interface{}, increment int64)

Source from the content-addressed store, hash-verified

647// int64: The updated value of the field after increment.
648// error: An error if occurred during the operation, or nil on success.
649func (hs *HashStructure) HIncrBy(key string, field interface{}, increment int64) (int64, error) {
650 // Convert the parameters to bytes
651 k := stringToBytesWithKey(key)
652
653 // Convert the parameters to bytes
654 f, err, _ := interfaceToBytes(field)
655 if err != nil {
656 return 0, err
657 }
658
659 // Check the parameters
660 if len(k) == 0 || len(f) == 0 {
661 return 0, _const.ErrKeyIsEmpty
662 }
663
664 // Find the hash metadata by the given key
665 hashMeta, err := hs.findHashMeta(key, Hash)
666 if err != nil {
667 return 0, err
668 }
669
670 // If the counter is 0, return 0
671 if hashMeta.counter == 0 {
672 return 0, nil
673 }
674
675 // Create a new HashField
676 hf := &HashField{
677 field: f,
678 key: k,
679 version: hashMeta.version,
680 }
681
682 // Encode the HashField
683 hfBuf := hf.encodeHashField()
684
685 // Get the field from the database
686 value, err := hs.db.Get(hfBuf)
687 if err != nil && err == _const.ErrKeyNotFound {
688 return 0, nil
689 }
690
691 // Convert the value to int64
692 val, err := strconv.ParseInt(string(value), 10, 64)
693 if err != nil {
694 return 0, err
695 }
696
697 // Add the increment to the value
698 val += increment
699
700 // Convert the value to string
701 value = []byte(strconv.FormatInt(val, 10))
702
703 // Create a new write batch
704 batch := hs.db.NewWriteBatch(config.DefaultWriteBatchOptions)
705
706 // Put the field to the database

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