Skip to content

Commit

Permalink
refactor: optimize zipmap zipset entry
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Aug 21, 2024
1 parent 110e630 commit 68e9d26
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 62 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.9.0
github.com/tidwall/mmap v0.3.0
github.com/zeebo/xxh3 v1.0.2
golang.org/x/sys v0.24.0
)

Expand All @@ -22,7 +21,6 @@ require (
github.com/dolthub/maphash v0.1.0 // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/influxdata/tdigest v0.0.1 h1:XpFptwYmnEKUqmkcDjrzffswZ3nvNeevbUSLPP/ZzIY=
github.com/influxdata/tdigest v0.0.1/go.mod h1:Z0kXnxzbTC2qrx4NaIzYkE1k66+6oEDQTvL95hQFh5Y=
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
Expand Down Expand Up @@ -56,16 +54,11 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/mmap v0.3.0 h1:XXt1YsiXCF5/UAu3pLbu6g7iulJ9jsbs6vt7UpiV0sY=
github.com/tidwall/mmap v0.3.0/go.mod h1:2/dNzF5zA+te/JVHfrqNLcRkb8LjdH3c80vYHFQEZRk=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
Expand Down
22 changes: 8 additions & 14 deletions internal/hash/zipmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ import (
"unsafe"

"github.com/xgzlucario/rotom/internal/list"
"github.com/zeebo/xxh3"
)

var _ MapI = (*ZipMap)(nil)

// ZipMap store datas as [entry1, entry2, entry3...] in listpack.
/*
entry format:
+-----------------+-----+-----+--------------+
| val_len(varint) | val | key | hash(1 Byte) |
+-----------------+-----+-----+--------------+
+-----------------+-----+-----+
| val_len(varint) | val | key |
+-----------------+-----+-----+
*/
type ZipMap struct {
data *list.ListPack
Expand All @@ -29,28 +28,23 @@ 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...)
return append(buf, byte(xxh3.HashString(key)))
return append(buf, key...)
}

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])
key = b2s(src[n+int(vlen):])
return
}

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 {
kb, vb := zm.decode(entry)
if key == kb {
return it, vb
}
keyData, valData := zm.decode(entry)
if key == keyData {
return it, valData
}
}
return nil, nil
Expand Down
45 changes: 6 additions & 39 deletions internal/hash/zipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ package hash

import (
"github.com/xgzlucario/rotom/internal/list"
"github.com/zeebo/xxh3"
)

var _ SetI = (*ZipSet)(nil)

// ZipSet store datas as [entry1, entry2, entry3...] in listpack.
/*
entry format:
+-----+--------------+
| key | hash(1 Byte) |
+-----+--------------+
*/
// ZipSet store datas as [key1, key2, key3...] in listpack.
type ZipSet struct {
data *list.ListPack
}
Expand All @@ -22,36 +15,20 @@ func NewZipSet() *ZipSet {
return &ZipSet{list.NewListPack()}
}

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 (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 := zs.encode(key)
zs.data.RPush(b2s(entry))
zs.data.RPush(key)
return true
}

func (zs *ZipSet) Exist(key string) bool {
it := zs.data.Iterator().SeekLast()
hash := byte(xxh3.HashString(key))

for !it.IsFirst() {
entry := it.Prev()
if entry[len(entry)-1] != hash {
continue
}
kb := zs.decode(entry)
if key == kb {
if key == b2s(entry) {
return true
}
}
Expand All @@ -60,15 +37,10 @@ func (zs *ZipSet) Exist(key string) bool {

func (zs *ZipSet) Remove(key string) bool {
it := zs.data.Iterator().SeekLast()
hash := byte(xxh3.HashString(key))

for !it.IsFirst() {
entry := it.Prev()
if entry[len(entry)-1] != hash {
continue
}
kb := zs.decode(entry)
if key == kb {
if key == b2s(entry) {
it.RemoveNexts(1, nil)
return true
}
Expand All @@ -80,17 +52,12 @@ func (zs *ZipSet) Scan(fn func(string)) {
it := zs.data.Iterator().SeekLast()
for !it.IsFirst() {
entry := it.Prev()
key := zs.decode(entry)
fn(key)
fn(b2s(entry))
}
}

func (zs *ZipSet) Pop() (string, bool) {
key, ok := zs.data.RPop()
if ok {
return key[:len(key)-1], true
}
return "", false
return zs.data.RPop()
}

func (zs *ZipSet) Len() int { return zs.data.Size() }
Expand Down

0 comments on commit 68e9d26

Please sign in to comment.