package shard import ( "encoding/json" "fmt" "os" "path/filepath" "archer/internal/atomicfile" "archer/internal/row" ) // PartialMeta describes the durable prefix of an in-progress shard. It is // written before each chunk is committed, so a resume can truncate the file // back to a consistent offset and continue appending. type PartialMeta struct { Seq int64 `json:"seq"` Bucket string `json:"bucket,omitempty"` Rows int64 `json:"rows"` Bytes int64 `json:"bytes"` FirstKey row.Key `json:"first_key"` LastKey row.Key `json:"last_key"` } func partialPath(dir string, seq int64) string { return filepath.Join(dir, fmt.Sprintf("%06d.partial", seq)) } func partialMetaPath(dir string, seq int64) string { return partialPath(dir, seq) + ".meta" } // PartialPath exposes the partial path for tests and diagnostics. func PartialPath(root, bucket string, seq int64) string { return partialPath(BucketDir(root, bucket), seq) } // LoadPartialMeta reads the sidecar for an in-progress shard. A missing file // returns (nil, nil). func LoadPartialMeta(root, bucket string, seq int64) (*PartialMeta, error) { path := partialMetaPath(BucketDir(root, bucket), seq) b, err := os.ReadFile(path) if os.IsNotExist(err) { return nil, nil } if err != nil { return nil, fmt.Errorf("read partial meta: %w", err) } var m PartialMeta if err := json.Unmarshal(b, &m); err != nil { return nil, fmt.Errorf("parse partial meta: %w", err) } return &m, nil } func savePartialMeta(dir string, m *PartialMeta) error { path := partialMetaPath(dir, m.Seq) b, err := json.Marshal(m) if err != nil { return err } tmp := path + ".tmp" f, err := os.OpenFile(tmp, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) if err != nil { return err } if _, err := f.Write(b); err != nil { f.Close() os.Remove(tmp) return err } if err := f.Sync(); err != nil { f.Close() os.Remove(tmp) return err } if err := f.Close(); err != nil { os.Remove(tmp) return err } if err := atomicfile.Replace(tmp, path); err != nil { os.Remove(tmp) return err } return nil } func removePartialMeta(dir string, seq int64) { os.Remove(partialMetaPath(dir, seq)) }