Beispiel #1
0
// Append appends an item to the list, and returns its index.
func (l List) Append(value string) (int, error) {
	indexBytes := l.b.Get(lengthKey)
	index, err := strconv.Atoi(string(indexBytes))
	if err != nil {
		return index, err
	}

	if err := helpers.PutWrap(l.b, lengthKey, []byte(strconv.Itoa(index+1))); err != nil {
		return index, err
	}

	return index, helpers.PutWrap(l.b, indexBytes, []byte(value))
}
Beispiel #2
0
// PrefixSet sets the prefix for a scope.
func (b *BotDB) PrefixSet(scope, prefix string) error {
	if len(prefix) != 1 {
		return errors.New("invalid prefix length")
	}

	return b.db.Batch(func(tx *bolt.Tx) error {
		bucket := tx.Bucket([]byte(scope))
		if bucket == nil {
			return ErrBucketNotFound{scope}
		}

		return helpers.PutWrap(bucket, constants.PrefixNameBytes, []byte(prefix))
	})
}
Beispiel #3
0
// InitializeBucket initializes a bucket as a scope.
func InitializeBucket(b *bolt.Bucket) error {
	for i := 0; i < len(defaultKV)-1; i += 2 {
		k, v := defaultKV[i], defaultKV[i+1]
		if currentValue := b.Get(k); currentValue == nil {
			if err := helpers.PutWrap(b, k, v); err != nil {
				return err
			}
		}
	}

	for _, name := range defaultBuckets {
		if _, err := helpers.CreateBucketIfNotExistsWrap(b, []byte(name)); err != nil {
			return err
		}
	}

	return nil
}
Beispiel #4
0
// Delete removes an item from the list. If the item is the last item in the list,
// the length will decrease.
func (l List) Delete(i int) error {
	if i < 0 {
		return ErrInvalidIndex
	}
	length, err := strconv.Atoi(string(l.b.Get(lengthKey)))
	if err != nil {
		return err
	}
	if i >= length {
		return ErrInvalidIndex
	}

	if i == length-1 {
		newLength := strconv.Itoa(i)
		if err := helpers.PutWrap(l.b, lengthKey, []byte(newLength)); err != nil {
			return err
		}
	}
	if err := helpers.DeleteWrap(l.b, []byte(strconv.Itoa(i))); err != nil {
		return err
	}

	return nil
}
Beispiel #5
0
// Set sets the value of a counter.
func (c Counter) Set(x int64) error {
	return helpers.PutWrap(c.b, valueKey, []byte(strconv.FormatInt(x, 10)))
}
Beispiel #6
0
// Init initializes a counter.
func (c Counter) Init() error {
	return helpers.PutWrap(c.b, valueKey, []byte("0"))
}
Beispiel #7
0
// Set sets the item at index i to the specified value.
func (l List) Set(i int, value string) error {
	return helpers.PutWrap(l.b, []byte(strconv.Itoa(i)), []byte(value))
}
Beispiel #8
0
// Init initializes a list.
func (l List) Init() error {
	return helpers.PutWrap(l.b, lengthKey, []byte("0"))
}
Beispiel #9
0
// Put puts into the map.
func (m Map) Put(key, value string) error {
	return helpers.PutWrap(m.b, []byte(key), []byte(value))
}