Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Nov 4, 2023
1 parent 188feb5 commit a7f0fec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
16 changes: 0 additions & 16 deletions examples/rotom/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package main

import (
"fmt"
"net/http"
_ "net/http/pprof"
"strconv"

"github.com/xgzlucario/rotom"
)
Expand All @@ -21,20 +19,6 @@ func main() {
panic(err)
}

key := "key"

for j := 0; j < 20; j++ {
db.RPush(key, strconv.Itoa(j))
}
for j := 0; j < 20; j++ {
res, err := db.LPop(key)
fmt.Println(res, err)
}

// LLen
num, err := db.LLen(key)
fmt.Println(num, err)

// run for web server
if err := db.Listen("0.0.0.0:7676"); err != nil {
panic(err)
Expand Down
10 changes: 1 addition & 9 deletions rotom.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,12 +1076,7 @@ func (e *Engine) load() error {

// rewrite write data to the file.
func (e *Engine) shrink() {
if e.SyncPolicy == base.Never {
return
}

var _type Type
// Marshal any
data, err := e.m.MarshalBytesFunc(func(key string, v any, i int64) {
switch v := v.(type) {
case Map:
Expand All @@ -1097,7 +1092,6 @@ func (e *Engine) shrink() {
default:
panic(fmt.Errorf("%w: %d", base.ErrUnSupportDataType, v))

Check warning on line 1093 in rotom.go

View check run for this annotation

Codecov / codecov/patch

rotom.go#L1093

Added line #L1093 was not covered by tests
}

// SetTx
if cd, err := NewCodec(OpSetTx).Type(_type).Str(key).Int(i / timeCarry).Any(v); err == nil {
e.rwbuf.Write(cd.B)
Expand All @@ -1124,10 +1118,8 @@ func (e *Engine) shrink() {
}

// Shrink forced to shrink db file.
// Warning: will panic if SyncPolicy is never.
func (e *Engine) Shrink() error {
if e.tickers[2] == nil {
return base.ErrUnSupportOperation
}
return e.tickers[2].ForceFunc()
}

Expand Down
13 changes: 12 additions & 1 deletion rotom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func newDBInstance() (*Engine, *Client, error) {

// listen
go db.Listen(addr)
time.Sleep(time.Second / 20)
time.Sleep(time.Millisecond)

cli, err := NewClient(addr)
if err != nil {
Expand Down Expand Up @@ -385,6 +385,17 @@ func TestList(t *testing.T) {
assert.Equal(n, 0)
assert.ErrorContains(err, base.ErrWrongType.Error())

cli.RPush("list", "1")
cli.RPop("list")
// empty list
res, err = cli.LPop("list")
assert.Equal(res, "")
assert.Equal(err, base.ErrEmptyList)

res, err = cli.RPop("list")
assert.Equal(res, "")
assert.Equal(err, base.ErrEmptyList)

db.Close()
}

Expand Down

0 comments on commit a7f0fec

Please sign in to comment.