getPageSize reads the pageSize from the meta pages. It tries to read the first meta page firstly. If the first page is invalid, then it tries to read the second page using the default page size.
()
| 330 | // to read the first meta page firstly. If the first page is invalid, |
| 331 | // then it tries to read the second page using the default page size. |
| 332 | func (db *DB) getPageSize() (int, error) { |
| 333 | var ( |
| 334 | meta0CanRead, meta1CanRead bool |
| 335 | ) |
| 336 | |
| 337 | // Read the first meta page to determine the page size. |
| 338 | if pgSize, canRead, err := db.getPageSizeFromFirstMeta(); err != nil { |
| 339 | // We cannot read the page size from page 0, but can read page 0. |
| 340 | meta0CanRead = canRead |
| 341 | } else { |
| 342 | return pgSize, nil |
| 343 | } |
| 344 | |
| 345 | // Read the second meta page to determine the page size. |
| 346 | if pgSize, canRead, err := db.getPageSizeFromSecondMeta(); err != nil { |
| 347 | // We cannot read the page size from page 1, but can read page 1. |
| 348 | meta1CanRead = canRead |
| 349 | } else { |
| 350 | return pgSize, nil |
| 351 | } |
| 352 | |
| 353 | // If we can't read the page size from both pages, but can read |
| 354 | // either page, then we assume it's the same as the OS or the one |
| 355 | // given, since that's how the page size was chosen in the first place. |
| 356 | // |
| 357 | // If both pages are invalid, and (this OS uses a different page size |
| 358 | // from what the database was created with or the given page size is |
| 359 | // different from what the database was created with), then we are out |
| 360 | // of luck and cannot access the database. |
| 361 | if meta0CanRead || meta1CanRead { |
| 362 | return db.pageSize, nil |
| 363 | } |
| 364 | |
| 365 | return 0, berrors.ErrInvalid |
| 366 | } |
| 367 | |
| 368 | // getPageSizeFromFirstMeta reads the pageSize from the first meta page |
| 369 | func (db *DB) getPageSizeFromFirstMeta() (int, bool, error) { |
no test coverage detected