| 1 | package session |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | ) |
| 9 | |
| 10 | // Cold metadata reads the durable log once, one complete transaction at a time. |
| 11 | // Repeated small pages would re-read up to 256 commits from sparse checkpoints. |
| 12 | func (h *readHandle) scanCatalog(ctx context.Context, apply func(Commit) error) error { |
| 13 | if h == nil { |
| 14 | return os.ErrClosed |
| 15 | } |
| 16 | h.mu.Lock() |
| 17 | closed, dir := h.closed, h.dir |
| 18 | h.mu.Unlock() |
| 19 | if closed { |
| 20 | return os.ErrClosed |
| 21 | } |
| 22 | if err := ctx.Err(); err != nil { |
| 23 | return err |
| 24 | } |
| 25 | manifest, err := readStoredManifest(filepath.Join(dir, "manifest.json")) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | file, err := os.Open(logPathForManifest(dir, manifest)) |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | defer file.Close() |
| 34 | var applyErr error |
| 35 | visit := func(_ int64, commit Commit) bool { |
| 36 | if ctx.Err() != nil { |
| 37 | return false |
| 38 | } |
| 39 | applyErr = apply(commit) |
| 40 | return applyErr == nil |
| 41 | } |
| 42 | if manifest.Codec == Codec { |
| 43 | err = scanV4CommitFile(ctx, file, 0, 1, contentStoreForSessionDir(dir), nil, visit) |
| 44 | } else { |
| 45 | err = scanCommitFileCodec(file, 0, 1, manifest.Codec, nil, visit) |
| 46 | } |
| 47 | return errors.Join(err, applyErr, ctx.Err()) |
| 48 | } |
| 49 |