loadFreelist reads the freelist if it is synced, or reconstructs it by scanning the DB if it is not synced. It assumes there are no concurrent accesses being made to the freelist.
()
| 420 | // by scanning the DB if it is not synced. It assumes there are no |
| 421 | // concurrent accesses being made to the freelist. |
| 422 | func (db *DB) loadFreelist() { |
| 423 | db.freelistLoad.Do(func() { |
| 424 | db.freelist = newFreelist(db.FreelistType) |
| 425 | if !db.hasSyncedFreelist() { |
| 426 | // Reconstruct free list by scanning the DB. |
| 427 | db.freelist.Init(db.freepages()) |
| 428 | } else { |
| 429 | // Read free list from freelist page. |
| 430 | db.freelist.Read(db.page(db.meta().Freelist())) |
| 431 | } |
| 432 | if db.stats != nil { |
| 433 | db.stats.FreePageN = db.freelist.FreeCount() |
| 434 | } |
| 435 | }) |
| 436 | } |
| 437 | |
| 438 | func (db *DB) hasSyncedFreelist() bool { |
| 439 | return db.meta().Freelist() != common.PgidNoFreelist |