Consolidate

This commit is contained in:
Philip O'Toole
2026-01-15 22:02:04 -05:00
parent d70fbd34f1
commit 4590dc326a
3 changed files with 37 additions and 44 deletions

View File

@@ -1,44 +0,0 @@
package snapshot2
import (
"fmt"
"path/filepath"
"sort"
)
// SnapshotResolver resolves a snapshot ID into its constituent DB file and WAL files. At a minimum,
// a DB file is returned. If the snapshot is incremental, associated WAL files are also returned. The
// order in the slice is the order in which the WAL files should be applied to the DB file.
func SnapshotResolver(rootDir string, snapshotID string) (dbfile string, walFiles []string, err error) {
snapshots, err := getSnapshots(rootDir)
if err != nil {
return "", nil, err
}
// Walk from newest to oldest.
snapshotMetas := snapMetaSlice(snapshots)
sort.Sort(sort.Reverse(snapshotMetas))
found := false
for _, snapMeta := range snapshots {
if snapMeta.ID != snapshotID && !found {
continue
}
found = true
if snapMeta.Type == SnapshotMetaTypeFull {
// Full DB file so, we're done.
return filepath.Join(rootDir, snapMeta.ID, dbfileName), walFiles, nil
}
// Must be incremental. We need to prepend the WAL file name to the list and
// keep walking backwards
walFile := filepath.Join(rootDir, snapMeta.ID, walfileName)
walFiles = append([]string{walFile}, walFiles...)
continue
}
if found {
return "", nil, fmt.Errorf("no full snapshot found in chain for snapshot ID %s", snapshotID)
}
return "", nil, ErrSnapshotNotFound
}

View File

@@ -75,6 +75,43 @@ const (
SnapshotMetaTypeIncremental
)
// SnapshotResolver resolves a snapshot ID into its constituent DB file and WAL files. At a minimum,
// a DB file is returned. If the snapshot is incremental, associated WAL files are also returned. The
// order in the slice is the order in which the WAL files should be applied to the DB file.
func SnapshotResolver(rootDir string, snapshotID string) (dbfile string, walFiles []string, err error) {
snapshots, err := getSnapshots(rootDir)
if err != nil {
return "", nil, err
}
// Walk from newest to oldest.
snapshotMetas := snapMetaSlice(snapshots)
sort.Sort(sort.Reverse(snapshotMetas))
found := false
for _, snapMeta := range snapshots {
if snapMeta.ID != snapshotID && !found {
continue
}
found = true
if snapMeta.Type == SnapshotMetaTypeFull {
// Full DB file so, we're done.
return filepath.Join(rootDir, snapMeta.ID, dbfileName), walFiles, nil
}
// Must be incremental. We need to prepend the WAL file name to the list and
// keep walking backwards
walFile := filepath.Join(rootDir, snapMeta.ID, walfileName)
walFiles = append([]string{walFile}, walFiles...)
continue
}
if found {
return "", nil, fmt.Errorf("no full snapshot found in chain for snapshot ID %s", snapshotID)
}
return "", nil, ErrSnapshotNotFound
}
// SnapshotMeta represents metadata about a snapshot.
type SnapshotMeta struct {
*raft.SnapshotMeta