]> Sergey Matveev's repositories - btrtrc.git/blob - torrent-piece-request-order.go
Switch Go CI to go1.20
[btrtrc.git] / torrent-piece-request-order.go
1 package torrent
2
3 import (
4         request_strategy "github.com/anacrolix/torrent/request-strategy"
5 )
6
7 func (t *Torrent) updatePieceRequestOrder(pieceIndex int) {
8         if t.storage == nil {
9                 return
10         }
11         t.cl.pieceRequestOrder[t.clientPieceRequestOrderKey()].Update(
12                 t.pieceRequestOrderKey(pieceIndex),
13                 t.requestStrategyPieceOrderState(pieceIndex))
14 }
15
16 func (t *Torrent) clientPieceRequestOrderKey() interface{} {
17         if t.storage.Capacity == nil {
18                 return t
19         }
20         return t.storage.Capacity
21 }
22
23 func (t *Torrent) deletePieceRequestOrder() {
24         if t.storage == nil {
25                 return
26         }
27         cpro := t.cl.pieceRequestOrder
28         key := t.clientPieceRequestOrderKey()
29         pro := cpro[key]
30         for i := 0; i < t.numPieces(); i++ {
31                 pro.Delete(t.pieceRequestOrderKey(i))
32         }
33         if pro.Len() == 0 {
34                 delete(cpro, key)
35         }
36 }
37
38 func (t *Torrent) initPieceRequestOrder() {
39         if t.storage == nil {
40                 return
41         }
42         if t.cl.pieceRequestOrder == nil {
43                 t.cl.pieceRequestOrder = make(map[interface{}]*request_strategy.PieceRequestOrder)
44         }
45         key := t.clientPieceRequestOrderKey()
46         cpro := t.cl.pieceRequestOrder
47         if cpro[key] == nil {
48                 cpro[key] = request_strategy.NewPieceOrder(request_strategy.NewAjwernerBtree(), t.numPieces())
49         }
50 }
51
52 func (t *Torrent) addRequestOrderPiece(i int) {
53         if t.storage == nil {
54                 return
55         }
56         t.cl.pieceRequestOrder[t.clientPieceRequestOrderKey()].Add(
57                 t.pieceRequestOrderKey(i),
58                 t.requestStrategyPieceOrderState(i))
59 }
60
61 func (t *Torrent) getPieceRequestOrder() *request_strategy.PieceRequestOrder {
62         return t.cl.pieceRequestOrder[t.clientPieceRequestOrderKey()]
63 }