2014-06-03 11:41:17 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2014-07-25 13:58:00 +04:00
|
|
|
"github.com/siddontang/ledisdb/store"
|
2014-10-29 11:02:46 +03:00
|
|
|
"sync"
|
2014-06-03 11:41:17 +04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-07-09 13:01:29 +04:00
|
|
|
var (
|
|
|
|
errExpMetaKey = errors.New("invalid expire meta key")
|
|
|
|
errExpTimeKey = errors.New("invalid expire time key")
|
|
|
|
)
|
2014-06-03 11:41:17 +04:00
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
type onExpired func(*batch, []byte) int64
|
2014-06-03 11:41:17 +04:00
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
type ttlChecker struct {
|
|
|
|
sync.Mutex
|
|
|
|
db *DB
|
|
|
|
txs []*batch
|
|
|
|
cbs []onExpired
|
|
|
|
|
|
|
|
//next check time
|
|
|
|
nc int64
|
2014-06-03 11:41:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
var errExpType = errors.New("invalid expire type")
|
|
|
|
|
2014-07-09 13:01:29 +04:00
|
|
|
func (db *DB) expEncodeTimeKey(dataType byte, key []byte, when int64) []byte {
|
|
|
|
buf := make([]byte, len(key)+11)
|
2014-06-03 11:41:17 +04:00
|
|
|
|
|
|
|
buf[0] = db.index
|
2014-07-11 09:27:40 +04:00
|
|
|
buf[1] = ExpTimeType
|
2014-10-29 11:02:46 +03:00
|
|
|
pos := 2
|
2014-06-03 11:41:17 +04:00
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(buf[pos:], uint64(when))
|
|
|
|
pos += 8
|
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
buf[pos] = dataType
|
|
|
|
pos++
|
|
|
|
|
2014-06-03 11:41:17 +04:00
|
|
|
copy(buf[pos:], key)
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2014-07-09 13:01:29 +04:00
|
|
|
func (db *DB) expEncodeMetaKey(dataType byte, key []byte) []byte {
|
|
|
|
buf := make([]byte, len(key)+3)
|
2014-06-03 11:41:17 +04:00
|
|
|
|
|
|
|
buf[0] = db.index
|
2014-07-11 09:27:40 +04:00
|
|
|
buf[1] = ExpMetaType
|
2014-07-09 13:01:29 +04:00
|
|
|
buf[2] = dataType
|
|
|
|
pos := 3
|
2014-06-03 11:41:17 +04:00
|
|
|
|
|
|
|
copy(buf[pos:], key)
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2014-07-09 13:01:29 +04:00
|
|
|
func (db *DB) expDecodeMetaKey(mk []byte) (byte, []byte, error) {
|
2014-07-11 09:27:40 +04:00
|
|
|
if len(mk) <= 3 || mk[0] != db.index || mk[1] != ExpMetaType {
|
2014-07-09 13:01:29 +04:00
|
|
|
return 0, nil, errExpMetaKey
|
|
|
|
}
|
|
|
|
|
|
|
|
return mk[2], mk[3:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) expDecodeTimeKey(tk []byte) (byte, []byte, int64, error) {
|
2014-07-11 09:27:40 +04:00
|
|
|
if len(tk) < 11 || tk[0] != db.index || tk[1] != ExpTimeType {
|
2014-07-09 13:01:29 +04:00
|
|
|
return 0, nil, 0, errExpTimeKey
|
2014-06-03 11:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
return tk[10], tk[11:], int64(binary.BigEndian.Uint64(tk[2:])), nil
|
2014-06-03 11:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func (db *DB) expire(t *batch, dataType byte, key []byte, duration int64) {
|
2014-07-09 13:01:29 +04:00
|
|
|
db.expireAt(t, dataType, key, time.Now().Unix()+duration)
|
2014-06-03 11:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func (db *DB) expireAt(t *batch, dataType byte, key []byte, when int64) {
|
2014-07-09 13:01:29 +04:00
|
|
|
mk := db.expEncodeMetaKey(dataType, key)
|
|
|
|
tk := db.expEncodeTimeKey(dataType, key, when)
|
2014-06-03 11:41:17 +04:00
|
|
|
|
|
|
|
t.Put(tk, mk)
|
|
|
|
t.Put(mk, PutInt64(when))
|
2014-10-29 11:02:46 +03:00
|
|
|
|
|
|
|
db.l.tcs[db.index].setNextCheckTime(when, false)
|
2014-06-03 11:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-07-09 13:01:29 +04:00
|
|
|
func (db *DB) ttl(dataType byte, key []byte) (t int64, err error) {
|
|
|
|
mk := db.expEncodeMetaKey(dataType, key)
|
2014-06-03 11:41:17 +04:00
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
if t, err = Int64(db.bucket.Get(mk)); err != nil || t == 0 {
|
2014-06-03 11:41:17 +04:00
|
|
|
t = -1
|
|
|
|
} else {
|
|
|
|
t -= time.Now().Unix()
|
|
|
|
if t <= 0 {
|
|
|
|
t = -1
|
|
|
|
}
|
|
|
|
// if t == -1 : to remove ????
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
func (db *DB) rmExpire(t *batch, dataType byte, key []byte) (int64, error) {
|
2014-07-09 13:01:29 +04:00
|
|
|
mk := db.expEncodeMetaKey(dataType, key)
|
2014-08-25 10:18:23 +04:00
|
|
|
if v, err := db.bucket.Get(mk); err != nil {
|
2014-06-23 07:12:20 +04:00
|
|
|
return 0, err
|
|
|
|
} else if v == nil {
|
|
|
|
return 0, nil
|
2014-06-05 12:13:35 +04:00
|
|
|
} else if when, err2 := Int64(v, nil); err2 != nil {
|
2014-06-23 07:12:20 +04:00
|
|
|
return 0, err2
|
2014-06-05 12:13:35 +04:00
|
|
|
} else {
|
2014-07-09 13:01:29 +04:00
|
|
|
tk := db.expEncodeTimeKey(dataType, key, when)
|
2014-06-03 11:41:17 +04:00
|
|
|
t.Delete(mk)
|
|
|
|
t.Delete(tk)
|
2014-06-23 07:12:20 +04:00
|
|
|
return 1, nil
|
2014-06-03 11:41:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
func newTTLChecker(db *DB) *ttlChecker {
|
|
|
|
c := new(ttlChecker)
|
|
|
|
c.db = db
|
|
|
|
c.txs = make([]*batch, maxDataType)
|
|
|
|
c.cbs = make([]onExpired, maxDataType)
|
|
|
|
c.nc = 0
|
|
|
|
return c
|
2014-06-03 11:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
func (c *ttlChecker) register(dataType byte, t *batch, f onExpired) {
|
|
|
|
c.txs[dataType] = t
|
|
|
|
c.cbs[dataType] = f
|
|
|
|
}
|
2014-07-08 13:10:44 +04:00
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
func (c *ttlChecker) setNextCheckTime(when int64, force bool) {
|
|
|
|
c.Lock()
|
|
|
|
if force {
|
|
|
|
c.nc = when
|
|
|
|
} else if !force && c.nc > when {
|
|
|
|
c.nc = when
|
|
|
|
}
|
|
|
|
c.Unlock()
|
2014-06-03 11:41:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
func (c *ttlChecker) check() {
|
2014-06-03 11:41:17 +04:00
|
|
|
now := time.Now().Unix()
|
2014-10-29 11:02:46 +03:00
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
nc := c.nc
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
if now < nc {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nc = now + 3600
|
|
|
|
|
|
|
|
db := c.db
|
2014-08-25 10:18:23 +04:00
|
|
|
dbGet := db.bucket.Get
|
2014-07-09 13:01:29 +04:00
|
|
|
|
2014-07-11 09:27:40 +04:00
|
|
|
minKey := db.expEncodeTimeKey(NoneType, nil, 0)
|
2014-10-29 11:02:46 +03:00
|
|
|
maxKey := db.expEncodeTimeKey(maxDataType, nil, nc)
|
2014-07-09 13:01:29 +04:00
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
it := db.bucket.RangeLimitIterator(minKey, maxKey, store.RangeROpen, 0, -1)
|
2014-07-09 13:01:29 +04:00
|
|
|
for ; it.Valid(); it.Next() {
|
|
|
|
tk := it.RawKey()
|
|
|
|
mk := it.RawValue()
|
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
dt, k, nt, err := db.expDecodeTimeKey(tk)
|
2014-07-09 13:01:29 +04:00
|
|
|
if err != nil {
|
2014-06-03 11:41:17 +04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
if nt > now {
|
|
|
|
//the next ttl check time is nt!
|
|
|
|
nc = nt
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
t := c.txs[dt]
|
|
|
|
cb := c.cbs[dt]
|
|
|
|
if tk == nil || cb == nil {
|
2014-07-09 13:01:29 +04:00
|
|
|
continue
|
|
|
|
}
|
2014-06-03 11:41:17 +04:00
|
|
|
|
2014-07-09 13:01:29 +04:00
|
|
|
t.Lock()
|
|
|
|
|
|
|
|
if exp, err := Int64(dbGet(mk)); err == nil {
|
|
|
|
// check expire again
|
|
|
|
if exp <= now {
|
2014-10-29 11:02:46 +03:00
|
|
|
cb(t, k)
|
2014-07-09 13:01:29 +04:00
|
|
|
t.Delete(tk)
|
|
|
|
t.Delete(mk)
|
2014-06-03 11:41:17 +04:00
|
|
|
|
2014-07-09 13:01:29 +04:00
|
|
|
t.Commit()
|
2014-06-03 11:41:17 +04:00
|
|
|
}
|
2014-07-09 13:01:29 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Unlock()
|
|
|
|
}
|
|
|
|
it.Close()
|
2014-06-03 11:41:17 +04:00
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
c.setNextCheckTime(nc, true)
|
|
|
|
|
2014-06-03 11:41:17 +04:00
|
|
|
return
|
|
|
|
}
|