make pieceCompletion configurable

This commit is contained in:
mlmhl 2017-05-22 10:15:48 +08:00 committed by luffyma(马林)
parent 8df5221fc8
commit d8c7f37d2b
6 changed files with 31 additions and 15 deletions

View File

@ -18,7 +18,7 @@ type boltPieceCompletion struct {
db *bolt.DB
}
func newBoltPieceCompletion(dir string) (ret *boltPieceCompletion, err error) {
func NewBoltPieceCompletion(dir string) (ret PieceCompletion, err error) {
p := filepath.Join(dir, ".torrent.bolt.db")
db, err := bolt.Open(p, 0660, &bolt.Options{
Timeout: time.Second,

View File

@ -16,7 +16,7 @@ func TestBoltPieceCompletion(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(td)
pc, err := newBoltPieceCompletion(td)
pc, err := NewBoltPieceCompletion(td)
require.NoError(t, err)
defer pc.Close()

View File

@ -7,17 +7,17 @@ import (
)
// Implementations track the completion of pieces. It must be concurrent-safe.
type pieceCompletion interface {
type PieceCompletion interface {
Get(metainfo.PieceKey) (bool, error)
Set(metainfo.PieceKey, bool) error
Close() error
}
func pieceCompletionForDir(dir string) (ret pieceCompletion) {
ret, err := newBoltPieceCompletion(dir)
func pieceCompletionForDir(dir string) (ret PieceCompletion) {
ret, err := NewBoltPieceCompletion(dir)
if err != nil {
log.Printf("couldn't open piece completion db in %q: %s", dir, err)
ret = new(mapPieceCompletion)
ret = NewMapPieceCompletion()
}
return
}

View File

@ -11,7 +11,11 @@ type mapPieceCompletion struct {
m map[metainfo.PieceKey]struct{}
}
func (mapPieceCompletion) Close() error { return nil }
func NewMapPieceCompletion() PieceCompletion {
return &mapPieceCompletion{m: make(map[metainfo.PieceKey]struct{})}
}
func (*mapPieceCompletion) Close() error { return nil }
func (me *mapPieceCompletion) Get(pk metainfo.PieceKey) (bool, error) {
me.mu.Lock()

View File

@ -15,7 +15,7 @@ import (
type fileClientImpl struct {
baseDir string
pathMaker func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string
pc pieceCompletion
pc PieceCompletion
}
// The Default path maker just returns the current path
@ -29,7 +29,11 @@ func infoHashPathMaker(baseDir string, info *metainfo.Info, infoHash metainfo.Ha
// All Torrent data stored in this baseDir
func NewFile(baseDir string) ClientImpl {
return NewFileWithCustomPathMaker(baseDir, nil)
return NewFileWithCompletion(baseDir, pieceCompletionForDir(baseDir))
}
func NewFileWithCompletion(baseDir string, completion PieceCompletion) ClientImpl {
return newFileWithCustomPathMakerAndCompletion(baseDir, nil, completion)
}
// All Torrent data stored in subdirectorys by infohash
@ -39,13 +43,17 @@ func NewFileByInfoHash(baseDir string) ClientImpl {
// Allows passing a function to determine the path for storing torrent data
func NewFileWithCustomPathMaker(baseDir string, pathMaker func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string) ClientImpl {
return newFileWithCustomPathMakerAndCompletion(baseDir, pathMaker, pieceCompletionForDir(baseDir))
}
func newFileWithCustomPathMakerAndCompletion(baseDir string, pathMaker func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string, completion PieceCompletion) ClientImpl {
if pathMaker == nil {
pathMaker = defaultPathMaker
}
return &fileClientImpl{
baseDir: baseDir,
pathMaker: pathMaker,
pc: pieceCompletionForDir(baseDir),
pc: completion,
}
}
@ -72,7 +80,7 @@ type fileTorrentImpl struct {
dir string
info *metainfo.Info
infoHash metainfo.Hash
completion pieceCompletion
completion PieceCompletion
}
func (fts *fileTorrentImpl) Piece(p metainfo.Piece) PieceImpl {

View File

@ -15,13 +15,17 @@ import (
type mmapStorage struct {
baseDir string
pc pieceCompletion
pc PieceCompletion
}
func NewMMap(baseDir string) ClientImpl {
return NewMMapWithCompletion(baseDir, pieceCompletionForDir(baseDir))
}
func NewMMapWithCompletion(baseDir string, completion PieceCompletion) ClientImpl {
return &mmapStorage{
baseDir: baseDir,
pc: pieceCompletionForDir(baseDir),
pc: completion,
}
}
@ -40,7 +44,7 @@ func (s *mmapStorage) Close() error {
type mmapTorrentStorage struct {
span mmap_span.MMapSpan
pc pieceCompletion
pc PieceCompletion
}
func (ts *mmapTorrentStorage) Piece(p metainfo.Piece) PieceImpl {
@ -58,7 +62,7 @@ func (ts *mmapTorrentStorage) Close() error {
}
type mmapStoragePiece struct {
pc pieceCompletion
pc PieceCompletion
p metainfo.Piece
ih metainfo.Hash
io.ReaderAt