rocksdb_ttl.go 915 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //go:build rocksdb
  2. // +build rocksdb
  3. package rocksdb
  4. import (
  5. "time"
  6. gorocksdb "github.com/linxGnu/grocksdb"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. )
  9. type TTLFilter struct {
  10. skipLevel0 bool
  11. }
  12. func NewTTLFilter() gorocksdb.CompactionFilter {
  13. return &TTLFilter{
  14. skipLevel0: true,
  15. }
  16. }
  17. func (t *TTLFilter) Filter(level int, key, val []byte) (remove bool, newVal []byte) {
  18. // decode could be slow, causing write stall
  19. // level >0 sst can run compaction in parallel
  20. if !t.skipLevel0 || level > 0 {
  21. entry := filer.Entry{}
  22. if err := entry.DecodeAttributesAndChunks(val); err == nil {
  23. if entry.TtlSec > 0 &&
  24. entry.Crtime.Add(time.Duration(entry.TtlSec)*time.Second).Before(time.Now()) {
  25. return true, nil
  26. }
  27. }
  28. }
  29. return false, val
  30. }
  31. func (t *TTLFilter) Name() string {
  32. return "TTLFilter"
  33. }
  34. func (t *TTLFilter) SetIgnoreSnapshots(value bool) {
  35. }
  36. func (t *TTLFilter) Destroy() {
  37. }