Skip to content

Commit

Permalink
replace set and set and bitmap test
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Nov 1, 2023
1 parent 6ed0453 commit 01843c4
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 554 deletions.
15 changes: 15 additions & 0 deletions base/strconv.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package base

import (
"github.com/bytedance/sonic"
)

const (
VALID = 255
RADIX = VALID - 1
Expand Down Expand Up @@ -29,3 +33,14 @@ func ParseInt[T Integer](b []byte) T {
}
return n
}

func FormatStrSlice(ss []string) []byte {
src, _ := sonic.Marshal(ss)
return src
}

func ParseStrSlice(b []byte) []string {
var ss []string
sonic.Unmarshal(b, &ss)
return ss
}
4 changes: 4 additions & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (s *Codec) Str(v string) *Codec {
return s.format(s2b(&v))
}

func (s *Codec) StrSlice(v []string) *Codec {
return s.format(base.FormatStrSlice(v))
}

func (s *Codec) Type(v VType) *Codec {
return s.format([]byte{byte(v)})
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/RoaringBitmap/roaring v1.6.0
github.com/brianvoe/gofakeit/v6 v6.23.2
github.com/bytedance/sonic v1.10.2
github.com/deckarep/golang-set/v2 v2.3.1
github.com/dolthub/swiss v0.2.1
github.com/panjf2000/gnet/v2 v2.3.3
github.com/sakeven/RbTree v0.0.0-20220710124251-94e35f9fed6c
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLI
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.3.1 h1:vjmkvJt/IV27WXPyYQpAh4bRyWJc5Y435D17XQ9QU5A=
github.com/deckarep/golang-set/v2 v2.3.1/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=
github.com/dolthub/swiss v0.2.1 h1:gs2osYs5SJkAaH5/ggVJqXQxRXtWshF6uE0lgR/Y3Gw=
Expand Down
222 changes: 93 additions & 129 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,22 @@ var cmdTable = []Cmd{
}},
{OpSRemove, 2, func(e *Engine, args [][]byte, _ base.Writer) error {
// key, item
_, err := e.SRemove(*b2s(args[0]), *b2s(args[1]))
return err
return e.SRemove(*b2s(args[0]), *b2s(args[1]))
}},
{OpSUnion, 3, func(e *Engine, args [][]byte, w base.Writer) error {
// key1, key2, dest
return e.SUnion(*b2s(args[0]), *b2s(args[1]), *b2s(args[2]))
{OpSUnion, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// dstKey, srcKeys...
srcKeys := base.ParseStrSlice(args[1])
return e.SUnion(*b2s(args[0]), srcKeys...)
}},
{OpSInter, 3, func(e *Engine, args [][]byte, w base.Writer) error {
// key1, key2, dest
return e.SInter(*b2s(args[0]), *b2s(args[1]), *b2s(args[2]))
{OpSInter, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// dstKey, srcKeys...
srcKeys := base.ParseStrSlice(args[1])
return e.SInter(*b2s(args[0]), srcKeys...)
}},
{OpSDiff, 3, func(e *Engine, args [][]byte, w base.Writer) error {
// key1, key2, dest
return e.SDiff(*b2s(args[0]), *b2s(args[1]), *b2s(args[2]))
{OpSDiff, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// dstKey, srcKeys...
srcKeys := base.ParseStrSlice(args[1])
return e.SDiff(*b2s(args[0]), srcKeys...)
}},
// list
{OpLPush, 2, func(e *Engine, args [][]byte, w base.Writer) error {
Expand Down Expand Up @@ -254,17 +256,20 @@ var cmdTable = []Cmd{
// key, offset
return e.BitFlip(*b2s(args[0]), base.ParseInt[uint32](args[1]))
}},
{OpBitOr, 3, func(e *Engine, args [][]byte, w base.Writer) error {
// key1, key2, dest
return e.BitOr(*b2s(args[0]), *b2s(args[1]), *b2s(args[2]))
{OpBitOr, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// dstKey, srcKeys...
srcKeys := base.ParseStrSlice(args[1])
return e.BitOr(*b2s(args[0]), srcKeys...)
}},
{OpBitAnd, 3, func(e *Engine, args [][]byte, w base.Writer) error {
// key1, key2, dest
return e.BitAnd(*b2s(args[0]), *b2s(args[1]), *b2s(args[2]))
{OpBitAnd, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// dstKey, srcKeys...
srcKeys := base.ParseStrSlice(args[1])
return e.BitAnd(*b2s(args[0]), srcKeys...)
}},
{OpBitXor, 3, func(e *Engine, args [][]byte, w base.Writer) error {
// key1, key2, dest
return e.BitXor(*b2s(args[0]), *b2s(args[1]), *b2s(args[2]))
{OpBitXor, 2, func(e *Engine, args [][]byte, w base.Writer) error {
// dstKey, srcKeys...
srcKeys := base.ParseStrSlice(args[1])
return e.BitXor(*b2s(args[0]), srcKeys...)
}},
// zset
{OpZAdd, 4, func(e *Engine, args [][]byte, _ base.Writer) error {
Expand Down Expand Up @@ -621,14 +626,15 @@ func (e *Engine) SAdd(key string, item string) error {
}

// SRemove
func (e *Engine) SRemove(key string, item string) (bool, error) {
func (e *Engine) SRemove(key string, item string) error {
s, err := e.fetchSet(key)
if err != nil {
return false, err
return err
}
e.encode(NewCodec(OpSRemove).Str(key).Str(item))
s.Remove(item)

return s.Remove(item), nil
return nil
}

// SHas
Expand All @@ -637,7 +643,7 @@ func (e *Engine) SHas(key string, item string) (bool, error) {
if err != nil {
return false, err
}
return s.Has(item), nil
return s.Contains(item), nil
}

// SCard
Expand All @@ -646,7 +652,7 @@ func (e *Engine) SCard(key string) (int, error) {
if err != nil {
return 0, err
}
return s.Len(), nil
return s.Cardinality(), nil
}

// SMembers
Expand All @@ -659,70 +665,49 @@ func (e *Engine) SMembers(key string) ([]string, error) {
}

// SUnion
func (e *Engine) SUnion(key1, key2, dest string) error {
s1, err := e.fetchSet(key1)
if err != nil {
return err
}
s2, err := e.fetchSet(key2)
if err != nil {
return err
}
e.encode(NewCodec(OpSUnion).Str(key1).Str(key2).Str(dest))

if key1 == dest {
s1.Union(s2)
} else if key2 == dest {
s2.Union(s1)
} else {
e.m.Set(dest, s1.Clone().Union(s2))
func (e *Engine) SUnion(dstKey string, srcKeys ...string) error {
s := structx.NewSet[string]()
for _, key := range srcKeys {
ts, err := e.fetchSet(key)
if err != nil {
return err
}
s.Union(ts)
}
e.encode(NewCodec(OpSUnion).Str(dstKey).StrSlice(srcKeys))
e.m.Set(dstKey, s)

return nil
}

// SInter
func (e *Engine) SInter(key1, key2, dest string) error {
s1, err := e.fetchSet(key1)
if err != nil {
return err
}
s2, err := e.fetchSet(key2)
if err != nil {
return err
}
e.encode(NewCodec(OpSInter).Str(key1).Str(key2).Str(dest))

if key1 == dest {
s1.Intersect(s2)
} else if key2 == dest {
s2.Intersect(s1)
} else {
e.m.Set(dest, s1.Clone().Intersect(s2))
func (e *Engine) SInter(dstKey string, srcKeys ...string) error {
s := structx.NewSet[string]()
for _, key := range srcKeys {
ts, err := e.fetchSet(key)
if err != nil {
return err
}
s.Intersect(ts)
}
e.encode(NewCodec(OpSInter).Str(dstKey).StrSlice(srcKeys))
e.m.Set(dstKey, s)

return nil
}

// SDiff
func (e *Engine) SDiff(key1, key2, dest string) error {
s1, err := e.fetchSet(key1)
if err != nil {
return err
}
s2, err := e.fetchSet(key2)
if err != nil {
return err
}
e.encode(NewCodec(OpSDiff).Str(key1).Str(key2).Str(dest))

if key1 == dest {
s1.Difference(s2)
} else if key2 == dest {
s2.Difference(s1)
} else {
e.m.Set(dest, s1.Clone().Difference(s2))
func (e *Engine) SDiff(dstKey string, srcKeys ...string) error {
s := structx.NewSet[string]()
for _, key := range srcKeys {
ts, err := e.fetchSet(key)
if err != nil {
return err
}
s.Difference(ts)
}
e.encode(NewCodec(OpSDiff).Str(dstKey).StrSlice(srcKeys))
e.m.Set(dstKey, s)

return nil
}
Expand Down Expand Up @@ -826,70 +811,49 @@ func (e *Engine) BitFlip(key string, offset uint32) error {
}

// BitOr
func (e *Engine) BitOr(key1, key2, dest string) error {
bm1, err := e.fetchBitMap(key1)
if err != nil {
return err
}
bm2, err := e.fetchBitMap(key2)
if err != nil {
return err
}
e.encode(NewCodec(OpBitOr).Str(key1).Str(key2).Str(dest))

if key1 == dest {
bm1.Or(bm2)
} else if key2 == dest {
bm2.Or(bm1)
} else {
e.m.Set(dest, bm1.Clone().Or(bm2))
func (e *Engine) BitOr(dstKey string, srcKeys ...string) error {
bm := structx.NewBitmap()
for _, key := range srcKeys {
tbm, err := e.fetchBitMap(key)
if err != nil {
return err
}
bm.Or(tbm)
}
e.encode(NewCodec(OpBitOr).Str(dstKey).StrSlice(srcKeys))
e.m.Set(dstKey, bm)

return nil
}

// BitXor
func (e *Engine) BitXor(key1, key2, dest string) error {
bm1, err := e.fetchBitMap(key1)
if err != nil {
return err
}
bm2, err := e.fetchBitMap(key2)
if err != nil {
return err
}
e.encode(NewCodec(OpBitXor).Str(key1).Str(key2).Str(dest))

if key1 == dest {
bm1.Xor(bm2)
} else if key2 == dest {
bm2.Xor(bm1)
} else {
e.m.Set(dest, bm1.Clone().Xor(bm2))
func (e *Engine) BitXor(dstKey string, srcKeys ...string) error {
bm := structx.NewBitmap()
for _, key := range srcKeys {
tbm, err := e.fetchBitMap(key)
if err != nil {
return err
}
bm.Xor(tbm)
}
e.encode(NewCodec(OpBitXor).Str(dstKey).StrSlice(srcKeys))
e.m.Set(dstKey, bm)

return nil
}

// BitAnd
func (e *Engine) BitAnd(key1, key2, dest string) error {
bm1, err := e.fetchBitMap(key1)
if err != nil {
return err
}
bm2, err := e.fetchBitMap(key2)
if err != nil {
return err
}
e.encode(NewCodec(OpBitAnd).Str(key1).Str(key2).Str(dest))

if key1 == dest {
bm1.And(bm2)
} else if key2 == dest {
bm2.And(bm1)
} else {
e.m.Set(dest, bm1.Clone().And(bm2))
func (e *Engine) BitAnd(dstKey string, srcKeys ...string) error {
bm := structx.NewBitmap()
for _, key := range srcKeys {
tbm, err := e.fetchBitMap(key)
if err != nil {
return err
}
bm.And(tbm)
}
e.encode(NewCodec(OpBitAnd).Str(dstKey).StrSlice(srcKeys))
e.m.Set(dstKey, bm)

return nil
}
Expand Down Expand Up @@ -1042,8 +1006,8 @@ func (e *Engine) shrink() {
e.logInfo("rotom rewrite done")
}

// ForceShrink force to shrink db file.
func (e *Engine) ForceShrink() error {
// Shrink forced to shrink db file.
func (e *Engine) Shrink() error {
if e.tickers[2] == nil {
return base.ErrUnSupportOperation
}
Expand Down
Loading

0 comments on commit 01843c4

Please sign in to comment.