2021-08-19 06:55:22 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2021-08-19 06:55:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (n *layer) ListObjectVersions(ctx context.Context, p *ListObjectVersionsParams) (*ListObjectVersionsInfo, error) {
|
2021-09-01 16:10:31 +00:00
|
|
|
var (
|
2022-05-31 15:03:58 +00:00
|
|
|
allObjects = make([]*data.ExtendedObjectInfo, 0, p.MaxKeys)
|
2021-09-01 16:10:31 +00:00
|
|
|
res = &ListObjectVersionsInfo{}
|
|
|
|
)
|
2021-08-19 06:55:22 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
versions, err := n.getAllObjectsVersions(ctx, p.BktInfo, p.Prefix, p.Delimiter)
|
2021-08-19 06:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
sortedNames := make([]string, 0, len(versions))
|
|
|
|
for k := range versions {
|
|
|
|
sortedNames = append(sortedNames, k)
|
|
|
|
}
|
|
|
|
sort.Strings(sortedNames)
|
2021-08-19 06:55:22 +00:00
|
|
|
|
2021-09-01 16:10:31 +00:00
|
|
|
for _, name := range sortedNames {
|
2022-05-20 15:02:00 +00:00
|
|
|
sortedVersions := versions[name]
|
|
|
|
sort.Slice(sortedVersions, func(i, j int) bool {
|
|
|
|
return sortedVersions[j].NodeVersion.Timestamp < sortedVersions[i].NodeVersion.Timestamp // sort in reverse order
|
|
|
|
})
|
|
|
|
|
2022-05-31 15:03:58 +00:00
|
|
|
for i, version := range sortedVersions {
|
|
|
|
version.IsLatest = i == 0
|
|
|
|
allObjects = append(allObjects, version)
|
2022-05-20 15:02:00 +00:00
|
|
|
}
|
2021-08-19 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, obj := range allObjects {
|
2022-08-04 17:31:33 +00:00
|
|
|
if obj.ObjectInfo.Name >= p.KeyMarker && obj.ObjectInfo.VersionID() >= p.VersionIDMarker {
|
2021-08-19 06:55:22 +00:00
|
|
|
allObjects = allObjects[i:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 15:03:58 +00:00
|
|
|
res.CommonPrefixes, allObjects = triageExtendedObjects(allObjects)
|
2021-08-19 06:55:22 +00:00
|
|
|
|
|
|
|
if len(allObjects) > p.MaxKeys {
|
|
|
|
res.IsTruncated = true
|
2022-05-31 15:03:58 +00:00
|
|
|
res.NextKeyMarker = allObjects[p.MaxKeys].ObjectInfo.Name
|
2022-08-04 17:31:33 +00:00
|
|
|
res.NextVersionIDMarker = allObjects[p.MaxKeys].ObjectInfo.VersionID()
|
2021-08-19 06:55:22 +00:00
|
|
|
|
|
|
|
allObjects = allObjects[:p.MaxKeys]
|
2022-05-31 15:03:58 +00:00
|
|
|
res.KeyMarker = allObjects[p.MaxKeys-1].ObjectInfo.Name
|
2022-08-04 17:31:33 +00:00
|
|
|
res.VersionIDMarker = allObjects[p.MaxKeys-1].ObjectInfo.VersionID()
|
2021-08-19 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 21:48:24 +00:00
|
|
|
res.Version, res.DeleteMarker = triageVersions(allObjects)
|
2021-08-19 06:55:22 +00:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:48:24 +00:00
|
|
|
func triageVersions(objVersions []*data.ExtendedObjectInfo) ([]*data.ExtendedObjectInfo, []*data.ExtendedObjectInfo) {
|
2021-08-19 06:55:22 +00:00
|
|
|
if len(objVersions) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-08-01 21:48:24 +00:00
|
|
|
var resVersion []*data.ExtendedObjectInfo
|
|
|
|
var resDelMarkVersions []*data.ExtendedObjectInfo
|
2021-08-19 06:55:22 +00:00
|
|
|
|
|
|
|
for _, version := range objVersions {
|
2022-08-09 12:10:04 +00:00
|
|
|
if version.NodeVersion.IsDeleteMarker() {
|
2021-08-19 06:55:22 +00:00
|
|
|
resDelMarkVersions = append(resDelMarkVersions, version)
|
|
|
|
} else {
|
|
|
|
resVersion = append(resVersion, version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resVersion, resDelMarkVersions
|
|
|
|
}
|