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

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

View File

@@ -1,4 +1,4 @@
package db package godis
import ( import (
"github.com/hdt3213/godis/config" "github.com/hdt3213/godis/config"
@@ -64,12 +64,12 @@ func TestAof(t *testing.T) {
aofWriteDB.Close() // wait for aof finished aofWriteDB.Close() // wait for aof finished
aofReadDB := MakeDB() // start new db and read aof file aofReadDB := MakeDB() // start new db and read aof file
for _, key := range keys { for _, key := range keys {
expect, ok := aofWriteDB.Get(key) expect, ok := aofWriteDB.GetEntity(key)
if !ok { if !ok {
t.Errorf("key not found in origin: %s", key) t.Errorf("key not found in origin: %s", key)
continue continue
} }
actual, ok := aofReadDB.Get(key) actual, ok := aofReadDB.GetEntity(key)
if !ok { if !ok {
t.Errorf("key not found: %s", key) t.Errorf("key not found: %s", key)
continue continue
@@ -150,12 +150,12 @@ func TestRewriteAOF(t *testing.T) {
aofWriteDB.Close() // wait for aof finished aofWriteDB.Close() // wait for aof finished
aofReadDB := MakeDB() // start new db and read aof file aofReadDB := MakeDB() // start new db and read aof file
for _, key := range keys { for _, key := range keys {
expect, ok := aofWriteDB.Get(key) expect, ok := aofWriteDB.GetEntity(key)
if !ok { if !ok {
t.Errorf("key not found in origin: %s", key) t.Errorf("key not found in origin: %s", key)
continue continue
} }
actual, ok := aofReadDB.Get(key) actual, ok := aofReadDB.GetEntity(key)
if !ok { if !ok {
t.Errorf("key not found: %s", key) t.Errorf("key not found: %s", key)
continue continue

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
package db // Package godis is a memory database with redis compatible interface
package godis
import ( import (
"fmt" "fmt"
@@ -17,7 +18,7 @@ import (
"time" "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 { type DataEntity struct {
Data interface{} Data interface{}
} }
@@ -101,9 +102,9 @@ func (db *DB) Close() {
} }
} }
// Exec execute command // Exec executes command
// parameter `args` is a RESP message including command and its params // parameter `cmdArgs` contains command and its arguments, for example: "set key value"
func (db *DB) Exec(c redis.Connection, args [][]byte) (result redis.Reply) { func (db *DB) Exec(c redis.Connection, cmdArgs [][]byte) (result redis.Reply) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
logger.Warn(fmt.Sprintf("error occurs: %v\n%s", err, string(debug.Stack()))) 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" { if cmd == "auth" {
return Auth(db, c, args[1:]) return Auth(db, c, cmdArgs[1:])
} }
if !isAuthenticated(c) { if !isAuthenticated(c) {
return reply.MakeErrReply("NOAUTH Authentication required") return reply.MakeErrReply("NOAUTH Authentication required")
} }
// special commands // special commands
if cmd == "subscribe" { if cmd == "subscribe" {
if len(args) < 2 { if len(cmdArgs) < 2 {
return &reply.ArgNumErrReply{Cmd: "subscribe"} 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" { } else if cmd == "publish" {
return pubsub.Publish(db.hub, args[1:]) return pubsub.Publish(db.hub, cmdArgs[1:])
} else if cmd == "unsubscribe" { } else if cmd == "unsubscribe" {
return pubsub.UnSubscribe(db.hub, c, args[1:]) return pubsub.UnSubscribe(db.hub, c, cmdArgs[1:])
} else if cmd == "bgrewriteaof" { } else if cmd == "bgrewriteaof" {
// aof.go imports router.go, router.go cannot import BGRewriteAOF from aof.go // 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 // normal commands
@@ -138,8 +139,8 @@ func (db *DB) Exec(c redis.Connection, args [][]byte) (result redis.Reply) {
if !ok { if !ok {
return reply.MakeErrReply("ERR unknown command '" + cmd + "'") return reply.MakeErrReply("ERR unknown command '" + cmd + "'")
} }
if len(args) > 1 { if len(cmdArgs) > 1 {
result = fun(db, args[1:]) result = fun(db, cmdArgs[1:])
} else { } else {
result = fun(db, [][]byte{}) result = fun(db, [][]byte{})
} }
@@ -148,8 +149,8 @@ func (db *DB) Exec(c redis.Connection, args [][]byte) (result redis.Reply) {
/* ---- Data Access ----- */ /* ---- Data Access ----- */
// Get returns DataEntity bind to given key // GetEntity returns DataEntity bind to given key
func (db *DB) Get(key string) (*DataEntity, bool) { func (db *DB) GetEntity(key string) (*DataEntity, bool) {
db.stopWorld.Wait() db.stopWorld.Wait()
raw, ok := db.data.Get(key) raw, ok := db.data.Get(key)
@@ -163,8 +164,8 @@ func (db *DB) Get(key string) (*DataEntity, bool) {
return entity, true return entity, true
} }
// Put a DataEntity into DB // PutEntity a DataEntity into DB
func (db *DB) Put(key string, entity *DataEntity) int { func (db *DB) PutEntity(key string, entity *DataEntity) int {
db.stopWorld.Wait() db.stopWorld.Wait()
return db.data.Put(key, entity) return db.data.Put(key, entity)
} }
@@ -269,8 +270,7 @@ func (db *DB) Expire(key string, expireTime time.Time) {
taskKey := genExpireTask(key) taskKey := genExpireTask(key)
timewheel.At(expireTime, taskKey, func() { timewheel.At(expireTime, taskKey, func() {
logger.Info("expire " + key) logger.Info("expire " + key)
db.ttlMap.Remove(key) db.Remove(key)
db.data.Remove(key)
}) })
} }

View File

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

View File

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

1
go.mod
View File

@@ -5,4 +5,5 @@ go 1.16
require ( require (
github.com/jolestar/go-commons-pool/v2 v2.1.1 github.com/jolestar/go-commons-pool/v2 v2.1.1
github.com/shopspring/decimal v1.2.0 github.com/shopspring/decimal v1.2.0
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
) )

15
go.sum
View File

@@ -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/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 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= 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/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 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@@ -1,4 +1,4 @@
package db package godis
import ( import (
Dict "github.com/hdt3213/godis/datastruct/dict" Dict "github.com/hdt3213/godis/datastruct/dict"
@@ -9,7 +9,7 @@ import (
) )
func (db *DB) getAsDict(key string) (Dict.Dict, reply.ErrorReply) { func (db *DB) getAsDict(key string) (Dict.Dict, reply.ErrorReply) {
entity, exists := db.Get(key) entity, exists := db.GetEntity(key)
if !exists { if !exists {
return nil, nil return nil, nil
} }
@@ -28,7 +28,7 @@ func (db *DB) getOrInitDict(key string) (dict Dict.Dict, inited bool, errReply r
inited = false inited = false
if dict == nil { if dict == nil {
dict = Dict.MakeSimple() dict = Dict.MakeSimple()
db.Put(key, &DataEntity{ db.PutEntity(key, &DataEntity{
Data: dict, Data: dict,
}) })
inited = true inited = true

View File

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

View File

@@ -1,4 +1,4 @@
package db package godis
import ( import (
"github.com/hdt3213/godis/datastruct/dict" "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") return reply.MakeErrReply("ERR wrong number of arguments for 'exists' command")
} }
key := string(args[0]) key := string(args[0])
_, exists := db.Get(key) _, exists := db.GetEntity(key)
if exists { if exists {
return reply.MakeIntReply(1) 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") return reply.MakeErrReply("ERR wrong number of arguments for 'type' command")
} }
key := string(args[0]) key := string(args[0])
entity, exists := db.Get(key) entity, exists := db.GetEntity(key)
if !exists { if !exists {
return reply.MakeStatusReply("none") return reply.MakeStatusReply("none")
} }
@@ -101,12 +101,12 @@ func Rename(db *DB, args [][]byte) redis.Reply {
db.Locks(src, dest) db.Locks(src, dest)
defer db.UnLocks(src, dest) defer db.UnLocks(src, dest)
entity, ok := db.Get(src) entity, ok := db.GetEntity(src)
if !ok { if !ok {
return reply.MakeErrReply("no such key") return reply.MakeErrReply("no such key")
} }
rawTTL, hasTTL := db.ttlMap.Get(src) rawTTL, hasTTL := db.ttlMap.Get(src)
db.Put(dest, entity) db.PutEntity(dest, entity)
db.Remove(src) db.Remove(src)
if hasTTL { if hasTTL {
db.Persist(src) // clean src and dest with their ttl 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) db.Locks(src, dest)
defer db.UnLocks(src, dest) defer db.UnLocks(src, dest)
_, ok := db.Get(dest) _, ok := db.GetEntity(dest)
if ok { if ok {
return reply.MakeIntReply(0) return reply.MakeIntReply(0)
} }
entity, ok := db.Get(src) entity, ok := db.GetEntity(src)
if !ok { if !ok {
return reply.MakeErrReply("no such key") return reply.MakeErrReply("no such key")
} }
rawTTL, hasTTL := db.ttlMap.Get(src) rawTTL, hasTTL := db.ttlMap.Get(src)
db.Removes(src, dest) // clean src and dest with their ttl db.Removes(src, dest) // clean src and dest with their ttl
db.Put(dest, entity) db.PutEntity(dest, entity)
if hasTTL { if hasTTL {
db.Persist(src) // clean src and dest with their ttl db.Persist(src) // clean src and dest with their ttl
db.Persist(dest) db.Persist(dest)
@@ -164,7 +164,7 @@ func Expire(db *DB, args [][]byte) redis.Reply {
} }
ttl := time.Duration(ttlArg) * time.Second ttl := time.Duration(ttlArg) * time.Second
_, exists := db.Get(key) _, exists := db.GetEntity(key)
if !exists { if !exists {
return reply.MakeIntReply(0) return reply.MakeIntReply(0)
} }
@@ -188,7 +188,7 @@ func ExpireAt(db *DB, args [][]byte) redis.Reply {
} }
expireTime := time.Unix(raw, 0) expireTime := time.Unix(raw, 0)
_, exists := db.Get(key) _, exists := db.GetEntity(key)
if !exists { if !exists {
return reply.MakeIntReply(0) return reply.MakeIntReply(0)
} }
@@ -211,7 +211,7 @@ func PExpire(db *DB, args [][]byte) redis.Reply {
} }
ttl := time.Duration(ttlArg) * time.Millisecond ttl := time.Duration(ttlArg) * time.Millisecond
_, exists := db.Get(key) _, exists := db.GetEntity(key)
if !exists { if !exists {
return reply.MakeIntReply(0) return reply.MakeIntReply(0)
} }
@@ -235,7 +235,7 @@ func PExpireAt(db *DB, args [][]byte) redis.Reply {
} }
expireTime := time.Unix(0, raw*int64(time.Millisecond)) expireTime := time.Unix(0, raw*int64(time.Millisecond))
_, exists := db.Get(key) _, exists := db.GetEntity(key)
if !exists { if !exists {
return reply.MakeIntReply(0) 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") return reply.MakeErrReply("ERR wrong number of arguments for 'ttl' command")
} }
key := string(args[0]) key := string(args[0])
_, exists := db.Get(key) _, exists := db.GetEntity(key)
if !exists { if !exists {
return reply.MakeIntReply(-2) 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") return reply.MakeErrReply("ERR wrong number of arguments for 'pttl' command")
} }
key := string(args[0]) key := string(args[0])
_, exists := db.Get(key) _, exists := db.GetEntity(key)
if !exists { if !exists {
return reply.MakeIntReply(-2) 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") return reply.MakeErrReply("ERR wrong number of arguments for 'persist' command")
} }
key := string(args[0]) key := string(args[0])
_, exists := db.Get(key) _, exists := db.GetEntity(key)
if !exists { if !exists {
return reply.MakeIntReply(0) return reply.MakeIntReply(0)
} }

View File

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

View File

@@ -1,4 +1,4 @@
package db package godis
import ( import (
List "github.com/hdt3213/godis/datastruct/list" List "github.com/hdt3213/godis/datastruct/list"
@@ -8,7 +8,7 @@ import (
) )
func (db *DB) getAsList(key string) (*List.LinkedList, reply.ErrorReply) { func (db *DB) getAsList(key string) (*List.LinkedList, reply.ErrorReply) {
entity, ok := db.Get(key) entity, ok := db.GetEntity(key)
if !ok { if !ok {
return nil, nil return nil, nil
} }
@@ -27,7 +27,7 @@ func (db *DB) getOrInitList(key string) (list *List.LinkedList, isNew bool, errR
isNew = false isNew = false
if list == nil { if list == nil {
list = &List.LinkedList{} list = &List.LinkedList{}
db.Put(key, &DataEntity{ db.PutEntity(key, &DataEntity{
Data: list, Data: list,
}) })
isNew = true isNew = true

View File

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

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"
DBImpl "github.com/hdt3213/godis/db"
"github.com/hdt3213/godis/interface/db" "github.com/hdt3213/godis/interface/db"
"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 = DBImpl.MakeDB() db = godis.MakeDB()
} }
return &Handler{ return &Handler{
db: db, db: db,

View File

@@ -1,4 +1,4 @@
package db package godis
func makeRouter() map[string]cmdFunc { func makeRouter() map[string]cmdFunc {
routerMap := make(map[string]cmdFunc) routerMap := make(map[string]cmdFunc)

View File

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

View File

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

View File

@@ -1,4 +1,4 @@
package db package godis
import ( import (
HashSet "github.com/hdt3213/godis/datastruct/set" HashSet "github.com/hdt3213/godis/datastruct/set"
@@ -8,7 +8,7 @@ import (
) )
func (db *DB) getAsSet(key string) (*HashSet.Set, reply.ErrorReply) { func (db *DB) getAsSet(key string) (*HashSet.Set, reply.ErrorReply) {
entity, exists := db.Get(key) entity, exists := db.GetEntity(key)
if !exists { if !exists {
return nil, nil return nil, nil
} }
@@ -27,7 +27,7 @@ func (db *DB) getOrInitSet(key string) (set *HashSet.Set, inited bool, errReply
inited = false inited = false
if set == nil { if set == nil {
set = HashSet.Make() set = HashSet.Make()
db.Put(key, &DataEntity{ db.PutEntity(key, &DataEntity{
Data: set, Data: set,
}) })
inited = true inited = true
@@ -253,7 +253,7 @@ func SInterStore(db *DB, args [][]byte) redis.Reply {
} }
set := HashSet.MakeFromVals(result.ToSlice()...) set := HashSet.MakeFromVals(result.ToSlice()...)
db.Put(dest, &DataEntity{ db.PutEntity(dest, &DataEntity{
Data: set, Data: set,
}) })
db.AddAof(makeAofCmd("sinterstore", args)) db.AddAof(makeAofCmd("sinterstore", args))
@@ -348,7 +348,7 @@ func SUnionStore(db *DB, args [][]byte) redis.Reply {
} }
set := HashSet.MakeFromVals(result.ToSlice()...) set := HashSet.MakeFromVals(result.ToSlice()...)
db.Put(dest, &DataEntity{ db.PutEntity(dest, &DataEntity{
Data: set, Data: set,
}) })
@@ -460,7 +460,7 @@ func SDiffStore(db *DB, args [][]byte) redis.Reply {
return &reply.EmptyMultiBulkReply{} return &reply.EmptyMultiBulkReply{}
} }
set := HashSet.MakeFromVals(result.ToSlice()...) set := HashSet.MakeFromVals(result.ToSlice()...)
db.Put(dest, &DataEntity{ db.PutEntity(dest, &DataEntity{
Data: set, Data: set,
}) })

View File

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

View File

@@ -1,4 +1,4 @@
package db package godis
import ( import (
SortedSet "github.com/hdt3213/godis/datastruct/sortedset" SortedSet "github.com/hdt3213/godis/datastruct/sortedset"
@@ -9,7 +9,7 @@ import (
) )
func (db *DB) getAsSortedSet(key string) (*SortedSet.SortedSet, reply.ErrorReply) { func (db *DB) getAsSortedSet(key string) (*SortedSet.SortedSet, reply.ErrorReply) {
entity, exists := db.Get(key) entity, exists := db.GetEntity(key)
if !exists { if !exists {
return nil, nil return nil, nil
} }
@@ -28,7 +28,7 @@ func (db *DB) getOrInitSortedSet(key string) (sortedSet *SortedSet.SortedSet, in
inited = false inited = false
if sortedSet == nil { if sortedSet == nil {
sortedSet = SortedSet.Make() sortedSet = SortedSet.Make()
db.Put(key, &DataEntity{ db.PutEntity(key, &DataEntity{
Data: sortedSet, Data: sortedSet,
}) })
inited = true inited = true

View File

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

View File

@@ -1,4 +1,4 @@
package db package godis
import ( import (
"github.com/hdt3213/godis/interface/redis" "github.com/hdt3213/godis/interface/redis"
@@ -10,7 +10,7 @@ import (
) )
func (db *DB) getAsString(key string) ([]byte, reply.ErrorReply) { func (db *DB) getAsString(key string) ([]byte, reply.ErrorReply) {
entity, ok := db.Get(key) entity, ok := db.GetEntity(key)
if !ok { if !ok {
return nil, nil return nil, nil
} }
@@ -116,7 +116,7 @@ func Set(db *DB, args [][]byte) redis.Reply {
var result int var result int
switch policy { switch policy {
case upsertPolicy: case upsertPolicy:
result = db.Put(key, entity) result = db.PutEntity(key, entity)
case insertPolicy: case insertPolicy:
result = db.PutIfAbsent(key, entity) result = db.PutIfAbsent(key, entity)
case updatePolicy: case updatePolicy:
@@ -188,7 +188,7 @@ func SetEX(db *DB, args [][]byte) redis.Reply {
db.Lock(key) db.Lock(key)
defer db.UnLock(key) defer db.UnLock(key)
db.Put(key, entity) db.PutEntity(key, entity)
expireTime := time.Now().Add(time.Duration(ttl) * time.Millisecond) expireTime := time.Now().Add(time.Duration(ttl) * time.Millisecond)
db.Expire(key, expireTime) db.Expire(key, expireTime)
db.AddAof(makeAofCmd("setex", args)) db.AddAof(makeAofCmd("setex", args))
@@ -219,7 +219,7 @@ func PSetEX(db *DB, args [][]byte) redis.Reply {
db.Lock(key) db.Lock(key)
defer db.UnLock(key) defer db.UnLock(key)
db.Put(key, entity) db.PutEntity(key, entity)
expireTime := time.Now().Add(time.Duration(ttlArg) * time.Millisecond) expireTime := time.Now().Add(time.Duration(ttlArg) * time.Millisecond)
db.Expire(key, expireTime) db.Expire(key, expireTime)
db.AddAof(makeAofCmd("setex", args)) db.AddAof(makeAofCmd("setex", args))
@@ -247,7 +247,7 @@ func MSet(db *DB, args [][]byte) redis.Reply {
for i, key := range keys { for i, key := range keys {
value := values[i] value := values[i]
db.Put(key, &DataEntity{Data: value}) db.PutEntity(key, &DataEntity{Data: value})
} }
db.AddAof(makeAofCmd("mset", args)) db.AddAof(makeAofCmd("mset", args))
return &reply.OkReply{} return &reply.OkReply{}
@@ -300,7 +300,7 @@ func MSetNX(db *DB, args [][]byte) redis.Reply {
defer db.UnLocks(keys...) defer db.UnLocks(keys...)
for _, key := range keys { for _, key := range keys {
_, exists := db.Get(key) _, exists := db.GetEntity(key)
if exists { if exists {
return reply.MakeIntReply(0) return reply.MakeIntReply(0)
} }
@@ -308,7 +308,7 @@ func MSetNX(db *DB, args [][]byte) redis.Reply {
for i, key := range keys { for i, key := range keys {
value := values[i] value := values[i]
db.Put(key, &DataEntity{Data: value}) db.PutEntity(key, &DataEntity{Data: value})
} }
db.AddAof(makeAofCmd("msetnx", args)) db.AddAof(makeAofCmd("msetnx", args))
return reply.MakeIntReply(1) return reply.MakeIntReply(1)
@@ -327,7 +327,7 @@ func GetSet(db *DB, args [][]byte) redis.Reply {
return err return err
} }
db.Put(key, &DataEntity{Data: value}) db.PutEntity(key, &DataEntity{Data: value})
db.Persist(key) // override ttl db.Persist(key) // override ttl
db.AddAof(makeAofCmd("getset", args)) db.AddAof(makeAofCmd("getset", args))
if old == nil { if old == nil {
@@ -355,13 +355,13 @@ func Incr(db *DB, args [][]byte) redis.Reply {
if err != nil { if err != nil {
return reply.MakeErrReply("ERR value is not an integer or out of range") 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)), Data: []byte(strconv.FormatInt(val+1, 10)),
}) })
db.AddAof(makeAofCmd("incr", args)) db.AddAof(makeAofCmd("incr", args))
return reply.MakeIntReply(val + 1) return reply.MakeIntReply(val + 1)
} }
db.Put(key, &DataEntity{ db.PutEntity(key, &DataEntity{
Data: []byte("1"), Data: []byte("1"),
}) })
db.AddAof(makeAofCmd("incr", args)) db.AddAof(makeAofCmd("incr", args))
@@ -393,13 +393,13 @@ func IncrBy(db *DB, args [][]byte) redis.Reply {
if err != nil { if err != nil {
return reply.MakeErrReply("ERR value is not an integer or out of range") 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)), Data: []byte(strconv.FormatInt(val+delta, 10)),
}) })
db.AddAof(makeAofCmd("incrby", args)) db.AddAof(makeAofCmd("incrby", args))
return reply.MakeIntReply(val + delta) return reply.MakeIntReply(val + delta)
} }
db.Put(key, &DataEntity{ db.PutEntity(key, &DataEntity{
Data: args[1], Data: args[1],
}) })
db.AddAof(makeAofCmd("incrby", args)) 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") return reply.MakeErrReply("ERR value is not a valid float")
} }
resultBytes := []byte(val.Add(delta).String()) resultBytes := []byte(val.Add(delta).String())
db.Put(key, &DataEntity{ db.PutEntity(key, &DataEntity{
Data: resultBytes, Data: resultBytes,
}) })
db.AddAof(makeAofCmd("incrbyfloat", args)) db.AddAof(makeAofCmd("incrbyfloat", args))
return reply.MakeBulkReply(resultBytes) return reply.MakeBulkReply(resultBytes)
} }
db.Put(key, &DataEntity{ db.PutEntity(key, &DataEntity{
Data: args[1], Data: args[1],
}) })
db.AddAof(makeAofCmd("incrbyfloat", args)) db.AddAof(makeAofCmd("incrbyfloat", args))
@@ -463,7 +463,7 @@ func Decr(db *DB, args [][]byte) redis.Reply {
if err != nil { if err != nil {
return reply.MakeErrReply("ERR value is not an integer or out of range") 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)), Data: []byte(strconv.FormatInt(val-1, 10)),
}) })
db.AddAof(makeAofCmd("decr", args)) db.AddAof(makeAofCmd("decr", args))
@@ -472,7 +472,7 @@ func Decr(db *DB, args [][]byte) redis.Reply {
entity := &DataEntity{ entity := &DataEntity{
Data: []byte("-1"), Data: []byte("-1"),
} }
db.Put(key, entity) db.PutEntity(key, entity)
db.AddAof(makeAofCmd("decr", args)) db.AddAof(makeAofCmd("decr", args))
return reply.MakeIntReply(-1) return reply.MakeIntReply(-1)
} }
@@ -501,14 +501,14 @@ func DecrBy(db *DB, args [][]byte) redis.Reply {
if err != nil { if err != nil {
return reply.MakeErrReply("ERR value is not an integer or out of range") 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)), Data: []byte(strconv.FormatInt(val-delta, 10)),
}) })
db.AddAof(makeAofCmd("decrby", args)) db.AddAof(makeAofCmd("decrby", args))
return reply.MakeIntReply(val - delta) return reply.MakeIntReply(val - delta)
} }
valueStr := strconv.FormatInt(-delta, 10) valueStr := strconv.FormatInt(-delta, 10)
db.Put(key, &DataEntity{ db.PutEntity(key, &DataEntity{
Data: []byte(valueStr), Data: []byte(valueStr),
}) })
db.AddAof(makeAofCmd("decrby", args)) db.AddAof(makeAofCmd("decrby", args))

View File

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

View File

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