Skip to content

Commit

Permalink
feat: optimize internal resp parser
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Dec 1, 2024
1 parent a9aa60e commit 387ede1
Show file tree
Hide file tree
Showing 26 changed files with 493 additions and 903 deletions.
32 changes: 13 additions & 19 deletions aof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package main

import (
"bytes"
"github.com/xgzlucario/rotom/internal/resp"
"github.com/tidwall/redcon"
"io"
"os"

"github.com/tidwall/mmap"
)

// Aof manages an append-only file system for storing data.
Expand Down Expand Up @@ -39,28 +37,24 @@ func (a *Aof) Flush() error {
return a.file.Sync()
}

func (a *Aof) Read(fn func(args []resp.RESP)) error {
// Read file data by mmap.
data, err := mmap.MapFile(a.file, false)
if len(data) == 0 {
return nil
}
func (a *Aof) Read(fn func(args []redcon.RESP)) error {
rd := redcon.NewReader(a.file)
cmds, err := rd.ReadCommands()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
respBuf := make([]redcon.RESP, 8)

// Iterate over the records in the file, applying the function to each.
reader := resp.NewReader(data)
argsBuf := make([]resp.RESP, 8)
for {
args, _, err := reader.ReadNextCommand(argsBuf)
if err != nil {
if err == io.EOF {
break
}
return err
for _, cmd := range cmds {
respBuf = respBuf[:0]
for _, arg := range cmd.Args {
respBuf = append(respBuf, redcon.RESP{Data: arg})
}
fn(args)
fn(respBuf)
}
return nil
}
14 changes: 7 additions & 7 deletions aof_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/xgzlucario/rotom/internal/resp"
"github.com/tidwall/redcon"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -24,27 +24,27 @@ func TestAof(t *testing.T) {
t.Run("read", func(t *testing.T) {
aof, err := NewAof("test.aof")
ast.Nil(err)
_ = aof.Read(func(args []resp.RESP) {
_ = aof.Read(func(args []redcon.RESP) {
// SET foo bar
ast.Equal(len(args), 3)
ast.Equal(args[0].ToString(), "set")
ast.Equal(args[1].ToString(), "foo")
ast.Equal(args[2].ToString(), "bar")
ast.Equal(args[0].String(), "set")
ast.Equal(args[1].String(), "foo")
ast.Equal(args[2].String(), "bar")
})
defer aof.Close()
})

t.Run("read-err-content", func(t *testing.T) {
aof, _ := NewAof("LICENSE")
err := aof.Read(func(args []resp.RESP) {})
err := aof.Read(func(args []redcon.RESP) {})
ast.NotNil(err)
})

t.Run("empty-aof", func(t *testing.T) {
aof, _ := NewAof("not-exist.aof")
defer aof.Close()

_ = aof.Read(func(args []resp.RESP) {
_ = aof.Read(func(args []redcon.RESP) {
panic("should not call")
})
})
Expand Down
2 changes: 1 addition & 1 deletion bench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ OUTPUT_FILE="output/$TEST_NAME"

COMMANDS="set,get,incr,lpush,rpush,hset,sadd,zadd"

PIPELINES=(1 100 1000)
PIPELINES=(1 10 100)

mkdir -p output

Expand Down
Loading

0 comments on commit 387ede1

Please sign in to comment.