aboutsummaryrefslogtreecommitdiff
path: root/db.go
diff options
context:
space:
mode:
Diffstat (limited to 'db.go')
-rw-r--r--db.go24
1 files changed, 11 insertions, 13 deletions
diff --git a/db.go b/db.go
index d759b1b..8f80515 100644
--- a/db.go
+++ b/db.go
@@ -6,7 +6,6 @@ import (
"hash/fnv"
"os"
"sync"
- "syscall"
"unsafe"
)
@@ -68,7 +67,9 @@ type DB struct {
path string
file *os.File
- data []byte
+ dataref []byte
+ data *[maxMapSize]byte
+ datasz int
meta0 *meta
meta1 *meta
pageSize int
@@ -120,7 +121,7 @@ func Open(path string, mode os.FileMode) (*DB, error) {
// Lock file so that other processes using Bolt cannot use the database
// at the same time. This would cause corruption since the two processes
// would write meta pages and free pages separately.
- if err := syscall.Flock(int(db.file.Fd()), syscall.LOCK_EX); err != nil {
+ if err := flock(db.file); err != nil {
_ = db.close()
return nil, err
}
@@ -193,7 +194,7 @@ func (db *DB) mmap(minsz int) error {
size = db.mmapSize(size)
// Memory-map the data file as a byte slice.
- if db.data, err = syscall.Mmap(int(db.file.Fd()), 0, size, syscall.PROT_READ, syscall.MAP_SHARED); err != nil {
+ if err := mmap(db, size); err != nil {
return err
}
@@ -214,11 +215,8 @@ func (db *DB) mmap(minsz int) error {
// munmap unmaps the data file from memory.
func (db *DB) munmap() error {
- if db.data != nil {
- if err := syscall.Munmap(db.data); err != nil {
- return fmt.Errorf("unmap error: " + err.Error())
- }
- db.data = nil
+ if err := munmap(db); err != nil {
+ return fmt.Errorf("unmap error: " + err.Error())
}
return nil
}
@@ -314,7 +312,7 @@ func (db *DB) close() error {
// Close file handles.
if db.file != nil {
// Unlock the file.
- _ = syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)
+ _ = funlock(db.file)
// Close the file descriptor.
if err := db.file.Close(); err != nil {
@@ -503,7 +501,7 @@ func (db *DB) Stats() Stats {
// This is for internal access to the raw data bytes from the C cursor, use
// carefully, or not at all.
func (db *DB) Info() *Info {
- return &Info{db.data, db.pageSize}
+ return &Info{uintptr(unsafe.Pointer(&db.data[0])), db.pageSize}
}
// page retrieves a page reference from the mmap based on the current page size.
@@ -540,7 +538,7 @@ func (db *DB) allocate(count int) (*page, error) {
// Resize mmap() if we're at the end.
p.id = db.rwtx.meta.pgid
var minsz = int((p.id+pgid(count))+1) * db.pageSize
- if minsz >= len(db.data) {
+ if minsz >= db.datasz {
if err := db.mmap(minsz); err != nil {
return nil, fmt.Errorf("mmap allocate error: %s", err)
}
@@ -575,7 +573,7 @@ func (s *Stats) add(other *Stats) {
}
type Info struct {
- Data []byte
+ Data uintptr
PageSize int
}