Skip to content

Commit

Permalink
Add stubs for Collection.Compact (#3485)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored Oct 3, 2023
1 parent dd6e9dd commit 531f91c
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ labels:
- name: area/cursors
color: "#D93F0B"
description: Issues about cursors
- name: area/diag
color: "#909BA6"
description: Issues about diagnostic commands
- name: area/fuzz
color: "#D4C5F9"
description: Issues about fuzzing and smithing
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/FerretDB/FerretDB

go 1.21

toolchain go1.21.1

require (
github.com/AlekSi/pointer v1.2.0
github.com/SAP/go-hdb v1.5.5
Expand Down
2 changes: 1 addition & 1 deletion integration/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/FerretDB/FerretDB/integration

go 1.21.1
go 1.21

replace github.com/FerretDB/FerretDB => ../

Expand Down
25 changes: 25 additions & 0 deletions internal/backends/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Collection interface {
Explain(context.Context, *ExplainParams) (*ExplainResult, error)

Stats(context.Context, *CollectionStatsParams) (*CollectionStatsResult, error)
Compact(context.Context, *CompactParams) (*CompactResult, error)

ListIndexes(context.Context, *ListIndexesParams) (*ListIndexesResult, error)
CreateIndexes(context.Context, *CreateIndexesParams) (*CreateIndexesResult, error)
Expand Down Expand Up @@ -214,6 +215,8 @@ func (cc *collectionContract) Explain(ctx context.Context, params *ExplainParams
type CollectionStatsParams struct{}

// CollectionStatsResult represents the results of Collection.Stats method.
//
// TODO https://github.com/FerretDB/FerretDB/issues/2447
type CollectionStatsResult struct {
CountObjects int64
CountIndexes int64
Expand All @@ -232,6 +235,28 @@ func (cc *collectionContract) Stats(ctx context.Context, params *CollectionStats
return res, err
}

// CompactParams represents the parameters of Collection.Compact method.
type CompactParams struct {
Full bool
}

// CompactResult represents the results of Collection.Compact method.
type CompactResult struct{}

// Compact reduces the disk space collection takes (by defragmenting, removing dead rows, etc)
// and refreshes its statistics.
//
// If full is true, the operation should try to reduce the disk space as much as possible,
// even if collection or the whole database will be locked for some time.
func (cc *collectionContract) Compact(ctx context.Context, params *CompactParams) (*CompactResult, error) {
defer observability.FuncCall(ctx)()

res, err := cc.c.Compact(ctx, params)
checkError(err, ErrorCodeDatabaseDoesNotExist, ErrorCodeCollectionDoesNotExist)

return res, err
}

// ListIndexesParams represents the parameters of Collection.ListIndexes method.
type ListIndexesParams struct{}

Expand Down
55 changes: 55 additions & 0 deletions internal/backends/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,58 @@ func TestCollectionUpdateAll(t *testing.T) {
})
}
}

func TestCollectionCompact(t *testing.T) {
t.Skip("https://github.com/FerretDB/FerretDB/issues/3484")
t.Skip("https://github.com/FerretDB/FerretDB/issues/3469")

t.Parallel()

ctx := conninfo.Ctx(testutil.Ctx(t), conninfo.New())

for _, b := range testBackends(t) {
b := b
t.Run(b.Name(), func(t *testing.T) {
t.Parallel()

t.Run("DatabaseDoesNotExist", func(t *testing.T) {
t.Parallel()

dbName, collName := testutil.DatabaseName(t), testutil.CollectionName(t)
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)

coll, err := db.Collection(collName)
require.NoError(t, err)

_, err = coll.Compact(ctx, nil)
assertErrorCode(t, err, backends.ErrorCodeDatabaseDoesNotExist)
})

t.Run("CollectionDoesNotExist", func(t *testing.T) {
t.Parallel()

dbName, collName := testutil.DatabaseName(t), testutil.CollectionName(t)
otherCollName := collName + "_other"
cleanupDatabase(t, ctx, b, dbName)

db, err := b.Database(dbName)
require.NoError(t, err)

// to create database
err = db.CreateCollection(ctx, &backends.CreateCollectionParams{
Name: otherCollName,
})
require.NoError(t, err)

coll, err := db.Collection(collName)
require.NoError(t, err)

_, err = coll.Compact(ctx, nil)
assertErrorCode(t, err, backends.ErrorCodeCollectionDoesNotExist)
})
})
}
}
5 changes: 5 additions & 0 deletions internal/backends/decorators/dummy/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func (c *collection) Stats(ctx context.Context, params *backends.CollectionStats
return c.c.Stats(ctx, params)
}

// Compact implements backends.Collection interface.
func (c *collection) Compact(ctx context.Context, params *backends.CompactParams) (*backends.CompactResult, error) {
return c.c.Compact(ctx, params)
}

// ListIndexes implements backends.Collection interface.
func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndexesParams) (*backends.ListIndexesResult, error) {
return c.c.ListIndexes(ctx, params)
Expand Down
5 changes: 5 additions & 0 deletions internal/backends/decorators/oplog/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ func (c *collection) Stats(ctx context.Context, params *backends.CollectionStats
return c.c.Stats(ctx, params)
}

// Compact implements backends.Collection interface.
func (c *collection) Compact(ctx context.Context, params *backends.CompactParams) (*backends.CompactResult, error) {
return c.c.Compact(ctx, params)
}

// ListIndexes implements backends.Collection interface.
func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndexesParams) (*backends.ListIndexesResult, error) {
return c.c.ListIndexes(ctx, params)
Expand Down
5 changes: 5 additions & 0 deletions internal/backends/hana/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func (c *collection) Stats(ctx context.Context, params *backends.CollectionStats
return nil, lazyerrors.New("not implemented yet")
}

// Compact implements backends.Collection interface.
func (c *collection) Compact(ctx context.Context, params *backends.CompactParams) (*backends.CompactResult, error) {
return nil, lazyerrors.New("not implemented yet")
}

// ListIndexes implements backends.Collection interface.
func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndexesParams) (*backends.ListIndexesResult, error) {
return nil, lazyerrors.New("not implemented yet")
Expand Down
6 changes: 6 additions & 0 deletions internal/backends/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/FerretDB/FerretDB/internal/backends"
Expand Down Expand Up @@ -83,3 +84,8 @@ func cleanupDatabase(t *testing.T, ctx context.Context, b backends.Backend, dbNa
_ = b.DropDatabase(ctx, p)
})
}

