]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Allow Storage Backends to do their own Hashing (#518)
authorZilog8 <zeuscoding@gmail.com>
Tue, 8 Jun 2021 05:45:35 +0000 (01:45 -0400)
committerGitHub <noreply@github.com>
Tue, 8 Jun 2021 05:45:35 +0000 (15:45 +1000)
* Allow Storage Backends to do their own Hashing

- Describes an optional interface 'SelfHashing' that a storage backend's type implementing 'PieceImpl' may also implement in order to calculate piece hashsums itself.

- Alters the 'hashPiece' function in the torrent package to look for types implementing 'SelfHashing' . If not implemented, calculate the hash as usual.

storage/interface.go
torrent.go

index 869556f80a547265fad99ba61d919f77e57e59c1..584338cb4ae178d30ab1c04ee138981cb41f7071 100644 (file)
@@ -22,8 +22,9 @@ type TorrentImpl interface {
        Close() error
 }
 
-// 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
@@ -43,3 +44,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 e38764742499d9db8fcd4b58b898cc9626d848aa..82b70c132cff0b25b58b10f3edc893d750d48b72 100644 (file)
@@ -810,10 +810,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