FedP2P/pending-requests.go

51 lines
851 B
Go
Raw Normal View History

2021-10-09 16:00:58 +08:00
package torrent
2021-10-10 08:19:08 +08:00
import (
rbm "github.com/RoaringBitmap/roaring"
roaring "github.com/RoaringBitmap/roaring/BitSliceIndexing"
)
2021-10-09 16:00:58 +08:00
type pendingRequests struct {
2021-10-10 08:19:08 +08:00
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)
if prev <= 0 {
panic(prev)
2021-10-09 16:00:58 +08:00
}
2021-10-10 08:19:08 +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() {
2021-10-10 08:19:08 +08:00
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() {
2021-10-10 08:19:08 +08:00
if p.m == nil {
2021-10-09 16:00:58 +08:00
panic(p.m)
}
2021-10-10 08:19:08 +08:00
sum, _ := p.m.Sum(&allBits)
if sum != 0 {
panic(sum)
}
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
}