2021-10-09 08:00:58 +00:00
|
|
|
package torrent
|
|
|
|
|
2021-11-25 11:32:52 +00:00
|
|
|
import (
|
|
|
|
rbm "github.com/RoaringBitmap/roaring"
|
|
|
|
roaring "github.com/RoaringBitmap/roaring/BitSliceIndexing"
|
|
|
|
)
|
|
|
|
|
2021-10-09 08:00:58 +00:00
|
|
|
type pendingRequests struct {
|
2021-11-25 11:32:52 +00:00
|
|
|
m *roaring.BSI
|
2021-10-09 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 00:19:08 +00:00
|
|
|
func (p *pendingRequests) Dec(r RequestIndex) {
|
2021-11-25 11:32:52 +00:00
|
|
|
_r := uint64(r)
|
|
|
|
prev, _ := p.m.GetValue(_r)
|
2021-10-10 00:19:08 +00:00
|
|
|
if prev <= 0 {
|
|
|
|
panic(prev)
|
2021-10-09 08:00:58 +00:00
|
|
|
}
|
2021-11-25 11:32:52 +00:00
|
|
|
p.m.SetValue(_r, prev-1)
|
2021-10-09 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 00:19:08 +00:00
|
|
|
func (p *pendingRequests) Inc(r RequestIndex) {
|
2021-11-25 11:32:52 +00:00
|
|
|
_r := uint64(r)
|
|
|
|
prev, _ := p.m.GetValue(_r)
|
|
|
|
p.m.SetValue(_r, prev+1)
|
2021-10-09 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 00:32:27 +00:00
|
|
|
func (p *pendingRequests) Init(maxIndex RequestIndex) {
|
2021-11-25 11:32:52 +00:00
|
|
|
p.m = roaring.NewDefaultBSI()
|
|
|
|
}
|
|
|
|
|
|
|
|
var allBits rbm.Bitmap
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
allBits.AddRange(0, rbm.MaxRange)
|
2021-10-09 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pendingRequests) AssertEmpty() {
|
2021-11-25 11:32:52 +00:00
|
|
|
if p.m == nil {
|
|
|
|
panic(p.m)
|
|
|
|
}
|
|
|
|
sum, _ := p.m.Sum(&allBits)
|
|
|
|
if sum != 0 {
|
|
|
|
panic(sum)
|
2021-10-10 00:19:08 +00:00
|
|
|
}
|
2021-10-09 08:00:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 00:19:08 +00:00
|
|
|
func (p *pendingRequests) Get(r RequestIndex) int {
|
2021-11-25 11:32:52 +00:00
|
|
|
count, _ := p.m.GetValue(uint64(r))
|
|
|
|
return int(count)
|
2021-10-09 08:00:58 +00:00
|
|
|
}
|