zset

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: MIT Imports: 1 Imported by: 0

README

Go Report Card

zset

Implementing sorted set in Redis with golang.

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

Overview

Package zset is a port of t_zset.c in Redis

  • Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
  • Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  • All rights reserved. *
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are met: *
  • * Redistributions of source code must retain the above copyright notice,
  • this list of conditions and the following disclaimer.
  • * Redistributions in binary form must reproduce the above copyright
  • notice, this list of conditions and the following disclaimer in the
  • documentation and/or other materials provided with the distribution.
  • * Neither the name of Redis nor the names of its contributors may be used
  • to endorse or promote products derived from this software without
  • specific prior written permission. *
  • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  • AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  • IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  • ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  • LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  • CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  • SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  • INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  • CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  • ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  • POSSIBILITY OF SUCH DAMAGE.

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) GetScore

func (z *SortedSet) GetScore(key int64) (score float64, ok bool)

GetScore implements ZScore

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