2014-06-11 17:11:21 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
2014-06-12 15:23:30 +00:00
|
|
|
"fmt"
|
2014-06-11 17:11:21 +00:00
|
|
|
"os"
|
|
|
|
"syscall"
|
2014-06-21 20:44:22 +00:00
|
|
|
"time"
|
2014-06-11 17:11:21 +00:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var odirect int
|
|
|
|
|
|
|
|
// fdatasync flushes written data to a file descriptor.
|
2014-09-18 20:15:52 +00:00
|
|
|
func fdatasync(db *DB) error {
|
|
|
|
return db.file.Sync()
|
2014-06-11 17:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// flock acquires an advisory lock on a file descriptor.
|
2015-05-18 18:07:12 +00:00
|
|
|
func flock(f *os.File, _ bool, _ time.Duration) error {
|
2014-06-11 17:11:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// funlock releases an advisory lock on a file descriptor.
|
|
|
|
func funlock(f *os.File) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-12 15:23:30 +00:00
|
|
|
// mmap memory maps a DB's data file.
|
2014-06-11 17:11:21 +00:00
|
|
|
// Based on: https://github.com/edsrzf/mmap-go
|
2014-06-12 15:23:30 +00:00
|
|
|
func mmap(db *DB, sz int) error {
|
|
|
|
// Truncate the database to the size of the mmap.
|
2015-05-14 22:43:13 +00:00
|
|
|
if err := db.ops.Truncate(int64(sz)); err != nil {
|
2014-06-12 15:23:30 +00:00
|
|
|
return fmt.Errorf("truncate: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-06-11 17:11:21 +00:00
|
|
|
// Open a file mapping handle.
|
2014-06-12 15:23:30 +00:00
|
|
|
sizelo := uint32(sz >> 32)
|
2014-07-13 08:14:27 +00:00
|
|
|
sizehi := uint32(sz) & 0xffffffff
|
2014-06-12 15:23:30 +00:00
|
|
|
h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizelo, sizehi, nil)
|
2014-06-11 17:11:21 +00:00
|
|
|
if h == 0 {
|
2014-06-12 15:23:30 +00:00
|
|
|
return os.NewSyscallError("CreateFileMapping", errno)
|
2014-06-11 17:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the memory map.
|
|
|
|
addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
|
|
|
|
if addr == 0 {
|
2014-06-12 15:23:30 +00:00
|
|
|
return os.NewSyscallError("MapViewOfFile", errno)
|
2014-06-11 17:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close mapping handle.
|
|
|
|
if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
|
2014-06-12 15:23:30 +00:00
|
|
|
return os.NewSyscallError("CloseHandle", err)
|
2014-06-11 17:11:21 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 15:23:30 +00:00
|
|
|
// Convert to a byte array.
|
|
|
|
db.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr)))
|
|
|
|
db.datasz = sz
|
|
|
|
|
|
|
|
return nil
|
2014-06-11 17:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// munmap unmaps a pointer from a file.
|
|
|
|
// Based on: https://github.com/edsrzf/mmap-go
|
2014-06-12 15:23:30 +00:00
|
|
|
func munmap(db *DB) error {
|
|
|
|
if db.data == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := (uintptr)(unsafe.Pointer(&db.data[0]))
|
2014-06-11 17:11:21 +00:00
|
|
|
if err := syscall.UnmapViewOfFile(addr); err != nil {
|
|
|
|
return os.NewSyscallError("UnmapViewOfFile", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|