FedP2P/pending-requests.go

51 lines
872 B
Go
Raw Normal View History

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