update WriteTo() docs

This commit updates and revises some of the documentation around
Tx.WriteTo() and how O_DIRECT is no longer the default.
master
Ben Johnson 2015-11-06 13:18:58 -07:00
parent b986966361
commit 6b1bbf0ab4
3 changed files with 13 additions and 15 deletions

View File

@ -426,8 +426,11 @@ func (*Bucket) DeleteBucket(key []byte) error
Bolt is a single file so it's easy to backup. You can use the `Tx.WriteTo()` Bolt is a single file so it's easy to backup. You can use the `Tx.WriteTo()`
function to write a consistent view of the database to a writer. If you call function to write a consistent view of the database to a writer. If you call
this from a read-only transaction, it will perform a hot backup and not block this from a read-only transaction, it will perform a hot backup and not block
your other database reads and writes. It will also use `O_DIRECT` when available your other database reads and writes.
to prevent page cache trashing.
By default, it will use a regular file handle which will utilize the operating
system's page cache. See the [`Tx`](https://godoc.org/github.com/boltdb/bolt#Tx)
documentation for information about optimizing for larger-than-RAM datasets.
One common use case is to backup over HTTP so you can use tools like `cURL` to One common use case is to backup over HTTP so you can use tools like `cURL` to
do database backups: do database backups:

View File

@ -2,8 +2,6 @@
package bolt package bolt
var odirect int
// fdatasync flushes written data to a file descriptor. // fdatasync flushes written data to a file descriptor.
func fdatasync(db *DB) error { func fdatasync(db *DB) error {
return db.file.Sync() return db.file.Sync()

19
tx.go
View File

@ -30,14 +30,12 @@ type Tx struct {
stats TxStats stats TxStats
commitHandlers []func() commitHandlers []func()
// WriteFlag specifies the flag for write related methods // WriteFlag specifies the flag for write-related methods like WriteTo().
// like WriteTo. // Tx opens the database file with the specified flag to copy the data.
// Tx opens the database file with the specified
// flag to copy the data.
// //
// By default, the flag is set to empty for in-memory workload. // By default, the flag is unset, which works well for mostly in-memory
// To avoid cache trashing for large on disk workload, set this // workloads. For databases that are much larger than available RAM,
// flag with o_direct. // set the flag to syscall.O_DIRECT to avoid trashing the page cache.
WriteFlag int WriteFlag int
} }
@ -283,17 +281,17 @@ func (tx *Tx) Copy(w io.Writer) error {
// If err == nil then exactly tx.Size() bytes will be written into the writer. // If err == nil then exactly tx.Size() bytes will be written into the writer.
func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) { func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) {
// Attempt to open reader with WriteFlag // Attempt to open reader with WriteFlag
var f *os.File f, err := os.OpenFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0)
if f, err = os.OpenFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0); err != nil { if err != nil {
return 0, err return 0, err
} }
defer f.Close()
// Copy the meta pages. // Copy the meta pages.
tx.db.metalock.Lock() tx.db.metalock.Lock()
n, err = io.CopyN(w, f, int64(tx.db.pageSize*2)) n, err = io.CopyN(w, f, int64(tx.db.pageSize*2))
tx.db.metalock.Unlock() tx.db.metalock.Unlock()
if err != nil { if err != nil {
_ = f.Close()
return n, fmt.Errorf("meta copy: %s", err) return n, fmt.Errorf("meta copy: %s", err)
} }
@ -301,7 +299,6 @@ func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) {
wn, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2)) wn, err := io.CopyN(w, f, tx.Size()-int64(tx.db.pageSize*2))
n += wn n += wn
if err != nil { if err != nil {
_ = f.Close()
return n, err return n, err
} }