Skip to content

Commit

Permalink
Merge 87a5179 into 2bbf1a1
Browse files Browse the repository at this point in the history
  • Loading branch information
nioshield committed Nov 29, 2018
2 parents 2bbf1a1 + 87a5179 commit 5ecc792
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 41 deletions.
25 changes: 24 additions & 1 deletion command/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,17 @@ func HVals(ctx *Context, txn *db.Transaction) (OnCommit, error) {

// HLen returns the number of fields contained in the hash stored at key
func HLen(ctx *Context, txn *db.Transaction) (OnCommit, error) {
var len int64
key := []byte(ctx.Args[0])
hash, err := txn.Hash(key)
if err != nil {
return nil, err
}
return Integer(ctx.Out, hash.HLen()), nil
len, err = hash.HLen()
if err != nil {
return nil, err
}
return Integer(ctx.Out, len), nil
}

// HStrLen returns the string length of the value associated with field in the hash stored at key
Expand Down Expand Up @@ -265,3 +270,21 @@ func HMSet(ctx *Context, txn *db.Transaction) (OnCommit, error) {
}
return SimpleString(ctx.Out, "OK"), nil
}

// HMSlot
func HMSlot(ctx *Context, txn *db.Transaction) (OnCommit, error) {
key := []byte(ctx.Args[0])
count, err := strconv.ParseInt(ctx.Args[1], 10, 64)
if err != nil || count < 0 {
return nil, ErrInteger
}
hash, err := txn.Hash(key)
if err != nil {
return nil, errors.New("ERR " + err.Error())
}

if err := hash.HMSlot(count); err != nil {
return nil, errors.New("ERR " + err.Error())
}
return SimpleString(ctx.Out, "OK"), nil
}
2 changes: 2 additions & 0 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func init() {
"hsetnx": HSetNX,
"hmget": HMGet,
"hmset": HMSet,
"hmslot": HMSlot,

// sets
"sadd": SAdd,
Expand Down Expand Up @@ -169,6 +170,7 @@ func init() {
"hsetnx": Desc{Proc: AutoCommit(HSetNX), Cons: Constraint{4, flags("wmF"), 1, 1, 1}},
"hmget": Desc{Proc: AutoCommit(HMGet), Cons: Constraint{-3, flags("rF"), 1, 1, 1}},
"hmset": Desc{Proc: AutoCommit(HMSet), Cons: Constraint{-3, flags("wmF"), 1, 1, 1}},
"hmslot": Desc{Proc: AutoCommit(HMSlot), Cons: Constraint{3, flags("wF"), 1, 1, 1}},

// sets
"sadd": Desc{Proc: AutoCommit(SAdd), Cons: Constraint{-3, flags("wmF"), 1, 1, 1}},
Expand Down
11 changes: 10 additions & 1 deletion command/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,20 @@ func Object(ctx *Context, txn *db.Transaction) (OnCommit, error) {
}
return nil, errors.New("ERR " + err.Error())
}

switch subCmd {
case "refcount", "freq":
return Integer(ctx.Out, 0), nil
case "idletime":
if obj.Type == db.ObjectHash {
hash, err := txn.Hash(key)
if err != nil {
return nil, errors.New("ERR " + err.Error())
}
obj, err = hash.Object()
if err != nil {
return nil, errors.New("ERR " + err.Error())
}
}
sec := int64(time.Since(time.Unix(0, obj.UpdatedAt)).Seconds())
return Integer(ctx.Out, sec), nil
case "encoding":
Expand Down
14 changes: 12 additions & 2 deletions command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,21 @@ func Debug(ctx *Context, txn *db.Transaction) (OnCommit, error) {
}
}
func debugObject(ctx *Context, txn *db.Transaction) (OnCommit, error) {
key := ctx.Args[1]
obj, err := txn.Object([]byte(key))
key := []byte(ctx.Args[1])
obj, err := txn.Object(key)
if err != nil {
return nil, err
}
if obj.Type == db.ObjectHash {
hash, err := txn.Hash(key)
if err != nil {
return nil, errors.New("ERR " + err.Error())
}
obj, err = hash.Object()
if err != nil {
return nil, errors.New("ERR " + err.Error())
}
}
return SimpleString(ctx.Out, obj.String()), nil
}

Expand Down
5 changes: 3 additions & 2 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type Server struct {

//Tikv config is the config of tikv sdk
type Tikv struct {
PdAddrs string `cfg:"pd-addrs;required; ;pd address in tidb"`
ZT ZT `cfg:"zt"`
PdAddrs string `cfg:"pd-addrs;required; ;pd address in tidb"`
MetaSlot int64 `cfg:"meta-slot;0;numeric;hashes slot key count"`
ZT ZT `cfg:"zt"`
}

//ZT config is the config of zlist
Expand Down
10 changes: 8 additions & 2 deletions conf/titan.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ auth = ""
[server.tikv]

#type: string
#rules: nonempty
#description: pd address in tidb
#required
pd-addrs = ""

#type: int64
#rules: numeric
#description: hashes slot key count
#default: 0
#meta-slot = 0

[server.tikv.zt]

#type: int
Expand Down Expand Up @@ -104,7 +109,7 @@ pd-addrs = ""
#type: string
#description: log level(debug, info, warn, error, panic, fatal)
#default: info
#level = "debug"
#level = "info"

#type: bool
#rules: boolean
Expand All @@ -116,3 +121,4 @@ pd-addrs = ""
#description: log time rotate pattern(s m h D M W)
#default: 0 0 0 * * *
#time-rotate = "0 0 0 * * *"

17 changes: 16 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func Open(conf *conf.Tikv) (*RedisStore, error) {
go StartGC(sysdb)
go StartExpire(sysdb)
go StartZT(sysdb, &conf.ZT)

if conf.MetaSlot != defaultHashMetaSlot {
defaultHashMetaSlot = conf.MetaSlot
}
return rds, nil
}

Expand Down Expand Up @@ -252,6 +254,19 @@ func DataKey(db *DB, key []byte) []byte {
return dkey
}

// MetaSlotKey builds a meta slot key from a slot id
func MetaSlotKey(db *DB, objID, slotID []byte) []byte {
var skey []byte
skey = append(skey, []byte(db.Namespace)...)
skey = append(skey, ':')
skey = append(skey, db.ID.Bytes()...)
skey = append(skey, ':', 'M', 'S', ':')
skey = append(skey, objID...)
skey = append(skey, ':')
skey = append(skey, slotID...)
return skey
}

func flushLease(txn store.Transaction, key, id []byte, interval time.Duration) error {
databytes := make([]byte, 24)
copy(databytes, id)
Expand Down

0 comments on commit 5ecc792

Please sign in to comment.