Esempio n. 1
0
// CounterSet sets a counter in a scope.
func (b *BotDB) CounterSet(scope, counterName string, value int64) error {
	return b.db.Batch(func(tx *bolt.Tx) error {
		counters, err := getCountersBucket(tx, scope)
		if err != nil {
			return err
		}

		bucket := counters.Bucket([]byte(counterName))
		if bucket == nil {
			return ErrCounterNotFound
		}

		return types.AsCounter(bucket).Set(value)
	})
}
Esempio n. 2
0
// CounterGet gets a counter's value from a scope.
func (b *BotDB) CounterGet(scope, counterName string) (int64, error) {
	var value int64
	err := b.db.View(func(tx *bolt.Tx) error {
		counters, err := getCountersBucket(tx, scope)
		if err != nil {
			return err
		}

		bucket := counters.Bucket([]byte(counterName))
		if bucket == nil {
			return ErrCounterNotFound
		}

		value, err = types.AsCounter(bucket).Get()
		return err
	})
	return value, err
}
Esempio n. 3
0
// CounterCreate creates a new counter in a scope.
func (b *BotDB) CounterCreate(scope, counterName string) error {
	return b.db.Batch(func(tx *bolt.Tx) error {
		counters, err := getCountersBucket(tx, scope)
		if err != nil {
			return err
		}

		counterNameBytes := []byte(counterName)
		if bucket := counters.Bucket(counterNameBytes); bucket != nil {
			return ErrCounterExists
		}

		bucket, err := helpers.CreateBucketWrap(counters, counterNameBytes)
		if err != nil {
			return err
		}

		return types.AsCounter(bucket).Init()
	})
}