]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Merge branch 'request-strategy-rewrite'
authorMatt Joiner <anacrolix@gmail.com>
Mon, 21 Jun 2021 02:04:06 +0000 (12:04 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 21 Jun 2021 02:04:06 +0000 (12:04 +1000)
storage/interface.go
storage/sqlite/sqlite-storage.go
storage/sqlite/sqlite-storage_test.go
torrent.go

index 271bc7e60b92e2c2ba7c2734ccfd6941adc9db5a..a52ee6dca1cbc05e42033c164b56ca34515b28c2 100644 (file)
@@ -24,8 +24,9 @@ type TorrentImpl struct {
        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
@@ -45,3 +46,8 @@ type Completion struct {
        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)
+}
index 38f734fe5096f4d54a04d475620279dbf004800c..9eb97e85abceac600b67a41f251c8737be19e78f 100644 (file)
@@ -252,7 +252,7 @@ func NewPiecesStorage(opts NewPiecesStorageOpts) (_ storage.ClientImplCloser, er
        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
@@ -494,7 +494,7 @@ type ConnPool interface {
 }
 
 func withPoolConn(pool ConnPool, with func(conn)) {
-       c := pool.Get(nil)
+       c := pool.Get(context.TODO())
        defer pool.Put(c)
        with(c)
 }
index 2494ce6d1a7602e925afa41cc95b2752a5a49c43..1fe1314ea3e1cc442f5045801864d2dfb2463701 100644 (file)
@@ -5,6 +5,7 @@ package sqliteStorage
 
 import (
        "bytes"
+       "context"
        "errors"
        "fmt"
        "io"
@@ -34,7 +35,7 @@ func newConnsAndProv(t *testing.T, opts NewPoolOpts) (ConnPool, *provider) {
        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() })
index 41460412cc81dafa9f47745c0f7d8b74e05ec3be..b4dda85d740acbb6e275e6f116187d762f6f4241 100644 (file)
@@ -872,10 +872,20 @@ func (t *Torrent) pieceLength(piece pieceIndex) pp.Integer {
 }
 
 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