optimize code structure

This commit is contained in:
hdt3213
2022-04-11 15:49:15 +08:00
parent 374606f6f1
commit e378731a26
6 changed files with 28 additions and 20 deletions

View File

@@ -142,8 +142,8 @@ I suggest focusing on the following directories:
- set: a hash set based on map - set: a hash set based on map
- sortedset: a sorted set implements based on skiplist - sortedset: a sorted set implements based on skiplist
- database: the core of storage engine - database: the core of storage engine
- server.go: a standalone redis server, with multiple database - database.go: a standalone redis server, with multiple database
- db.go: data structure and base functions of single database - single_db.go: data structure and base functions of single database
- exec.go: the gateway of database - exec.go: the gateway of database
- router.go: the command table - router.go: the command table
- keys.go: handlers for keys commands - keys.go: handlers for keys commands

View File

@@ -132,7 +132,8 @@ MSET (10 keys): 65487.89 requests per second
- set 基于hash表的集合 - set 基于hash表的集合
- sortedset: 基于跳表实现的有序集合 - sortedset: 基于跳表实现的有序集合
- database: 存储引擎核心 - database: 存储引擎核心
- db.go: 单个 database 的数据结构和基本功能 - database.go: 支持多数据库的单机版 redis 服务实例
- single_db.go: 单个 database 的数据结构和基本功能
- router.go: 将命令路由给响应的处理函数 - router.go: 将命令路由给响应的处理函数
- keys.go: del、ttl、expire 等通用命令实现 - keys.go: del、ttl、expire 等通用命令实现
- string.go: get、set 等字符串命令实现 - string.go: get、set 等字符串命令实现

View File

@@ -87,8 +87,7 @@ func (mdb *MultiDB) Exec(c redis.Connection, cmdLine [][]byte) (result redis.Rep
return protocol.MakeErrReply("NOAUTH Authentication required") return protocol.MakeErrReply("NOAUTH Authentication required")
} }
// todo: merge special commands into router // special commands which cannot execute within transaction
// special commands
if cmdName == "subscribe" { if cmdName == "subscribe" {
if len(cmdLine) < 2 { if len(cmdLine) < 2 {
return protocol.MakeArgNumErrReply("subscribe") return protocol.MakeArgNumErrReply("subscribe")

View File

@@ -321,6 +321,5 @@ func init() {
RegisterCommand("Type", execType, readFirstKey, nil, 2) RegisterCommand("Type", execType, readFirstKey, nil, 2)
RegisterCommand("Rename", execRename, prepareRename, undoRename, 3) RegisterCommand("Rename", execRename, prepareRename, undoRename, 3)
RegisterCommand("RenameNx", execRenameNx, prepareRename, undoRename, 3) RegisterCommand("RenameNx", execRenameNx, prepareRename, undoRename, 3)
RegisterCommand("FlushDB", execFlushDB, noPrepare, nil, -1)
RegisterCommand("Keys", execKeys, noPrepare, nil, 2) RegisterCommand("Keys", execKeys, noPrepare, nil, 2)
} }

View File

@@ -80,6 +80,7 @@ func makeBasicDB() *DB {
// Exec executes command within one database // Exec executes command within one database
func (db *DB) Exec(c redis.Connection, cmdLine [][]byte) redis.Reply { func (db *DB) Exec(c redis.Connection, cmdLine [][]byte) redis.Reply {
// transaction control commands and other commands which cannot execute within transaction
cmdName := strings.ToLower(string(cmdLine[0])) cmdName := strings.ToLower(string(cmdLine[0]))
if cmdName == "multi" { if cmdName == "multi" {
if len(cmdLine) != 1 { if len(cmdLine) != 1 {
@@ -101,6 +102,14 @@ func (db *DB) Exec(c redis.Connection, cmdLine [][]byte) redis.Reply {
return protocol.MakeArgNumErrReply(cmdName) return protocol.MakeArgNumErrReply(cmdName)
} }
return Watch(db, c, cmdLine[1:]) return Watch(db, c, cmdLine[1:])
} else if cmdName == "flushdb" {
if !validateArity(1, cmdLine) {
return protocol.MakeArgNumErrReply(cmdName)
}
if c.InMultiState() {
return protocol.MakeErrReply("ERR command 'FlushDB' cannot be used in MULTI")
}
return execFlushDB(db, cmdLine[1:])
} }
if c != nil && c.InMultiState() { if c != nil && c.InMultiState() {
EnqueueCmd(c, cmdLine) EnqueueCmd(c, cmdLine)
@@ -129,6 +138,20 @@ func (db *DB) execNormalCommand(cmdLine [][]byte) redis.Reply {
return fun(db, cmdLine[1:]) return fun(db, cmdLine[1:])
} }
// execWithLock executes normal commands, invoker should provide locks
func (db *DB) execWithLock(cmdLine [][]byte) redis.Reply {
cmdName := strings.ToLower(string(cmdLine[0]))
cmd, ok := cmdTable[cmdName]
if !ok {
return protocol.MakeErrReply("ERR unknown command '" + cmdName + "'")
}
if !validateArity(cmd.arity, cmdLine) {
return protocol.MakeArgNumErrReply(cmdName)
}
fun := cmd.executor
return fun(db, cmdLine[1:])
}
func validateArity(arity int, cmdArgs [][]byte) bool { func validateArity(arity int, cmdArgs [][]byte) bool {
argNum := len(cmdArgs) argNum := len(cmdArgs)
if arity >= 0 { if arity >= 0 {

View File

@@ -164,20 +164,6 @@ func (db *DB) GetUndoLogs(cmdLine [][]byte) []CmdLine {
return undo(db, cmdLine[1:]) return undo(db, cmdLine[1:])
} }
// execWithLock executes normal commands, invoker should provide locks
func (db *DB) execWithLock(cmdLine [][]byte) redis.Reply {
cmdName := strings.ToLower(string(cmdLine[0]))
cmd, ok := cmdTable[cmdName]
if !ok {
return protocol.MakeErrReply("ERR unknown command '" + cmdName + "'")
}
if !validateArity(cmd.arity, cmdLine) {
return protocol.MakeArgNumErrReply(cmdName)
}
fun := cmd.executor
return fun(db, cmdLine[1:])
}
// GetRelatedKeys analysis related keys // GetRelatedKeys analysis related keys
func GetRelatedKeys(cmdLine [][]byte) ([]string, []string) { func GetRelatedKeys(cmdLine [][]byte) ([]string, []string) {
cmdName := strings.ToLower(string(cmdLine[0])) cmdName := strings.ToLower(string(cmdLine[0]))