Skip to content

Commit

Permalink
snap: expose GetDBFilePathByID() as a class function
Browse files Browse the repository at this point in the history
  • Loading branch information
fanminshi committed May 4, 2017
1 parent 505bf8c commit 334eb80
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions snap/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package snap

import (
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -24,6 +25,8 @@ import (
"github.com/coreos/etcd/pkg/fileutil"
)

var ErrDBSnapFileNotFound = errors.New("snap: db snapshot file doesn't exist")

// SaveDBFrom saves snapshot of the database from the given reader. It
// guarantees the save operation is atomic.
func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) {
Expand Down Expand Up @@ -52,23 +55,26 @@ func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) {
return n, err
}

plog.Infof("saved database snapshot to disk [total bytes: %d]", n)

plog.Infof("saved database snapshot %v to disk [total bytes: %d]", fn, n)
return n, nil
}

// DBFilePath returns the file path for the snapshot of the database with
// given id. If the snapshot does not exist, it returns error.
func (s *Snapshotter) DBFilePath(id uint64) (string, error) {
fns, err := fileutil.ReadDir(s.dir)
return GetDBFilePathByID(s.dir, id)
}

func GetDBFilePathByID(snapDir string, id uint64) (string, error) {
fns, err := fileutil.ReadDir(snapDir)
if err != nil {
return "", err
}
wfn := fmt.Sprintf("%016x.snap.db", id)
for _, fn := range fns {
if fn == wfn {
return filepath.Join(s.dir, fn), nil
return filepath.Join(snapDir, fn), nil
}
}
return "", fmt.Errorf("snap: snapshot file doesn't exist")
return "", ErrDBSnapFileNotFound
}

0 comments on commit 334eb80

Please sign in to comment.