示例#1
1
func (store *BoltStore) Del(uri string) error {
	namespace, key := util.NamespaceKey(uri)
	bucketkeys := util.PathFolders(namespace)
	var bucket *bolt.Bucket = nil
	var err error
	e := store.db.Update(func(tx *bolt.Tx) error {
		for _, bucketkey := range bucketkeys {
			if bucket == nil {
				bucket, err = tx.CreateBucketIfNotExists([]byte(bucketkey))
				if err != nil {
					return err
				}
			} else {
				bucket, err = bucket.CreateBucketIfNotExists([]byte(bucketkey))
				if err != nil {
					return err
				}
			}
		}
		bucket.DeleteBucket([]byte(key))
		bucket.Delete([]byte(key))
		return nil
	})
	return e
}
示例#2
0
func (store *BoltStore) Get(uri string, nested bool) (interface{}, error) {
	namespace, key := util.NamespaceKey(uri)
	bucketkeys := util.PathFolders(namespace)
	var bucket *bolt.Bucket = nil
	var result interface{}
	e := store.db.View(func(tx *bolt.Tx) error {
		for _, bucketkey := range bucketkeys {
			if bucket == nil {
				bucket = tx.Bucket([]byte(bucketkey))
				if bucket == nil {
					return errors.New(uri + " not exists")
				}
			} else {
				bucket = bucket.Bucket([]byte(bucketkey))
				if bucket == nil {
					return errors.New(uri + " not exists")
				}
			}
		}
		if bucket == nil {
			return errors.New(uri + " not exists")
		}

		val, err := store.get(bucket, key, nested)
		if err != nil {
			return err
		}
		result = val
		return nil
	})
	return result, e
}
示例#3
0
func (store *BoltStore) Size(uri string) (int64, error) {
	namespace, key := util.NamespaceKey(uri)
	bucketkeys := util.PathFolders(namespace)
	var bucket *bolt.Bucket = nil
	var result int64 = 0
	e := store.db.View(func(tx *bolt.Tx) error {
		for _, bucketkey := range bucketkeys {
			if bucket == nil {
				bucket = tx.Bucket([]byte(bucketkey))
				if bucket == nil {
					return errors.New(uri + " not exists")
				}
			} else {
				bucket = bucket.Bucket([]byte(bucketkey))
				if bucket == nil {
					return errors.New(uri + " not exists")
				}
			}
		}
		if bucket == nil {
			return errors.New(uri + " not exists")
		}

		result = store.size(bucket, key, nil)
		return nil
	})
	return result, e
}
示例#4
0
func (store *BoltStore) Put(uri string, value interface{}) error {
	namespace, key := util.NamespaceKey(uri)
	bucketkeys := util.PathFolders(namespace)
	var bucket *bolt.Bucket = nil
	var err error
	e := store.db.Update(func(tx *bolt.Tx) error {
		for _, bucketkey := range bucketkeys {
			if bucket == nil {
				bucket, err = tx.CreateBucketIfNotExists([]byte(bucketkey))
				if err != nil {
					return err
				}
			} else {
				bucket, err = bucket.CreateBucketIfNotExists([]byte(bucketkey))
				if err != nil {
					return err
				}
			}
		}
		return store.put(bucket, key, value)
	})
	return e
}