You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
808 B

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