Skip to content

Commit

Permalink
add: AeLoop add readQueryFromClient
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Jun 8, 2024
1 parent f579396 commit 9b122e0
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 174 deletions.
350 changes: 296 additions & 54 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ae.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type AeLoop struct {
stop bool
}

var fe2ep [3]uint32 = [3]uint32{0, unix.EPOLLIN, unix.EPOLLOUT}
var fe2ep = [3]uint32{0, unix.EPOLLIN, unix.EPOLLOUT}

func getFeKey(fd int, mask FeType) int {
if mask == AE_READABLE {
Expand Down
13 changes: 12 additions & 1 deletion aof.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"io"
"os"

"github.com/tidwall/mmap"
)

const (
Expand Down Expand Up @@ -44,8 +46,17 @@ func (aof *Aof) Flush() error {
}

func (aof *Aof) Read(fn func(value Value)) error {
// Read file data by mmap.
data, err := mmap.Open(aof.filePath, false)
if len(data) == 0 {
return nil
}
if err != nil {
return err
}

// Iterate over the records in the file, applying the function to each.
reader := NewResp(aof.file)
reader := NewResp(data)
for {
value, err := reader.Read()
if err != nil {
Expand Down
Binary file added bench.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/rs/zerolog v1.33.0
github.com/sakeven/RbTree v0.0.0-20240321014605-9899538dc980
github.com/stretchr/testify v1.9.0
github.com/tidwall/mmap v0.3.0
github.com/xgzlucario/GigaCache v0.0.0-20240605031700-e88a04a9dd84
github.com/xgzlucario/quicklist v0.0.0-20240530174658-6f1a884f579b
golang.org/x/sys v0.21.0
Expand All @@ -19,6 +20,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80N
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/edsrzf/mmap-go v1.1.0 h1:6EUwBLQ/Mcr1EYLE4Tn1VdW1A4ckqCQWZBw8Hr0kjpQ=
github.com/edsrzf/mmap-go v1.1.0/go.mod h1:19H/e8pUPLicwkyNgOykDXkJ9F0MHE+Z52B8EIth78Q=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
Expand Down Expand Up @@ -47,12 +49,15 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/mmap v0.3.0 h1:XXt1YsiXCF5/UAu3pLbu6g7iulJ9jsbs6vt7UpiV0sY=
github.com/tidwall/mmap v0.3.0/go.mod h1:2/dNzF5zA+te/JVHfrqNLcRkb8LjdH3c80vYHFQEZRk=
github.com/xgzlucario/GigaCache v0.0.0-20240605031700-e88a04a9dd84 h1:YZQ7pvAASgoW0FsOF4pXkzgdWJQSS7j4JsOaU8Oc724=
github.com/xgzlucario/GigaCache v0.0.0-20240605031700-e88a04a9dd84/go.mod h1:sPwGPAuvd9WdiONTmusXGNocqcY5L/J7+st1upAMlX8=
github.com/xgzlucario/quicklist v0.0.0-20240530174658-6f1a884f579b h1:C/+nN/kFJ6yrmEhIu+5Ra2jx/W8w+Ayu8pTiZfuU5Xc=
github.com/xgzlucario/quicklist v0.0.0-20240530174658-6f1a884f579b/go.mod h1:1ZgyZNk91XIllYdOPpwP+9L2RCw6QGSy6alTYF+Z0iU=
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 h1:LoYXNGAShUG3m/ehNk4iFctuhGX/+R1ZpfJ4/ia80JM=
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
32 changes: 16 additions & 16 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
)

func pingCommand(_ []Value) Value {
return Value{typ: STRING, str: "PONG"}
return Value{typ: STRING, raw: []byte("PONG")}
}

func setCommand(args []Value) Value {
key := args[0].bulk
value := args[1].bulk
db.strs.Set(string(key), value)
key := args[0].ToString()
value := args[1].ToBytes()
db.strs.Set(key, value)
return ValueOK
}

func getCommand(args []Value) Value {
key := string(args[0].bulk)
key := args[0].ToString()

value, _, ok := db.strs.Get(key)
if ok {
Expand All @@ -33,7 +33,7 @@ func getCommand(args []Value) Value {
}

func hsetCommand(args []Value) Value {
hash := string(args[0].bulk)
hash := args[0].ToString()
args = args[1:]

// check arguments number
Expand All @@ -48,18 +48,18 @@ func hsetCommand(args []Value) Value {

var newFields int
for i := 0; i < len(args); i += 2 {
key := args[i].bulk
value := args[i+1].bulk
if hmap.Set(string(key), value) {
key := args[i].ToString()
value := args[i+1].ToBytes()
if hmap.Set(key, value) {
newFields++
}
}
return newIntegerValue(newFields)
}

func hgetCommand(args []Value) Value {
hash := string(args[0].bulk)
key := string(args[1].bulk)
hash := args[0].ToString()
key := args[1].ToString()

hmap, err := fetchMap(hash)
if err != nil {
Expand All @@ -73,7 +73,7 @@ func hgetCommand(args []Value) Value {
}

func hdelCommand(args []Value) Value {
hash := string(args[0].bulk)
hash := args[0].ToString()
keys := args[1:]

hmap, err := fetchMap(hash)
Expand All @@ -82,15 +82,15 @@ func hdelCommand(args []Value) Value {
}
var success int
for _, v := range keys {
if hmap.Remove(string(v.bulk)) {
if hmap.Remove(v.ToString()) {
success++
}
}
return newIntegerValue(success)
}

func hgetallCommand(args []Value) Value {
hash := string(args[0].bulk)
hash := args[0].ToString()

hmap, err := fetchMap(hash)
if err != nil {
Expand All @@ -99,8 +99,8 @@ func hgetallCommand(args []Value) Value {

res := make([]Value, 0, 8)
hmap.Scan(func(key, value []byte) {
res = append(res, Value{typ: BULK, bulk: key})
res = append(res, Value{typ: BULK, bulk: value})
res = append(res, newBulkValue(key))
res = append(res, newBulkValue(value))
})
return newArrayValue(res)
}
Expand Down
4 changes: 2 additions & 2 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func TestHandler(t *testing.T) {
assert := assert.New(t)

go startup()
time.Sleep(time.Second / 3)
time.Sleep(time.Second / 5)

// wait for client starup
rdb := redis.NewClient(&redis.Options{
Addr: ":20082",
})
time.Sleep(time.Second / 3)
time.Sleep(time.Second / 5)

t.Run("ping", func(t *testing.T) {
res, _ := rdb.Ping(ctx).Result()
Expand Down
Loading

0 comments on commit 9b122e0

Please sign in to comment.