From 519d65228ef2bd99336a251f02c278a94b2df4d4 Mon Sep 17 00:00:00 2001 From: Martin Kobetic Date: Wed, 21 May 2014 15:08:37 +0000 Subject: move Copy and CopyFile from DB to Tx --- db.go | 60 ------------------------------------------------------------ 1 file changed, 60 deletions(-) (limited to 'db.go') diff --git a/db.go b/db.go index c768ce3..1a1f997 100644 --- a/db.go +++ b/db.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "hash/fnv" - "io" "os" "strings" "sync" @@ -483,65 +482,6 @@ func (db *DB) View(fn func(*Tx) error) error { return nil } -// Copy writes the entire database to a writer. -// A reader transaction is maintained during the copy so it is safe to continue -// using the database while a copy is in progress. -func (db *DB) Copy(w io.Writer) error { - // Maintain a reader transaction so pages don't get reclaimed. - t, err := db.Begin(false) - if err != nil { - return err - } - - // Open reader on the database. - f, err := os.Open(db.path) - if err != nil { - _ = t.Rollback() - return err - } - - // Copy the meta pages. - db.metalock.Lock() - _, err = io.CopyN(w, f, int64(db.pageSize*2)) - db.metalock.Unlock() - if err != nil { - _ = t.Rollback() - _ = f.Close() - return fmt.Errorf("meta copy: %s", err) - } - - // Copy data pages. - if _, err := io.Copy(w, f); err != nil { - _ = t.Rollback() - _ = f.Close() - return err - } - - // Close read transaction and exit. - if err := t.Rollback(); err != nil { - _ = f.Close() - return err - } - return f.Close() -} - -// CopyFile copies the entire database to file at the given path. -// A reader transaction is maintained during the copy so it is safe to continue -// using the database while a copy is in progress. -func (db *DB) CopyFile(path string, mode os.FileMode) error { - f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode) - if err != nil { - return err - } - - err = db.Copy(f) - if err != nil { - _ = f.Close() - return err - } - return f.Close() -} - // Stats retrieves ongoing performance stats for the database. // This is only updated when a transaction closes. func (db *DB) Stats() Stats { -- cgit v1.2.3