]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use ConsecutiveChunkWriter in MarkComplete for piece resource storage if available
authorMatt Joiner <anacrolix@gmail.com>
Sat, 21 Nov 2020 02:41:45 +0000 (13:41 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 21 Nov 2020 02:44:23 +0000 (13:44 +1100)
storage/piece_resource.go

index 36088f32cf26718558e687baa3e8c168d5a81820..02c1e7e8ad7e26b99bd017d65985e1cf58881a42 100644 (file)
@@ -61,12 +61,16 @@ func (s piecePerResourcePiece) WriteTo(w io.Writer) (int64, error) {
                if s.mustIsComplete() {
                        return ccw.WriteConsecutiveChunks(s.completedInstancePath(), w)
                } else {
-                       return ccw.WriteConsecutiveChunks(s.incompleteDirPath()+"/", w)
+                       return s.writeConsecutiveIncompleteChunks(ccw, w)
                }
        }
        return io.Copy(w, io.NewSectionReader(s, 0, s.mp.Length()))
 }
 
+func (s piecePerResourcePiece) writeConsecutiveIncompleteChunks(ccw ConsecutiveChunkWriter, w io.Writer) (int64, error) {
+       return ccw.WriteConsecutiveChunks(s.incompleteDirPath()+"/", w)
+}
+
 // Returns if the piece is complete. Ok should be true, because we are the definitive source of
 // truth here.
 func (s piecePerResourcePiece) mustIsComplete() bool {
@@ -87,7 +91,17 @@ func (s piecePerResourcePiece) Completion() Completion {
 
 func (s piecePerResourcePiece) MarkComplete() error {
        incompleteChunks := s.getChunks()
-       err := s.completed().Put(io.NewSectionReader(incompleteChunks, 0, s.mp.Length()))
+       r, w := io.Pipe()
+       go func() {
+               var err error
+               if ccw, ok := s.rp.(ConsecutiveChunkWriter); ok {
+                       _, err = s.writeConsecutiveIncompleteChunks(ccw, w)
+               } else {
+                       _, err = io.Copy(w, io.NewSectionReader(incompleteChunks, 0, s.mp.Length()))
+               }
+               w.CloseWithError(err)
+       }()
+       err := s.completed().Put(r)
        if err == nil {
                for _, c := range incompleteChunks {
                        c.instance.Delete()