Delete old checkpoints
This commit is contained in:
parent
5e2dde384d
commit
6d8ca4c4d4
|
@ -8,7 +8,6 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -17,6 +16,8 @@ import (
|
|||
. "git.exozy.me/exozyme/status/scanner"
|
||||
)
|
||||
|
||||
const NumberOfCheckpointsToKeep = 3
|
||||
|
||||
type Row struct {
|
||||
time time.Time
|
||||
kind EventType
|
||||
|
@ -123,9 +124,6 @@ func OpenDatabase(dir string) (Database, error) {
|
|||
})
|
||||
for _, file := range files {
|
||||
filename := file.Name()
|
||||
if strings.HasSuffix(filename, ".tmp") {
|
||||
continue
|
||||
}
|
||||
_, err := ulid.Parse(filename)
|
||||
if err != nil {
|
||||
continue // because database files have ULID as names
|
||||
|
@ -165,10 +163,37 @@ func (db *dumbDatabase) Persist() error {
|
|||
defer db.mutex.Unlock()
|
||||
|
||||
db.sortRows()
|
||||
id := ulid.Make().String() // checkpoint filename is ULID
|
||||
|
||||
dir := db.rootDir
|
||||
// [section] clean up old checkpoints
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// sort the file name desc. Since the file names are ULID, newer files are sorted first
|
||||
sort.Slice(files, func(i, j int) bool {
|
||||
return files[i].Name() > files[j].Name()
|
||||
})
|
||||
// delete old files
|
||||
if len(files) > NumberOfCheckpointsToKeep {
|
||||
for _, file := range files[NumberOfCheckpointsToKeep:] {
|
||||
filename := file.Name()
|
||||
_, err := ulid.Parse(filename)
|
||||
if err != nil {
|
||||
continue // because database files have ULID as names
|
||||
}
|
||||
fullpath := path.Join(dir, filename)
|
||||
err = os.Remove(fullpath)
|
||||
if err != nil {
|
||||
log.Printf("Error when os.Delete: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
id := ulid.Make().String() // checkpoint filename is ULID
|
||||
filename_final := path.Join(db.rootDir, id)
|
||||
filename_tmp := path.Join(db.rootDir, id+".tmp")
|
||||
|
||||
file, err := os.Create(filename_tmp)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -187,6 +212,7 @@ func (db *dumbDatabase) Persist() error {
|
|||
return err
|
||||
}
|
||||
log.Printf("Saved %d rows to: %v", len(db.rows), filename_final)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue