- ZRangeByRank
ZRangeByRank
返回指定bucket有序集合的排名start到end的范围(包括start和end)的所有元素。
// ZAdd add items
if err := db.Update(
func(tx *nutsdb.Tx) error {
bucket := "myZSet2"
key1 := []byte("key1")
return tx.ZAdd(bucket, key1, 1, []byte("val1"))
}); err != nil {
log.Fatal(err)
}
if err := db.Update(
func(tx *nutsdb.Tx) error {
bucket := "myZSet2"
key2 := []byte("key2")
return tx.ZAdd(bucket, key2, 2, []byte("val2"))
}); err != nil {
log.Fatal(err)
}
if err := db.Update(
func(tx *nutsdb.Tx) error {
bucket := "myZSet2"
key3 := []byte("key3")
return tx.ZAdd(bucket, key3, 3, []byte("val3"))
}); err != nil {
log.Fatal(err)
}
// ZRangeByRank
if err := db.View(
func(tx *nutsdb.Tx) error {
bucket := "myZSet2"
if nodes, err := tx.ZRangeByRank(bucket, 1, 2); err != nil {
return err
} else {
fmt.Println("ZRangeByRank nodes :", nodes)
for _, node := range nodes {
fmt.Println("item:", node.Key(), node.Score())
}
//item: key1 1
//item: key2 2
}
return nil
}); err != nil {
log.Fatal(err)
}