Skip to content

Commit

Permalink
db add setTTL api
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Apr 2, 2024
1 parent c11231f commit b3dfa10
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
28 changes: 22 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Operation byte

const (
OpSetTx Operation = iota
OpSetTTL
OpRemove
// map
OpHSet
Expand Down Expand Up @@ -115,6 +116,11 @@ var cmdTable = []Cmd{
}
return nil
}},
{OpSetTTL, func(db *DB, reader *codeman.Reader) error {
// key, ts
db.SetTTL(reader.Str(), reader.Int64())
return nil
}},
{OpRemove, func(db *DB, reader *codeman.Reader) error {
// keys
db.Remove(reader.StrSlice()...)
Expand Down Expand Up @@ -385,7 +391,7 @@ func (db *DB) SetEx(key string, val []byte, ttl time.Duration) {
db.SetTx(key, val, cache.GetNanoSec()+int64(ttl))
}

// SetTx store key-value pair with deadlindb.
// SetTx store key-value pair with deadline.
func (db *DB) SetTx(key string, val []byte, ts int64) {
if ts < 0 {
return
Expand All @@ -394,21 +400,32 @@ func (db *DB) SetTx(key string, val []byte, ts int64) {
db.m.SetTx(key, val, ts)
}

// SetTTL set expired time of key-value.
func (db *DB) SetTTL(key string, ts int64) bool {
if ts < 0 {
return false
}
db.encode(newCodec(OpSetTTL).Str(key).Int(ts))
return db.m.SetTTL(key, ts)
}

// Remove
func (db *DB) Remove(keys ...string) (n int) {
db.encode(newCodec(OpRemove).StrSlice(keys))
for _, key := range keys {
if db.m.Remove(key) {
n++
} else if db.cm.Has(key) {
db.cm.Remove(key)
n++
}
}
return
}

// Len
func (db *DB) Len() int {
stat := db.m.Stat()
return stat.Conflict + stat.Len + db.cm.Count()
return db.m.Stat().Len + db.cm.Count()
}

// GC triggers the garbage collection to evict expired kv datas.
Expand All @@ -419,9 +436,9 @@ func (db *DB) GC() {
}

// Scan
func (db *DB) Scan(f func(string, []byte, int64) bool) {
func (db *DB) Scan(f func([]byte, []byte, int64) bool) {
db.m.Scan(func(key, value []byte, ttl int64) bool {
return f(string(key), value, ttl)
return f(key, value, ttl)
})
}

Expand Down Expand Up @@ -455,7 +472,6 @@ func (db *DB) HSet(key, field string, val []byte) error {
}
db.encode(newCodec(OpHSet).Str(key).Str(field).Bytes(val))
m.Set(field, val)

return nil
}

Expand Down
38 changes: 36 additions & 2 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestDB(t *testing.T) {

// Scan
var count int
db.Scan(func(key string, val []byte, ts int64) bool {
db.Scan(func(key, val []byte, ts int64) bool {
count++
return true
})
Expand All @@ -81,7 +81,7 @@ func TestDB(t *testing.T) {
// GC
db.GC()
count = 0
db.Scan(func(key string, val []byte, ts int64) bool {
db.Scan(func(key, val []byte, ts int64) bool {
count++
return true
})
Expand All @@ -108,6 +108,40 @@ func TestDB(t *testing.T) {
// Load Success
_, err = Open(db.GetOptions())
assert.Nil(err)

t.Run("setTTL", func(t *testing.T) {
db, err := createDB()
assert.Nil(err)

db.HSet("hmap", "k", []byte("v"))
n := db.Remove("hmap")
assert.Equal(n, 1)

assert.False(db.SetTTL("h", -1))

ts := time.Now().Add(time.Minute).UnixNano()
for i := 0; i < 100; i++ {
k := fmt.Sprintf("%08d", i)
db.SetTx(k, []byte(k), ts)
}
// set ttl
for i := 0; i < 100; i++ {
k := fmt.Sprintf("%08d", i)
assert.True(db.SetTTL(k, 0))
}

db.Close()
db, _ = Open(db.GetOptions())

// check ttl
for i := 0; i < 100; i++ {
k := fmt.Sprintf("%08d", i)
v, ts, err := db.Get(k)
assert.Equal(string(v), k)
assert.Equal(int64(0), ts)
assert.Nil(err)
}
})
}

func TestHmap(t *testing.T) {
Expand Down

0 comments on commit b3dfa10

Please sign in to comment.