optimize project structure

This commit is contained in:
hdt3213
2021-11-08 23:05:53 +08:00
parent 3c5a44b7c0
commit 4163a45278
34 changed files with 82 additions and 56 deletions

View File

@@ -126,22 +126,22 @@ MSET (10 keys): 65487.89 requests per second
If you want to read my code in this repository, here is a simple guidance. If you want to read my code in this repository, here is a simple guidance.
- github.com/hdt3213/godis/cmd: only the entry point - project root: only the entry point
- github.com/hdt3213/godis/config: config parser - config: config parser
- github.com/hdt3213/godis/interface: some interface definitions - interface: some interface definitions
- github.com/hdt3213/godis/lib: some utils, such as logger, sync utils and wildcard - lib: some utils, such as logger, sync utils and wildcard
I suggest focusing on the following directories: I suggest focusing on the following directories:
- github.com/hdt3213/godis/tcp: the tcp server - tcp: the tcp server
- github.com/hdt3213/godis/redis: the redis protocol parser - redis: the redis protocol parser
- github.com/hdt3213/godis/datastruct: the implements of data structures - datastruct: the implements of data structures
- dict: a concurrent hash map - dict: a concurrent hash map
- list: a linked list - list: a linked list
- lock: it is used to lock keys to ensure thread safety - lock: it is used to lock keys to ensure thread safety
- 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
- github.com/hdt3213/godis: the core of storage engine - database: the core of storage engine
- server.go: a standalone redis server, - server.go: a standalone redis server,
- db.go: the basement function of database - db.go: the basement function of database
- exec.go: the gateway of database - exec.go: the gateway of database
@@ -155,6 +155,19 @@ I suggest focusing on the following directories:
- pubsub.go: implements of publish / subscribe - pubsub.go: implements of publish / subscribe
- aof.go: implements of AOF persistence and rewrite - aof.go: implements of AOF persistence and rewrite
- geo.go: implements of geography features - geo.go: implements of geography features
- sys.go: authentication and other system function
- transaction.go: local transaction
- cluster: 集群
- cluster.go: entrance of cluster mode
- com.go: communication within nodes
- del.go: atomic implementation of `delete` command in cluster
- keys.go: keys command
- mset.go: atomic implementation of `mset` command in cluster
- multi.go: entrance of distributed transaction
- pubsub.go: pub/sub in cluster
- rename.go: `rename` command in cluster
- tcc.go: try-commit-catch distributed transaction implementation
- aof: AOF persistence
# License # License

View File

@@ -116,22 +116,22 @@ MSET (10 keys): 65487.89 requests per second
本项目的目录结构: 本项目的目录结构:
- github.com/hdt3213/godis/cmd: main 函数,执行入口 - 根目录: main 函数,执行入口
- github.com/hdt3213/godis/config: 配置文件解析 - config: 配置文件解析
- github.com/hdt3213/godis/interface: 一些模块间的接口定义 - interface: 一些模块间的接口定义
- github.com/hdt3213/godis/lib: 各种工具比如logger、同步和通配符 - lib: 各种工具比如logger、同步和通配符
建议按照下列顺序阅读各包: 建议按照下列顺序阅读各包:
- github.com/hdt3213/godis/tcp: tcp 服务器实现 - tcp: tcp 服务器实现
- github.com/hdt3213/godis/redis: redis 协议解析器 - redis: redis 协议解析器
- github.com/hdt3213/godis/datastruct: redis 的各类数据结构实现 - datastruct: redis 的各类数据结构实现
- dict: hash 表 - dict: hash 表
- list: 链表 - list: 链表
- lock: 用于锁定 key 的锁组件 - lock: 用于锁定 key 的锁组件
- set 基于hash表的集合 - set 基于hash表的集合
- sortedset: 基于跳表实现的有序集合 - sortedset: 基于跳表实现的有序集合
- github.com/hdt3213/godis: 存储引擎核心 - database: 存储引擎核心
- db.go: 引擎的基础功能 - db.go: 引擎的基础功能
- router.go: 将命令路由给响应的处理函数 - router.go: 将命令路由给响应的处理函数
- keys.go: del、ttl、expire 等通用命令实现 - keys.go: del、ttl、expire 等通用命令实现
@@ -141,4 +141,17 @@ MSET (10 keys): 65487.89 requests per second
- set.go: sadd 等集合命令实现 - set.go: sadd 等集合命令实现
- sortedset.go: zadd 等有序集合命令实现 - sortedset.go: zadd 等有序集合命令实现
- pubsub.go: 发布订阅命令实现 - pubsub.go: 发布订阅命令实现
- aof.go: aof持久化实现 - geo.go: GEO 相关命令实现
- sys.go: Auth 等系统功能实现
- transaction.go: 单机事务实现
- cluster: 集群
- cluster.go: 集群入口
- com.go: 节点间通信
- del.go: delete 命令原子性实现
- keys.go: key 相关命令集群中实现
- mset.go: mset 命令原子性实现
- multi.go: 集群内事务实现
- pubsub.go: 发布订阅实现
- rename.go: rename 命令集群实现
- tcc.go: tcc 分布式事务底层实现
- aof: AOF 持久化实现

