Skip to content

Commit 9fa58d9

Browse files
committed
refactor: delete package c2h5oh/datasize and create human readable functions
1 parent a88ca7c commit 9fa58d9

8 files changed

Lines changed: 160 additions & 21 deletions

File tree

frac/common/info.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"path"
77
"time"
88

9-
"github.com/c2h5oh/datasize"
9+
"github.com/ozontech/seq-db/util"
1010

1111
"github.com/ozontech/seq-db/buildinfo"
1212
"github.com/ozontech/seq-db/config"
@@ -58,8 +58,8 @@ func NewInfo(filename string, docsOnDisk, metaOnDisk uint64) *Info {
5858
func (s *Info) String() string {
5959
return fmt.Sprintf(
6060
"raw docs=%s, disk docs=%s",
61-
datasize.ByteSize(s.DocsRaw).HR(),
62-
datasize.ByteSize(s.DocsOnDisk).HR(),
61+
util.ByteToHumanReadable(s.DocsRaw),
62+
util.ByteToHumanReadable(s.DocsOnDisk),
6363
)
6464
}
6565

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ require (
1111
github.com/aws/aws-sdk-go-v2/credentials v1.18.17
1212
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.19.13
1313
github.com/aws/aws-sdk-go-v2/service/s3 v1.88.5
14-
github.com/c2h5oh/datasize v0.0.0-20200112174442-28bbd4740fee
1514
github.com/cep21/circuit/v3 v3.2.2
1615
github.com/felixge/fgprof v0.9.5
1716
github.com/golang/mock v1.6.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ github.com/aws/smithy-go v1.23.1 h1:sLvcH6dfAFwGkHLZ7dGiYF7aK6mg4CgKA/iDKjLDt9M=
7171
github.com/aws/smithy-go v1.23.1/go.mod h1:LEj2LM3rBRQJxPZTB4KuzZkaZYnZPnvgIhb4pu07mx0=
7272
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
7373
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
74-
github.com/c2h5oh/datasize v0.0.0-20200112174442-28bbd4740fee h1:BnPxIde0gjtTnc9Er7cxvBk8DHLWhEux0SxayC8dP6I=
75-
github.com/c2h5oh/datasize v0.0.0-20200112174442-28bbd4740fee/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M=
7674
github.com/cactus/go-statsd-client v3.1.1+incompatible/go.mod h1:cMRcwZDklk7hXp+Law83urTHUiHMzCev/r4JMYr/zU0=
7775
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
7876
github.com/cep21/circuit/v3 v3.2.2 h1:Z3M1n0+YbEvEvJsjCNV8oYNr2vLhB39drQCdawbS/kk=

storeapi/docs_stream.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package storeapi
33
import (
44
"context"
55

6-
"github.com/c2h5oh/datasize"
6+
"github.com/ozontech/seq-db/util"
77
"go.uber.org/zap"
88

99
"github.com/ozontech/seq-db/config"
@@ -107,7 +107,7 @@ func (d *docsStream) calcChunkSize(docs [][]byte, prevChunkSize int) int {
107107
zap.Int("total_len", d.totalIDs),
108108
zap.Int("prev_chunk_size", prevChunkSize),
109109
zap.Int("new_chunk_size", newChunkSize),
110-
zap.String("batch_size", datasize.ByteSize(batchSize).HumanReadable()),
110+
zap.String("batch_size", util.ByteToHumanReadable(uint64(batchSize))),
111111
)
112112
}
113113

util/bytes.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/alecthomas/units"
7+
)
8+
9+
func ByteToHumanReadable(n uint64) string {
10+
switch {
11+
case n >= uint64(units.EiB):
12+
return fmt.Sprintf("%d EiB", n/uint64(units.EiB))
13+
case n >= uint64(units.PiB):
14+
return fmt.Sprintf("%d PiB", n/uint64(units.PiB))
15+
case n >= uint64(units.TiB):
16+
return fmt.Sprintf("%d TiB", n/uint64(units.TiB))
17+
case n >= uint64(units.GiB):
18+
return fmt.Sprintf("%d GiB", n/uint64(units.GiB))
19+
case n >= uint64(units.MiB):
20+
return fmt.Sprintf("%d MiB", n/uint64(units.MiB))
21+
case n >= uint64(units.KiB):
22+
return fmt.Sprintf("%d KiB", n/uint64(units.KiB))
23+
default:
24+
return fmt.Sprintf("%d B", n)
25+
}
26+
}
27+
28+
func ByteToEBytes(b int64) float64 {
29+
v := b / int64(units.EiB)
30+
r := b % int64(units.EiB)
31+
return float64(v) + float64(r)/float64(units.EiB)
32+
}
33+
34+
func ByteToPBytes(b int64) float64 {
35+
v := b / int64(units.PiB)
36+
r := b % int64(units.PiB)
37+
return float64(v) + float64(r)/float64(units.PiB)
38+
}
39+
40+
func ByteToTBytes(b int64) float64 {
41+
v := b / int64(units.TiB)
42+
r := b % int64(units.TiB)
43+
return float64(v) + float64(r)/float64(units.TiB)
44+
}
45+
46+
func ByteToGBytes(b int64) float64 {
47+
v := b / int64(units.GiB)
48+
r := b % int64(units.GiB)
49+
return float64(v) + float64(r)/float64(units.GiB)
50+
}
51+
52+
func ByteToMBytes(b int64) float64 {
53+
v := b / int64(units.MiB)
54+
r := b % int64(units.MiB)
55+
return float64(v) + float64(r)/float64(units.MiB)
56+
}
57+
58+
func ByteToKBytes(b int64) float64 {
59+
v := b / int64(units.KiB)
60+
r := b % int64(units.KiB)
61+
return float64(v) + float64(r)/float64(units.KiB)
62+
}

util/bytes_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package util
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
// Добавлены проверки границ и больших значений.
9+
func TestByteToHumanReadable(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
input uint64
13+
expected string
14+
}{
15+
{
16+
name: "ZeroBytes",
17+
input: 0,
18+
expected: "0 B",
19+
},
20+
{
21+
name: "Bytes",
22+
input: 512,
23+
expected: "512 B",
24+
},
25+
{
26+
name: "Exactly1KiB",
27+
input: 1024,
28+
expected: "1 KiB",
29+
},
30+
{
31+
name: "JustBelow1MiB",
32+
input: 1024*1024 - 1,
33+
expected: "1023 KiB",
34+
},
35+
{
36+
name: "Exactly1MiB",
37+
input: 1024 * 1024,
38+
expected: "1 MiB",
39+
},
40+
{
41+
name: "Megabytes",
42+
input: 10 * 1024 * 1024,
43+
expected: "10 MiB",
44+
},
45+
{
46+
name: "Gigabytes",
47+
input: 15 * 1024 * 1024 * 1024,
48+
expected: "15 GiB",
49+
},
50+
{
51+
name: "Terabytes",
52+
input: 5 * 1024 * 1024 * 1024 * 1024,
53+
expected: "5 TiB",
54+
},
55+
{
56+
name: "Petabytes",
57+
input: 2 * 1024 * 1024 * 1024 * 1024 * 1024,
58+
expected: "2 PiB",
59+
},
60+
{
61+
name: "Exabytes",
62+
input: 1 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
63+
expected: "1 EiB",
64+
},
65+
{
66+
name: "SmallFractionalKB",
67+
input: 1025,
68+
expected: "1 KiB",
69+
},
70+
{
71+
name: "VeryLargeValue_NoOverflow",
72+
input: math.MaxUint64,
73+
expected: "15 EiB",
74+
},
75+
}
76+
77+
for _, tt := range tests {
78+
t.Run(tt.name, func(t *testing.T) {
79+
result := ByteToHumanReadable(tt.input)
80+
if result != tt.expected {
81+
t.Errorf("ByteToHumanReadable(%d): expected %s, got %s", tt.input, tt.expected, result)
82+
}
83+
})
84+
}
85+
}

