mirror of https://github.com/etcd-io/bbolt.git
make ignoring Truncate() explicit
https://github.com/boltdb/bolt/pull/371#issuecomment-103176330pull/34/head
parent
fda75748b5
commit
aa13f7f94f
|
@ -48,8 +48,8 @@ func funlock(f *os.File) error {
|
||||||
func mmap(db *DB, sz int) error {
|
func mmap(db *DB, sz int) error {
|
||||||
// Truncate and fsync to ensure file size metadata is flushed.
|
// Truncate and fsync to ensure file size metadata is flushed.
|
||||||
// https://github.com/boltdb/bolt/issues/284
|
// https://github.com/boltdb/bolt/issues/284
|
||||||
if !db.NoGrowSync {
|
if !db.NoGrowSync && !db.readOnly {
|
||||||
if err := db.ops.Truncate(int64(sz)); err != nil {
|
if err := db.file.Truncate(int64(sz)); err != nil {
|
||||||
return fmt.Errorf("file resize error: %s", err)
|
return fmt.Errorf("file resize error: %s", err)
|
||||||
}
|
}
|
||||||
if err := db.file.Sync(); err != nil {
|
if err := db.file.Sync(); err != nil {
|
||||||
|
|
|
@ -28,10 +28,12 @@ func funlock(f *os.File) error {
|
||||||
// mmap memory maps a DB's data file.
|
// mmap memory maps a DB's data file.
|
||||||
// Based on: https://github.com/edsrzf/mmap-go
|
// Based on: https://github.com/edsrzf/mmap-go
|
||||||
func mmap(db *DB, sz int) error {
|
func mmap(db *DB, sz int) error {
|
||||||
|
if !db.readOnly {
|
||||||
// Truncate the database to the size of the mmap.
|
// Truncate the database to the size of the mmap.
|
||||||
if err := db.ops.Truncate(int64(sz)); err != nil {
|
if err := db.file.Truncate(int64(sz)); err != nil {
|
||||||
return fmt.Errorf("truncate: %s", err)
|
return fmt.Errorf("truncate: %s", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Open a file mapping handle.
|
// Open a file mapping handle.
|
||||||
sizelo := uint32(sz >> 32)
|
sizelo := uint32(sz >> 32)
|
||||||
|
|
7
db.go
7
db.go
|
@ -103,7 +103,6 @@ type DB struct {
|
||||||
|
|
||||||
ops struct {
|
ops struct {
|
||||||
writeAt func(b []byte, off int64) (n int, err error)
|
writeAt func(b []byte, off int64) (n int, err error)
|
||||||
Truncate func(size int64) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
readOnly bool // Read only mode. Update()/Begin(true) would return ErrDatabaseReadOnly immediately.
|
readOnly bool // Read only mode. Update()/Begin(true) would return ErrDatabaseReadOnly immediately.
|
||||||
|
@ -144,8 +143,6 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
|
||||||
if options.ReadOnly {
|
if options.ReadOnly {
|
||||||
flag = os.O_RDONLY
|
flag = os.O_RDONLY
|
||||||
db.readOnly = true
|
db.readOnly = true
|
||||||
// Ignore truncations.
|
|
||||||
db.ops.Truncate = func(int64) error { return nil }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open data file and separate sync handler for metadata writes.
|
// Open data file and separate sync handler for metadata writes.
|
||||||
|
@ -156,10 +153,6 @@ func Open(path string, mode os.FileMode, options *Options) (*DB, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !db.readOnly {
|
|
||||||
db.ops.Truncate = db.file.Truncate
|
|
||||||
}
|
|
||||||
|
|
||||||
// Lock file so that other processes using Bolt in read-write mode cannot
|
// Lock file so that other processes using Bolt in read-write mode cannot
|
||||||
// use the database at the same time. This would cause corruption since
|
// use the database at the same time. This would cause corruption since
|
||||||
// the two processes would write meta pages and free pages separately.
|
// the two processes would write meta pages and free pages separately.
|
||||||
|
|
Loading…
Reference in New Issue