add persist support

This commit is contained in:
siddontang 2014-06-23 11:12:20 +08:00
parent 794cb12ba4
commit b3e0ce9ae2
9 changed files with 170 additions and 5 deletions

View File

@ -525,3 +525,21 @@ func (db *DB) HTTL(key []byte) (int64, error) {
return db.ttl(hExpType, key)
}
func (db *DB) HPersist(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
t := db.hashTx
t.Lock()
defer t.Unlock()
n, err := db.rmExpire(t, hExpType, key)
if err != nil {
return 0, err
}
err = t.Commit()
return n, err
}

View File

@ -83,5 +83,27 @@ func TestDBHScan(t *testing.T) {
} else if len(v) != 3 {
t.Fatal(len(v))
}
}
func TestHashPersist(t *testing.T) {
db := getTestDB()
key := []byte("persist")
db.HSet(key, []byte("field"), []byte{})
if n, err := db.HPersist(key); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
if _, err := db.HExpire(key, 10); err != nil {
t.Fatal(err)
}
if n, err := db.HPersist(key); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
}

View File

@ -395,3 +395,20 @@ func (db *DB) TTL(key []byte) (int64, error) {
return db.ttl(kvExpType, key)
}
func (db *DB) Persist(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
t := db.kvTx
t.Lock()
defer t.Unlock()
n, err := db.rmExpire(t, kvExpType, key)
if err != nil {
return 0, err
}
err = t.Commit()
return n, err
}

View File

@ -76,3 +76,26 @@ func TestDBScan(t *testing.T) {
t.Fatal(len(v))
}
}
func TestKVPersist(t *testing.T) {
db := getTestDB()
key := []byte("persist")
db.Set(key, []byte{})
if n, err := db.Persist(key); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
if _, err := db.Expire(key, 10); err != nil {
t.Fatal(err)
}
if n, err := db.Persist(key); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
}

View File

@ -449,3 +449,21 @@ func (db *DB) LTTL(key []byte) (int64, error) {
return db.ttl(lExpType, key)
}
func (db *DB) LPersist(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
t := db.listTx
t.Lock()
defer t.Unlock()
n, err := db.rmExpire(t, lExpType, key)
if err != nil {
return 0, err
}
err = t.Commit()
return n, err
}

View File

@ -65,3 +65,26 @@ func TestDBList(t *testing.T) {
t.Fatal(llen)
}
}
func TestListPersist(t *testing.T) {
db := getTestDB()
key := []byte("persist")
db.LPush(key, []byte("a"))
if n, err := db.LPersist(key); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
if _, err := db.LExpire(key, 10); err != nil {
t.Fatal(err)
}
if n, err := db.LPersist(key); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
}

View File

@ -90,16 +90,19 @@ func (db *DB) ttl(expType byte, key []byte) (t int64, err error) {
return t, err
}
func (db *DB) rmExpire(t *tx, expType byte, key []byte) {
func (db *DB) rmExpire(t *tx, expType byte, key []byte) (int64, error) {
mk := db.expEncodeMetaKey(expType+1, key)
if v, err := db.db.Get(mk); err != nil || v == nil {
return
if v, err := db.db.Get(mk); err != nil {
return 0, err
} else if v == nil {
return 0, nil
} else if when, err2 := Int64(v, nil); err2 != nil {
return
return 0, err2
} else {
tk := db.expEncodeTimeKey(expType, key, when)
t.Delete(mk)
t.Delete(tk)
return 1, nil
}
}

View File

@ -804,3 +804,21 @@ func (db *DB) ZTTL(key []byte) (int64, error) {
return db.ttl(zExpType, key)
}
func (db *DB) ZPersist(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
t := db.zsetTx
t.Lock()
defer t.Unlock()
n, err := db.rmExpire(t, zExpType, key)
if err != nil {
return 0, err
}
err = t.Commit()
return n, err
}

View File

@ -241,3 +241,26 @@ func TestDBZScan(t *testing.T) {
t.Fatal(len(v))
}
}
func TestZSetPersist(t *testing.T) {
db := getTestDB()
key := []byte("persist")
db.ZAdd(key, ScorePair{1, []byte("a")})
if n, err := db.ZPersist(key); err != nil {
t.Fatal(err)
} else if n != 0 {
t.Fatal(n)
}
if _, err := db.ZExpire(key, 10); err != nil {
t.Fatal(err)
}
if n, err := db.ZPersist(key); err != nil {
t.Fatal(err)
} else if n != 1 {
t.Fatal(n)
}
}