-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compatible with redis protocol(RESP)
- Loading branch information
1 parent
4a36750
commit e2250d7
Showing
8 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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) | ||
|
||
var FlyDBSupportCommands = map[string]CmdHandler{ | ||
// string | ||
"use-string": UseString, | ||
"set": Set, | ||
"get": Get, | ||
|
||
// hash | ||
"use-hash": UseHash, | ||
"hset": HSet, | ||
"hget": HGet, | ||
} | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/ByteStorage/FlyDB/config" | ||
"github.com/ByteStorage/FlyDB/lib/redis" | ||
flydb_stru "github.com/ByteStorage/FlyDB/structure" | ||
"github.com/tidwall/redcon" | ||
) | ||
|
||
func main() { | ||
// 打开 Redis 数据结构服务 | ||
stringStructure, err := flydb_stru.NewStringStructure(config.DefaultOptions) | ||
if err != nil { | ||
panic(err) | ||
} | ||
hashStructure, err := flydb_stru.NewHashStructure(config.DefaultOptions) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// 初始化 FlyDBServer | ||
flydbServer := &redis.FlyDBServer{ | ||
Dbs: make(map[int]interface{}), | ||
} | ||
flydbServer.Dbs[0] = stringStructure | ||
flydbServer.Dbs[1] = hashStructure | ||
|
||
// 初始化一个 Redis 服务端 | ||
flydbServer.Server = redcon.NewServer(config.DefaultAddr, | ||
redis.ClientCommands, flydbServer.Accept, nil) | ||
flydbServer.Listen() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package redis | ||
|
||
import ( | ||
"github.com/ByteStorage/FlyDB/structure" | ||
"github.com/tidwall/redcon" | ||
) | ||
|
||
func HSet(cli *FlyDBClient, args [][]byte) (interface{}, error) { | ||
if len(args) != 3 { | ||
return nil, NewWrongNumberOfArgsError("hset") | ||
} | ||
|
||
var ok = 0 | ||
key, field, value := args[0], args[1], args[2] | ||
res, err := cli.DB[1].(*structure.HashStructure).HSet(string(key), field, value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if res { | ||
ok = 1 | ||
} | ||
return redcon.SimpleInt(ok), nil | ||
} | ||
|
||
func HGet(cli *FlyDBClient, args [][]byte) (interface{}, error) { | ||
if len(args) != 2 { | ||
return nil, NewWrongNumberOfArgsError("hget") | ||
} | ||
|
||
value, err := cli.DB[1].(*structure.HashStructure).HGet(string(args[0]), args[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return value, nil | ||
} | ||
|
||
func UseHash(cli *FlyDBClient, args [][]byte) (interface{}, error) { | ||
if len(args) != 0 { | ||
return nil, NewWrongNumberOfArgsError("use-hash") | ||
} | ||
|
||
cli.DB[0].(*structure.StringStructure).Close() | ||
return redcon.SimpleString("OK"), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package redis | ||
|
||
import ( | ||
"github.com/ByteStorage/FlyDB/structure" | ||
"log" | ||
"sync" | ||
|
||
"github.com/tidwall/redcon" | ||
) | ||
|
||
type FlyDBServer struct { | ||
Dbs map[int]interface{} | ||
Server *redcon.Server | ||
lock sync.RWMutex | ||
} | ||
|
||
func (svr *FlyDBServer) Listen() { | ||
log.Println("FlyDB-Redis Server running, ready to accept connections...") | ||
_ = svr.Server.ListenAndServe() | ||
} | ||
|
||
func (svr *FlyDBServer) Accept(conn redcon.Conn) bool { | ||
svr.lock.Lock() | ||
defer svr.lock.Unlock() | ||
|
||
cli := new(FlyDBClient) | ||
cli.Server = svr | ||
cli.DB = svr.Dbs | ||
conn.SetContext(cli) | ||
return true | ||
} | ||
|
||
func (svr *FlyDBServer) Close() error { | ||
if db, ok := svr.Dbs[0].(*structure.StringStructure); ok { | ||
db.Clean() | ||
} else if dbh, ok := svr.Dbs[1].(*structure.HashStructure); ok { | ||
dbh.Clean() | ||
} | ||
return svr.Server.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package redis | ||
|
||
import ( | ||
"github.com/ByteStorage/FlyDB/structure" | ||
"github.com/tidwall/redcon" | ||
) | ||
|
||
func Set(cli *FlyDBClient, args [][]byte) (interface{}, error) { | ||
if len(args) != 2 { | ||
return nil, NewWrongNumberOfArgsError("set") | ||
} | ||
|
||
key, value := args[0], args[1] | ||
if err := cli.DB[0].(*structure.StringStructure).Set(string(key), string(value), 0); err != nil { | ||
return nil, err | ||
} | ||
return redcon.SimpleString("OK"), nil | ||
} | ||
|
||
func Get(cli *FlyDBClient, args [][]byte) (interface{}, error) { | ||
if len(args) != 1 { | ||
return nil, NewWrongNumberOfArgsError("get") | ||
} | ||
|
||
value, err := cli.DB[0].(*structure.StringStructure).Get(string(args[0])) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return value, nil | ||
} | ||
|
||
func UseString(cli *FlyDBClient, args [][]byte) (interface{}, error) { | ||
if len(args) != 0 { | ||
return nil, NewWrongNumberOfArgsError("use-string") | ||
} | ||
|
||
cli.DB[1].(*structure.HashStructure).Stop() | ||
Check failure on line 37 in lib/redis/string.go GitHub Actions / build (1.18)
|
||
|
||
return redcon.SimpleString("OK"), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters