ZRemRangeByRank
删除所有成员满足排名start到end(包括start和end)在一个指定的有序集合bucket中。其中排名以1开始,排名1表示第一个节点元素,排名-1表示最后的节点元素。
if err := db.Update(
func(tx *nutsdb.Tx) error {
bucket := "myZSet6"
key1 := []byte("key1")
return tx.ZAdd(bucket, key1, 10, []byte("val1"))
}); err != nil {
log.Fatal(err)
}
if err := db.Update(
func(tx *nutsdb.Tx) error {
bucket := "myZSet6"
key2 := []byte("key2")
return tx.ZAdd(bucket, key2, 20, []byte("val2"))
}); err != nil {
log.Fatal(err)
}
if err := db.Update(
func(tx *nutsdb.Tx) error {
bucket := "myZSet6"
key3 := []byte("key3")
return tx.ZAdd(bucket, key3, 30, []byte("val2"))
}); err != nil {
log.Fatal(err)
}
if err := db.View(
func(tx *nutsdb.Tx) error {
bucket := "myZSet6"
if nodes,err := tx.ZMembers(bucket); err != nil {
return err
} else {
fmt.Println("before ZRemRangeByRank, ZMembers nodes",nodes)
for _,node:=range nodes {
fmt.Println("item:",node.Key(),node.Score())
}
// before ZRemRangeByRank, ZMembers nodes map[key3:0xc00008d450 key1:0xc00008d270 key2:0xc00008d360]
// item: key1 10
// item: key2 20
// item: key3 30
}
return nil
}); err != nil {
log.Fatal(err)
}
if err := db.Update(
func(tx *nutsdb.Tx) error {
bucket := "myZSet6"
if err := tx.ZRemRangeByRank(bucket, 1,2); err != nil {
return err
}
return nil
}); err != nil {
log.Fatal(err)
}
if err := db.View(
func(tx *nutsdb.Tx) error {
bucket := "myZSet6"
if nodes,err := tx.ZMembers(bucket); err != nil {
return err
} else {
fmt.Println("after ZRemRangeByRank, ZMembers nodes",nodes)
for _,node:=range nodes {
fmt.Println("item:",node.Key(),node.Score())
}
// after ZRemRangeByRank, ZMembers nodes map[key3:0xc00008d450]
// item: key3 30
// key1 ZScore 10
}
return nil
}); err != nil {
log.Fatal(err)
}