Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Delete Expired Keys #280

Merged
merged 4 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package rosedb

import (
"bytes"
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -49,6 +51,7 @@ type DB struct {
batchPool sync.Pool
watchCh chan *Event // user consume channel for watch events
watcher *Watcher
cursorKey []byte // the location to which DeleteExpiredKeys executes.
Jeremy-Run marked this conversation as resolved.
Show resolved Hide resolved
}

// Stat represents the statistics of the database.
Expand Down Expand Up @@ -489,8 +492,6 @@ func (db *DB) checkValue(chunk []byte) ([]byte, error) {
record := decodeLogRecord(chunk)
now := time.Now().UnixNano()
if record.Type == LogRecordDeleted || record.IsExpired(now) {
// delete from index if the key is expired.
db.index.Delete(record.Key)
Jeremy-Run marked this conversation as resolved.
Show resolved Hide resolved
return nil, ErrKeyNotFound
}
return record.Value, nil
Expand Down Expand Up @@ -576,3 +577,58 @@ func (db *DB) loadIndexFromWAL() error {
}
return nil
}

// DeleteExpiredKeys scan the entire index in ascending order to delete expired keys.
// It is a time-consuming operation, so we need to specify a timeout
// to prevent the DB from being unavailable for a long time.
func (db *DB) DeleteExpiredKeys(timeout time.Duration) {
db.mu.Lock()
defer db.mu.Unlock()

// set expiration time
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

now := time.Now().UnixNano()
go func(ctx context.Context) {
for {
// get 100 key's positions
positions := make([]*wal.ChunkPosition, 0, 100)
db.index.AscendGreaterOrEqual(db.cursorKey, func(k []byte, pos *wal.ChunkPosition) (bool, error) {
// filter processed key
if bytes.Compare(k, db.cursorKey) == 0 {
return true, nil
}
positions = append(positions, pos)
if len(positions) >= 100 {
return false, nil
}
return true, nil
})

// If keys in the db.index has been traversed, len(positions) will be 0.
if len(positions) == 0 {
db.cursorKey = []byte{}
Jeremy-Run marked this conversation as resolved.
Show resolved Hide resolved
return
}

// delete from index if the key is expired.
for _, pos := range positions {
chunk, err := db.dataFiles.Read(pos)
if err != nil {
continue
Jeremy-Run marked this conversation as resolved.
Show resolved Hide resolved
}
record := decodeLogRecord(chunk)
if record.IsExpired(now) {
db.index.Delete(record.Key)
}
db.cursorKey = record.Key
}
}
}(ctx)

select {
case <-ctx.Done():
return
}
}
22 changes: 22 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,25 @@ func TestDB_Expire2(t *testing.T) {
err = db2.Expire(utils.GetTestKey(2), time.Second)
assert.Equal(t, err, ErrKeyNotFound)
}

func TestDB_DeleteExpiredKeys(t *testing.T) {
options := DefaultOptions
db, err := Open(options)
assert.Nil(t, err)
defer destroyDB(db)

for i := 0; i < 10000; i++ {
err = db.Put(utils.GetTestKey(i), utils.RandomValue(10))
assert.Nil(t, err)
}
for i := 10000; i < 100001; i++ {
err = db.PutWithTTL(utils.GetTestKey(i), utils.RandomValue(10), time.Second*1)
assert.Nil(t, err)
}

// wait for key to expire
time.Sleep(time.Second * 2)

db.DeleteExpiredKeys(time.Second * 2)
assert.Equal(t, 10000, db.Stat().KeysNum)
}
Loading