Capacity *func() *int64
}
-// Interacts with torrent piece data. Optional interfaces to implement include io.WriterTo, such as
-// when a piece supports a more efficient way to write out incomplete chunks
+// Interacts with torrent piece data. Optional interfaces to implement include:
+// io.WriterTo, such as when a piece supports a more efficient way to write out incomplete chunks.
+// SelfHashing, such as when a piece supports a more efficient way to hash its contents.
type PieceImpl interface {
// These interfaces are not as strict as normally required. They can
// assume that the parameters are appropriate for the dimensions of the
Complete bool
Ok bool
}
+
+// Allows a storage backend to override hashing (i.e. if it can do it more efficiently than the torrent client can)
+type SelfHashing interface {
+ SelfHash() (metainfo.Hash, error)
+}
if opts.SetJournalMode == "" && !opts.Memory {
opts.SetJournalMode = "wal"
}
- err = initPoolConns(nil, conns, opts.InitConnOpts)
+ err = initPoolConns(context.TODO(), conns, opts.InitConnOpts)
if err != nil {
conns.Close()
return
}
func withPoolConn(pool ConnPool, with func(conn)) {
- c := pool.Get(nil)
+ c := pool.Get(context.TODO())
defer pool.Put(c)
with(c)
}
import (
"bytes"
+ "context"
"errors"
"fmt"
"io"
if !opts.Memory && opts.SetJournalMode == "" {
opts.SetJournalMode = "wal"
}
- qt.Assert(t, initPoolConns(nil, pool, opts.InitConnOpts), qt.IsNil)
+ qt.Assert(t, initPoolConns(context.TODO(), pool, opts.InitConnOpts), qt.IsNil)
prov, err := NewProvider(pool, ProviderOpts{BatchWrites: pool.NumConns() > 1})
require.NoError(t, err)
t.Cleanup(func() { prov.Close() })
}
func (t *Torrent) hashPiece(piece pieceIndex) (ret metainfo.Hash, err error) {
- hash := pieceHash.New()
p := t.piece(piece)
p.waitNoPendingWrites()
storagePiece := t.pieces[piece].Storage()
+
+ //Does the backend want to do its own hashing?
+ if i, ok := storagePiece.PieceImpl.(storage.SelfHashing); ok {
+ var sum metainfo.Hash
+ //log.Printf("A piece decided to self-hash: %d", piece)
+ sum, err = i.SelfHash()
+ missinggo.CopyExact(&ret, sum)
+ return
+ }
+
+ hash := pieceHash.New()
const logPieceContents = false
if logPieceContents {
var examineBuf bytes.Buffer