aboutsummaryrefslogtreecommitdiff
path: root/db.go
diff options
context:
space:
mode:
Diffstat (limited to 'db.go')
-rw-r--r--db.go46
1 files changed, 41 insertions, 5 deletions
diff --git a/db.go b/db.go
index 646c31b..41fbc64 100644
--- a/db.go
+++ b/db.go
@@ -1,6 +1,7 @@
package bolt
import (
+ "io"
"os"
"sync"
"syscall"
@@ -114,7 +115,7 @@ func (db *DB) mmap() error {
}
// TEMP(benbjohnson): Set max size to 1MB.
- size := 2 << 20
+ size := 2 << 30
// Memory-map the data file as a byte slice.
if db.data, err = db.syscall.Mmap(int(db.file.Fd()), 0, size, syscall.PROT_READ, syscall.MAP_SHARED); err != nil {
@@ -224,10 +225,7 @@ func (db *DB) RWTransaction() (*RWTransaction, error) {
}
// Create a transaction associated with the database.
- t := &RWTransaction{
- branches: make(map[pgid]*branch),
- leafs: make(map[pgid]*leaf),
- }
+ t := &RWTransaction{nodes: make(map[pgid]*node)}
t.init(db)
return t, nil
@@ -319,6 +317,44 @@ func (db *DB) Delete(name string, key []byte) error {
return t.Commit()
}
+// Copy writes the entire database to a writer.
+func (db *DB) Copy(w io.Writer) error {
+ if !db.opened {
+ return DatabaseNotOpenError
+ }
+
+ // Maintain a reader transaction so pages don't get reclaimed.
+ t, err := db.Transaction()
+ if err != nil {
+ return err
+ }
+ defer t.Close()
+
+ // Open reader on the database.
+ f, err := os.Open(db.path)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+
+ // Copy everything.
+ if _, err := io.Copy(w, f); err != nil {
+ return err
+ }
+ return nil
+}
+
+// CopyFile copies the entire database to file at the given path.
+func (db *DB) CopyFile(path string) error {
+ f, err := os.Create(path)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+
+ return db.Copy(f)
+}
+
// page retrieves a page reference from the mmap based on the current page size.
func (db *DB) page(id pgid) *page {
return (*page)(unsafe.Pointer(&db.data[id*pgid(db.pageSize)]))