Exemple #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
}
Exemple #2
0
func (self *TX) dig(keys [][]byte, create bool) (buckets []*bolt.Bucket, err error) {
	var bucket *bolt.Bucket
	if create {
		if bucket, err = self.tx.CreateBucketIfNotExists(keys[0]); err != nil {
			return
		}
	} else {
		if bucket = self.tx.Bucket(keys[0]); bucket == nil {
			err = ErrNotFound
			return
		}
	}
	buckets = append(buckets, bucket)
	for keys = keys[1:]; len(keys) > 0; keys = keys[1:] {
		if create {
			if bucket, err = bucket.CreateBucketIfNotExists(keys[0]); err != nil {
				return
			}
		} else {
			if bucket = bucket.Bucket(keys[0]); bucket == nil {
				err = ErrNotFound
				return
			}
		}
		buckets = append(buckets, bucket)
	}
	return
}
// Init initializes this component. It requires an amount and an interval in
// seconds to be present.
func (RateLimitFilter) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
	amount, ok := params["amount"]
	if !ok {
		return errors.New("amount is required")
	}

	if i, err := strconv.Atoi(amount); err != nil || i <= 0 {
		return fmt.Errorf("amount must be a positive number > 0: %s", err)
	}

	interval, ok := params["interval"]
	if !ok {
		return errors.New("interval is required")
	}

	if i, err := strconv.Atoi(interval); err != nil || i <= 0 {
		return fmt.Errorf("interval must be a positive number: %s", err)
	}

	if err := b.Put([]byte(fmt.Sprintf("%s-amount", h.ID)), []byte(amount)); err != nil {
		return err
	}
	if err := b.Put([]byte(fmt.Sprintf("%s-interval", h.ID)), []byte(interval)); err != nil {
		return err
	}
	_, err := b.CreateBucketIfNotExists([]byte("requests"))
	return err
}
Exemple #4
0
func (store *BoltStore) put(bucket *bolt.Bucket, key string, value interface{}) error {
	var err error
	if is_value_basic(value) {
		if key == "" {
			return fmt.Errorf("key is absent")
		}
		byts, err := store.coder.Encode(value)
		if err != nil {
			return err
		}
		return bucket.Put([]byte(key), byts)
	}

	if key != "" {
		bucket, err = bucket.CreateBucketIfNotExists([]byte(key))
		if err != nil {
			return err
		}
	}

	switch value_type_kind(value) {
	case reflect.Slice:
		vs := reflect.ValueOf(value)
		for i := 0; i < vs.Len(); i++ {
			if err := store.put(bucket, strconv.Itoa(i), vs.Index(i).Interface()); err != nil {
				return err
			}
		}
		return nil
	case reflect.Map:
		vs, err := cast.ToStringMapE(value)
		if err != nil {
			return err
		}
		for k, v := range vs {
			if err := store.put(bucket, k, v); err != nil {
				return err
			}
		}
		return nil
	case reflect.Struct:
		vt := reflect.TypeOf(value)
		vv := reflect.ValueOf(value)
		for i := 0; i < vt.NumField(); i++ {
			vfield := vt.Field(i)
			if tag := vfield.Tag.Get("object"); tag != "" {
				if err := store.put(bucket, tag, vv.Field(i).Interface()); err != nil {
					return err
				}
			}
		}
		return nil
	}
	return fmt.Errorf("unsupport store value type")
}
// Init initializes this component. It requires a secret to be present.
func (GithubValidator) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
	secret, ok := params["secret"]
	if !ok {
		return errors.New("secret is required")
	}
	if err := b.Put([]byte(fmt.Sprintf("%s-secret", h.ID)), []byte(secret)); err != nil {
		return err
	}
	_, err := b.CreateBucketIfNotExists([]byte("deliveries"))
	return err
}
// Init initializes this component. It requires a Mailgun API key to be
// present.
func (MailgunValidator) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
	apikey, ok := params["apikey"]
	if !ok {
		return errors.New("apikey is required")
	}
	if err := b.Put([]byte(fmt.Sprintf("%s-apikey", h.ID)), []byte(apikey)); err != nil {
		return err
	}
	_, err := b.CreateBucketIfNotExists([]byte("tokens"))
	return err
}
Exemple #7
0
// CreateBucketIfNotExists creates the bucket below the current node if it doesn't
// already exist.
func (n *node) CreateBucketIfNotExists(tx *bolt.Tx, bucket string) (*bolt.Bucket, error) {
	var b *bolt.Bucket
	var err error

	bucketNames := append(n.rootBucket, bucket)

	for _, bucketName := range bucketNames {
		if b != nil {
			if b, err = b.CreateBucketIfNotExists([]byte(bucketName)); err != nil {
				return nil, err
			}

		} else {
			if b, err = tx.CreateBucketIfNotExists([]byte(bucketName)); err != nil {
				return nil, err
			}
		}
	}

	return b, nil
}
Exemple #8
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
}
Exemple #9
0
func importBucket(b *bolt.Bucket, children []*rawMessage) error {
	// Decode each message into a key/value pair.
	for _, child := range children {
		// Bucket messages are handled recursively.
		if child.Type == "bucket" {
			// Create the bucket if it doesn't exist.
			subbucket, err := b.CreateBucketIfNotExists(child.Key)
			if err != nil {
				return fmt.Errorf("create bucket: %s", err)
			}

			// Decode child messages.
			var subchildren []*rawMessage
			if err := json.Unmarshal(child.Value, &subchildren); err != nil {
				return fmt.Errorf("decode children: %s", err)
			}

			// Import subbucket.
			if err := importBucket(subbucket, subchildren); err != nil {
				return fmt.Errorf("import bucket: %s", err)
			}
			continue
		}

		// Non-bucket values are decoded from base64.
		var value []byte
		if err := json.Unmarshal(child.Value, &value); err != nil {
			return fmt.Errorf("decode value: %s", err)
		}

		// Insert key/value into bucket.
		if err := b.Put(child.Key, value); err != nil {
			return fmt.Errorf("put: %s", err)
		}
	}
	return nil
}