[#335] Improve determining AccessBox latest version #391

Merged
alexvanin merged 1 commit from Anoke/frostfs-s3-gw:improve-determing-latest-version-accessbox into master 2024-06-06 12:35:52 +00:00
3 changed files with 31 additions and 10 deletions

View file

@ -76,7 +76,7 @@ func (x *AuthmateFrostFS) GetCredsObject(ctx context.Context, addr oid.Address)
credObjID := addr.Object()
if last := versions.GetLast(); last != nil {
credObjID = last.OjbID
credObjID = last.ObjID
}
res, err := x.frostFS.ReadObject(ctx, layer.PrmObjectRead{
@ -107,7 +107,7 @@ func (x *AuthmateFrostFS) CreateObject(ctx context.Context, prm tokens.PrmObject
}
if versions.GetLast() == nil {
versions.AppendVersion(&crdt.ObjectVersion{OjbID: addr.Object()})
versions.AppendVersion(&crdt.ObjectVersion{ObjID: addr.Object()})
}
for key, val := range versions.GetCRDTHeaders() {

View file

@ -20,13 +20,13 @@ type ObjectVersions struct {
}
type ObjectVersion struct {
OjbID oid.ID
ObjID oid.ID
Headers map[string]string
CreationEpoch uint64
}
func (o *ObjectVersion) VersionID() string {
return o.OjbID.EncodeToString()
return o.ObjID.EncodeToString()
}
func NewObjectVersions(name string) *ObjectVersions {
@ -42,7 +42,7 @@ func NewObjectVersion(obj *object.Object) *ObjectVersion {
}
return &ObjectVersion{
OjbID: objID,
ObjID: objID,
Headers: headers,
CreationEpoch: obj.CreationEpoch(),
}
@ -108,8 +108,15 @@ func (v *ObjectVersions) getAddHeader() string {
}
func less(ov1, ov2 *ObjectVersion) bool {
versionID1, versionID2 := ov1.VersionID(), ov2.VersionID()
if strings.Contains(ov1.Headers[versionsAddAttr], versionID2) {
return false
}
if strings.Contains(ov2.Headers[versionsAddAttr], versionID1) {
return true
}
if ov1.CreationEpoch == ov2.CreationEpoch {
dkirillov marked this conversation as resolved
Review

On this line we will compute VersionIDs, that have already been computed above (line 111, 114). So probably we can compute this just one in the beginning of the function:

func less(ov1, ov2 *ObjectVersion) bool {
    versionID1, versionID2 := ov1.VersionID(), ov2.VersionID()
    // ...
}
On this line we will compute VersionIDs, that have already been computed above (line 111, 114). So probably we can compute this just one in the beginning of the function: ```golang func less(ov1, ov2 *ObjectVersion) bool { versionID1, versionID2 := ov1.VersionID(), ov2.VersionID() // ... } ```
return ov1.VersionID() < ov2.VersionID()
return versionID1 < versionID2
}
return ov1.CreationEpoch < ov2.CreationEpoch
}

View file

@ -20,10 +20,10 @@ func TestObjectVersionsSort(t *testing.T) {
t.Run("sort by oids", func(t *testing.T) {
versions := NewObjectVersions("test")
versions.AppendVersion(&ObjectVersion{CreationEpoch: 3, OjbID: getTestOID(2)})
versions.AppendVersion(&ObjectVersion{CreationEpoch: 3, OjbID: getTestOID(1)})
versions.AppendVersion(&ObjectVersion{CreationEpoch: 1, OjbID: getTestOID(3)})
versions.AppendVersion(&ObjectVersion{CreationEpoch: 2, OjbID: getTestOID(4)})
versions.AppendVersion(&ObjectVersion{CreationEpoch: 3, ObjID: getTestOID(2)})
versions.AppendVersion(&ObjectVersion{CreationEpoch: 3, ObjID: getTestOID(1)})
versions.AppendVersion(&ObjectVersion{CreationEpoch: 1, ObjID: getTestOID(3)})
versions.AppendVersion(&ObjectVersion{CreationEpoch: 2, ObjID: getTestOID(4)})
last := versions.GetLast()
require.Equal(t, uint64(3), last.CreationEpoch)
@ -36,3 +36,17 @@ func getTestOID(val byte) oid.ID {
res.SetSHA256([32]byte{val})
return res
}
func TestObjectVersionsSort1(t *testing.T) {
t.Run("sort by headers", func(t *testing.T) {
versions := NewObjectVersions("test")
versions.AppendVersion(&ObjectVersion{Headers: versions.GetCRDTHeaders(), ObjID: getTestOID(2)})
versions.AppendVersion(&ObjectVersion{Headers: versions.GetCRDTHeaders(), ObjID: getTestOID(3)})
versions.AppendVersion(&ObjectVersion{Headers: versions.GetCRDTHeaders(), ObjID: getTestOID(4)})
versions.AppendVersion(&ObjectVersion{Headers: versions.GetCRDTHeaders(), ObjID: getTestOID(1)})
last := versions.GetLast()
require.Equal(t, uint64(0), last.CreationEpoch)
require.Equal(t, getTestOID(1).String(), last.VersionID())
})
}