Skip to content

Commit

Permalink
Merge pull request #750 from SiaFoundation/pj/health-sort-2
Browse files Browse the repository at this point in the history
Sorting
  • Loading branch information
ChrisSchinnerl authored Nov 22, 2023
2 parents ff89809 + 7d5c69d commit c85dba4
Show file tree
Hide file tree
Showing 4 changed files with 347 additions and 121 deletions.
20 changes: 16 additions & 4 deletions api/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (
const (
ObjectsRenameModeSingle = "single"
ObjectsRenameModeMulti = "multi"

ObjectSortByHealth = "health"
ObjectSortByName = "name"

ObjectSortDirAsc = "asc"
ObjectSortDirDesc = "desc"
)

var (
Expand All @@ -26,6 +32,10 @@ var (
// ErrObjectCorrupted is returned if we were unable to retrieve the object
// from the database.
ErrObjectCorrupted = errors.New("object corrupted")

// ErrInvalidObjectSortParameters is returned when invalid sort parameters
// were provided
ErrInvalidObjectSortParameters = errors.New("invalid sort parameters")
)

type (
Expand Down Expand Up @@ -74,10 +84,12 @@ type (

// ObjectsDeleteRequest is the request type for the /bus/objects/list endpoint.
ObjectsListRequest struct {
Bucket string `json:"bucket"`
Limit int `json:"limit"`
Prefix string `json:"prefix"`
Marker string `json:"marker"`
Bucket string `json:"bucket"`
Limit int `json:"limit"`
SortBy string `json:"sortBy"`
SortDir string `json:"sortDir"`
Prefix string `json:"prefix"`
Marker string `json:"marker"`
}

// ObjectsListResponse is the response type for the /bus/objects/list endpoint.
Expand Down
21 changes: 16 additions & 5 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ type (
UpdateBucketPolicy(ctx context.Context, bucketName string, policy api.BucketPolicy) error

CopyObject(ctx context.Context, srcBucket, dstBucket, srcPath, dstPath, mimeType string) (api.ObjectMetadata, error)
ListObjects(ctx context.Context, bucketName, prefix, marker string, limit int) (api.ObjectsListResponse, error)
ListObjects(ctx context.Context, bucketName, prefix, sortBy, sortDir, marker string, limit int) (api.ObjectsListResponse, error)
Object(ctx context.Context, bucketName, path string) (api.Object, error)
ObjectEntries(ctx context.Context, bucketName, path, prefix, marker string, offset, limit int) ([]api.ObjectMetadata, bool, error)
ObjectEntries(ctx context.Context, bucketName, path, prefix, sortBy, sortDir, marker string, offset, limit int) ([]api.ObjectMetadata, bool, error)
ObjectsBySlabKey(ctx context.Context, bucketName string, slabKey object.EncryptionKey) ([]api.ObjectMetadata, error)
ObjectsStats(ctx context.Context) (api.ObjectsStatsResponse, error)
RemoveObject(ctx context.Context, bucketName, path string) error
Expand Down Expand Up @@ -1203,6 +1203,16 @@ func (b *bus) objectEntriesHandlerGET(jc jape.Context, path string) {
return
}

var sortBy string
if jc.DecodeForm("sortBy", &sortBy) != nil {
return
}

var sortDir string
if jc.DecodeForm("sortDir", &sortDir) != nil {
return
}

var marker string
if jc.DecodeForm("marker", &marker) != nil {
return
Expand All @@ -1218,7 +1228,7 @@ func (b *bus) objectEntriesHandlerGET(jc jape.Context, path string) {
}

// look for object entries
entries, hasMore, err := b.ms.ObjectEntries(jc.Request.Context(), bucket, path, prefix, marker, offset, limit)
entries, hasMore, err := b.ms.ObjectEntries(jc.Request.Context(), bucket, path, prefix, sortBy, sortDir, marker, offset, limit)
if jc.Check("couldn't list object entries", err) != nil {
return
}
Expand Down Expand Up @@ -1256,10 +1266,11 @@ func (b *bus) objectsListHandlerPOST(jc jape.Context) {
var req api.ObjectsListRequest
if jc.Decode(&req) != nil {
return
} else if req.Bucket == "" {
}
if req.Bucket == "" {
req.Bucket = api.DefaultBucketName
}
resp, err := b.ms.ListObjects(jc.Request.Context(), req.Bucket, req.Prefix, req.Marker, req.Limit)
resp, err := b.ms.ListObjects(jc.Request.Context(), req.Bucket, req.Prefix, req.SortBy, req.SortDir, req.Marker, req.Limit)
if jc.Check("couldn't list objects", err) != nil {
return
}
Expand Down
Loading

0 comments on commit c85dba4

Please sign in to comment.