Ejemplo n.º 1
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()
	})
}
Ejemplo n.º 2
0
// MapCreate creates a map in a scope.
func (b *BotDB) MapCreate(scope, mapName string) error {
	return b.db.Batch(func(tx *bolt.Tx) error {
		maps, err := getMapsBucket(tx, scope)
		if err != nil {
			return err
		}

		mapNameBytes := []byte(mapName)
		if bucket := maps.Bucket(mapNameBytes); bucket != nil {
			return ErrMapExists
		}

		_, err = helpers.CreateBucketWrap(maps, mapNameBytes)
		if err != nil {
			return err
		}

		return nil
	})
}
Ejemplo n.º 3
0
// ListCreate creates a list in a scope.
func (b *BotDB) ListCreate(scope, listName string) error {
	return b.db.Batch(func(tx *bolt.Tx) error {
		lists, err := getListsBucket(tx, scope)
		if err != nil {
			return err
		}

		listNameBytes := []byte(listName)
		if bucket := lists.Bucket(listNameBytes); bucket != nil {
			return ErrListExists
		}

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

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