zset

package module
v0.0.0-...-6bbd453 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2020 License: MIT Imports: 1 Imported by: 0

README

Go Report Card

zset

Implementing sorted set in Redis with golang.

Forked from github.com/liyiheng/zset

TODO

Key type int64 to string, or just waiting for generics.

Installation

go get -u github.com/liyiheng/zset

Usage

Removed RWLock in the SortedSet. Just implement it yourself if needed.

s := zset.New()
// add data
s.Set(66, 1001, "test1")
s.Set(77, 1002, "test2")
s.Set(88, 1003, "test3")
s.Set(100, 1004, "liyiheng")
s.Set(99, 1005, "test4")
s.Set(44, 1006, "test5")
// update data
s.Set(44, 1001, "test1")

// get rank by id
rank, score, extra := s.GetRank(1004, false)
// get data by rank
id, score, extra := s.GetDataByRank(0, true)
// get data by id
dat, ok := s.GetData(1001)

// delete data by id
s.Delete(1001)

// Increase score
s.IncrBy(5.0, 1001)

// ZRANGE, ASC
five := make([]int64, 0, 5)
s.Range(0, 5, func(score float64, k int64, _ interface{}) {
	five = append(five, k)
})

// ZREVRANGE, DESC
all := make([]int64, 0)
s.RevRange(0, -1, func(score float64, k int64, _ interface{}) {
	all = append(all, k)
})


Benchmark

 OS: Arch Linux 
 Kernel: x86_64 Linux 5.1.5-arch1-2-ARCH
 CPU: Intel Core i7-8750H @ 12x 4.1GHz [46.0°C]
 RAM: 3295MiB / 7821MiB
go test -test.bench=".*"
goos: linux
goarch: amd64
BenchmarkSortedSet_Add-12              	 1000000	      3050 ns/op
BenchmarkSortedSet_GetRank-12          	  500000	      2963 ns/op
BenchmarkSortedSet_GetDataByRank-12    	 2000000	       620 ns/op
PASS

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SortedSet

type SortedSet struct {
	// contains filtered or unexported fields
}

SortedSet is the final exported sorted set we can use

func New

func New() *SortedSet

New creates a new SortedSet and return its pointer

func (*SortedSet) Delete

func (z *SortedSet) Delete(key int64) (ok bool)

Delete removes an element from the SortedSet by its key.

func (*SortedSet) GetData

func (z *SortedSet) GetData(key int64) (data interface{}, ok bool)

GetData returns data stored in the map by its key

func (*SortedSet) GetDataByRank

func (z *SortedSet) GetDataByRank(rank int64, reverse bool) (key int64, score float64, data interface{})

GetDataByRank returns the id,score and extra data of an element which found by position in the rank. The parameter rank is the position, reverse says if in the descend rank.

func (*SortedSet) GetRank

func (z *SortedSet) GetRank(key int64, reverse bool) (rank int64, score float64, data interface{})

GetRank returns position,score and extra data of an element which found by the parameter key. The parameter reverse determines the rank is descent or ascend, true means descend and false means ascend.

func (*SortedSet) IncrBy

func (z *SortedSet) IncrBy(score float64, key int64) (float64, interface{})

IncrBy ..

func (*SortedSet) Length

func (z *SortedSet) Length() int64

Length returns counts of elements

func (*SortedSet) Range

func (z *SortedSet) Range(start, end int64, f func(float64, int64, interface{}))

Range implements ZRANGE

func (*SortedSet) RevRange

func (z *SortedSet) RevRange(start, end int64, f func(float64, int64, interface{}))

RevRange implements ZREVRANGE

func (*SortedSet) Set

func (z *SortedSet) Set(score float64, key int64, dat interface{})

Set is used to add or update an element

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL