mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 00:42:43 +08:00
refactor project structure
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"github.com/hdt3213/godis/config"
|
@@ -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
|
@@ -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 -------*/
|
||||
|
@@ -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{}
|
||||
|
@@ -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()))
|
||||
|
@@ -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
|
||||
|
@@ -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)
|
||||
})
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"fmt"
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"fmt"
|
1
go.mod
1
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
|
||||
)
|
||||
|
15
go.sum
15
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=
|
||||
|
@@ -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
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"fmt"
|
@@ -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)
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"fmt"
|
@@ -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
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"fmt"
|
@@ -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,
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
func makeRouter() map[string]cmdFunc {
|
||||
routerMap := make(map[string]cmdFunc)
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"github.com/hdt3213/godis/config"
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"github.com/hdt3213/godis/config"
|
@@ -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,
|
||||
})
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"fmt"
|
@@ -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
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"github.com/hdt3213/godis/lib/utils"
|
@@ -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))
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"fmt"
|
@@ -1,4 +1,4 @@
|
||||
package db
|
||||
package godis
|
||||
|
||||
import (
|
||||
"github.com/hdt3213/godis/datastruct/dict"
|
Reference in New Issue
Block a user