diff --git a/request-strategy/order.go b/request-strategy/order.go index 0fa51698..50c9838f 100644 --- a/request-strategy/order.go +++ b/request-strategy/order.go @@ -21,24 +21,19 @@ type ( ChunkSpec = types.ChunkSpec ) -type pieceOrderInput struct { - PieceRequestOrderState - PieceRequestOrderKey -} - -func pieceOrderLess(i, j pieceOrderInput) multiless.Computation { +func pieceOrderLess(i, j *pieceRequestOrderItem) multiless.Computation { return multiless.New().Int( - int(j.Priority), int(i.Priority), + int(j.state.Priority), int(i.state.Priority), ).Bool( - j.Partial, i.Partial, + j.state.Partial, i.state.Partial, ).Int64( - i.Availability, j.Availability, + i.state.Availability, j.state.Availability, ).Int( - i.Index, j.Index, + i.key.Index, j.key.Index, ).Lazy(func() multiless.Computation { return multiless.New().Cmp(bytes.Compare( - i.InfoHash[:], - j.InfoHash[:], + i.key.InfoHash[:], + j.key.InfoHash[:], )) }) } diff --git a/request-strategy/piece-request-order.go b/request-strategy/piece-request-order.go index d906b835..c9a89663 100644 --- a/request-strategy/piece-request-order.go +++ b/request-strategy/piece-request-order.go @@ -37,16 +37,7 @@ type pieceRequestOrderItem struct { func (me *pieceRequestOrderItem) Less(other btree.Item) bool { otherConcrete := other.(*pieceRequestOrderItem) - return pieceOrderLess( - pieceOrderInput{ - PieceRequestOrderState: me.state, - PieceRequestOrderKey: me.key, - }, - pieceOrderInput{ - PieceRequestOrderState: otherConcrete.state, - PieceRequestOrderKey: otherConcrete.key, - }, - ).Less() + return pieceOrderLess(me, otherConcrete).Less() } func (me *PieceRequestOrder) Add(key PieceRequestOrderKey, state PieceRequestOrderState) { @@ -63,10 +54,17 @@ func (me *PieceRequestOrder) Add(key PieceRequestOrderKey, state PieceRequestOrd } func (me *PieceRequestOrder) Update(key PieceRequestOrderKey, state PieceRequestOrderState) { - item := me.existingItemForKey(key) - if item.state == state { + oldState, ok := me.keys[key] + if !ok { + panic("key should have been added already") + } + if state == oldState { return } + item := pieceRequestOrderItem{ + key: key, + state: oldState, + } if me.tree.Delete(&item) == nil { panic(fmt.Sprintf("%#v", key)) }