Skip to content

Commit

Permalink
code refact
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Jun 2, 2024
1 parent 8971f9a commit a57ab6a
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 217 deletions.
24 changes: 5 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
run:
rm -f rotom.db
go run example/*.go

run-db:
go run example/*.go
go run .

run-gc:
rm -f rotom.db
GODEBUG=gctrace=1 go run main.go
GODEBUG=gctrace=1 go run .

test-cover:
go test -race -v \
-coverpkg=./... \
-coverprofile=coverage.txt -covermode=atomic
go test -race -v -coverprofile=coverage.txt -covermode=atomic
go tool cover -html=coverage.txt -o coverage.html
make clean

pprof:
go tool pprof -http=:18081 "http://localhost:6060/debug/pprof/profile?seconds=60"
go tool pprof -http=:18081 "http://localhost:6060/debug/pprof/profile?seconds=30"

heap:
go tool pprof http://localhost:6060/debug/pprof/heap

run-bench:
go run benchmark/*.go
make clean

clean:
rm -f coverage.txt
rm -rf tmp-*
go tool pprof http://localhost:6060/debug/pprof/heap
43 changes: 0 additions & 43 deletions bench_test.go

This file was deleted.

16 changes: 6 additions & 10 deletions epoll.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"sync"
"syscall"

"github.com/cockroachdb/swiss"
"golang.org/x/sys/unix"
)

type epoll struct {
fd int
connections *swiss.Map[int, net.Conn]
connections map[int]net.Conn
lock *sync.RWMutex
}

Expand All @@ -26,7 +25,7 @@ func MkEpoll() (*epoll, error) {
return &epoll{
fd: fd,
lock: &sync.RWMutex{},
connections: swiss.New[int, net.Conn](100),
connections: make(map[int]net.Conn),
}, nil
}

Expand All @@ -39,7 +38,7 @@ func (e *epoll) Add(conn net.Conn) error {
}

e.lock.Lock()
e.connections.Put(fd, conn)
e.connections[fd] = conn
e.lock.Unlock()

return nil
Expand All @@ -53,15 +52,14 @@ func (e *epoll) Remove(conn net.Conn) error {
}

e.lock.Lock()
e.connections.Delete(fd)
delete(e.connections, fd)
e.lock.Unlock()

return nil
}

func (e *epoll) Wait() ([]net.Conn, error) {
events := make([]unix.EpollEvent, 100)

retry:
n, err := unix.EpollWait(e.fd, events, 0)
if err != nil {
Expand All @@ -78,10 +76,8 @@ retry:
e.lock.RLock()
defer e.lock.RUnlock()
for i := 0; i < n; i++ {
conn, ok := e.connections.Get(int(events[i].Fd))
if ok {
connections = append(connections, conn)
}
conn := e.connections[int(events[i].Fd)]
connections = append(connections, conn)
}

return connections, nil
Expand Down
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ go 1.22

require (
github.com/RoaringBitmap/roaring v1.9.3
github.com/bytedance/sonic v1.11.8
github.com/cockroachdb/swiss v0.0.0-20240303172742-c161743eb608
github.com/deckarep/golang-set/v2 v2.6.0
github.com/sakeven/RbTree v0.0.0-20240321014605-9899538dc980
github.com/sourcegraph/conc v0.3.0
Expand All @@ -16,15 +14,9 @@ require (

require (
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/mschoch/smat v0.2.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc // indirect
)
29 changes: 0 additions & 29 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
github.com/RoaringBitmap/roaring v1.9.3 h1:t4EbC5qQwnisr5PrP9nt0IRhRTb9gMUgQF4t4S2OByM=
github.com/RoaringBitmap/roaring v1.9.3/go.mod h1:6AXUsoIEzDTFFQCe1RbGA6uFONMhvejWj5rqITANK90=
github.com/aclements/go-perfevent v0.0.0-20240301234650-f7843625020f h1:JjxwchlOepwsUWcQwD2mLUAGE9aCp0/ehy6yCHFBOvo=
github.com/aclements/go-perfevent v0.0.0-20240301234650-f7843625020f/go.mod h1:tMDTce/yLLN/SK8gMOxQfnyeMeCg8KGzp0D1cbECEeo=
github.com/bits-and-blooms/bitset v1.12.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE=
github.com/bits-and-blooms/bitset v1.13.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LRA=
github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/cockroachdb/swiss v0.0.0-20240303172742-c161743eb608 h1:GXGrLNSC5+LGrwVgtczB6JCITxB9WGaLv7XilPkBDvc=
github.com/cockroachdb/swiss v0.0.0-20240303172742-c161743eb608/go.mod h1:yBRu/cnL4ks9bgy4vAASdjIW+/xMlFwuHKqtmh3GZQg=
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.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
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=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/mschoch/smat v0.2.0 h1:8imxQsjDm8yFEAVBe7azKmKSgzSkZXDuKkSq9374khM=
github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -33,17 +17,10 @@ github.com/sakeven/RbTree v0.0.0-20240321014605-9899538dc980/go.mod h1:zwEumjdcK
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
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/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/xgzlucario/GigaCache v0.0.0-20240531152919-576765cef731 h1:frRQxMZFCPWfoiWau4bPcYmNDGNVPLqM9nqnsp6Uakg=
github.com/xgzlucario/GigaCache v0.0.0-20240531152919-576765cef731/go.mod h1:sPwGPAuvd9WdiONTmusXGNocqcY5L/J7+st1upAMlX8=
github.com/xgzlucario/quicklist v0.0.0-20240530174658-6f1a884f579b h1:C/+nN/kFJ6yrmEhIu+5Ra2jx/W8w+Ayu8pTiZfuU5Xc=
Expand All @@ -52,17 +29,11 @@ go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc h1:O9NuF4s+E/PvMIy+9IUZB9znFwUIXEWSstNjek6VpVg=
golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
26 changes: 13 additions & 13 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func setCommand(args []Value) Value {
seconds, _ := strconv.Atoi(b2s(args[i+3].bulk))
ttl = cache.GetNanoSec() + int64(seconds)*int64(time.Second)
} else {
return NewErrValue(ErrWrongArgs("set"))
return newErrValue(ErrWrongArgs("set"))
}
}
}
Expand All @@ -40,12 +40,12 @@ func getCommand(args []Value) Value {

value, _, ok := db.strs.Get(b2s(key))
if ok {
return NewBulkValue(value)
return newBulkValue(value)
}
// check extra maps
_, ok = db.extras[b2s(key)]
if ok {
return NewErrValue(ErrWrongType)
return newErrValue(ErrWrongType)
}
return ValueNull
}
Expand All @@ -57,7 +57,7 @@ func hsetCommand(args []Value) Value {

m, err := fetchMap(hash, true)
if err != nil {
return NewErrValue(err)
return newErrValue(err)
}
m.Set(key, value)
return ValueOK
Expand All @@ -69,13 +69,13 @@ func hgetCommand(args []Value) Value {

m, err := fetchMap(b2s(hash))
if err != nil {
return NewErrValue(ErrWrongType)
return newErrValue(ErrWrongType)
}
value, _, ok := m.Get(b2s(key))
if !ok {
return ValueNull
}
return NewBulkValue(value)
return newBulkValue(value)
}

func hdelCommand(args []Value) Value {
Expand All @@ -84,31 +84,31 @@ func hdelCommand(args []Value) Value {

m, err := fetchMap(b2s(hash))
if err != nil {
return NewErrValue(err)
return newErrValue(err)
}
var success int
for _, v := range keys {
if m.Remove(b2s(v.bulk)) {
success++
}
}
return NewIntegerValue(success)
return newIntegerValue(success)
}

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

m, err := fetchMap(b2s(hash))
if err != nil {
return NewErrValue(err)
return newErrValue(err)
}

res := Value{typ: ARRAY}
res := make([]Value, 0, 8)
m.Scan(func(key, value []byte) {
res.array = append(res.array, Value{typ: BULK, bulk: key})
res.array = append(res.array, Value{typ: BULK, bulk: value})
res = append(res, Value{typ: BULK, bulk: key})
res = append(res, Value{typ: BULK, bulk: value})
})
return res
return newArrayValue(res)
}

func fetchMap(key string, setnx ...bool) (Map, error) {
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package main

import (
"log"
"net/http"
_ "net/http/pprof"
"syscall"
)

func debug() {
go http.ListenAndServe(":6060", nil)
}

func main() {
var err error
config, err = LoadConfig("config.json")
Expand All @@ -15,6 +21,7 @@ func main() {
log.Panicf("init db error: %v\n", err)
}
setLimit()
debug()
server.RunServe()
}

Expand Down
10 changes: 5 additions & 5 deletions resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
INTEGER = ':'
BULK = '$'
ARRAY = '*'
NULL = 255
NULL = 0xff
)

var (
Expand Down Expand Up @@ -44,19 +44,19 @@ func NewResp(rd io.Reader) *Resp {
return &Resp{reader: bufio.NewReader(rd)}
}

func NewErrValue(err error) Value {
func newErrValue(err error) Value {
return Value{typ: ERROR, str: err.Error()}
}

func NewBulkValue(bulk []byte) Value {
func newBulkValue(bulk []byte) Value {
return Value{typ: BULK, bulk: bulk}
}

func NewIntegerValue(n int) Value {
func newIntegerValue(n int) Value {
return Value{typ: INTEGER, num: int64(n)}
}

func NewArrayValue(value []Value) Value {
func newArrayValue(value []Value) Value {
return Value{typ: ARRAY, array: value}
}

Expand Down
Loading

0 comments on commit a57ab6a

Please sign in to comment.