(cfg checkConfig, ch chan error)
| 36 | } |
| 37 | |
| 38 | func (tx *Tx) check(cfg checkConfig, ch chan error) { |
| 39 | defer func() { |
| 40 | if r := recover(); r != nil { |
| 41 | ch <- panicked{r} |
| 42 | } |
| 43 | }() |
| 44 | // Force loading free list if opened in ReadOnly mode. |
| 45 | tx.db.loadFreelist() |
| 46 | |
| 47 | // Check if any pages are double freed. |
| 48 | freed := make(map[common.Pgid]bool) |
| 49 | all := make([]common.Pgid, tx.db.freelist.Count()) |
| 50 | tx.db.freelist.Copyall(all) |
| 51 | for _, id := range all { |
| 52 | if freed[id] { |
| 53 | ch <- fmt.Errorf("page %d: already freed", id) |
| 54 | } |
| 55 | freed[id] = true |
| 56 | } |
| 57 | |
| 58 | // Track every reachable page. |
| 59 | reachable := make(map[common.Pgid]*common.Page) |
| 60 | reachable[0] = tx.page(0) // meta0 |
| 61 | reachable[1] = tx.page(1) // meta1 |
| 62 | if tx.meta.Freelist() != common.PgidNoFreelist { |
| 63 | for i := uint32(0); i <= tx.page(tx.meta.Freelist()).Overflow(); i++ { |
| 64 | reachable[tx.meta.Freelist()+common.Pgid(i)] = tx.page(tx.meta.Freelist()) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if cfg.pageId == 0 { |
| 69 | // Check the whole db file, starting from the root bucket and |
| 70 | // recursively check all child buckets. |
| 71 | tx.recursivelyCheckBucket(&tx.root, reachable, freed, cfg.kvStringer, ch) |
| 72 | |
| 73 | // Ensure all pages below high water mark are either reachable or freed. |
| 74 | for i := common.Pgid(0); i < tx.meta.Pgid(); i++ { |
| 75 | _, isReachable := reachable[i] |
| 76 | if !isReachable && !freed[i] { |
| 77 | ch <- fmt.Errorf("page %d: unreachable unfreed", int(i)) |
| 78 | } |
| 79 | } |
| 80 | } else { |
| 81 | // Check the db file starting from a specified pageId. |
| 82 | if cfg.pageId < 2 || cfg.pageId >= uint64(tx.meta.Pgid()) { |
| 83 | ch <- fmt.Errorf("page ID (%d) out of range [%d, %d)", cfg.pageId, 2, tx.meta.Pgid()) |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | tx.recursivelyCheckPage(common.Pgid(cfg.pageId), reachable, freed, cfg.kvStringer, ch) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func (tx *Tx) recursivelyCheckPage(pageId common.Pgid, reachable map[common.Pgid]*common.Page, freed map[common.Pgid]bool, |
| 92 | kvStringer KVStringer, ch chan error) { |
no test coverage detected