-
Notifications
You must be signed in to change notification settings - Fork 38
/
sizes.go
51 lines (38 loc) · 949 Bytes
/
sizes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package blobovnicza
import (
"encoding/binary"
"fmt"
"strconv"
)
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 {
return strconv.FormatUint(sz, 10)
}
func bucketKeyFromBounds(upperBound uint64) []byte {
buf := make([]byte, binary.MaxVarintLen64)
ln := binary.PutUvarint(buf, upperBound)
return buf[:ln]
}
func bucketForSize(sz uint64) []byte {
return bucketKeyFromBounds(upperPowerOfTwo(sz))
}
func upperPowerOfTwo(v uint64) (upperBound uint64) {
for upperBound = firstBucketBound; upperBound < v; upperBound *= 2 {
}
return
}
func (b *Blobovnicza) incSize(sz uint64) {
b.filled.Add(sz)
}
func (b *Blobovnicza) decSize(sz uint64) {
b.filled.Sub(sz)
}
func (b *Blobovnicza) full() bool {
return b.filled.Load() >= b.fullSizeLimit
}