diff --git a/container/id/id.go b/container/id/id.go index 569968a..1cbd60b 100644 --- a/container/id/id.go +++ b/container/id/id.go @@ -3,6 +3,7 @@ package cid import ( "crypto/sha256" "fmt" + "strings" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs" "github.com/mr-tron/base58" @@ -113,3 +114,9 @@ func (id *ID) DecodeString(s string) error { func (id ID) String() string { return id.EncodeToString() } + +// Cmp returns an integer comparing two base58 encoded container ID lexicographically. +// The result will be 0 if id1 == id2, -1 if id1 < id2, and +1 if id1 > id2. +func (id ID) Cmp(id2 ID) int { + return strings.Compare(id.EncodeToString(), id2.EncodeToString()) +} diff --git a/container/id/id_test.go b/container/id/id_test.go index 6f60d92..2bbd8b3 100644 --- a/container/id/id_test.go +++ b/container/id/id_test.go @@ -3,6 +3,8 @@ package cid_test import ( "crypto/rand" "crypto/sha256" + "slices" + "strings" "testing" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs" @@ -106,3 +108,17 @@ func TestID_Encode(t *testing.T) { require.Equal(t, emptyID, id.EncodeToString()) }) } + +func TestID_Cmp(t *testing.T) { + var arr []cid.ID + for i := 0; i < 3; i++ { + checksum := randSHA256Checksum() + arr = append(arr, cidtest.IDWithChecksum(checksum)) + } + + slices.SortFunc(arr, cid.ID.Cmp) + + for i := 1; i < len(arr); i++ { + require.NotEqual(t, strings.Compare(arr[i-1].EncodeToString(), arr[i].EncodeToString()), 1, "array is not sorted correctly") + } +} diff --git a/object/id/id.go b/object/id/id.go index d1dfe80..29e6a3a 100644 --- a/object/id/id.go +++ b/object/id/id.go @@ -4,6 +4,7 @@ import ( "crypto/ecdsa" "crypto/sha256" "fmt" + "strings" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs" frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto" @@ -167,3 +168,9 @@ func (id *ID) UnmarshalJSON(data []byte) error { return nil } + +// Cmp returns an integer comparing two base58 encoded object ID lexicographically. +// The result will be 0 if id1 == id2, -1 if id1 < id2, and +1 if id1 > id2. +func (id ID) Cmp(id2 ID) int { + return strings.Compare(id.EncodeToString(), id2.EncodeToString()) +} diff --git a/object/id/id_test.go b/object/id/id_test.go index e1a2e26..9df6186 100644 --- a/object/id/id_test.go +++ b/object/id/id_test.go @@ -3,7 +3,9 @@ package oid import ( "crypto/rand" "crypto/sha256" + "slices" "strconv" + "strings" "testing" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs" @@ -180,3 +182,16 @@ func TestID_Encode(t *testing.T) { require.Equal(t, emptyID, id.EncodeToString()) }) } + +func TestID_Cmp(t *testing.T) { + id1 := randID(t) + id2 := randID(t) + id3 := randID(t) + + arr := []ID{id1, id2, id3} + + slices.SortFunc(arr, ID.Cmp) + for i := 1; i < len(arr); i++ { + require.NotEqual(t, strings.Compare(arr[i-1].EncodeToString(), arr[i].EncodeToString()), 1, "array is not sorted correctly") + } +} diff --git a/user/id.go b/user/id.go index c8e2d14..c77ae67 100644 --- a/user/id.go +++ b/user/id.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "strings" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs" "github.com/mr-tron/base58" @@ -123,3 +124,9 @@ func (x *ID) setUserID(w []byte) error { return nil } + +// Cmp returns an integer comparing two base58 encoded user ID lexicographically. +// The result will be 0 if id1 == id2, -1 if id1 < id2, and +1 if id1 > id2. +func (x ID) Cmp(x2 ID) int { + return strings.Compare(x.EncodeToString(), x2.EncodeToString()) +} diff --git a/user/id_test.go b/user/id_test.go index c517dc5..c253336 100644 --- a/user/id_test.go +++ b/user/id_test.go @@ -3,6 +3,8 @@ package user_test import ( "bytes" "crypto/rand" + "slices" + "strings" "testing" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs" @@ -132,3 +134,16 @@ func TestID_Equal(t *testing.T) { require.True(t, id3.Equals(id1)) // commutativity require.False(t, id1.Equals(id2)) } + +func TestID_Cmp(t *testing.T) { + id1 := usertest.ID() + id2 := usertest.ID() + id3 := usertest.ID() + + arr := []ID{id1, id2, id3} + + slices.SortFunc(arr, ID.Cmp) + for i := 1; i < len(arr); i++ { + require.NotEqual(t, strings.Compare(arr[i-1].EncodeToString(), arr[i].EncodeToString()), 1, "array is not sorted correctly") + } +}