Skip to content

Commit

Permalink
refact list with quickList
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Apr 2, 2024
1 parent 83a2bea commit c11231f
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 113 deletions.
32 changes: 0 additions & 32 deletions bench_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var cmdTable = []Cmd{
switch tp {
case TypeList:
ls := structx.NewList()
if err := ls.UnmarshalJSON(val); err != nil {
if err := ls.Unmarshal(val); err != nil {
return err
}
db.cm.Set(key, ls)
Expand Down Expand Up @@ -923,7 +923,7 @@ func (db *DB) Shrink() error {
data, err = item.MarshalBinary()
case List:
types = TypeList
data, err = item.MarshalJSON()
data = item.Marshal()
case Set:
types = TypeSet
data, err = item.MarshalJSON()
Expand Down
40 changes: 35 additions & 5 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/xgzlucario/rotom/codeman"
"github.com/xgzlucario/rotom/structx"
)

var (
Expand Down Expand Up @@ -215,9 +216,10 @@ func TestList(t *testing.T) {
assert := assert.New(t)
db, err := createDB()
assert.Nil(err)
structx.SetEachNodeMaxSize(512)

for i := 0; i < 10000; i++ {
key := "list" + strconv.Itoa(i/100)
for i := 0; i < 5000; i++ {
key := "list" + strconv.Itoa(i/1000)
val := randString()

if i%2 == 0 {
Expand All @@ -231,7 +233,7 @@ func TestList(t *testing.T) {
assert.Equal(res, val)
}

if i > 8000 {
if i > 4000 {
if i%2 == 0 {
res, err := db.LRPop(key)
assert.Nil(err)
Expand Down Expand Up @@ -310,6 +312,34 @@ func TestList(t *testing.T) {
db.Close()
_, err = Open(db.GetOptions())
assert.Nil(err)

t.Run("lpop", func(t *testing.T) {
db, err := createDB()
assert.Nil(err)

for i := 0; i < 1000; i++ {
db.LRPush("ls", strconv.Itoa(i))
}
for i := 0; i < 1000; i++ {
v, err := db.LLPop("ls")
assert.Equal(v, strconv.Itoa(i))
assert.Nil(err)
}
})

t.Run("rpop", func(t *testing.T) {
db, err := createDB()
assert.Nil(err)

for i := 0; i < 1000; i++ {
db.LLPush("ls", strconv.Itoa(i))
}
for i := 0; i < 1000; i++ {
v, err := db.LRPop("ls")
assert.Equal(v, strconv.Itoa(i))
assert.Nil(err)
}
})
}

func TestSet(t *testing.T) {
Expand Down Expand Up @@ -761,8 +791,8 @@ func TestShrink(t *testing.T) {

go func() {
time.Sleep(time.Millisecond)
err = db.Shrink()
assert.Equal(err, ErrShrinkRunning)
err1 := db.Shrink()
assert.Equal(err1, ErrShrinkRunning)
}()

err = db.Shrink()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/cockroachdb/swiss v0.0.0-20240303172742-c161743eb608
github.com/deckarep/golang-set/v2 v2.6.0
github.com/gofrs/flock v0.8.1
github.com/klauspost/compress v1.17.7
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/robfig/cron/v3 v3.0.1
github.com/rosedblabs/wal v1.3.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
Expand Down
Loading

0 comments on commit c11231f

Please sign in to comment.