// assertErrorCode asserts that err is *Error with one of the given error codes.
func assertErrorCode(t *testing.T, err error, code backends.ErrorCode, codes ...backends.ErrorCode) {
assert.True(t, backends.ErrorCodeIs(err, code, codes...), "err = %v", err)
}
6 changes: 6 additions & 0 deletions internal/backends/postgresql/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ func (c *collection) Stats(ctx context.Context, params *backends.CollectionStats
return new(backends.CollectionStatsResult), nil
}

// Compact implements backends.Collection interface.
func (c *collection) Compact(ctx context.Context, params *backends.CompactParams) (*backends.CompactResult, error) {
// TODO https://github.com/FerretDB/FerretDB/issues/3484
return new(backends.CompactResult), nil
}

// ListIndexes implements backends.Collection interface.
func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndexesParams) (*backends.ListIndexesResult, error) {
// TODO https://github.com/FerretDB/FerretDB/issues/3394
Expand Down
6 changes: 6 additions & 0 deletions internal/backends/sqlite/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ func (c *collection) Stats(ctx context.Context, params *backends.CollectionStats
}, nil
}

// Compact implements backends.Collection interface.
func (c *collection) Compact(ctx context.Context, params *backends.CompactParams) (*backends.CompactResult, error) {
// TODO https://github.com/FerretDB/FerretDB/issues/3469
return new(backends.CompactResult), nil
}

// ListIndexes implements backends.Collection interface.
func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndexesParams) (*backends.ListIndexesResult, error) {
db := c.r.DatabaseGetExisting(ctx, c.dbName)
Expand Down
2 changes: 1 addition & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/FerretDB/FerretDB/tools

go 1.21.1
go 1.21

require (
github.com/BurntSushi/go-sumtype v0.0.0-20221020234012-480526a59796
Expand Down
2 changes: 1 addition & 1 deletion tools/golangci/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module github.com/FerretDB/FerretDB/tools/golangci

go 1.21.1
go 1.21

require github.com/golangci/golangci-lint v1.54.2

Expand Down

0 comments on commit 531f91c

Please sign in to comment.