Example #1
0
func findData(backetName string, option Option) error {
	var bucket *bolt.Bucket
	return db.View(func(tx *bolt.Tx) error {
		tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			if backetName == string(name) {
				bucket = b
			}
			return nil
		})
		if bucket == nil {
			return errors.New("bucket not found")
		}
		c := bucket.Cursor()
		prefix := []byte(option.key)
		data := map[string]interface{}{}

	OUTER:
		for pk, v := c.Seek(prefix); bytes.HasPrefix(pk, prefix); pk, v = c.Next() {
			u := map[string]interface{}{}
			json.Unmarshal(v, &u)
			for fk, fv := range option.params {
				if u[fk] != fv {
					continue OUTER
				}
			}
			data[string(pk)] = u
		}

		showData(data)
		return nil
	})
}
Example #2
0
// Allocate returns the next available inode number, and marks it
// used.
//
// Returns OutOfInodes if there are no free inodes.
func Allocate(bucket *bolt.Bucket) (uint64, error) {
	c := bucket.Cursor()
	var i uint64
	k, _ := c.Last()
	if k != nil {
		i = bytesToInode(k)
	}

	// reserve a few inodes for internal use
	if i < tokens.MaxReservedInode {
		i = tokens.MaxReservedInode
	}

	i++

	if i&tokens.InodeKindMask != tokens.InodeKindNormal {
		return 0, OutOfInodes
	}

	var buf [8]byte
	inodeToBytes(i, buf[:])
	err := bucket.Put(buf[:], nil)
	if err != nil {
		return 0, err
	}
	return i, nil
}
Example #3
0
func executeSelect(stmt *boltq.SelectStatement, db *bolt.DB) error {
	return db.View(func(tx *bolt.Tx) error {
		var bucket *bolt.Bucket

		for _, name := range stmt.BucketPath {
			log.Debugln("navigating to bucket", name)
			bucket = tx.Bucket([]byte(name))

			if bucket == nil {
				return fmt.Errorf("cannot find bucket %s", name)
			}
		}

		if containsAsterisk(stmt.Fields) {
			log.Debugln("interating keys")
			cursor := bucket.Cursor()

			for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
				emitKeypair(k, v)
			}
		} else {
			for _, k := range stmt.Fields {
				keyBytes := []byte(k)
				v := bucket.Get(keyBytes)
				emitKeypair(keyBytes, v)
			}
		}

		return nil
	})
}
Example #4
0
func (store *BoltStore) get(bucket *bolt.Bucket, key string, nested bool) (interface{}, error) {
	if key != "" {
		b := bucket.Get([]byte(key))
		if b != nil {
			return store.coder.Decode(b)
		}
		bucket = bucket.Bucket([]byte(key))
	}

	if bucket != nil {
		m := make(map[string]interface{})
		c := bucket.Cursor()
		for k, v := c.First(); k != nil; k, v = c.Next() {
			if v != nil {
				vv, err := store.coder.Decode(v)
				if err != nil {
					return nil, err
				}
				m[string(k)] = vv
			}

			if v == nil && nested == true {
				vv, err := store.get(bucket, string(k), nested)
				if err != nil {
					return nil, err
				}
				m[string(k)] = vv
			}
		}
		return m, nil
	}

	return nil, fmt.Errorf("bucket not exist")
}
Example #5
0
func (store *BoltStore) size(bucket *bolt.Bucket, key string, size *AtomicInt64) int64 {
	var statics *AtomicInt64 = size
	if statics == nil {
		statics = NewAtomicInt64(0)
	}

	if key != "" {
		b := bucket.Get([]byte(key))
		if b != nil {
			return statics.Caculate(int64(len(key) + len(b)))
		}

		bucket = bucket.Bucket([]byte(key))
	}

	if bucket != nil {
		c := bucket.Cursor()
		// to do staticsing
		for k, v := c.First(); k != nil; k, v = c.Next() {
			if v != nil {
				statics.Caculate(int64(len(k) + len(v)))
			} else {
				store.size(bucket, string(k), statics)
			}
		}
		statics.Caculate(int64(len(key)))
		return statics.Value()
	}

	//! null
	return statics.Value()
}
Example #6
0
func deleteRange(bucket *bolt.Bucket, prefix tuple.Key) error {
	cursor := bucket.Cursor()
	for k, _ := cursor.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, _ = cursor.Next() {
		if err := bucket.Delete(k); err != nil {
			return err
		}
	}
	return nil
}
Example #7
0
func countItems(t *testing.T, bucket *bolt.Bucket) int {
	c := bucket.Cursor()
	count := 0
	for k, id := c.First(); k != nil; k, id = c.Next() {
		if id == nil || bytes.Equal(k, []byte("storm__ids")) {
			continue
		}
		count++
	}

	return count
}
Example #8
0
func (store *BoltStore) multi_get(bucket *bolt.Bucket, suffix string, nested bool) ([]interface{}, error) {
	results := []interface{}{}
	if bucket != nil {
		c := bucket.Cursor()
		for k, v := c.First(); k != nil; k, v = c.Next() {
			if suffix == "" {
				if v != nil {
					vv, err := store.coder.Decode(v)
					if err != nil {
						return nil, err
					}
					results = append(results, vv)
					continue
				}

				if nested {
					bucket_nested := bucket.Bucket(k)
					if bucket_nested != nil {
						nests, err := store.multi_get(bucket_nested, suffix, nested)
						if err != nil {
							return results, err
						}
						results = append(results, nests...)
					}
				}
			} else {
				if strings.HasSuffix(string(k), suffix) {
					vv, err := store.get(bucket, string(k), true)
					if err != nil {
						return results, err
					}
					results = append(results, vv)
					continue
				} else {
					if nested && v == nil {
						bucket_nested := bucket.Bucket(k)
						if bucket_nested != nil {
							nests, err := store.multi_get(bucket_nested, suffix, nested)
							if err != nil {
								return results, err
							}
							results = append(results, nests...)
						}
					}
				}
			}
		}
	}
	return results, nil
}
Example #9
0
// Process makes sure incoming requests do not exceed the configured rate
// limit.
func (RateLimitFilter) Process(h Hook, r Request, b *bolt.Bucket) error {
	amount, _ := strconv.Atoi(string(b.Get([]byte(fmt.Sprintf("%s-amount", h.ID)))))
	interval, _ := strconv.Atoi(string(b.Get([]byte(fmt.Sprintf("%s-interval", h.ID)))))
	if amount <= 0 || interval <= 0 {
		return errors.New("rate limit filter not initialized")
	}

	b = b.Bucket([]byte("requests"))

	// store current timestamp
	now := time.Now()
	k := []byte(fmt.Sprintf("%d", now.UnixNano()))
	if err := b.Put(k, nil); err != nil {
		return err
	}

	// count requests
	c := b.Cursor()
	from := []byte(fmt.Sprintf("%d", now.Add(time.Duration(-interval)*time.Second).UnixNano()))

	var count int
	for k, _ := c.Seek(from); k != nil; k, _ = c.Next() {
		count++
	}

	if count > amount {
		return fmt.Errorf("rate limit exceeded (limit=%d count=%d)", amount, count)
	}

	// cleanup old entries
	for k, _ := c.First(); k != nil && bytes.Compare(k, from) <= 0; k, _ = c.Next() {
		if err := b.Delete(k); err != nil {
			return err
		}
	}
	return nil
}