Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatible with redis protocol(RESP) #305

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,37 @@ import (
"fmt"
"github.com/ByteStorage/FlyDB/config"
base "github.com/ByteStorage/FlyDB/db/grpc"
"github.com/ByteStorage/FlyDB/lib/redis"
"os"
"sync"
)

func StartServer() {
options := config.DefaultOptions
options.FIOType = config.MmapIOType
service, err := base.NewService(options, config.DefaultAddr)
if err != nil {
fmt.Println("flydb start error: ", err)
return
}
service.StartGrpcServer()
var wg sync.WaitGroup

// start flydb server
wg.Add(1)
go func() {
defer wg.Done()
options := config.DefaultOptions
options.FIOType = config.MmapIOType
service, err := base.NewService(options, config.DefaultAddr)
if err != nil {
fmt.Println("flydb start error: ", err)
return
}
service.StartGrpcServer()
}()

// start flydb-redis server
wg.Add(1)
go func() {
defer wg.Done()
redis.StartRedisServer()
}()

// wait for signal
wg.Wait()
}

func CleanServer() {
Expand Down
13 changes: 12 additions & 1 deletion config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"github.com/ByteStorage/FlyDB/lib/wal"
"os"
"path/filepath"
)

// Options is a comprehensive configuration struct that
Expand Down Expand Up @@ -109,7 +110,8 @@ const (
)

const (
DefaultAddr = "127.0.0.1:8999"
DefaultAddr = "127.0.0.1:8999"
DefaultRedisAddr = "127.0.0.1:8998"
)

var DefaultOptions = Options{
Expand Down Expand Up @@ -140,3 +142,12 @@ var DefaultDbMemoryOptions = DbMemoryOptions{
ColumnName: "default",
Wal: nil,
}

// Redis
var (
RedisStringDirPath = filepath.Join(os.TempDir(), "flydb/redis/string")
RedisHashDirPath = filepath.Join(os.TempDir(), "flydb/redis/hash")
RedisListDirPath = filepath.Join(os.TempDir(), "flydb/redis/list")
RedisSetDirPath = filepath.Join(os.TempDir(), "flydb/redis/set")
RedisZSetDirPath = filepath.Join(os.TempDir(), "flydb/redis/zset")
)
5 changes: 3 additions & 2 deletions db/grpc/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"log"
"net"
"os"
"os/signal"
Expand Down Expand Up @@ -102,7 +103,7 @@ func (s *base) StartGrpcServer() {
}
break
}
fmt.Println("flydb start success on ", s.addr)
log.Println("FlyDB Server Start Success On: ", s.addr)
// graceful shutdown
signal.Notify(s.sig, syscall.SIGINT, syscall.SIGKILL)

Expand All @@ -114,7 +115,7 @@ func (s *base) StartGrpcServer() {
return
}
}
fmt.Println("flydb stop success on ", s.addr)
log.Println("FlyDB Server Stop Success On: ", s.addr)
}

func (s *base) StopGrpcServer() {
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ require (
github.com/spaolacci/murmur3 v1.1.0
github.com/stretchr/testify v1.8.2
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c
github.com/tidwall/redcon v1.6.2
go.etcd.io/bbolt v1.3.7
go.uber.org/zap v1.24.0
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4
golang.org/x/net v0.8.0
golang.org/x/sys v0.6.0
google.golang.org/grpc v1.55.0
google.golang.org/protobuf v1.31.0
)
Expand All @@ -47,13 +50,12 @@ require (
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/panjf2000/ants v1.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/tidwall/btree v1.1.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
github.com/panjf2000/ants v1.3.0 h1:8pQ+8leaLc9lys2viEEr8md0U4RN6uOSUCE9bOYjQ9M=
github.com/panjf2000/ants v1.3.0/go.mod h1:AaACblRPzq35m1g3enqYcxspbbiOJJYaxU2wMpm1cXY=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -190,6 +188,12 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c h1:g+WoO5jjkqGAzHWCjJB1zZfXPIAaDpzXIEJ0eS6B5Ok=
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c/go.mod h1:ahpPrc7HpcfEWDQRZEmnXMzHY03mLDYMCxeDzy46i+8=
github.com/tidwall/btree v1.1.0 h1:5P+9WU8ui5uhmcg3SoPyTwoI0mVyZ1nps7YQzTZFkYM=
github.com/tidwall/btree v1.1.0/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/redcon v1.6.2 h1:5qfvrrybgtO85jnhSravmkZyC0D+7WstbfCs3MmPhow=
github.com/tidwall/redcon v1.6.2/go.mod h1:p5Wbsgeyi2VSTBWOcA5vRXrOb9arFTcU2+ZzFjqV75Y=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
go.etcd.io/bbolt v1.3.7 h1:j+zJOnnEjF/kyHlDDgGnVL/AIqIJPq8UoB2GSNfkUfQ=
go.etcd.io/bbolt v1.3.7/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
Expand Down
104 changes: 104 additions & 0 deletions lib/redis/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package redis

import (
"fmt"
_const "github.com/ByteStorage/FlyDB/lib/const"
"strings"

"github.com/tidwall/redcon"
)

type FlyDBClient struct {
DB map[int]interface{}
Server *FlyDBServer
}

func NewWrongNumberOfArgsError(cmd string) error {
return fmt.Errorf("ERR wrong number of arguments for '%s' command", cmd)
}

type CmdHandler func(cli *FlyDBClient, args [][]byte) (interface{}, error)

// FlyDBSupportCommands is the map of all supported redis commands
var FlyDBSupportCommands = map[string]CmdHandler{
// string
"use-str": UseString,
"set": Set,
"get": Get,
"del": Del,
"getset": GetSet,
"append": Append,
"strlen": Strlen,
"incr": Incr,
"decr": Decr,
"incrby": IncrBy,
"decrby": DecrBy,
"keys": Keys,
"exists": Exists,
"expire": Expire,
"persist": Persist,
"ttl": TTL,
"size": Size,

// hash
"use-hash": UseHash,
"hset": HSet,
"hget": HGet,
"hdel": HDel,
"hdelall": HDelAll,
"hexists": HExists,
"hexpire": HExpire,
"hlen": HLen,
"hupdate": HUpdate,
"hkeys": HKeys,
"hstrlen": HStrlen,
"hmove": HMove,
"hsize": HSize,
"httl": HTTL,
"hincrby": HIncrBy,
"hdecrby": HDecrBy,

// list
"use-list": UseList,
"lpush": LPush,
"rpush": RPush,
"lpop": LPop,
"rpop": RPop,
"lrange": LRange,
"llen": LLen,
"lindex": LIndex,
"lset": LSet,
"lrem": LRem,
"ltrim": LTrim,
"lkeys": LKeys,
"lsize": LSize,
}

// ClientCommands is the handler for all redis commands
func ClientCommands(conn redcon.Conn, cmd redcon.Command) {
command := strings.ToLower(string(cmd.Args[0]))
cmdFunc, ok := FlyDBSupportCommands[command]
if !ok {
conn.WriteError("Err unsupported command: '" + command + "'")
return
}

cli, _ := conn.Context().(*FlyDBClient)
switch command {
case "ping":
conn.WriteString("PONG")
case "quit":
_ = conn.Close()
default:
res, err := cmdFunc(cli, cmd.Args[1:])
if err != nil {
if err == _const.ErrKeyNotFound {
conn.WriteNull()
} else {
conn.WriteError(err.Error())
}
return
}
conn.WriteAny(res)
}
}
Loading
Loading