refactor project structure

This commit is contained in:
hdt3213
2021-05-09 21:47:05 +08:00
parent 65fc1c3e62
commit 721d9c31a7
28 changed files with 126 additions and 110 deletions

View File

@@ -3,9 +3,9 @@ package cluster
import (
"context"
"fmt"
"github.com/hdt3213/godis"
"github.com/hdt3213/godis/config"
"github.com/hdt3213/godis/datastruct/dict"
"github.com/hdt3213/godis/db"
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/consistenthash"
"github.com/hdt3213/godis/lib/idgenerator"
@@ -23,7 +23,7 @@ type Cluster struct {
peerPicker *consistenthash.Map
peerConnection map[string]*pool.ObjectPool
db *db.DB
db *godis.DB
transactions *dict.SimpleDict // id -> Transaction
idGenerator *idgenerator.IdGenerator
@@ -42,7 +42,7 @@ func MakeCluster() *Cluster {
cluster := &Cluster{
self: config.Properties.Self,
db: db.MakeDB(),
db: godis.MakeDB(),
transactions: dict.MakeSimple(),
peerPicker: consistenthash.New(replicas, nil),
peerConnection: make(map[string]*pool.ObjectPool),
@@ -95,7 +95,7 @@ func (cluster *Cluster) Exec(c redis.Connection, args [][]byte) (result redis.Re
}()
cmd := strings.ToLower(string(args[0]))
if cmd == "auth" {
return db.Auth(cluster.db, c, args[1:])
return godis.Auth(cluster.db, c, args[1:])
}
if !isAuthenticated(c) {
return reply.MakeErrReply("NOAUTH Authentication required")
@@ -112,7 +112,7 @@ func (cluster *Cluster) AfterClientClose(c redis.Connection) {
}
func Ping(cluster *Cluster, c redis.Connection, args [][]byte) redis.Reply {
return db.Ping(cluster.db, args[1:])
return godis.Ping(cluster.db, args[1:])
}
/*----- utils -------*/

View File

@@ -2,7 +2,7 @@ package cluster
import (
"fmt"
"github.com/hdt3213/godis/db"
"github.com/hdt3213/godis"
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/redis/reply"
"strconv"
@@ -74,7 +74,7 @@ func CommitMSet(cluster *Cluster, c redis.Connection, tx *Transaction) redis.Rep
}
for i, key := range keys {
value := values[i]
cluster.db.Put(key, &db.DataEntity{Data: value})
cluster.db.PutEntity(key, &godis.DataEntity{Data: value})
}
cluster.db.AddAof(reply.MakeMultiBulkReply(tx.args))
return &reply.OkReply{}

View File

@@ -2,7 +2,7 @@ package cluster
import (
"fmt"
"github.com/hdt3213/godis/db"
"github.com/hdt3213/godis"
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/reply"
"github.com/hdt3213/godis/redis/reply/asserts"
@@ -11,22 +11,22 @@ import (
func TestRename(t *testing.T) {
testDB := testCluster.db
db.FlushAll(testDB, [][]byte{})
godis.FlushAll(testDB, [][]byte{})
key := utils.RandString(10)
value := utils.RandString(10)
newKey := key + utils.RandString(2)
db.Set(testDB, utils.ToBytesList(key, value, "ex", "1000"))
godis.Set(testDB, utils.ToBytesList(key, value, "ex", "1000"))
result := Rename(testCluster, nil, utils.ToBytesList("RENAME", key, newKey))
if _, ok := result.(*reply.OkReply); !ok {
t.Error("expect ok")
return
}
result = db.Exists(testDB, utils.ToBytesList(key))
result = godis.Exists(testDB, utils.ToBytesList(key))
asserts.AssertIntReply(t, result, 0)
result = db.Exists(testDB, utils.ToBytesList(newKey))
result = godis.Exists(testDB, utils.ToBytesList(newKey))
asserts.AssertIntReply(t, result, 1)
// check ttl
result = db.TTL(testDB, utils.ToBytesList(newKey))
result = godis.TTL(testDB, utils.ToBytesList(newKey))
intResult, ok := result.(*reply.IntReply)
if !ok {
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))
@@ -40,18 +40,18 @@ func TestRename(t *testing.T) {
func TestRenameNx(t *testing.T) {
testDB := testCluster.db
db.FlushAll(testDB, [][]byte{})
godis.FlushAll(testDB, [][]byte{})
key := utils.RandString(10)
value := utils.RandString(10)
newKey := key + utils.RandString(2)
db.Set(testCluster.db, utils.ToBytesList(key, value, "ex", "1000"))
godis.Set(testCluster.db, utils.ToBytesList(key, value, "ex", "1000"))
result := RenameNx(testCluster, nil, utils.ToBytesList("RENAMENX", key, newKey))
asserts.AssertIntReply(t, result, 1)
result = db.Exists(testDB, utils.ToBytesList(key))
result = godis.Exists(testDB, utils.ToBytesList(key))
asserts.AssertIntReply(t, result, 0)
result = db.Exists(testDB, utils.ToBytesList(newKey))
result = godis.Exists(testDB, utils.ToBytesList(newKey))
asserts.AssertIntReply(t, result, 1)
result = db.TTL(testDB, utils.ToBytesList(newKey))
result = godis.TTL(testDB, utils.ToBytesList(newKey))
intResult, ok := result.(*reply.IntReply)
if !ok {
t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))

View File

@@ -3,7 +3,7 @@ package cluster
import (
"errors"
"fmt"
"github.com/hdt3213/godis/db"
"github.com/hdt3213/godis"
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/lib/logger"
"github.com/hdt3213/godis/lib/timewheel"
@@ -80,9 +80,9 @@ func (tx *Transaction) prepare() error {
// build undoLog
tx.undoLog = make(map[string][][]byte)
for _, key := range tx.keys {
entity, ok := tx.cluster.db.Get(key)
entity, ok := tx.cluster.db.GetEntity(key)
if ok {
blob := db.EntityToCmd(key, entity)
blob := godis.EntityToCmd(key, entity)
tx.undoLog[key] = blob.Args
} else {
tx.undoLog[key] = nil // entity was nil, should be removed while rollback