rocksdb_ttl.go 795 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. entry := filer.Entry{}
  21. if err := entry.DecodeAttributesAndChunks(val); err == nil {
  22. if entry.TtlSec > 0 &&
  23. entry.Crtime.Add(time.Duration(entry.TtlSec)*time.Second).Before(time.Now()) {
  24. return true, nil
  25. }
  26. }
  27. }
  28. return false, val
  29. }
  30. func (t *TTLFilter) Name() string {
  31. return "TTLFilter"
  32. }