storj/satellite/metabase/project.go
Michał Niewrzał de8464e848 satellite/metabase: add GetProjectSegmentCount method
This new method will be used with mechanism to limit number
of segments per project, similar to limiting buckets or bandwidth.
This is only one of multiple changes we will do to implement this
limitation.

Change-Id: Ia516c2a006ad1d7b4431d780679be9d809848f4b
2021-11-24 13:20:02 +00:00

57 lines
1.3 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package metabase
import (
"context"
"time"
"storj.io/common/uuid"
)
// GetProjectSegmentCount contains arguments necessary for fetching an information
// about project segment count.
type GetProjectSegmentCount struct {
ProjectID uuid.UUID
AsOfSystemTime time.Time
AsOfSystemInterval time.Duration
}
// Verify verifies reqest fields.
func (g *GetProjectSegmentCount) Verify() error {
if g.ProjectID.IsZero() {
return ErrInvalidRequest.New("ProjectID missing")
}
return nil
}
// GetProjectSegmentCount returns number of segments that specified project has.
func (db *DB) GetProjectSegmentCount(ctx context.Context, opts GetProjectSegmentCount) (_ int64, err error) {
defer mon.Task()(&ctx)(&err)
if err := opts.Verify(); err != nil {
return 0, err
}
var segmentsCount *int64
err = db.db.QueryRowContext(ctx, `
SELECT
sum(segment_count)
FROM objects
`+db.asOfTime(opts.AsOfSystemTime, opts.AsOfSystemInterval)+`
WHERE
project_id = $1
`, opts.ProjectID).Scan(&segmentsCount)
if err != nil {
return 0, Error.New("unable to query project segment count: %w", err)
}
if segmentsCount == nil {
return 0, Error.New("project not found: %s", opts.ProjectID)
}
return *segmentsCount, nil
}