View File

@@ -1,3 +1,3 @@
#!/usr/bin/env bash #!/usr/bin/env bash
go build -o target/godis-darwin ./cmd go build -o target/godis-darwin ./

View File

@@ -1,3 +1,3 @@
#!/usr/bin/env bash #!/usr/bin/env bash
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o target/godis-linux ./cmd CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o target/godis-linux ./

View File

@@ -4,8 +4,8 @@ package cluster
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/hdt3213/godis"
"github.com/hdt3213/godis/config" "github.com/hdt3213/godis/config"
database2 "github.com/hdt3213/godis/database"
"github.com/hdt3213/godis/datastruct/dict" "github.com/hdt3213/godis/datastruct/dict"
"github.com/hdt3213/godis/interface/database" "github.com/hdt3213/godis/interface/database"
"github.com/hdt3213/godis/interface/redis" "github.com/hdt3213/godis/interface/redis"
@@ -47,7 +47,7 @@ func MakeCluster() *Cluster {
cluster := &Cluster{ cluster := &Cluster{
self: config.Properties.Self, self: config.Properties.Self,
db: godis.NewStandaloneServer(), db: database2.NewStandaloneServer(),
transactions: dict.MakeSimple(), transactions: dict.MakeSimple(),
peerPicker: consistenthash.New(replicas, nil), peerPicker: consistenthash.New(replicas, nil),
peerConnection: make(map[string]*pool.ObjectPool), peerConnection: make(map[string]*pool.ObjectPool),
@@ -102,7 +102,7 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
}() }()
cmdName := strings.ToLower(string(cmdLine[0])) cmdName := strings.ToLower(string(cmdLine[0]))
if cmdName == "auth" { if cmdName == "auth" {
return godis.Auth(c, cmdLine[1:]) return database2.Auth(c, cmdLine[1:])
} }
if !isAuthenticated(c) { if !isAuthenticated(c) {
return reply.MakeErrReply("NOAUTH Authentication required") return reply.MakeErrReply("NOAUTH Authentication required")
@@ -112,12 +112,12 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
if len(cmdLine) != 1 { if len(cmdLine) != 1 {
return reply.MakeArgNumErrReply(cmdName) return reply.MakeArgNumErrReply(cmdName)
} }
return godis.StartMulti(c) return database2.StartMulti(c)
} else if cmdName == "discard" { } else if cmdName == "discard" {
if len(cmdLine) != 1 { if len(cmdLine) != 1 {
return reply.MakeArgNumErrReply(cmdName) return reply.MakeArgNumErrReply(cmdName)
} }
return godis.DiscardMulti(c) return database2.DiscardMulti(c)
} else if cmdName == "exec" { } else if cmdName == "exec" {
if len(cmdLine) != 1 { if len(cmdLine) != 1 {
return reply.MakeArgNumErrReply(cmdName) return reply.MakeArgNumErrReply(cmdName)
@@ -130,7 +130,7 @@ func (cluster *Cluster) Exec(c redis.Connection, cmdLine [][]byte) (result redis
return execSelect(c, cmdLine) return execSelect(c, cmdLine)
} }
if c != nil && c.InMultiState() { if c != nil && c.InMultiState() {
return godis.EnqueueCmd(c, cmdLine) return database2.EnqueueCmd(c, cmdLine)
} }
cmdFunc, ok := router[cmdName] cmdFunc, ok := router[cmdName]
if !ok { if !ok {

View File

@@ -1,7 +1,7 @@
package cluster package cluster
import ( import (
"github.com/hdt3213/godis" "github.com/hdt3213/godis/database"
"github.com/hdt3213/godis/interface/redis" "github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/reply" "github.com/hdt3213/godis/redis/reply"
@@ -24,7 +24,7 @@ func execMulti(cluster *Cluster, conn redis.Connection, cmdLine CmdLine) redis.R
// analysis related keys // analysis related keys
keys := make([]string, 0) // may contains duplicate keys := make([]string, 0) // may contains duplicate
for _, cl := range cmdLines { for _, cl := range cmdLines {
wKeys, rKeys := godis.GetRelatedKeys(cl) wKeys, rKeys := database.GetRelatedKeys(cl)
keys = append(keys, wKeys...) keys = append(keys, wKeys...)
keys = append(keys, rKeys...) keys = append(keys, rKeys...)
} }

View File

@@ -2,7 +2,7 @@ package cluster
import ( import (
"fmt" "fmt"
"github.com/hdt3213/godis" "github.com/hdt3213/godis/database"
"github.com/hdt3213/godis/interface/redis" "github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/logger" "github.com/hdt3213/godis/lib/logger"
"github.com/hdt3213/godis/lib/timewheel" "github.com/hdt3213/godis/lib/timewheel"
@@ -77,7 +77,7 @@ func (tx *Transaction) prepare() error {
tx.mu.Lock() tx.mu.Lock()
defer tx.mu.Unlock() defer tx.mu.Unlock()
tx.writeKeys, tx.readKeys = godis.GetRelatedKeys(tx.cmdLine) tx.writeKeys, tx.readKeys = database.GetRelatedKeys(tx.cmdLine)
// lock writeKeys // lock writeKeys
tx.lockKeys() tx.lockKeys()

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/config" "github.com/hdt3213/godis/config"

View File

@@ -1,5 +1,5 @@
// Package godis is a memory database with redis compatible interface // Package godis is a memory database with redis compatible interface
package godis package database
import ( import (
"github.com/hdt3213/godis/datastruct/dict" "github.com/hdt3213/godis/datastruct/dict"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
Dict "github.com/hdt3213/godis/datastruct/dict" Dict "github.com/hdt3213/godis/datastruct/dict"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/aof" "github.com/hdt3213/godis/aof"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
List "github.com/hdt3213/godis/datastruct/list" List "github.com/hdt3213/godis/datastruct/list"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"strings" "strings"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
HashSet "github.com/hdt3213/godis/datastruct/set" HashSet "github.com/hdt3213/godis/datastruct/set"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
SortedSet "github.com/hdt3213/godis/datastruct/sortedset" SortedSet "github.com/hdt3213/godis/datastruct/sortedset"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/aof" "github.com/hdt3213/godis/aof"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"fmt" "fmt"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/config" "github.com/hdt3213/godis/config"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/config" "github.com/hdt3213/godis/config"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/datastruct/set" "github.com/hdt3213/godis/datastruct/set"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/aof" "github.com/hdt3213/godis/aof"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/lib/utils" "github.com/hdt3213/godis/lib/utils"

View File

@@ -1,4 +1,4 @@
package godis package database
import ( import (
"github.com/hdt3213/godis/datastruct/dict" "github.com/hdt3213/godis/datastruct/dict"

View File

@@ -6,9 +6,9 @@ package server
import ( import (
"context" "context"
"github.com/hdt3213/godis"
"github.com/hdt3213/godis/cluster" "github.com/hdt3213/godis/cluster"
"github.com/hdt3213/godis/config" "github.com/hdt3213/godis/config"
database2 "github.com/hdt3213/godis/database"
"github.com/hdt3213/godis/interface/database" "github.com/hdt3213/godis/interface/database"
"github.com/hdt3213/godis/lib/logger" "github.com/hdt3213/godis/lib/logger"
"github.com/hdt3213/godis/lib/sync/atomic" "github.com/hdt3213/godis/lib/sync/atomic"
@@ -39,7 +39,7 @@ func MakeHandler() *Handler {
len(config.Properties.Peers) > 0 { len(config.Properties.Peers) > 0 {
db = cluster.MakeCluster() db = cluster.MakeCluster()
} else { } else {
db = godis.NewStandaloneServer() db = database2.NewStandaloneServer()
} }
return &Handler{ return &Handler{
db: db, db: db,