From 721d9c31a7584116aca74d6f9f5ca5b5758c199c Mon Sep 17 00:00:00 2001 From: hdt3213 Date: Sun, 9 May 2021 21:47:05 +0800 Subject: [PATCH] refactor project structure --- db/aof.go => aof.go | 2 +- db/aof_test.go => aof_test.go | 10 +++--- cluster/cluster.go | 10 +++--- cluster/mset.go | 4 +-- cluster/rename_test.go | 22 ++++++------- cluster/transaction.go | 6 ++-- db/db.go => db.go | 40 +++++++++++------------ db/geo.go => geo.go | 2 +- db/geo_test.go => geo_test.go | 2 +- go.mod | 1 + go.sum | 15 +++++++++ db/hash.go => hash.go | 6 ++-- db/hash_test.go => hash_test.go | 2 +- db/keys.go => keys.go | 30 ++++++++--------- db/keys_test.go => keys_test.go | 2 +- db/list.go => list.go | 6 ++-- db/list_test.go => list_test.go | 2 +- redis/server/server.go | 4 +-- db/router.go => router.go | 2 +- db/server.go => server.go | 2 +- db/server_test.go => server_test.go | 2 +- db/set.go => set.go | 12 +++---- db/set_test.go => set_test.go | 2 +- db/sortedset.go => sortedset.go | 6 ++-- db/sortedset_test.go => sortedset_test.go | 2 +- db/string.go => string.go | 38 ++++++++++----------- db/string_test.go => string_test.go | 2 +- db/util_test.go => util_test.go | 2 +- 28 files changed, 126 insertions(+), 110 deletions(-) rename db/aof.go => aof.go (99%) rename db/aof_test.go => aof_test.go (96%) rename db/db.go => db.go (87%) rename db/geo.go => geo.go (99%) rename db/geo_test.go => geo_test.go (99%) rename db/hash.go => hash.go (99%) rename db/hash_test.go => hash_test.go (99%) rename db/keys.go => keys.go (94%) rename db/keys_test.go => keys_test.go (99%) rename db/list.go => list.go (99%) rename db/list_test.go => list_test.go (99%) rename db/router.go => router.go (99%) rename db/server.go => server.go (98%) rename db/server_test.go => server_test.go (98%) rename db/set.go => set.go (98%) rename db/set_test.go => set_test.go (99%) rename db/sortedset.go => sortedset.go (99%) rename db/sortedset_test.go => sortedset_test.go (99%) rename db/string.go => string.go (95%) rename db/string_test.go => string_test.go (99%) rename db/util_test.go => util_test.go (94%) diff --git a/db/aof.go b/aof.go similarity index 99% rename from db/aof.go rename to aof.go index 1bbf6983..93ec7c5e 100644 --- a/db/aof.go +++ b/aof.go @@ -1,4 +1,4 @@ -package db +package godis import ( "github.com/hdt3213/godis/config" diff --git a/db/aof_test.go b/aof_test.go similarity index 96% rename from db/aof_test.go rename to aof_test.go index 124ad784..75cbafef 100644 --- a/db/aof_test.go +++ b/aof_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "github.com/hdt3213/godis/config" @@ -64,12 +64,12 @@ func TestAof(t *testing.T) { aofWriteDB.Close() // wait for aof finished aofReadDB := MakeDB() // start new db and read aof file for _, key := range keys { - expect, ok := aofWriteDB.Get(key) + expect, ok := aofWriteDB.GetEntity(key) if !ok { t.Errorf("key not found in origin: %s", key) continue } - actual, ok := aofReadDB.Get(key) + actual, ok := aofReadDB.GetEntity(key) if !ok { t.Errorf("key not found: %s", key) continue @@ -150,12 +150,12 @@ func TestRewriteAOF(t *testing.T) { aofWriteDB.Close() // wait for aof finished aofReadDB := MakeDB() // start new db and read aof file for _, key := range keys { - expect, ok := aofWriteDB.Get(key) + expect, ok := aofWriteDB.GetEntity(key) if !ok { t.Errorf("key not found in origin: %s", key) continue } - actual, ok := aofReadDB.Get(key) + actual, ok := aofReadDB.GetEntity(key) if !ok { t.Errorf("key not found: %s", key) continue diff --git a/cluster/cluster.go b/cluster/cluster.go index b9907061..fca295d0 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -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 -------*/ diff --git a/cluster/mset.go b/cluster/mset.go index 29c76671..9c60e5f4 100644 --- a/cluster/mset.go +++ b/cluster/mset.go @@ -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{} diff --git a/cluster/rename_test.go b/cluster/rename_test.go index 20e85e86..03f76787 100644 --- a/cluster/rename_test.go +++ b/cluster/rename_test.go @@ -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())) diff --git a/cluster/transaction.go b/cluster/transaction.go index aa91b71c..c69fb84a 100644 --- a/cluster/transaction.go +++ b/cluster/transaction.go @@ -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 diff --git a/db/db.go b/db.go similarity index 87% rename from db/db.go rename to db.go index efbd3498..32d48ff0 100644 --- a/db/db.go +++ b/db.go @@ -1,4 +1,5 @@ -package db +// Package godis is a memory database with redis compatible interface +package godis import ( "fmt" @@ -17,7 +18,7 @@ import ( "time" ) -// DataEntity stores data bound to a key, may be a string, list, hash, set and so on +// DataEntity stores data bound to a key, including a string, list, hash, set and so on type DataEntity struct { Data interface{} } @@ -101,9 +102,9 @@ func (db *DB) Close() { } } -// Exec execute command -// parameter `args` is a RESP message including command and its params -func (db *DB) Exec(c redis.Connection, args [][]byte) (result redis.Reply) { +// Exec executes command +// parameter `cmdArgs` contains command and its arguments, for example: "set key value" +func (db *DB) Exec(c redis.Connection, cmdArgs [][]byte) (result redis.Reply) { defer func() { if err := recover(); err != nil { logger.Warn(fmt.Sprintf("error occurs: %v\n%s", err, string(debug.Stack()))) @@ -111,26 +112,26 @@ func (db *DB) Exec(c redis.Connection, args [][]byte) (result redis.Reply) { } }() - cmd := strings.ToLower(string(args[0])) + cmd := strings.ToLower(string(cmdArgs[0])) if cmd == "auth" { - return Auth(db, c, args[1:]) + return Auth(db, c, cmdArgs[1:]) } if !isAuthenticated(c) { return reply.MakeErrReply("NOAUTH Authentication required") } // special commands if cmd == "subscribe" { - if len(args) < 2 { + if len(cmdArgs) < 2 { return &reply.ArgNumErrReply{Cmd: "subscribe"} } - return pubsub.Subscribe(db.hub, c, args[1:]) + return pubsub.Subscribe(db.hub, c, cmdArgs[1:]) } else if cmd == "publish" { - return pubsub.Publish(db.hub, args[1:]) + return pubsub.Publish(db.hub, cmdArgs[1:]) } else if cmd == "unsubscribe" { - return pubsub.UnSubscribe(db.hub, c, args[1:]) + return pubsub.UnSubscribe(db.hub, c, cmdArgs[1:]) } else if cmd == "bgrewriteaof" { // aof.go imports router.go, router.go cannot import BGRewriteAOF from aof.go - return BGRewriteAOF(db, args[1:]) + return BGRewriteAOF(db, cmdArgs[1:]) } // normal commands @@ -138,8 +139,8 @@ func (db *DB) Exec(c redis.Connection, args [][]byte) (result redis.Reply) { if !ok { return reply.MakeErrReply("ERR unknown command '" + cmd + "'") } - if len(args) > 1 { - result = fun(db, args[1:]) + if len(cmdArgs) > 1 { + result = fun(db, cmdArgs[1:]) } else { result = fun(db, [][]byte{}) } @@ -148,8 +149,8 @@ func (db *DB) Exec(c redis.Connection, args [][]byte) (result redis.Reply) { /* ---- Data Access ----- */ -// Get returns DataEntity bind to given key -func (db *DB) Get(key string) (*DataEntity, bool) { +// GetEntity returns DataEntity bind to given key +func (db *DB) GetEntity(key string) (*DataEntity, bool) { db.stopWorld.Wait() raw, ok := db.data.Get(key) @@ -163,8 +164,8 @@ func (db *DB) Get(key string) (*DataEntity, bool) { return entity, true } -// Put a DataEntity into DB -func (db *DB) Put(key string, entity *DataEntity) int { +// PutEntity a DataEntity into DB +func (db *DB) PutEntity(key string, entity *DataEntity) int { db.stopWorld.Wait() return db.data.Put(key, entity) } @@ -269,8 +270,7 @@ func (db *DB) Expire(key string, expireTime time.Time) { taskKey := genExpireTask(key) timewheel.At(expireTime, taskKey, func() { logger.Info("expire " + key) - db.ttlMap.Remove(key) - db.data.Remove(key) + db.Remove(key) }) } diff --git a/db/geo.go b/geo.go similarity index 99% rename from db/geo.go rename to geo.go index 2785f9d7..dd08d141 100644 --- a/db/geo.go +++ b/geo.go @@ -1,4 +1,4 @@ -package db +package godis import ( "fmt" diff --git a/db/geo_test.go b/geo_test.go similarity index 99% rename from db/geo_test.go rename to geo_test.go index ef66b5e9..c5ec96cb 100644 --- a/db/geo_test.go +++ b/geo_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "fmt" diff --git a/go.mod b/go.mod index e397086b..374f8eae 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.16 require ( github.com/jolestar/go-commons-pool/v2 v2.1.1 github.com/shopspring/decimal v1.2.0 + golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect ) diff --git a/go.sum b/go.sum index 63a39695..94cbceb9 100644 --- a/go.sum +++ b/go.sum @@ -9,9 +9,24 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/db/hash.go b/hash.go similarity index 99% rename from db/hash.go rename to hash.go index 35ec04f3..09052118 100644 --- a/db/hash.go +++ b/hash.go @@ -1,4 +1,4 @@ -package db +package godis import ( Dict "github.com/hdt3213/godis/datastruct/dict" @@ -9,7 +9,7 @@ import ( ) func (db *DB) getAsDict(key string) (Dict.Dict, reply.ErrorReply) { - entity, exists := db.Get(key) + entity, exists := db.GetEntity(key) if !exists { return nil, nil } @@ -28,7 +28,7 @@ func (db *DB) getOrInitDict(key string) (dict Dict.Dict, inited bool, errReply r inited = false if dict == nil { dict = Dict.MakeSimple() - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: dict, }) inited = true diff --git a/db/hash_test.go b/hash_test.go similarity index 99% rename from db/hash_test.go rename to hash_test.go index a1c6085d..71bc5436 100644 --- a/db/hash_test.go +++ b/hash_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "fmt" diff --git a/db/keys.go b/keys.go similarity index 94% rename from db/keys.go rename to keys.go index 7b2e2cef..a2261501 100644 --- a/db/keys.go +++ b/keys.go @@ -1,4 +1,4 @@ -package db +package godis import ( "github.com/hdt3213/godis/datastruct/dict" @@ -38,7 +38,7 @@ func Exists(db *DB, args [][]byte) redis.Reply { return reply.MakeErrReply("ERR wrong number of arguments for 'exists' command") } key := string(args[0]) - _, exists := db.Get(key) + _, exists := db.GetEntity(key) if exists { return reply.MakeIntReply(1) } @@ -71,7 +71,7 @@ func Type(db *DB, args [][]byte) redis.Reply { return reply.MakeErrReply("ERR wrong number of arguments for 'type' command") } key := string(args[0]) - entity, exists := db.Get(key) + entity, exists := db.GetEntity(key) if !exists { return reply.MakeStatusReply("none") } @@ -101,12 +101,12 @@ func Rename(db *DB, args [][]byte) redis.Reply { db.Locks(src, dest) defer db.UnLocks(src, dest) - entity, ok := db.Get(src) + entity, ok := db.GetEntity(src) if !ok { return reply.MakeErrReply("no such key") } rawTTL, hasTTL := db.ttlMap.Get(src) - db.Put(dest, entity) + db.PutEntity(dest, entity) db.Remove(src) if hasTTL { db.Persist(src) // clean src and dest with their ttl @@ -129,18 +129,18 @@ func RenameNx(db *DB, args [][]byte) redis.Reply { db.Locks(src, dest) defer db.UnLocks(src, dest) - _, ok := db.Get(dest) + _, ok := db.GetEntity(dest) if ok { return reply.MakeIntReply(0) } - entity, ok := db.Get(src) + entity, ok := db.GetEntity(src) if !ok { return reply.MakeErrReply("no such key") } rawTTL, hasTTL := db.ttlMap.Get(src) db.Removes(src, dest) // clean src and dest with their ttl - db.Put(dest, entity) + db.PutEntity(dest, entity) if hasTTL { db.Persist(src) // clean src and dest with their ttl db.Persist(dest) @@ -164,7 +164,7 @@ func Expire(db *DB, args [][]byte) redis.Reply { } ttl := time.Duration(ttlArg) * time.Second - _, exists := db.Get(key) + _, exists := db.GetEntity(key) if !exists { return reply.MakeIntReply(0) } @@ -188,7 +188,7 @@ func ExpireAt(db *DB, args [][]byte) redis.Reply { } expireTime := time.Unix(raw, 0) - _, exists := db.Get(key) + _, exists := db.GetEntity(key) if !exists { return reply.MakeIntReply(0) } @@ -211,7 +211,7 @@ func PExpire(db *DB, args [][]byte) redis.Reply { } ttl := time.Duration(ttlArg) * time.Millisecond - _, exists := db.Get(key) + _, exists := db.GetEntity(key) if !exists { return reply.MakeIntReply(0) } @@ -235,7 +235,7 @@ func PExpireAt(db *DB, args [][]byte) redis.Reply { } expireTime := time.Unix(0, raw*int64(time.Millisecond)) - _, exists := db.Get(key) + _, exists := db.GetEntity(key) if !exists { return reply.MakeIntReply(0) } @@ -252,7 +252,7 @@ func TTL(db *DB, args [][]byte) redis.Reply { return reply.MakeErrReply("ERR wrong number of arguments for 'ttl' command") } key := string(args[0]) - _, exists := db.Get(key) + _, exists := db.GetEntity(key) if !exists { return reply.MakeIntReply(-2) } @@ -272,7 +272,7 @@ func PTTL(db *DB, args [][]byte) redis.Reply { return reply.MakeErrReply("ERR wrong number of arguments for 'pttl' command") } key := string(args[0]) - _, exists := db.Get(key) + _, exists := db.GetEntity(key) if !exists { return reply.MakeIntReply(-2) } @@ -292,7 +292,7 @@ func Persist(db *DB, args [][]byte) redis.Reply { return reply.MakeErrReply("ERR wrong number of arguments for 'persist' command") } key := string(args[0]) - _, exists := db.Get(key) + _, exists := db.GetEntity(key) if !exists { return reply.MakeIntReply(0) } diff --git a/db/keys_test.go b/keys_test.go similarity index 99% rename from db/keys_test.go rename to keys_test.go index e0fffb8a..37b35600 100644 --- a/db/keys_test.go +++ b/keys_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "fmt" diff --git a/db/list.go b/list.go similarity index 99% rename from db/list.go rename to list.go index dba57a27..f14e2ffb 100644 --- a/db/list.go +++ b/list.go @@ -1,4 +1,4 @@ -package db +package godis import ( List "github.com/hdt3213/godis/datastruct/list" @@ -8,7 +8,7 @@ import ( ) func (db *DB) getAsList(key string) (*List.LinkedList, reply.ErrorReply) { - entity, ok := db.Get(key) + entity, ok := db.GetEntity(key) if !ok { return nil, nil } @@ -27,7 +27,7 @@ func (db *DB) getOrInitList(key string) (list *List.LinkedList, isNew bool, errR isNew = false if list == nil { list = &List.LinkedList{} - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: list, }) isNew = true diff --git a/db/list_test.go b/list_test.go similarity index 99% rename from db/list_test.go rename to list_test.go index 74b964a5..42597199 100644 --- a/db/list_test.go +++ b/list_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "fmt" diff --git a/redis/server/server.go b/redis/server/server.go index 347265c4..d6cee1ea 100644 --- a/redis/server/server.go +++ b/redis/server/server.go @@ -6,9 +6,9 @@ package server import ( "context" + "github.com/hdt3213/godis" "github.com/hdt3213/godis/cluster" "github.com/hdt3213/godis/config" - DBImpl "github.com/hdt3213/godis/db" "github.com/hdt3213/godis/interface/db" "github.com/hdt3213/godis/lib/logger" "github.com/hdt3213/godis/lib/sync/atomic" @@ -39,7 +39,7 @@ func MakeHandler() *Handler { len(config.Properties.Peers) > 0 { db = cluster.MakeCluster() } else { - db = DBImpl.MakeDB() + db = godis.MakeDB() } return &Handler{ db: db, diff --git a/db/router.go b/router.go similarity index 99% rename from db/router.go rename to router.go index 006c03c6..56636c2f 100644 --- a/db/router.go +++ b/router.go @@ -1,4 +1,4 @@ -package db +package godis func makeRouter() map[string]cmdFunc { routerMap := make(map[string]cmdFunc) diff --git a/db/server.go b/server.go similarity index 98% rename from db/server.go rename to server.go index 2630c61b..a98c14a4 100644 --- a/db/server.go +++ b/server.go @@ -1,4 +1,4 @@ -package db +package godis import ( "github.com/hdt3213/godis/config" diff --git a/db/server_test.go b/server_test.go similarity index 98% rename from db/server_test.go rename to server_test.go index 5bdfcf27..31660f04 100644 --- a/db/server_test.go +++ b/server_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "github.com/hdt3213/godis/config" diff --git a/db/set.go b/set.go similarity index 98% rename from db/set.go rename to set.go index 77c8c931..5a20a6b3 100644 --- a/db/set.go +++ b/set.go @@ -1,4 +1,4 @@ -package db +package godis import ( HashSet "github.com/hdt3213/godis/datastruct/set" @@ -8,7 +8,7 @@ import ( ) func (db *DB) getAsSet(key string) (*HashSet.Set, reply.ErrorReply) { - entity, exists := db.Get(key) + entity, exists := db.GetEntity(key) if !exists { return nil, nil } @@ -27,7 +27,7 @@ func (db *DB) getOrInitSet(key string) (set *HashSet.Set, inited bool, errReply inited = false if set == nil { set = HashSet.Make() - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: set, }) inited = true @@ -253,7 +253,7 @@ func SInterStore(db *DB, args [][]byte) redis.Reply { } set := HashSet.MakeFromVals(result.ToSlice()...) - db.Put(dest, &DataEntity{ + db.PutEntity(dest, &DataEntity{ Data: set, }) db.AddAof(makeAofCmd("sinterstore", args)) @@ -348,7 +348,7 @@ func SUnionStore(db *DB, args [][]byte) redis.Reply { } set := HashSet.MakeFromVals(result.ToSlice()...) - db.Put(dest, &DataEntity{ + db.PutEntity(dest, &DataEntity{ Data: set, }) @@ -460,7 +460,7 @@ func SDiffStore(db *DB, args [][]byte) redis.Reply { return &reply.EmptyMultiBulkReply{} } set := HashSet.MakeFromVals(result.ToSlice()...) - db.Put(dest, &DataEntity{ + db.PutEntity(dest, &DataEntity{ Data: set, }) diff --git a/db/set_test.go b/set_test.go similarity index 99% rename from db/set_test.go rename to set_test.go index bb7fa177..2683fc7a 100644 --- a/db/set_test.go +++ b/set_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "fmt" diff --git a/db/sortedset.go b/sortedset.go similarity index 99% rename from db/sortedset.go rename to sortedset.go index e7c68127..09182510 100644 --- a/db/sortedset.go +++ b/sortedset.go @@ -1,4 +1,4 @@ -package db +package godis import ( SortedSet "github.com/hdt3213/godis/datastruct/sortedset" @@ -9,7 +9,7 @@ import ( ) func (db *DB) getAsSortedSet(key string) (*SortedSet.SortedSet, reply.ErrorReply) { - entity, exists := db.Get(key) + entity, exists := db.GetEntity(key) if !exists { return nil, nil } @@ -28,7 +28,7 @@ func (db *DB) getOrInitSortedSet(key string) (sortedSet *SortedSet.SortedSet, in inited = false if sortedSet == nil { sortedSet = SortedSet.Make() - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: sortedSet, }) inited = true diff --git a/db/sortedset_test.go b/sortedset_test.go similarity index 99% rename from db/sortedset_test.go rename to sortedset_test.go index 1ab1c3ce..20ed3388 100644 --- a/db/sortedset_test.go +++ b/sortedset_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "github.com/hdt3213/godis/lib/utils" diff --git a/db/string.go b/string.go similarity index 95% rename from db/string.go rename to string.go index 48f2f885..f3c97b83 100644 --- a/db/string.go +++ b/string.go @@ -1,4 +1,4 @@ -package db +package godis import ( "github.com/hdt3213/godis/interface/redis" @@ -10,7 +10,7 @@ import ( ) func (db *DB) getAsString(key string) ([]byte, reply.ErrorReply) { - entity, ok := db.Get(key) + entity, ok := db.GetEntity(key) if !ok { return nil, nil } @@ -116,7 +116,7 @@ func Set(db *DB, args [][]byte) redis.Reply { var result int switch policy { case upsertPolicy: - result = db.Put(key, entity) + result = db.PutEntity(key, entity) case insertPolicy: result = db.PutIfAbsent(key, entity) case updatePolicy: @@ -188,7 +188,7 @@ func SetEX(db *DB, args [][]byte) redis.Reply { db.Lock(key) defer db.UnLock(key) - db.Put(key, entity) + db.PutEntity(key, entity) expireTime := time.Now().Add(time.Duration(ttl) * time.Millisecond) db.Expire(key, expireTime) db.AddAof(makeAofCmd("setex", args)) @@ -219,7 +219,7 @@ func PSetEX(db *DB, args [][]byte) redis.Reply { db.Lock(key) defer db.UnLock(key) - db.Put(key, entity) + db.PutEntity(key, entity) expireTime := time.Now().Add(time.Duration(ttlArg) * time.Millisecond) db.Expire(key, expireTime) db.AddAof(makeAofCmd("setex", args)) @@ -247,7 +247,7 @@ func MSet(db *DB, args [][]byte) redis.Reply { for i, key := range keys { value := values[i] - db.Put(key, &DataEntity{Data: value}) + db.PutEntity(key, &DataEntity{Data: value}) } db.AddAof(makeAofCmd("mset", args)) return &reply.OkReply{} @@ -300,7 +300,7 @@ func MSetNX(db *DB, args [][]byte) redis.Reply { defer db.UnLocks(keys...) for _, key := range keys { - _, exists := db.Get(key) + _, exists := db.GetEntity(key) if exists { return reply.MakeIntReply(0) } @@ -308,7 +308,7 @@ func MSetNX(db *DB, args [][]byte) redis.Reply { for i, key := range keys { value := values[i] - db.Put(key, &DataEntity{Data: value}) + db.PutEntity(key, &DataEntity{Data: value}) } db.AddAof(makeAofCmd("msetnx", args)) return reply.MakeIntReply(1) @@ -327,7 +327,7 @@ func GetSet(db *DB, args [][]byte) redis.Reply { return err } - db.Put(key, &DataEntity{Data: value}) + db.PutEntity(key, &DataEntity{Data: value}) db.Persist(key) // override ttl db.AddAof(makeAofCmd("getset", args)) if old == nil { @@ -355,13 +355,13 @@ func Incr(db *DB, args [][]byte) redis.Reply { if err != nil { return reply.MakeErrReply("ERR value is not an integer or out of range") } - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: []byte(strconv.FormatInt(val+1, 10)), }) db.AddAof(makeAofCmd("incr", args)) return reply.MakeIntReply(val + 1) } - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: []byte("1"), }) db.AddAof(makeAofCmd("incr", args)) @@ -393,13 +393,13 @@ func IncrBy(db *DB, args [][]byte) redis.Reply { if err != nil { return reply.MakeErrReply("ERR value is not an integer or out of range") } - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: []byte(strconv.FormatInt(val+delta, 10)), }) db.AddAof(makeAofCmd("incrby", args)) return reply.MakeIntReply(val + delta) } - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: args[1], }) db.AddAof(makeAofCmd("incrby", args)) @@ -431,13 +431,13 @@ func IncrByFloat(db *DB, args [][]byte) redis.Reply { return reply.MakeErrReply("ERR value is not a valid float") } resultBytes := []byte(val.Add(delta).String()) - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: resultBytes, }) db.AddAof(makeAofCmd("incrbyfloat", args)) return reply.MakeBulkReply(resultBytes) } - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: args[1], }) db.AddAof(makeAofCmd("incrbyfloat", args)) @@ -463,7 +463,7 @@ func Decr(db *DB, args [][]byte) redis.Reply { if err != nil { return reply.MakeErrReply("ERR value is not an integer or out of range") } - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: []byte(strconv.FormatInt(val-1, 10)), }) db.AddAof(makeAofCmd("decr", args)) @@ -472,7 +472,7 @@ func Decr(db *DB, args [][]byte) redis.Reply { entity := &DataEntity{ Data: []byte("-1"), } - db.Put(key, entity) + db.PutEntity(key, entity) db.AddAof(makeAofCmd("decr", args)) return reply.MakeIntReply(-1) } @@ -501,14 +501,14 @@ func DecrBy(db *DB, args [][]byte) redis.Reply { if err != nil { return reply.MakeErrReply("ERR value is not an integer or out of range") } - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: []byte(strconv.FormatInt(val-delta, 10)), }) db.AddAof(makeAofCmd("decrby", args)) return reply.MakeIntReply(val - delta) } valueStr := strconv.FormatInt(-delta, 10) - db.Put(key, &DataEntity{ + db.PutEntity(key, &DataEntity{ Data: []byte(valueStr), }) db.AddAof(makeAofCmd("decrby", args)) diff --git a/db/string_test.go b/string_test.go similarity index 99% rename from db/string_test.go rename to string_test.go index b0fe546c..fa28afdb 100644 --- a/db/string_test.go +++ b/string_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "fmt" diff --git a/db/util_test.go b/util_test.go similarity index 94% rename from db/util_test.go rename to util_test.go index dcbcba6c..041716b4 100644 --- a/db/util_test.go +++ b/util_test.go @@ -1,4 +1,4 @@ -package db +package godis import ( "github.com/hdt3213/godis/datastruct/dict"