2021-12-01 03:38:47 +00:00
|
|
|
package request_strategy
|
|
|
|
|
|
|
|
import (
|
2021-12-03 10:31:40 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-12-01 03:38:47 +00:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
"github.com/google/btree"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewPieceOrder() *PieceRequestOrder {
|
|
|
|
return &PieceRequestOrder{
|
|
|
|
tree: btree.New(32),
|
|
|
|
keys: make(map[PieceRequestOrderKey]PieceRequestOrderState),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PieceRequestOrder struct {
|
|
|
|
tree *btree.BTree
|
|
|
|
keys map[PieceRequestOrderKey]PieceRequestOrderState
|
|
|
|
}
|
|
|
|
|
|
|
|
type PieceRequestOrderKey struct {
|
|
|
|
InfoHash metainfo.Hash
|
|
|
|
Index int
|
|
|
|
}
|
|
|
|
|
|
|
|
type PieceRequestOrderState struct {
|
|
|
|
Priority piecePriority
|
|
|
|
Partial bool
|
2021-12-17 11:06:21 +00:00
|
|
|
Availability int
|
2021-12-01 03:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type pieceRequestOrderItem struct {
|
|
|
|
key PieceRequestOrderKey
|
|
|
|
state PieceRequestOrderState
|
|
|
|
}
|
|
|
|
|
2021-12-09 10:49:29 +00:00
|
|
|
func (me *pieceRequestOrderItem) Less(other btree.Item) bool {
|
|
|
|
otherConcrete := other.(*pieceRequestOrderItem)
|
2021-12-15 07:07:17 +00:00
|
|
|
return pieceOrderLess(me, otherConcrete).Less()
|
2021-12-01 03:38:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *PieceRequestOrder) Add(key PieceRequestOrderKey, state PieceRequestOrderState) {
|
|
|
|
if _, ok := me.keys[key]; ok {
|
|
|
|
panic(key)
|
|
|
|
}
|
2021-12-09 10:49:29 +00:00
|
|
|
if me.tree.ReplaceOrInsert(&pieceRequestOrderItem{
|
2021-12-01 03:38:47 +00:00
|
|
|
key: key,
|
|
|
|
state: state,
|
|
|
|
}) != nil {
|
|
|
|
panic("shouldn't already have this")
|
|
|
|
}
|
|
|
|
me.keys[key] = state
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *PieceRequestOrder) Update(key PieceRequestOrderKey, state PieceRequestOrderState) {
|
2021-12-15 07:07:17 +00:00
|
|
|
oldState, ok := me.keys[key]
|
|
|
|
if !ok {
|
|
|
|
panic("key should have been added already")
|
|
|
|
}
|
|
|
|
if state == oldState {
|
2021-12-09 10:49:29 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-15 07:07:17 +00:00
|
|
|
item := pieceRequestOrderItem{
|
|
|
|
key: key,
|
|
|
|
state: oldState,
|
|
|
|
}
|
2021-12-09 10:49:29 +00:00
|
|
|
if me.tree.Delete(&item) == nil {
|
2021-12-03 10:31:40 +00:00
|
|
|
panic(fmt.Sprintf("%#v", key))
|
2021-12-01 03:38:47 +00:00
|
|
|
}
|
2021-12-09 10:49:29 +00:00
|
|
|
item.state = state
|
|
|
|
if me.tree.ReplaceOrInsert(&item) != nil {
|
2021-12-01 03:38:47 +00:00
|
|
|
panic(key)
|
|
|
|
}
|
|
|
|
me.keys[key] = state
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *PieceRequestOrder) existingItemForKey(key PieceRequestOrderKey) pieceRequestOrderItem {
|
|
|
|
return pieceRequestOrderItem{
|
|
|
|
key: key,
|
|
|
|
state: me.keys[key],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *PieceRequestOrder) Delete(key PieceRequestOrderKey) {
|
2021-12-09 10:49:29 +00:00
|
|
|
item := me.existingItemForKey(key)
|
|
|
|
if me.tree.Delete(&item) == nil {
|
2021-12-01 03:38:47 +00:00
|
|
|
panic(key)
|
|
|
|
}
|
|
|
|
delete(me.keys, key)
|
2021-12-03 10:31:40 +00:00
|
|
|
// log.Printf("deleting %#v", key)
|
2021-12-01 03:38:47 +00:00
|
|
|
}
|
2021-12-11 02:46:29 +00:00
|
|
|
|
|
|
|
func (me *PieceRequestOrder) Len() int {
|
|
|
|
return len(me.keys)
|
|
|
|
}
|