mirror of
https://github.com/cubefs/cubefs.git
synced 2026-08-02 02:00:56 +00:00
feat(util): add generic math max and min
. #1000153033 Signed-off-by: slasher <shenjie1@oppo.com>
This commit is contained in:
parent
44b3ab643b
commit
2300b0f382
@ -539,13 +539,6 @@ func blobCount(size uint64, blobSize uint32) uint64 {
|
||||
return (size + uint64(blobSize) - 1) / uint64(blobSize)
|
||||
}
|
||||
|
||||
func minU64(a, b uint64) uint64 {
|
||||
if a <= b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func errorTimeout(err error) bool {
|
||||
msg := err.Error()
|
||||
return strings.Contains(msg, "Timeout") || strings.Contains(msg, "timeout")
|
||||
|
||||
@ -26,6 +26,7 @@ import (
|
||||
errcode "github.com/cubefs/cubefs/blobstore/common/errors"
|
||||
"github.com/cubefs/cubefs/blobstore/common/proto"
|
||||
"github.com/cubefs/cubefs/blobstore/common/trace"
|
||||
"github.com/cubefs/cubefs/blobstore/util"
|
||||
"github.com/cubefs/cubefs/blobstore/util/errors"
|
||||
"github.com/cubefs/cubefs/blobstore/util/retry"
|
||||
)
|
||||
@ -189,7 +190,7 @@ func (h *Handler) allocFromAllocator(ctx context.Context,
|
||||
break
|
||||
}
|
||||
|
||||
count := minU64(blobN, uint64(bidRet.BidEnd)-uint64(bidRet.BidStart)+1)
|
||||
count := util.Min(blobN, uint64(bidRet.BidEnd)-uint64(bidRet.BidStart)+1)
|
||||
blobN -= count
|
||||
|
||||
blobs = append(blobs, proto.Slice{
|
||||
|
||||
@ -34,6 +34,7 @@ import (
|
||||
"github.com/cubefs/cubefs/blobstore/common/proto"
|
||||
"github.com/cubefs/cubefs/blobstore/common/rpc"
|
||||
"github.com/cubefs/cubefs/blobstore/common/trace"
|
||||
"github.com/cubefs/cubefs/blobstore/util"
|
||||
"github.com/cubefs/cubefs/blobstore/util/errors"
|
||||
"github.com/cubefs/cubefs/blobstore/util/retry"
|
||||
)
|
||||
@ -258,7 +259,7 @@ func (h *Handler) Get(ctx context.Context, w io.Writer, location proto.Location,
|
||||
continue
|
||||
}
|
||||
|
||||
toRead := minU64(toReadSize, l-off)
|
||||
toRead := util.Min(toReadSize, l-off)
|
||||
if _, e := w.Write(buf[off : off+toRead]); e != nil {
|
||||
err = errors.Info(e, "write to response")
|
||||
break
|
||||
@ -607,7 +608,7 @@ func (h *Handler) getDataShardOnly(ctx context.Context, getTime *timeReadWrite,
|
||||
break
|
||||
}
|
||||
|
||||
toReadSize := minU64(remainSize, uint64(shardSize-shardOffset))
|
||||
toReadSize := util.Min(remainSize, uint64(shardSize-shardOffset))
|
||||
args := blobnode.RangeGetShardArgs{
|
||||
GetShardArgs: blobnode.GetShardArgs{
|
||||
DiskID: shard.DiskID,
|
||||
@ -791,10 +792,10 @@ func genLocationBlobs(location *proto.Location, readSize uint64, offset uint64)
|
||||
}
|
||||
|
||||
if idx >= firstBlobIdx {
|
||||
toReadSize := minU64(remainSize, blobSize-blobOffset)
|
||||
toReadSize := util.Min(remainSize, blobSize-blobOffset)
|
||||
if toReadSize > 0 {
|
||||
// update the last blob size
|
||||
fixedBlobSize := minU64(location.Size_-idx*blobSize, blobSize)
|
||||
fixedBlobSize := util.Min(location.Size_-idx*blobSize, blobSize)
|
||||
|
||||
sizes, _ := ec.GetBufferSizes(int(fixedBlobSize), tactic)
|
||||
shardSize := sizes.ShardSize
|
||||
|
||||
@ -23,3 +23,17 @@ type Integer interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
|
||||
}
|
||||
|
||||
func Max[T Float | Integer](x, y T) T {
|
||||
if x > y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
func Min[T Float | Integer](x, y T) T {
|
||||
if x < y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
@ -26,6 +26,22 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUtilMath(t *testing.T) {
|
||||
require.True(t, Max(1, math.Inf(1)) == math.Inf(1))
|
||||
require.True(t, Max(-1, math.Inf(-1)) == -1)
|
||||
require.True(t, Max(math.Inf(-1), math.Inf(1)) == math.Inf(1))
|
||||
require.True(t, math.IsNaN(Max(1, math.NaN())))
|
||||
|
||||
require.True(t, Min(1, math.Inf(1)) == 1)
|
||||
require.True(t, Min(-1, math.Inf(-1)) == math.Inf(-1))
|
||||
require.True(t, Min(math.Inf(-1), math.Inf(1)) == math.Inf(-1))
|
||||
require.True(t, math.IsNaN(Max(1, math.NaN())))
|
||||
|
||||
require.True(t, Max[uint16](33, 44) == 44)
|
||||
require.True(t, Min[int8](33, 44) == 33)
|
||||
require.True(t, Max[int32](33, 33) == Min[int32](33, 33))
|
||||
}
|
||||
|
||||
func TestGenTmpPath(t *testing.T) {
|
||||
path, err := GenTmpPath()
|
||||
require.NoError(t, err)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user