2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2023-08-18 10:01:27 +00:00
|
|
|
"math"
|
2023-04-26 08:29:33 +00:00
|
|
|
"math/bits"
|
2021-06-16 08:58:38 +00:00
|
|
|
"strconv"
|
2020-11-26 14:26:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const firstBucketBound = uint64(32 * 1 << 10) // 32KB
|
|
|
|
|
|
|
|
func stringifyBounds(lower, upper uint64) string {
|
|
|
|
return fmt.Sprintf("[%s:%s]",
|
|
|
|
stringifyByteSize(lower),
|
|
|
|
stringifyByteSize(upper),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringifyByteSize(sz uint64) string {
|
2021-06-16 08:58:38 +00:00
|
|
|
return strconv.FormatUint(sz, 10)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func bucketKeyFromBounds(upperBound uint64) []byte {
|
|
|
|
buf := make([]byte, binary.MaxVarintLen64)
|
|
|
|
|
|
|
|
ln := binary.PutUvarint(buf, upperBound)
|
|
|
|
|
|
|
|
return buf[:ln]
|
|
|
|
}
|
|
|
|
|
2023-10-12 18:35:08 +00:00
|
|
|
func bucketForSize(sz uint64) []byte {
|
|
|
|
return bucketKeyFromBounds(upperPowerOfTwo(sz))
|
2020-12-14 09:25:05 +00:00
|
|
|
}
|
2020-11-26 14:26:53 +00:00
|
|
|
|
2023-04-26 08:29:33 +00:00
|
|
|
func upperPowerOfTwo(v uint64) uint64 {
|
|
|
|
if v <= firstBucketBound {
|
|
|
|
return firstBucketBound
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
2023-04-26 08:29:33 +00:00
|
|
|
return 1 << bits.Len64(v-1)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 10:01:27 +00:00
|
|
|
func (b *Blobovnicza) itemAdded(itemSize uint64) {
|
|
|
|
b.dataSize.Add(itemSize)
|
|
|
|
b.itemsCount.Add(1)
|
|
|
|
b.metrics.AddOpenBlobovniczaSize(itemSize)
|
|
|
|
b.metrics.AddOpenBlobovniczaItems(1)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 10:01:27 +00:00
|
|
|
func (b *Blobovnicza) itemDeleted(itemSize uint64) {
|
|
|
|
b.dataSize.Add(^(itemSize - 1))
|
|
|
|
b.itemsCount.Add(math.MaxUint64)
|
|
|
|
b.metrics.SubOpenBlobovniczaSize(itemSize)
|
|
|
|
b.metrics.SubOpenBlobovniczaItems(1)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 20:36:48 +00:00
|
|
|
func (b *Blobovnicza) IsFull() bool {
|
2023-08-16 08:12:19 +00:00
|
|
|
return b.dataSize.Load() >= b.fullSizeLimit
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
2024-08-29 08:34:18 +00:00
|
|
|
|
|
|
|
func (b *Blobovnicza) FillPercent() int {
|
|
|
|
return int(100.0 * (float64(b.dataSize.Load()) / float64(b.fullSizeLimit)))
|
|
|
|
}
|