Example #1
0
// ListForEach runs a function over a list in a scope.
func (b *BotDB) ListForEach(scope, listName string, fn func(int, string) error) error {
	return b.db.Batch(func(tx *bolt.Tx) error {
		lists, err := getListsBucket(tx, scope)
		if err != nil {
			return err
		}

		bucket := lists.Bucket([]byte(listName))
		if bucket == nil {
			return ErrListNotFound
		}

		return types.AsList(bucket).ForEach(fn)
	})
}
Example #2
0
// ListRemove removes the value at an index of a list in a scope.
func (b *BotDB) ListRemove(scope, listName string, i int) error {
	return b.db.Batch(func(tx *bolt.Tx) error {
		lists, err := getListsBucket(tx, scope)
		if err != nil {
			return err
		}

		bucket := lists.Bucket([]byte(listName))
		if bucket == nil {
			return ErrListNotFound
		}

		return types.AsList(bucket).Delete(i)
	})
}
Example #3
0
// ListAt get the value at the index of a list in a scope.
func (b *BotDB) ListAt(scope, listName string, i int) (string, error) {
	var value string
	err := b.db.View(func(tx *bolt.Tx) error {
		lists, err := getListsBucket(tx, scope)
		if err != nil {
			return err
		}

		bucket := lists.Bucket([]byte(listName))
		if bucket == nil {
			return ErrListNotFound
		}

		value, err = types.AsList(bucket).At(i)
		return err
	})
	return value, err
}
Example #4
0
// ListLength gets the length of a list in a scope.
func (b *BotDB) ListLength(scope, listName string) (int, error) {
	var length int
	err := b.db.View(func(tx *bolt.Tx) error {
		lists, err := getListsBucket(tx, scope)
		if err != nil {
			return err
		}

		bucket := lists.Bucket([]byte(listName))
		if bucket == nil {
			return ErrListNotFound
		}

		length, err = types.AsList(bucket).Length()
		return err
	})
	return length, err
}
Example #5
0
// ListAppend appends a value to a list in a scope, returning the value's index.
func (b *BotDB) ListAppend(scope, listName, value string) (int, error) {
	var index int
	err := b.db.Batch(func(tx *bolt.Tx) error {
		lists, err := getListsBucket(tx, scope)
		if err != nil {
			return err
		}

		bucket := lists.Bucket([]byte(listName))
		if bucket == nil {
			return ErrListNotFound
		}

		index, err = types.AsList(bucket).Append(value)
		return err
	})
	return index, err
}
Example #6
0
// ListRandom returns a random item of a list in a scope.
func (b *BotDB) ListRandom(scope, listName string) (int, string, error) {
	var index int
	var value string
	err := b.db.View(func(tx *bolt.Tx) error {
		lists, err := getListsBucket(tx, scope)
		if err != nil {
			return err
		}

		bucket := lists.Bucket([]byte(listName))
		if bucket == nil {
			return ErrListNotFound
		}

		index, value, err = types.AsList(bucket).Random()
		return err
	})
	return index, value, err
}
Example #7
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()
	})
}