Skip to content

Commit

Permalink
feat: add encode/decode form hash
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Oct 27, 2024
1 parent 1466bd9 commit 8347fd7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
26 changes: 25 additions & 1 deletion internal/hash/set.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package hash

import (
"github.com/bytedance/sonic"
mapset "github.com/deckarep/golang-set/v2"
"io"
)

const (
defaultSetSize = 512
)

type SetI interface {
Expand All @@ -11,6 +17,8 @@ type SetI interface {
Pop() (key string, ok bool)
Scan(fn func(key string))
Len() int
Encode(writer io.Writer) error
Decode([]byte) error
}

var _ SetI = (*Set)(nil)
Expand All @@ -20,7 +28,7 @@ type Set struct {
}

func NewSet() *Set {
return &Set{mapset.NewThreadUnsafeSetWithSize[string](512)}
return &Set{mapset.NewThreadUnsafeSetWithSize[string](defaultSetSize)}
}

func (s Set) Remove(key string) bool {
Expand All @@ -41,3 +49,19 @@ func (s Set) Scan(fn func(string)) {
func (s Set) Exist(key string) bool { return s.Set.ContainsOne(key) }

func (s Set) Len() int { return s.Cardinality() }

func (s Set) Encode(writer io.Writer) error {
items := s.Set.ToSlice()
return sonic.ConfigDefault.NewEncoder(writer).Encode(items)
}

func (s Set) Decode(src []byte) error {
var items []string
err := sonic.Unmarshal(src, &items)
if err != nil {
return err
}
s.Set = mapset.NewThreadUnsafeSetWithSize[string](defaultSetSize)
s.Set.Append(items...)
return nil
}
9 changes: 9 additions & 0 deletions internal/hash/zipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hash

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

var _ SetI = (*ZipSet)(nil)
Expand Down Expand Up @@ -69,3 +70,11 @@ func (zs *ZipSet) ToSet() *Set {
})
return s
}

func (zs *ZipSet) Encode(writer io.Writer) error {
return zs.data.Encode(writer)
}

func (zs *ZipSet) Decode(src []byte) error {
return zs.data.Decode(src)
}
10 changes: 3 additions & 7 deletions internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func newNode() *Node {
return &Node{ListPack: NewListPack()}
}

// LPush
func (ls *QuickList) LPush(key string) {
if len(ls.head.data)+len(key) >= maxListPackSize {
n := newNode()
Expand All @@ -39,7 +38,6 @@ func (ls *QuickList) LPush(key string) {
ls.head.LPush(key)
}

// RPush
func (ls *QuickList) RPush(key string) {
if len(ls.tail.data)+len(key) >= maxListPackSize {
n := newNode()
Expand All @@ -51,7 +49,6 @@ func (ls *QuickList) RPush(key string) {
ls.tail.RPush(key)
}

// LPop
func (ls *QuickList) LPop() (key string, ok bool) {
for lp := ls.head; lp != nil; lp = lp.next {
if lp.size > 0 {
Expand All @@ -63,7 +60,6 @@ func (ls *QuickList) LPop() (key string, ok bool) {
return
}

// RPop
func (ls *QuickList) RPop() (key string, ok bool) {
for lp := ls.tail; lp != nil; lp = lp.prev {
if lp.size > 0 {
Expand Down Expand Up @@ -117,16 +113,16 @@ func (ls *QuickList) Range(start, stop int, fn func(data []byte)) {
start -= lp.Size()
lp = lp.next
}
if lp == nil {
return
}
it := lp.Iterator()
for range start {
it.Next()
}

for range count {
if it.IsLast() {
if lp.next == nil {
return
}
lp = lp.next
it = lp.Iterator()
}
Expand Down
4 changes: 2 additions & 2 deletions internal/list/listpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func (lp *ListPack) Iterator() *LpIterator {
}

func (lp *ListPack) Encode(writer io.Writer) error {
sizeBytes := binary.AppendUvarint(nil, uint64(lp.size))
_, err := writer.Write(sizeBytes)
size := binary.AppendUvarint(nil, uint64(lp.size))
_, err := writer.Write(size)
if err != nil {
return err
}
Expand Down

0 comments on commit 8347fd7

Please sign in to comment.