Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for tailable cursor with non-capped collection #3677

Merged
merged 22 commits into from
Nov 13, 2023
Prev Previous commit
Next Next commit
wip
  • Loading branch information
noisersup committed Nov 8, 2023
commit 0f8479fe04afb040f14da4f80020aebdcbc10310
24 changes: 12 additions & 12 deletions integration/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,20 +1072,20 @@ func TestQueryTailableCursors(t *testing.T) {
_, err := collection.InsertOne(ctx, bson.D{{"v", int32(42)}})
require.NoError(t, err)

expectedErr := mongo.CommandError{
Code: 238,
Message: "find: support for field \"tailable\" with non-default value true is not implemented yet",
Name: "NotImplemented",
}
//expectedErr := mongo.CommandError{
// Code: 238,
// Message: "find: support for field \"tailable\" with non-default value true is not implemented yet",
// Name: "NotImplemented",
//}

if setup.IsMongoDB(t) {
expectedErr = mongo.CommandError{
Code: 2,
Message: "error processing query: ns=TestQueryTailableCursors.TestQueryTailableCursorsTree:" +
" $eq null\nSort: {}\nProj: {}\n tailable cursor requested on non capped collection",
Name: "BadValue",
}
//if setup.IsMongoDB(t) {
expectedErr := mongo.CommandError{
Code: 2,
Message: "error processing query: ns=TestQueryTailableCursors.TestQueryTailableCursorsTree:" +
" $eq null\nSort: {}\nProj: {}\n tailable cursor requested on non capped collection",
Name: "BadValue",
}
//}

_, err = collection.Find(ctx, bson.D{{}}, options.Find().SetCursorType(options.Tailable))
AssertEqualCommandError(t, expectedErr, err)
noisersup marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
13 changes: 13 additions & 0 deletions internal/backends/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Collection interface {

Stats(context.Context, *CollectionStatsParams) (*CollectionStatsResult, error)
Compact(context.Context, *CompactParams) (*CompactResult, error)
Capped(context.Context) (bool, error)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

ListIndexes(context.Context, *ListIndexesParams) (*ListIndexesResult, error)
CreateIndexes(context.Context, *CreateIndexesParams) (*CreateIndexesResult, error)
Expand Down Expand Up @@ -288,6 +289,18 @@ func (cc *collectionContract) Compact(ctx context.Context, params *CompactParams
return res, err
}

// Capped returns true if collection is capped.
//
// The errors for non-existing database and non-existing collection are the same.
func (cc *collectionContract) Capped(ctx context.Context) (bool, error) {
defer observability.FuncCall(ctx)()

res, err := cc.c.Capped(ctx)
checkError(err, ErrorCodeCollectionDoesNotExist)

return res, err
}

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

Expand Down
29 changes: 29 additions & 0 deletions internal/backends/postgresql/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,35 @@ func (c *collection) Compact(ctx context.Context, params *backends.CompactParams
return new(backends.CompactResult), nil
}

// Capped implements backends.Collection interface.
func (c *collection) Capped(ctx context.Context) (bool, error) {
db, err := c.r.DatabaseGetExisting(ctx, c.dbName)
if err != nil {
return false, lazyerrors.Error(err)
}

if db == nil {
return false, backends.NewError(
backends.ErrorCodeCollectionDoesNotExist,
lazyerrors.Errorf("no ns %s.%s", c.dbName, c.name),
)
}

coll, err := c.r.CollectionGet(ctx, c.dbName, c.name)
if err != nil {
return false, lazyerrors.Error(err)
}

if coll == nil {
return false, backends.NewError(
backends.ErrorCodeCollectionDoesNotExist,
lazyerrors.Errorf("no ns %s.%s", c.dbName, c.name),
)
}

return coll.Capped(), nil
}

// ListIndexes implements backends.Collection interface.
func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndexesParams) (*backends.ListIndexesResult, error) {
db, err := c.r.DatabaseGetExisting(ctx, c.dbName)
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/common/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type FindParams struct {
SingleBatch bool `ferretdb:"singleBatch,opt"`
Comment string `ferretdb:"comment,opt"`
MaxTimeMS int64 `ferretdb:"maxTimeMS,opt,wholePositiveNumber"`
Tailable bool `ferretdb:"tailable,opt"`

Collation *types.Document `ferretdb:"collation,unimplemented"`
Let *types.Document `ferretdb:"let,unimplemented"`
Expand All @@ -52,7 +53,6 @@ type FindParams struct {

ReturnKey bool `ferretdb:"returnKey,unimplemented-non-default"`
ShowRecordId bool `ferretdb:"showRecordId,unimplemented-non-default"` //nolint:lll // TODO https://github.com/FerretDB/FerretDB/issues/3467
Tailable bool `ferretdb:"tailable,unimplemented-non-default"`
OplogReplay bool `ferretdb:"oplogReplay,unimplemented-non-default"`
NoCursorTimeout bool `ferretdb:"noCursorTimeout,unimplemented-non-default"`
AwaitData bool `ferretdb:"awaitData,unimplemented-non-default"`
Expand Down