From b3e0ce9ae2ef4a5894ceac6c5b528577480f5fea Mon Sep 17 00:00:00 2001 From: siddontang Date: Mon, 23 Jun 2014 11:12:20 +0800 Subject: [PATCH] add persist support --- ledis/t_hash.go | 18 ++++++++++++++++++ ledis/t_hash_test.go | 24 +++++++++++++++++++++++- ledis/t_kv.go | 17 +++++++++++++++++ ledis/t_kv_test.go | 23 +++++++++++++++++++++++ ledis/t_list.go | 18 ++++++++++++++++++ ledis/t_list_test.go | 23 +++++++++++++++++++++++ ledis/t_ttl.go | 11 +++++++---- ledis/t_zset.go | 18 ++++++++++++++++++ ledis/t_zset_test.go | 23 +++++++++++++++++++++++ 9 files changed, 170 insertions(+), 5 deletions(-) diff --git a/ledis/t_hash.go b/ledis/t_hash.go index 1478a21..aa1a75f 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -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 +} diff --git a/ledis/t_hash_test.go b/ledis/t_hash_test.go index b18a616..33a7e60 100644 --- a/ledis/t_hash_test.go +++ b/ledis/t_hash_test.go @@ -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) + } } diff --git a/ledis/t_kv.go b/ledis/t_kv.go index 5d4c4fc..7497cc2 100644 --- a/ledis/t_kv.go +++ b/ledis/t_kv.go @@ -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 +} diff --git a/ledis/t_kv_test.go b/ledis/t_kv_test.go index 6c5e7e0..680aa5b 100644 --- a/ledis/t_kv_test.go +++ b/ledis/t_kv_test.go @@ -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) + } +} diff --git a/ledis/t_list.go b/ledis/t_list.go index 1d3c91e..6306968 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -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 +} diff --git a/ledis/t_list_test.go b/ledis/t_list_test.go index b7cb660..bb525eb 100644 --- a/ledis/t_list_test.go +++ b/ledis/t_list_test.go @@ -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) + } +} diff --git a/ledis/t_ttl.go b/ledis/t_ttl.go index df64118..d1cd4f6 100644 --- a/ledis/t_ttl.go +++ b/ledis/t_ttl.go @@ -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 } } diff --git a/ledis/t_zset.go b/ledis/t_zset.go index 681996b..cd0e001 100644 --- a/ledis/t_zset.go +++ b/ledis/t_zset.go @@ -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 +} diff --git a/ledis/t_zset_test.go b/ledis/t_zset_test.go index 4b08136..74cf526 100644 --- a/ledis/t_zset_test.go +++ b/ledis/t_zset_test.go @@ -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) + } +}