This commit is contained in:
Matt Joiner 2016-07-12 16:45:22 +10:00
parent 507daba528
commit a1ba463750
5 changed files with 19 additions and 23 deletions

View File

@ -7,8 +7,8 @@ import (
)
type pieceCompletion interface {
Get(metainfo.Piece) bool
Set(metainfo.Piece, bool)
Get(metainfo.Piece) (bool, error)
Set(metainfo.Piece, bool) error
Close()
}

View File

@ -10,12 +10,12 @@ type mapPieceCompletion struct {
func (mapPieceCompletion) Close() {}
func (me *mapPieceCompletion) Get(p metainfo.Piece) bool {
func (me *mapPieceCompletion) Get(p metainfo.Piece) (bool, error) {
_, ok := me.m[p.Key()]
return ok
return ok, nil
}
func (me *mapPieceCompletion) Set(p metainfo.Piece, b bool) {
func (me *mapPieceCompletion) Set(p metainfo.Piece, b bool) error {
if b {
if me.m == nil {
me.m = make(map[metainfo.PieceKey]struct{})
@ -24,4 +24,5 @@ func (me *mapPieceCompletion) Set(p metainfo.Piece, b bool) {
} else {
delete(me.m, p.Key())
}
return nil
}

View File

@ -26,25 +26,19 @@ func newDBPieceCompletion(dir string) (ret *dbPieceCompletion, err error) {
return
}
func (me *dbPieceCompletion) Get(p metainfo.Piece) (ret bool) {
func (me *dbPieceCompletion) Get(p metainfo.Piece) (ret bool, err error) {
row := me.db.QueryRow(`select exists(select * from completed where infohash=? and "index"=?)`, p.Info.Hash().HexString(), p.Index())
err := row.Scan(&ret)
if err != nil {
panic(err)
}
err = row.Scan(&ret)
return
}
func (me *dbPieceCompletion) Set(p metainfo.Piece, b bool) {
var err error
func (me *dbPieceCompletion) Set(p metainfo.Piece, b bool) (err error) {
if b {
_, err = me.db.Exec(`insert into completed (infohash, "index") values (?, ?)`, p.Info.Hash().HexString(), p.Index())
} else {
_, err = me.db.Exec(`delete from completed where infohash=? and "index"=?`, p.Info.Hash().HexString(), p.Index())
}
if err != nil {
panic(err)
}
return
}
func (me *dbPieceCompletion) Close() {

View File

@ -14,10 +14,10 @@ type fileStoragePiece struct {
r io.ReaderAt
}
func (fs *fileStoragePiece) GetIsComplete() (ret bool) {
ret = fs.completion.Get(fs.p)
if !ret {
return
func (fs *fileStoragePiece) GetIsComplete() bool {
ret, err := fs.completion.Get(fs.p)
if err != nil || !ret {
return false
}
// If it's allegedly complete, check that its constituent files have the
// necessary length.
@ -29,11 +29,11 @@ func (fs *fileStoragePiece) GetIsComplete() (ret bool) {
}
}
if ret {
return
return true
}
// The completion was wrong, fix it.
fs.completion.Set(fs.p, false)
return
return false
}
func (fs *fileStoragePiece) MarkComplete() error {

View File

@ -58,8 +58,9 @@ type mmapStoragePiece struct {
io.WriterAt
}
func (sp mmapStoragePiece) GetIsComplete() bool {
return sp.pc.Get(sp.p)
func (sp mmapStoragePiece) GetIsComplete() (ret bool) {
ret, _ = sp.pc.Get(sp.p)
return
}
func (sp mmapStoragePiece) MarkComplete() error {