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"
|
2023-10-25 13:35:23 +00:00
|
|
|
s3errors "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
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
|
|
|
}
|
|
|
|
|
2023-10-25 13:35:23 +00:00
|
|
|
if allObjects, err = filterVersionsByMarker(allObjects, p); err != nil {
|
|
|
|
return nil, err
|
2021-08-19 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
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
|
2023-10-25 13:35:23 +00:00
|
|
|
res.NextKeyMarker = allObjects[p.MaxKeys-1].ObjectInfo.Name
|
|
|
|
res.NextVersionIDMarker = allObjects[p.MaxKeys-1].ObjectInfo.VersionID()
|
2021-08-19 06:55:22 +00:00
|
|
|
|
|
|
|
allObjects = allObjects[:p.MaxKeys]
|
2023-10-25 13:35:23 +00:00
|
|
|
res.KeyMarker = p.KeyMarker
|
|
|
|
res.VersionIDMarker = p.VersionIDMarker
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-25 13:35:23 +00:00
|
|
|
func filterVersionsByMarker(objects []*data.ExtendedObjectInfo, p *ListObjectVersionsParams) ([]*data.ExtendedObjectInfo, error) {
|
|
|
|
if p.KeyMarker == "" {
|
|
|
|
return objects, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, obj := range objects {
|
|
|
|
if obj.ObjectInfo.Name == p.KeyMarker {
|
|
|
|
for j := i; j < len(objects); j++ {
|
|
|
|
if objects[j].ObjectInfo.Name != obj.ObjectInfo.Name {
|
|
|
|
if p.VersionIDMarker == "" {
|
|
|
|
return objects[j:], nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if objects[j].ObjectInfo.VersionID() == p.VersionIDMarker {
|
|
|
|
return objects[j+1:], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, s3errors.GetAPIError(s3errors.ErrInvalidVersion)
|
|
|
|
} else if obj.ObjectInfo.Name > p.KeyMarker {
|
|
|
|
if p.VersionIDMarker != "" {
|
|
|
|
return nil, s3errors.GetAPIError(s3errors.ErrInvalidVersion)
|
|
|
|
}
|
|
|
|
return objects[i:], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't use nil as empty slice to be consistent with `return objects[j+1:], nil` above
|
|
|
|
// that can be empty
|
|
|
|
return []*data.ExtendedObjectInfo{}, 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
|
|
|
|
}
|