util/log_helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// ZapUint64AsSizeStr forms string zap.Field with val converted to size string.
1010
func ZapUint64AsSizeStr(key string, val uint64) zap.Field {
11-
return zap.String(key, SizeStr(val))
11+
return zap.String(key, ByteToHumanReadable(val))
1212
}
1313

1414
// ZapFloat64WithPrec forms float64 zap.Field with removed numerics after prec

util/util.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111
"unsafe"
1212

13-
"github.com/c2h5oh/datasize"
1413
"go.uber.org/zap"
1514

1615
"github.com/ozontech/seq-db/consts"
@@ -27,10 +26,6 @@ func StringToByteUnsafe(str string) []byte { // this works fine
2726
return buf
2827
}
2928

30-
func SizeStr(bytes uint64) string {
31-
return datasize.ByteSize(bytes).HR()
32-
}
33-
3429
func RunEvery(done <-chan struct{}, runInterval time.Duration, actionFn func()) {
3530
runTicker := time.NewTicker(runInterval)
3631
defer runTicker.Stop()
@@ -73,20 +68,20 @@ func Float64ToPrec(val float64, prec uint32) float64 {
7368

7469
// SizeToUnit converts size in bytes to unit and returns as float64.
7570
func SizeToUnit(sizeVal uint64, unit string) float64 {
76-
val := datasize.ByteSize(sizeVal)
71+
val := int64(sizeVal)
7772
switch strings.ToLower(unit) {
7873
case "kb":
79-
return val.KBytes()
74+
return ByteToKBytes(val)
8075
case "mb":
81-
return val.MBytes()
76+
return ByteToMBytes(val)
8277
case "gb":
83-
return val.GBytes()
78+
return ByteToGBytes(val)
8479
case "tb":
85-
return val.TBytes()
80+
return ByteToTBytes(val)
8681
case "pb":
87-
return val.PBytes()
82+
return ByteToPBytes(val)
8883
case "eb":
89-
return val.EBytes()
84+
return ByteToEBytes(val)
9085
default:
9186
logger.Panic("unsupported unit", zap.String("unit", unit))
9287
panic("_")

0 commit comments

Comments
 (0)