Giter Club home page Giter Club logo

zset's Introduction

Go Report Card

zset

Implementing sorted set in Redis with golang.

Installation

go get -u github.com/liyiheng/zset

Usage

s := zset.New[int64]()
// add data
s.Set(66, 1001)
s.Set(77, 1002)
s.Set(88, 1003)
s.Set(100, 1004)
s.Set(99, 1005)
s.Set(44, 1006)
// update data
s.Set(44, 1001)

// get rank by key
rank, score := s.GetRank(1004, false)
// get data by rank
id, score := s.GetDataByRank(0, true)

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) {
	five = append(five, k)
})

// ZREVRANGE, DESC
all := make([]int64, 0)
s.RevRange(0, -1, func(score float64, k int64) {
	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

zset's People

Contributors

liyiheng avatar stackcats avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

zset's Issues

为什么引入找不到cmp

➜ zset go get -u github.com/liyiheng/zset
package cmp: unrecognized import path "cmp": import path does not begin with hostname

如何实现多条件排序

比如战斗力相同的情况下,再按等级排,等级相同,再按境界排。

请问这类需求,应该如何实现?

ZRANGE support

非常感谢作者的作品
下午测试的时候读取发现十分缓慢
使用pprof分析后发现如下情况
.GetDataByRank这个函数如果一个个调用获取10w排行性能不佳
如能按当前节点继续按顺序输出指定limit个数预计会好很多
特申请增加类似功能

(pprof) list main
Total: 2.51mins
ROUTINE ======================== main.getTop in /Users/changlong.xu/vagrant/share/golang/Bifrost/src/main.go
1.52s 1.96mins (flat, cum) 78.12% of Total
. . 103:
. . 104: var buffer bytes.Buffer
. . 105: if _, ok := sortSetListMap[mapKey]; ok {
. . 106: var mapObj = sortSetListMap[mapKey]
. . 107:
410ms 460ms 108: for i := int64(0); i < mapObj.SortSet.Length(); i++ {
. . 109:// mapObj.Lock.RLock()
. . 110:
80ms 1.44mins 111: id, _, _ := mapObj.SortSet.GetDataByRank(int64(i), false)
. . 112:// mapObj.Lock.RUnlock()
. . 113:
1.03s 30.57s 114: buffer.WriteString("id:" + strconv.FormatInt(id, 10))
. . 115: //buffer.WriteString(" sc:" + strconv.FormatFloat(score, 'f', -1, 64))
. . 116: //buffer.WriteString(" ext:" + extra.(string))
. . 117: }
. . 118:
. . 119: }
. . 120:
. 130ms 121: w.Write(buffer.Bytes())
. . 122:
. . 123:}
. . 124:
. . 125:func main() {
. . 126:
ROUTINE ======================== main.main in /Users/changlong.xu/vagrant/share/golang/Bifrost/src/main.go
0 1.65s (flat, cum) 1.10% of Total
. . 126:
. . 127: //fmt.Println("length is ", sortset.Length())
. . 128: http.HandleFunc("/addtop", addTop)
. . 129: http.HandleFunc("/gettop", getTop)
. . 130:
. 1.65s 131: err := http.ListenAndServe(":9229", nil)
. . 132: if err != nil {
. . 133: log.Fatal("Listen Server Fail: ", err)
. . 134: return
. . 135: }
. . 136:}
ROUTINE ======================== runtime.main in /usr/local/Cellar/go/1.12/libexec/src/runtime/proc.go
0 1.65s (flat, cum) 1.10% of Total
. . 195: // A program compiled with -buildmode=c-archive or c-shared
. . 196: // has a main, but it is not executed.
. . 197: return
. . 198: }
. . 199: fn = main_main // make an indirect call, as the linker doesn't know the address of the main package when laying down the runtime
. 1.65s 200: fn()
. . 201: if raceenabled {
. . 202: racefini()
. . 203: }
. . 204:
. . 205: // Make racy client program work: if panicking on

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.