Skip to content

Commit

Permalink
levels: Expired keys and delete markers are never purged
Browse files Browse the repository at this point in the history
  • Loading branch information
damz committed Jun 2, 2020
1 parent a7e239e commit 56cbfba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,17 @@ func TestExpiry(t *testing.T) {
return nil
})
require.NoError(t, err)

// Reopen the database to force the memory table to disk.
err = db.Close()
require.NoError(t, err)
db, err = Open(db.opt)
require.NoError(t, err)

// Only one table and two keys remain, the `!badger!head` key and `answer1`.
tables := db.Tables(true)
require.Equal(t, 1, len(tables))
require.Equal(t, uint64(2), tables[0].KeyCount)
})
}

Expand Down
6 changes: 4 additions & 2 deletions levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,15 @@ func (s *levelsController) compactBuildTables(
lastValidVersion := vs.Meta&bitDiscardEarlierVersions > 0 ||
numVersions == s.kv.opt.NumVersionsToKeep

if isDeletedOrExpired(vs.Meta, vs.ExpiresAt) || lastValidVersion {
isExpired := isDeletedOrExpired(vs.Meta, vs.ExpiresAt)

if isExpired || lastValidVersion {
// If this version of the key is deleted or expired, skip all the rest of the
// versions. Ensure that we're only removing versions below readTs.
skipKey = y.SafeCopy(skipKey, it.Key())

switch {
case lastValidVersion:
case !isExpired && lastValidVersion:
// Add this key. We have set skipKey, so the following key versions
// would be skipped.
case hasOverlap:
Expand Down

0 comments on commit 56cbfba

Please sign in to comment.