LastChunkSnapshot returns the directory name and index of the most recent chunk snapshot. If dir does not contain any chunk snapshots, ErrNotFound is returned.
(dir string)
| 1556 | // LastChunkSnapshot returns the directory name and index of the most recent chunk snapshot. |
| 1557 | // If dir does not contain any chunk snapshots, ErrNotFound is returned. |
| 1558 | func LastChunkSnapshot(dir string) (string, int, int, error) { |
| 1559 | files, err := os.ReadDir(dir) |
| 1560 | if err != nil { |
| 1561 | return "", 0, 0, err |
| 1562 | } |
| 1563 | maxIdx, maxOffset := -1, -1 |
| 1564 | maxFileName := "" |
| 1565 | for i := range files { |
| 1566 | fi := files[i] |
| 1567 | |
| 1568 | if !strings.HasPrefix(fi.Name(), chunkSnapshotPrefix) { |
| 1569 | continue |
| 1570 | } |
| 1571 | if !fi.IsDir() { |
| 1572 | return "", 0, 0, fmt.Errorf("chunk snapshot %s is not a directory", fi.Name()) |
| 1573 | } |
| 1574 | |
| 1575 | splits := strings.Split(fi.Name()[len(chunkSnapshotPrefix):], ".") |
| 1576 | if len(splits) != 2 { |
| 1577 | // Chunk snapshots is not in the right format, we do not care about it. |
| 1578 | continue |
| 1579 | } |
| 1580 | |
| 1581 | idx, err := strconv.Atoi(splits[0]) |
| 1582 | if err != nil { |
| 1583 | continue |
| 1584 | } |
| 1585 | |
| 1586 | offset, err := strconv.Atoi(splits[1]) |
| 1587 | if err != nil { |
| 1588 | continue |
| 1589 | } |
| 1590 | |
| 1591 | if idx > maxIdx || (idx == maxIdx && offset > maxOffset) { |
| 1592 | maxIdx, maxOffset = idx, offset |
| 1593 | maxFileName = filepath.Join(dir, fi.Name()) |
| 1594 | } |
| 1595 | } |
| 1596 | if maxFileName == "" { |
| 1597 | return "", 0, 0, record.ErrNotFound |
| 1598 | } |
| 1599 | return maxFileName, maxIdx, maxOffset, nil |
| 1600 | } |
| 1601 | |
| 1602 | // DeleteChunkSnapshots deletes all chunk snapshots in a directory below a given index. |
| 1603 | func DeleteChunkSnapshots(dir string, maxIndex, maxOffset int) error { |
searching dependent graphs…