| 559 | } |
| 560 | |
| 561 | func (tx *Tx) integerIncr(bucket string, key []byte, increment int64) error { |
| 562 | return tx.update(bucket, key, func(value []byte) ([]byte, error) { |
| 563 | intValue, err := strconv2.StrToInt64(string(value)) |
| 564 | |
| 565 | if err != nil && errors.Is(err, strconv.ErrRange) { |
| 566 | return []byte(bigIntIncr(string(value), strconv2.Int64ToStr(increment))), nil |
| 567 | } |
| 568 | |
| 569 | if err != nil { |
| 570 | return nil, ErrValueNotInteger |
| 571 | } |
| 572 | |
| 573 | if (increment > 0 && math.MaxInt64-increment < intValue) || (increment < 0 && math.MinInt64-increment > intValue) { |
| 574 | return []byte(bigIntIncr(string(value), strconv2.Int64ToStr(increment))), nil |
| 575 | } |
| 576 | |
| 577 | atomic.AddInt64(&intValue, increment) |
| 578 | return []byte(strconv2.Int64ToStr(intValue)), nil |
| 579 | }, func(oldTTL uint32) (uint32, error) { |
| 580 | return oldTTL, nil |
| 581 | }) |
| 582 | } |
| 583 | |
| 584 | func (tx *Tx) Incr(bucket string, key []byte) error { |
| 585 | return tx.integerIncr(bucket, key, 1) |