github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/examples/zset/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/flower-corp/rosedb"
     6  	"path/filepath"
     7  	"strconv"
     8  )
     9  
    10  func main() {
    11  	path := filepath.Join("/tmp", "rosedb")
    12  	opts := rosedb.DefaultOptions(path)
    13  	db, err := rosedb.Open(opts)
    14  	if err != nil {
    15  		fmt.Printf("open rosedb err: %v", err)
    16  		return
    17  	}
    18  
    19  	for i := 1; i <= 10; i++ {
    20  		err = db.ZAdd([]byte("zset-key"), float64(i*2), []byte("member-"+strconv.Itoa(i)))
    21  		if err != nil {
    22  			fmt.Printf("write data err: %v", err)
    23  			return
    24  		}
    25  	}
    26  
    27  	ok, score := db.ZScore([]byte("zset-key"), []byte("member-1"))
    28  	if ok {
    29  		fmt.Println("score is ", score)
    30  	}
    31  
    32  	err = db.ZRem([]byte("zset-key"), []byte("member-1"))
    33  	if err != nil {
    34  		fmt.Printf("delete data err: %v", err)
    35  		return
    36  	}
    37  
    38  	card := db.ZCard([]byte("zset-key"))
    39  	fmt.Println("card of zset-key : ", card)
    40  
    41  	members, err := db.ZRange([]byte("zset-key"), 0, -1)
    42  	if err != nil {
    43  		fmt.Printf("get data err: %v", err)
    44  		return
    45  	}
    46  	for _, v := range members {
    47  		fmt.Println(string(v))
    48  	}
    49  }