Skip to content

Commit

Permalink
refactor project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
HDT3213 committed May 9, 2021
1 parent 65fc1c3 commit 721d9c3
Show file tree
Hide file tree
Showing 28 changed files with 126 additions and 110 deletions.
2 changes: 1 addition & 1 deletion db/aof.go → aof.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package godis

import (
"github.com/hdt3213/godis/config"
Expand Down
10 changes: 5 additions & 5 deletions db/aof_test.go → aof_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package godis

import (
"github.com/hdt3213/godis/config"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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")
Expand All @@ -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 -------*/
Expand Down
4 changes: 2 additions & 2 deletions cluster/mset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand Down
22 changes: 11 additions & 11 deletions cluster/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()))
Expand All @@ -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()))
Expand Down
6 changes: 3 additions & 3 deletions cluster/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions db/db.go → db.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package db
// Package godis is a memory database with redis compatible interface
package godis

import (
"fmt"
Expand All @@ -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{}
}
Expand Down Expand Up @@ -101,45 +102,45 @@ 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())))
result = &reply.UnknownErrReply{}
}
}()

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
fun, ok := router[cmd]
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{})
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
})
}

Expand Down
2 changes: 1 addition & 1 deletion db/geo.go → geo.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package godis

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion db/geo_test.go → geo_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package godis

import (
"fmt"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 3 additions & 3 deletions db/hash.go → hash.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package godis

import (
Dict "github.com/hdt3213/godis/datastruct/dict"
Expand All @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion db/hash_test.go → hash_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package db
package godis

import (
"fmt"
Expand Down
Loading

0 comments on commit 721d9c3

Please sign in to comment.