aboutsummaryrefslogtreecommitdiff
path: root/weed/filer/rocksdb/rocksdb_ttl.go
blob: 08ad31061acc777ee3955e3deecbe15795ae67f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//go:build rocksdb
// +build rocksdb

package rocksdb

import (
	"time"

	gorocksdb "github.com/linxGnu/grocksdb"

	"github.com/seaweedfs/seaweedfs/weed/filer"
)

type TTLFilter struct {
	skipLevel0 bool
}

func NewTTLFilter() gorocksdb.CompactionFilter {
	return &TTLFilter{
		skipLevel0: true,
	}
}

func (t *TTLFilter) Filter(level int, key, val []byte) (remove bool, newVal []byte) {
	// decode could be slow, causing write stall
	// level >0 sst can run compaction in parallel
	if !t.skipLevel0 || level > 0 {
		entry := filer.Entry{}
		if err := entry.DecodeAttributesAndChunks(val); err == nil {
			if entry.TtlSec > 0 &&
				entry.Crtime.Add(time.Duration(entry.TtlSec)*time.Second).Before(time.Now()) {
				return true, nil
			}
		}
	}
	return false, val
}

func (t *TTLFilter) Name() string {
	return "TTLFilter"
}
func (t *TTLFilter) SetIgnoreSnapshots(value bool) {
}

func (t *TTLFilter) Destroy() {
}