package table
import (
"context"
"path/filepath"
"regexp"
"time"
"github.com/thanos-io/objstore"
)
var (
metadataRegexp = regexp .MustCompile (`v[0-9]+\.metadata.json` )
dataRegexp = regexp .MustCompile (`[0-9A-Z]{26}.parquet` )
snapshotRegexp = regexp .MustCompile (`snap-[0-9]{19}-[0-9A-Z]{26}\.avro` )
manifestRegexp = regexp .MustCompile (`[0-9A-Z]{26}\.avro` )
deletableFile = []*regexp .Regexp {metadataRegexp , dataRegexp , snapshotRegexp , manifestRegexp }
)
func DeleteOrphanFiles (ctx context .Context , table Table , age time .Duration ) error {
foundFiles := map [string ]struct {}{
filepath .Base (table .MetadataLocation ()): {},
}
for _ , logEntry := range table .Metadata ().GetMetadataLog () {
foundFiles [filepath .Base (logEntry .MetadataFile )] = struct {}{}
}
for _ , snapshot := range table .Metadata ().Snapshots () {
foundFiles [filepath .Base (snapshot .ManifestList )] = struct {}{}
manifests , err := snapshot .Manifests (table .Bucket ())
if err != nil {
return err
}
for _ , manifest := range manifests {
foundFiles [filepath .Base (manifest .FilePath ())] = struct {}{}
entries , _ , err := manifest .FetchEntries (table .Bucket (), false )
if err != nil {
return err
}
for _ , entry := range entries {
foundFiles [filepath .Base (entry .DataFile ().FilePath ())] = struct {}{}
}
}
}
return table .Bucket ().Iter (ctx , table .Location (), func (file string ) error {
if _ , ok := foundFiles [filepath .Base (file )]; !ok {
if !isDeletable (file ) {
return nil
}
info , err := table .Bucket ().Attributes (ctx , file )
if err != nil {
return err
}
if time .Since (info .LastModified ) < age {
return nil
}
return table .Bucket ().Delete (ctx , file )
}
return nil
}, objstore .WithRecursiveIter )
}
func isDeletable(file string ) bool {
for _ , reg := range deletableFile {
if reg .MatchString (file ) {
return true
}
}
return false
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .