diff --git a/satellite/accounting/projectusage.go b/satellite/accounting/projectusage.go index 03d9b1a63..7b8afc625 100644 --- a/satellite/accounting/projectusage.go +++ b/satellite/accounting/projectusage.go @@ -203,18 +203,6 @@ func (usage *Service) GetProjectBandwidth(ctx context.Context, projectID uuid.UU return total, ErrProjectUsage.Wrap(err) } -// GetProjectSegments returns project allocated segment usage. -func (usage *Service) GetProjectSegments(ctx context.Context, projectID uuid.UUID) (_ int64, err error) { - defer mon.Task()(&ctx, projectID)(&err) - - total, err := usage.metabaseDB.GetProjectSegmentCount(ctx, metabase.GetProjectSegmentCount{ - ProjectID: projectID, - - AsOfSystemInterval: usage.asOfSystemInterval, - }) - return total, ErrProjectUsage.Wrap(err) -} - // GetProjectStorageLimit returns current project storage limit. func (usage *Service) GetProjectStorageLimit(ctx context.Context, projectID uuid.UUID) (_ memory.Size, err error) { defer mon.Task()(&ctx, projectID)(&err) diff --git a/satellite/metabase/metabasetest/test.go b/satellite/metabase/metabasetest/test.go index 2ee469c6b..2d29fb197 100644 --- a/satellite/metabase/metabasetest/test.go +++ b/satellite/metabase/metabasetest/test.go @@ -676,18 +676,3 @@ func (step FinishCopyObject) Check(ctx *testcontext.Context, t testing.TB, db *m require.Zero(t, diff) return result } - -// GetProjectSegmentCount is for testing metabase.GetProjectSegmentCount. -type GetProjectSegmentCount struct { - Opts metabase.GetProjectSegmentCount - Result int64 - ErrClass *errs.Class - ErrText string -} - -// Check runs the test. -func (step GetProjectSegmentCount) Check(ctx *testcontext.Context, t testing.TB, db *metabase.DB) { - result, err := db.GetProjectSegmentCount(ctx, step.Opts) - checkError(t, err, step.ErrClass, step.ErrText) - require.Equal(t, step.Result, result) -} diff --git a/satellite/metabase/project.go b/satellite/metabase/project.go deleted file mode 100644 index ab244ae10..000000000 --- a/satellite/metabase/project.go +++ /dev/null @@ -1,56 +0,0 @@ -// 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 -} diff --git a/satellite/metabase/project_test.go b/satellite/metabase/project_test.go deleted file mode 100644 index 3cdda6f09..000000000 --- a/satellite/metabase/project_test.go +++ /dev/null @@ -1,133 +0,0 @@ -// Copyright (C) 2021 Storj Labs, Inc. -// See LICENSE for copying information. - -package metabase_test - -import ( - "testing" - "time" - - "storj.io/common/testcontext" - "storj.io/common/testrand" - "storj.io/common/uuid" - "storj.io/storj/satellite/metabase" - "storj.io/storj/satellite/metabase/metabasetest" -) - -func TestGetProjectSegmentsCount(t *testing.T) { - metabasetest.Run(t, func(ctx *testcontext.Context, t *testing.T, db *metabase.DB) { - obj := metabasetest.RandObjectStream() - - t.Run("ProjectID missing", func(t *testing.T) { - defer metabasetest.DeleteAll{}.Check(ctx, t, db) - - metabasetest.GetProjectSegmentCount{ - Opts: metabase.GetProjectSegmentCount{}, - ErrClass: &metabase.ErrInvalidRequest, - ErrText: "ProjectID missing", - }.Check(ctx, t, db) - - metabasetest.Verify{}.Check(ctx, t, db) - }) - - t.Run("empty database", func(t *testing.T) { - defer metabasetest.DeleteAll{}.Check(ctx, t, db) - - metabasetest.GetProjectSegmentCount{ - Opts: metabase.GetProjectSegmentCount{ - ProjectID: obj.ProjectID, - }, - ErrClass: &metabase.Error, - ErrText: "project not found: " + obj.ProjectID.String(), - }.Check(ctx, t, db) - }) - - t.Run("object without segments", func(t *testing.T) { - defer metabasetest.DeleteAll{}.Check(ctx, t, db) - - metabasetest.CreateObject(ctx, t, db, obj, 0) - - metabasetest.GetProjectSegmentCount{ - Opts: metabase.GetProjectSegmentCount{ - ProjectID: obj.ProjectID, - }, - Result: 0, - }.Check(ctx, t, db) - }) - - t.Run("object with segments", func(t *testing.T) { - defer metabasetest.DeleteAll{}.Check(ctx, t, db) - - metabasetest.CreateObject(ctx, t, db, obj, 1) - - metabasetest.GetProjectSegmentCount{ - Opts: metabase.GetProjectSegmentCount{ - ProjectID: obj.ProjectID, - }, - Result: 1, - }.Check(ctx, t, db) - }) - - t.Run("object with segments (as of system time)", func(t *testing.T) { - defer metabasetest.DeleteAll{}.Check(ctx, t, db) - - metabasetest.CreateObject(ctx, t, db, obj, 1) - - metabasetest.GetProjectSegmentCount{ - Opts: metabase.GetProjectSegmentCount{ - ProjectID: obj.ProjectID, - AsOfSystemTime: time.Now(), - AsOfSystemInterval: time.Millisecond, - }, - Result: 1, - }.Check(ctx, t, db) - }) - - t.Run("multiple projects", func(t *testing.T) { - defer metabasetest.DeleteAll{}.Check(ctx, t, db) - - project1 := testrand.UUID() - project2 := testrand.UUID() - project3 := testrand.UUID() - - type Object struct { - ProjectID uuid.UUID - Segments byte - } - - objects := []Object{ - {project1, 1}, - {project1, 4}, - - {project2, 5}, - {project2, 3}, - {project2, 1}, - - {project3, 2}, - {project3, 0}, - } - - for _, object := range objects { - obj := metabasetest.RandObjectStream() - obj.ProjectID = object.ProjectID - metabasetest.CreateObject(ctx, t, db, obj, object.Segments) - } - - // expected number of segments per project - projects := map[uuid.UUID]int{ - project1: 5, - project2: 9, - project3: 2, - } - - for project, segments := range projects { - metabasetest.GetProjectSegmentCount{ - Opts: metabase.GetProjectSegmentCount{ - ProjectID: project, - }, - Result: int64(segments), - }.Check(ctx, t, db) - } - }) - }) -}