compactBlocks compacts all the eligible on-disk blocks. The db.cmtx should be held before calling this method.
()
| 1761 | // compactBlocks compacts all the eligible on-disk blocks. |
| 1762 | // The db.cmtx should be held before calling this method. |
| 1763 | func (db *DB) compactBlocks() (err error) { |
| 1764 | // Check for compactions of multiple blocks. |
| 1765 | for { |
| 1766 | // If we have a lot of blocks to compact the whole process might take |
| 1767 | // long enough that we end up with a HEAD block that needs to be written. |
| 1768 | // Check if that's the case and stop compactions early. |
| 1769 | if db.head.compactable() && !db.waitingForCompactionDelay() { |
| 1770 | db.logger.Warn("aborting block compactions to persist the head block") |
| 1771 | return nil |
| 1772 | } |
| 1773 | |
| 1774 | plan, err := db.compactor.Plan(db.dir) |
| 1775 | if err != nil { |
| 1776 | return fmt.Errorf("plan compaction: %w", err) |
| 1777 | } |
| 1778 | if len(plan) == 0 { |
| 1779 | break |
| 1780 | } |
| 1781 | |
| 1782 | select { |
| 1783 | case <-db.stopc: |
| 1784 | return nil |
| 1785 | default: |
| 1786 | } |
| 1787 | |
| 1788 | uids, err := db.compactor.Compact(db.dir, plan, db.blocks) |
| 1789 | if err != nil { |
| 1790 | return fmt.Errorf("compact %s: %w", plan, err) |
| 1791 | } |
| 1792 | |
| 1793 | if err := db.reloadBlocks(); err != nil { |
| 1794 | errs := []error{fmt.Errorf("reloadBlocks blocks: %w", err)} |
| 1795 | for _, uid := range uids { |
| 1796 | if errRemoveAll := os.RemoveAll(filepath.Join(db.dir, uid.String())); errRemoveAll != nil { |
| 1797 | errs = append(errs, fmt.Errorf("delete persisted block after failed db reloadBlocks:%s: %w", uid, errRemoveAll)) |
| 1798 | } |
| 1799 | } |
| 1800 | return errors.Join(errs...) |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | return nil |
| 1805 | } |
| 1806 | |
| 1807 | // getBlock iterates a given block range to find a block by a given id. |
| 1808 | // If found it returns the block itself and a boolean to indicate that it was found. |
no test coverage detected