From a97d810f9d53538f0b5b16e7f468069578e24625 Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Tue, 3 Dec 2024 15:38:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B4=E6=95=B0=E5=88=87?= =?UTF-8?q?=E5=88=86=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkgs/ioswitch/dag/node.go | 5 ++++ sdks/storage/models.go | 12 ++------- utils/math2/math.go | 27 +++++++++++++++++++ utils/math2/math_test.go | 57 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 utils/math2/math_test.go diff --git a/pkgs/ioswitch/dag/node.go b/pkgs/ioswitch/dag/node.go index 90269e2..f3703be 100644 --- a/pkgs/ioswitch/dag/node.go +++ b/pkgs/ioswitch/dag/node.go @@ -35,6 +35,11 @@ func (e *NodeEnv) ToEnvWorker(worker exec.WorkerInfo) { e.Worker = worker } +func (e *NodeEnv) CopyFrom(other *NodeEnv) { + e.Type = other.Type + e.Worker = other.Worker +} + func (e *NodeEnv) Equals(other *NodeEnv) bool { if e.Type != other.Type { return false diff --git a/sdks/storage/models.go b/sdks/storage/models.go index 2b48c9c..e821b08 100644 --- a/sdks/storage/models.go +++ b/sdks/storage/models.go @@ -7,6 +7,7 @@ import ( "github.com/samber/lo" "gitlink.org.cn/cloudream/common/pkgs/types" + "gitlink.org.cn/cloudream/common/utils/math2" "gitlink.org.cn/cloudream/common/utils/serder" ) @@ -170,18 +171,9 @@ type SegmentRedundancy struct { } func NewSegmentRedundancy(totalSize int64, segmentCount int) *SegmentRedundancy { - var segs []int64 - segLen := int64(0) - // 计算每一段的大小。大小不一定都相同,但总和应该等于总大小。 - for i := 0; i < segmentCount; i++ { - curLen := totalSize*int64(i+1)/int64(segmentCount) - segLen - segs = append(segs, curLen) - segLen += curLen - } - return &SegmentRedundancy{ Type: "segment", - Segments: segs, + Segments: math2.SplitN(totalSize, segmentCount), } } diff --git a/utils/math2/math.go b/utils/math2/math.go index ee65faa..7a8508b 100644 --- a/utils/math2/math.go +++ b/utils/math2/math.go @@ -45,3 +45,30 @@ func Clamp[T constraints.Integer](v, min, max T) T { return v } + +// 将一个整数切分成小于maxValue的整数列表,尽量均匀 +func SplitLessThan[T constraints.Integer](v T, maxValue T) []T { + cnt := int(CeilDiv(v, maxValue)) + result := make([]T, cnt) + last := int64(0) + for i := 0; i < cnt; i++ { + cur := int64(v) * int64(i+1) / int64(cnt) + result[i] = T(cur - last) + last = cur + } + + return result +} + +// 将一个整数切分成n个整数,尽量均匀 +func SplitN[T constraints.Integer](v T, n int) []T { + result := make([]T, n) + last := int64(0) + for i := 0; i < n; i++ { + cur := int64(v) * int64(i+1) / int64(n) + result[i] = T(cur - last) + last = cur + } + + return result +} diff --git a/utils/math2/math_test.go b/utils/math2/math_test.go new file mode 100644 index 0000000..7e75164 --- /dev/null +++ b/utils/math2/math_test.go @@ -0,0 +1,57 @@ +package math2 + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func Test_SplitLessThan(t *testing.T) { + checker := func(t *testing.T, arr []int, total int, maxValue int) { + t.Logf("arr: %v, total: %d, maxValue: %d", arr, total, maxValue) + + sum := 0 + for _, v := range arr { + sum += v + + if v > maxValue { + t.Errorf("value should be less than %d", maxValue) + } + } + + if sum != total { + t.Errorf("sum should be %d", total) + } + } + + Convey("测试", t, func() { + checker(t, SplitLessThan(9, 9), 9, 9) + checker(t, SplitLessThan(9, 3), 9, 3) + checker(t, SplitLessThan(10, 3), 10, 3) + checker(t, SplitLessThan(11, 3), 11, 3) + checker(t, SplitLessThan(12, 3), 12, 3) + }) +} + +func Test_SplitN(t *testing.T) { + checker := func(t *testing.T, arr []int, total int) { + t.Logf("arr: %v, total: %d", arr, total) + + sum := 0 + for _, v := range arr { + sum += v + } + + if sum != total { + t.Errorf("sum should be %d", total) + } + } + + Convey("测试", t, func() { + checker(t, SplitN(9, 9), 9) + checker(t, SplitN(9, 3), 9) + checker(t, SplitN(10, 3), 10) + checker(t, SplitN(11, 3), 11) + checker(t, SplitN(12, 3), 12) + }) +}