Skip to content

Commit

Permalink
docs: update README
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Aug 5, 2024
1 parent 0b20fb1 commit e72922a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 78 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rotom

[![Go Report Card](https://goreportcard.com/badge/github.com/xgzlucario/rotom)](https://goreportcard.com/report/github.com/xgzlucario/rotom) [![Awesome](https://awesome.re/badge.svg)](https://awesome.re) [![Go Reference](https://pkg.go.dev/badge/github.com/xgzlucario/rotom.svg)](https://pkg.go.dev/github.com/xgzlucario/rotom) ![](https://img.shields.io/github/languages/code-size/xgzlucario/rotom.svg) [![codecov](https://codecov.io/gh/xgzlucario/rotom/graph/badge.svg?token=2V0HJ4KO3E)](https://codecov.io/gh/xgzlucario/rotom) [![tests](https://github.com/xgzlucario/rotom/actions/workflows/rotom.yml/badge.svg)](https://github.com/xgzlucario/rotom/actions/workflows/rotom.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/xgzlucario/rotom)](https://goreportcard.com/report/github.com/xgzlucario/rotom) [![Go Reference](https://pkg.go.dev/badge/github.com/xgzlucario/rotom.svg)](https://pkg.go.dev/github.com/xgzlucario/rotom) ![](https://img.shields.io/github/languages/code-size/xgzlucario/rotom.svg) [![codecov](https://codecov.io/gh/xgzlucario/rotom/graph/badge.svg?token=2V0HJ4KO3E)](https://codecov.io/gh/xgzlucario/rotom) [![tests](https://github.com/xgzlucario/rotom/actions/workflows/rotom.yml/badge.svg)](https://github.com/xgzlucario/rotom/actions/workflows/rotom.yml) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)

English | [中文](README_CN.md)

Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Rotom

[![Go Report Card](https://goreportcard.com/badge/github.com/xgzlucario/rotom)](https://goreportcard.com/report/github.com/xgzlucario/rotom) [![Awesome](https://awesome.re/badge.svg)](https://awesome.re) [![Go Reference](https://pkg.go.dev/badge/github.com/xgzlucario/rotom.svg)](https://pkg.go.dev/github.com/xgzlucario/rotom) ![](https://img.shields.io/github/languages/code-size/xgzlucario/rotom.svg) [![codecov](https://codecov.io/gh/xgzlucario/rotom/graph/badge.svg?token=2V0HJ4KO3E)](https://codecov.io/gh/xgzlucario/rotom) [![tests](https://github.com/xgzlucario/rotom/actions/workflows/rotom.yml/badge.svg)](https://github.com/xgzlucario/rotom/actions/workflows/rotom.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/xgzlucario/rotom)](https://goreportcard.com/report/github.com/xgzlucario/rotom) [![Go Reference](https://pkg.go.dev/badge/github.com/xgzlucario/rotom.svg)](https://pkg.go.dev/github.com/xgzlucario/rotom) ![](https://img.shields.io/github/languages/code-size/xgzlucario/rotom.svg) [![codecov](https://codecov.io/gh/xgzlucario/rotom/graph/badge.svg?token=2V0HJ4KO3E)](https://codecov.io/gh/xgzlucario/rotom) [![tests](https://github.com/xgzlucario/rotom/actions/workflows/rotom.yml/badge.svg)](https://github.com/xgzlucario/rotom/actions/workflows/rotom.yml) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)

中文 | [English](README.md)

Expand Down
4 changes: 4 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ func TestCommand(t *testing.T) {

_, err = rdb.LRange(ctx, "key", 0, -1).Result()
assert.Equal(err.Error(), errWrongType.Error())

rdb.RPush(ctx, "ls-test", "A")
_, err = rdb.Incr(ctx, "ls-test").Result()
assert.Equal(err.Error(), errWrongType.Error())
})

t.Run("set", func(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions internal/hash/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func (s Set) Scan(fn func(string)) {
})
}

func (s Set) Exist(key string) bool {
return s.Set.ContainsOne(key)
}
func (s Set) Exist(key string) bool { return s.Set.ContainsOne(key) }

func (s Set) Len() int { return s.Cardinality() }
47 changes: 20 additions & 27 deletions internal/hash/zipmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,53 @@ import (
var _ MapI = (*ZipMap)(nil)

// ZipMap store datas as [entry1, entry2, entry3...] in listpack.
type ZipMap struct {
data *list.ListPack
}

// zipmapEntry is data format in zipmap.
/*
entry format:
+-----------------+-----+-----+--------------+
| val_len(varint) | val | key | hash(1 Byte) |
+-----------------+-----+-----+--------------+
*/
type zipmapEntry struct{}
type ZipMap struct {
data *list.ListPack
}

func NewZipMap() *ZipMap {
return &ZipMap{list.NewListPack()}
}

func (zipmapEntry) encode(key string, val []byte) []byte {
func (ZipMap) encode(key string, val []byte) []byte {
buf := make([]byte, len(key)+len(val)+2)[:0]
buf = binary.AppendUvarint(buf, uint64(len(val)))
buf = append(buf, val...)
buf = append(buf, key...)
buf = append(buf, byte(xxh3.HashString(key)))
return buf
return append(buf, byte(xxh3.HashString(key)))
}

func (zipmapEntry) decode(buf []byte) (key string, val []byte) {
vlen, n := binary.Uvarint(buf)
val = buf[n : n+int(vlen)]
key = b2s(buf[n+int(vlen) : len(buf)-1])
func (ZipMap) decode(src []byte) (key string, val []byte) {
vlen, n := binary.Uvarint(src)
val = src[n : n+int(vlen)]
key = b2s(src[n+int(vlen) : len(src)-1])
return
}

func NewZipMap() *ZipMap {
return &ZipMap{list.NewListPack()}
}

func (zm *ZipMap) find(key string) (it *list.LpIterator, val []byte) {
it = zm.data.Iterator().SeekLast()
hash := byte(xxh3.HashString(key))

for !it.IsFirst() {
entry := it.Prev()

if entry[len(entry)-1] != hash {
continue
}

kb, vb := zipmapEntry{}.decode(entry)
if key == kb {
return it, vb
if entry[len(entry)-1] == hash {
kb, vb := zm.decode(entry)
if key == kb {
return it, vb
}
}
}
return nil, nil
}

func (zm *ZipMap) Set(key string, val []byte) (newField bool) {
entry := zipmapEntry{}.encode(key, val)
entry := zm.encode(key, val)
it, _ := zm.find(key)
if it != nil {
it.ReplaceNext(b2s(entry))
Expand Down Expand Up @@ -94,7 +87,7 @@ func (zm *ZipMap) Remove(key string) bool {
func (zm *ZipMap) Scan(fn func(string, []byte)) {
it := zm.data.Iterator().SeekLast()
for !it.IsFirst() {
kb, vb := zipmapEntry{}.decode(it.Prev())
kb, vb := zm.decode(it.Prev())
fn(kb, vb)
}
}
Expand Down
36 changes: 16 additions & 20 deletions internal/hash/zipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,36 @@ import (

var _ SetI = (*ZipSet)(nil)

// ZipSet store datas as [key1, key2, key3...] in listpack.
type ZipSet struct {
data *list.ListPack
}

// zipsetEntry is data format in zipset.
// ZipSet store datas as [entry1, entry2, entry3...] in listpack.
/*
entry format:
+-----+--------------+
| key | hash(1 Byte) |
+-----+--------------+
*/
type zipsetEntry struct{}
type ZipSet struct {
data *list.ListPack
}

func (zipsetEntry) encode(key string) []byte {
buf := make([]byte, len(key)+1)[:0]
buf = append(buf, key...)
buf = append(buf, byte(xxh3.HashString(key)))
return buf
func NewZipSet() *ZipSet {
return &ZipSet{list.NewListPack()}
}

func (zipsetEntry) decode(buf []byte) (key string) {
return b2s(buf[:len(buf)-1])
func (ZipSet) encode(key string) []byte {
buf := make([]byte, len(key)+1)[:0]
buf = append(buf, key...)
return append(buf, byte(xxh3.HashString(key)))
}

func NewZipSet() *ZipSet {
return &ZipSet{list.NewListPack()}
func (ZipSet) decode(src []byte) (key string) {
return b2s(src[:len(src)-1])
}

func (zs *ZipSet) Add(key string) (newField bool) {
if zs.Exist(key) {
return false
}
entry := zipsetEntry{}.encode(key)
entry := zs.encode(key)
zs.data.RPush(b2s(entry))
return true
}
Expand All @@ -54,7 +50,7 @@ func (zs *ZipSet) Exist(key string) bool {
if entry[len(entry)-1] != hash {
continue
}
kb := zipsetEntry{}.decode(entry)
kb := zs.decode(entry)
if key == kb {
return true
}
Expand All @@ -71,7 +67,7 @@ func (zs *ZipSet) Remove(key string) bool {
if entry[len(entry)-1] != hash {
continue
}
kb := zipsetEntry{}.decode(entry)
kb := zs.decode(entry)
if key == kb {
it.RemoveNexts(1, nil)
return true
Expand All @@ -84,7 +80,7 @@ func (zs *ZipSet) Scan(fn func(string)) {
it := zs.data.Iterator().SeekLast()
for !it.IsFirst() {
entry := it.Prev()
key := zipsetEntry{}.decode(entry)
key := zs.decode(entry)
fn(key)
}
}
Expand Down
14 changes: 0 additions & 14 deletions internal/zset/zipzset.go

This file was deleted.

12 changes: 0 additions & 12 deletions internal/zset/zset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ import (
"github.com/cockroachdb/swiss"
)

type ZSetI interface {
Set(key string, score float64) bool
Get(key string) (float64, bool)
Remove(key string) bool
Len() int
PopMin() (key string, score float64)
Rank(key string) (rank int, score float64)
Range(start, stop int, fn func(key string, score float64))
}

var _ ZSetI = (*ZSet)(nil)

type node struct {
key string
score float64
Expand Down

0 comments on commit e72922a

Please sign in to comment.