Merge pull request #367 from ahrtr/resolve_conflict_20221229

Resolve conflict
pull/368/head
Benjamin Wang 2022-12-29 17:24:47 +08:00 committed by GitHub
commit cc44e8614e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 42 deletions

4
db.go
View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"hash/fnv" "hash/fnv"
"io"
"os" "os"
"runtime" "runtime"
"sort" "sort"
@ -377,7 +378,8 @@ func (db *DB) getPageSizeFromSecondMeta() (int, bool, error) {
if pos >= fileSize-1024 { if pos >= fileSize-1024 {
break break
} }
if bw, err := db.file.ReadAt(buf[:], pos); err == nil && bw == len(buf) { bw, err := db.file.ReadAt(buf[:], pos)
if (err == nil && bw == len(buf)) || (err == io.EOF && int64(bw) == (fileSize-pos)) {
metaCanRead = true metaCanRead = true
if m := db.pageInBuffer(buf[:], 0).meta(); m.validate() == nil { if m := db.pageInBuffer(buf[:], 0).meta(); m.validate() == nil {
return int(m.pageSize), metaCanRead, nil return int(m.pageSize), metaCanRead, nil

View File

@ -215,14 +215,10 @@ func TestOpen_ErrChecksum(t *testing.T) {
// The page size is expected to be the OS's page size in this case. // The page size is expected to be the OS's page size in this case.
func TestOpen_ReadPageSize_FromMeta1_OS(t *testing.T) { func TestOpen_ReadPageSize_FromMeta1_OS(t *testing.T) {
// Create empty database. // Create empty database.
db := MustOpenDB() db := btesting.MustCreateDB(t)
path := db.Path() path := db.Path()
defer db.MustClose() // Close the database
db.MustClose()
// Close database.
if err := db.DB.Close(); err != nil {
t.Fatal(err)
}
// Read data file. // Read data file.
buf, err := os.ReadFile(path) buf, err := os.ReadFile(path)
@ -238,16 +234,8 @@ func TestOpen_ReadPageSize_FromMeta1_OS(t *testing.T) {
} }
// Reopen data file. // Reopen data file.
if db, err := bolt.Open(path, 0666, nil); err != nil { db = btesting.MustOpenDBWithOption(t, path, nil)
t.Fatalf("unexpected error: %s", err) require.Equalf(t, os.Getpagesize(), db.Info().PageSize, "check page size failed")
} else {
if db.Info().PageSize != os.Getpagesize() {
t.Fatalf("The page size is expected to be %d, but actually is %d", os.Getpagesize(), db.Info().PageSize)
}
if err := db.Close(); err != nil {
panic(err)
}
}
} }
// Ensure that it can read the page size from the second meta page if the first one is invalid. // Ensure that it can read the page size from the second meta page if the first one is invalid.
@ -256,40 +244,30 @@ func TestOpen_ReadPageSize_FromMeta1_Given(t *testing.T) {
// test page size from 1KB (1024<<0) to 16MB(1024<<14) // test page size from 1KB (1024<<0) to 16MB(1024<<14)
for i := 0; i <= 14; i++ { for i := 0; i <= 14; i++ {
givenPageSize := 1024 << uint(i) givenPageSize := 1024 << uint(i)
t.Logf("Testing page size %d", givenPageSize)
// Create empty database. // Create empty database.
db := MustOpenWithOption(&bolt.Options{PageSize: givenPageSize}) db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: givenPageSize})
path := db.Path() path := db.Path()
defer db.MustClose() // Close the database
db.MustClose()
// Close database.
if err := db.DB.Close(); err != nil {
t.Fatal(err)
}
// Read data file. // Read data file.
buf, err := os.ReadFile(path) buf, err := os.ReadFile(path)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
// Rewrite meta pages. // Rewrite meta pages.
meta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize])) if i%3 == 0 {
meta0.pgid++ t.Logf("#%d: Intentionally corrupt the first meta page for pageSize %d", i, givenPageSize)
if err := os.WriteFile(path, buf, 0666); err != nil { meta0 := (*meta)(unsafe.Pointer(&buf[pageHeaderSize]))
t.Fatal(err) meta0.pgid++
err = os.WriteFile(path, buf, 0666)
require.NoError(t, err)
} }
// Reopen data file. // Reopen data file.
if db, err := bolt.Open(path, 0666, &bolt.Options{PageSize: givenPageSize}); err != nil { db = btesting.MustOpenDBWithOption(t, path, nil)
t.Fatalf("unexpected error: %s", err) require.Equalf(t, givenPageSize, db.Info().PageSize, "check page size failed")
} else { db.MustClose()
if db.Info().PageSize != givenPageSize {
t.Fatalf("The page size is expected to be %d, but actually is %d", givenPageSize, db.Info().PageSize)
}
if err := db.Close(); err != nil {
panic(err)
}
}
} }
} }