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[:],
))
})
}
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) {
}
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))
}