Skip to content

Commit b986966

Browse files
committed
add WriteToFlag to Tx
For in memory workload, it does not make sense to use o_direct to copy the file. Adding a option to clear out o_direct and for other future cases.
1 parent 47d80ed commit b986966

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

bolt_linux.go

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"syscall"
55
)
66

7-
var odirect = syscall.O_DIRECT
8-
97
// fdatasync flushes written data to a file descriptor.
108
func fdatasync(db *DB) error {
119
return syscall.Fdatasync(int(db.file.Fd()))

bolt_openbsd.go

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const (
1111
msInvalidate // invalidate cached data
1212
)
1313

14-
var odirect int
15-
1614
func msync(db *DB) error {
1715
_, _, errno := syscall.Syscall(syscall.SYS_MSYNC, uintptr(unsafe.Pointer(db.data)), uintptr(db.datasz), msInvalidate)
1816
if errno != 0 {

bolt_windows.go

-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ func unlockFileEx(h syscall.Handle, reserved, locklow, lockhigh uint32, ol *sysc
4040
return nil
4141
}
4242

43-
var odirect int
44-
4543
// fdatasync flushes written data to a file descriptor.
4644
func fdatasync(db *DB) error {
4745
return db.file.Sync()

tx.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ type Tx struct {
2929
pages map[pgid]*page
3030
stats TxStats
3131
commitHandlers []func()
32+
33+
// WriteFlag specifies the flag for write related methods
34+
// like WriteTo.
35+
// Tx opens the database file with the specified
36+
// flag to copy the data.
37+
//
38+
// By default, the flag is set to empty for in-memory workload.
39+
// To avoid cache trashing for large on disk workload, set this
40+
// flag with o_direct.
41+
WriteFlag int
3242
}
3343

3444
// init initializes the transaction.
@@ -272,13 +282,10 @@ func (tx *Tx) Copy(w io.Writer) error {
272282
// WriteTo writes the entire database to a writer.
273283
// If err == nil then exactly tx.Size() bytes will be written into the writer.
274284
func (tx *Tx) WriteTo(w io.Writer) (n int64, err error) {
275-
// Attempt to open reader directly.
285+
// Attempt to open reader with WriteFlag
276286
var f *os.File
277-
if f, err = os.OpenFile(tx.db.path, os.O_RDONLY|odirect, 0); err != nil {
278-
// Fallback to a regular open if that doesn't work.
279-
if f, err = os.OpenFile(tx.db.path, os.O_RDONLY, 0); err != nil {
280-
return 0, err
281-
}
287+
if f, err = os.OpenFile(tx.db.path, os.O_RDONLY|tx.WriteFlag, 0); err != nil {
288+
return 0, err
282289
}
283290

284291
// Copy the meta pages.

0 commit comments

Comments
 (0)