当前位置: 首页>>代码示例>>Golang>>正文


Golang Bucket.Cursor方法代码示例

本文整理汇总了Golang中github.com/boltdb/bolt.Bucket.Cursor方法的典型用法代码示例。如果您正苦于以下问题:Golang Bucket.Cursor方法的具体用法?Golang Bucket.Cursor怎么用?Golang Bucket.Cursor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/boltdb/bolt.Bucket的用法示例。


在下文中一共展示了Bucket.Cursor方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: findData

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
	})
}
开发者ID:willcraft,项目名称:sample-boltdb-cli,代码行数:32,代码来源:main.go

示例2: Allocate

// 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
}
开发者ID:voidException,项目名称:bazil,代码行数:31,代码来源:inodes.go

示例3: executeSelect

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
	})
}
开发者ID:mnadel,项目名称:boltq,代码行数:31,代码来源:program.go

示例4: get

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")
}
开发者ID:h2object,项目名称:h2object,代码行数:34,代码来源:store_bolt.go

示例5: size

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()
}
开发者ID:h2object,项目名称:h2object,代码行数:32,代码来源:store_bolt.go

示例6: deleteRange

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
}
开发者ID:smarterclayton,项目名称:kube-archiver,代码行数:9,代码来源:bolt.go

示例7: countItems

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
}
开发者ID:asdine,项目名称:storm,代码行数:12,代码来源:list_test.go

示例8: multi_get

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
}
开发者ID:h2object,项目名称:h2object,代码行数:50,代码来源:store_bolt.go

示例9: Process

// 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
}
开发者ID:vampirewalk,项目名称:rehook,代码行数:39,代码来源:rate_limit_filter.go


注:本文中的github.com/boltdb/bolt.Bucket.Cursor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。