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

Allow filtering by name in ListDatabases and ListCollections #3851

Merged
merged 36 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1a00290
adding the first level of changes for ListCollections and ListDatabases
sachinpuranik Dec 13, 2023
1efcbec
adding the test cases for the changes made
sachinpuranik Dec 13, 2023
5385a18
implement the ListCollections and also improve test
sachinpuranik Dec 13, 2023
600c7d6
correction in test and improve functions
sachinpuranik Dec 13, 2023
1bc99b2
fix TestListCollections in sqlite
sachinpuranik Dec 13, 2023
989cc7c
fix TestListDatabases in sqlite
sachinpuranik Dec 13, 2023
537098e
adding test case to backends contract
sachinpuranik Dec 14, 2023
2ae563c
remove unwanted comments
sachinpuranik Dec 14, 2023
991cd0f
clear the unwanted comments
sachinpuranik Dec 14, 2023
c8fc26c
remove unwanted code after self review
sachinpuranik Dec 14, 2023
3e04885
remove unwanted code after self review
sachinpuranik Dec 14, 2023
5da1a34
uncomenting some code
sachinpuranik Dec 14, 2023
46bd356
Merge branch 'main' into Fix_for_issue_3601
sachinpuranik Dec 14, 2023
4b1ad3d
Merge branch 'main' into Fix_for_issue_3601
sachinpuranik Dec 19, 2023
a14c063
fixing the review comments
sachinpuranik Dec 19, 2023
91ec761
Merge branch 'main' into Fix_for_issue_3601
henvic Dec 19, 2023
80c901b
Merge branch 'main' into Fix_for_issue_3601
sachinpuranik Dec 19, 2023
7275c6f
remove unwanted line
sachinpuranik Dec 19, 2023
2aca7dc
Merge branch 'main' into Fix_for_issue_3601
AlekSi Dec 20, 2023
1c4bf56
Merge branch 'main' into Fix_for_issue_3601
sachinpuranik Dec 21, 2023
399ae91
fix the logical error in errorhandling
sachinpuranik Dec 21, 2023
4835f6f
Merge branch 'main' into Fix_for_issue_3601
sachinpuranik Dec 21, 2023
36d5e80
fix linting
sachinpuranik Dec 21, 2023
834f73d
fix linting
sachinpuranik Dec 21, 2023
1f81bb9
Merge branch 'main' into Fix_for_issue_3601
AlekSi Dec 21, 2023
cdebc26
Merge branch 'main' into Fix_for_issue_3601
sachinpuranik Dec 25, 2023
cd7d6fc
fix test broken due to linting fix
sachinpuranik Dec 25, 2023
7204d82
Merge branch 'main' into Fix_for_issue_3601
sachinpuranik Dec 26, 2023
0799979
Merge branch 'main' into Fix_for_issue_3601
AlekSi Dec 27, 2023
11cf5b2
Merge branch 'main' into Fix_for_issue_3601
sachinpuranik Dec 28, 2023
51c490a
Don't cleanup capped collections if there is nothing to cleanup (#3909)
Dec 28, 2023
93c8870
Drop test users on cleanup (#3914)
AlekSi Dec 28, 2023
f8b1f40
Implement `awaitData` tailable cursors (#3900)
noisersup Dec 28, 2023
975acba
Merge branch 'main' into Fix_for_issue_3601
sachinpuranik Jan 2, 2024
d409e36
improve test cases
sachinpuranik Jan 2, 2024
5e4eb89
Merge branch 'main' into Fix_for_issue_3601
AlekSi Jan 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adding the first level of changes for ListCollections and ListDatabases
  • Loading branch information
sachinpuranik committed Dec 13, 2023
commit 1a00290e377d3886fd9fd7b76efbe17b3a901b96
13 changes: 0 additions & 13 deletions internal/backends/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
package backends

import (
"cmp"
"context"
"slices"

"github.com/prometheus/client_golang/prometheus"

Expand Down Expand Up @@ -149,17 +147,6 @@ func (bc *backendContract) ListDatabases(ctx context.Context, params *ListDataba
res, err := bc.b.ListDatabases(ctx, params)
checkError(err)

if res != nil && len(res.Databases) > 0 {
must.BeTrue(slices.IsSortedFunc(res.Databases, func(a, b DatabaseInfo) int {
return cmp.Compare(a.Name, b.Name)
}))

if params != nil && params.Name != "" {
must.BeTrue(len(res.Databases) == 1)
must.BeTrue(res.Databases[0].Name == params.Name)
}
}

return res, err
}

Expand Down
13 changes: 0 additions & 13 deletions internal/backends/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
package backends

import (
"cmp"
"context"
"slices"

"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/util/observability"
Expand Down Expand Up @@ -112,17 +110,6 @@ func (dbc *databaseContract) ListCollections(ctx context.Context, params *ListCo
res, err := dbc.db.ListCollections(ctx, params)
checkError(err)

if res != nil && len(res.Collections) > 0 {
must.BeTrue(slices.IsSortedFunc(res.Collections, func(a, b CollectionInfo) int {
return cmp.Compare(a.Name, b.Name)
}))

if params != nil && params.Name != "" {
must.BeTrue(len(res.Collections) == 1)
must.BeTrue(res.Collections[0].Name == params.Name)
}
}

return res, err
}

Expand Down
12 changes: 4 additions & 8 deletions internal/backends/decorators/oplog/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
package oplog

import (
"cmp"
"context"
"slices"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -218,21 +216,19 @@ func (c *collection) DropIndexes(ctx context.Context, params *backends.DropIndex
func (c *collection) oplogCollection(ctx context.Context) backends.Collection {
db := must.NotFail(c.origB.Database(oplogDatabase))

cList, err := db.ListCollections(ctx, nil)
cList, err := db.ListCollections(ctx, &backends.ListCollectionsParams{Name: oplogCollection})
if err != nil {
c.l.Error("Failed to list collections", zap.Error(err))
return nil
}

// TODO https://github.com/FerretDB/FerretDB/issues/3601
_, found := slices.BinarySearchFunc(cList.Collections, oplogCollection, func(e backends.CollectionInfo, t string) int {
return cmp.Compare(e.Name, t)
})
if !found {
if len(cList.Collections) == 0 {
c.l.Debug("Collection not found")
return nil
}

// TODO https://github.com/FerretDB/FerretDB/issues/3601
// TODO(SP) - Check what to return here
return must.NotFail(db.Collection(oplogCollection))
}

Expand Down
24 changes: 22 additions & 2 deletions internal/backends/postgresql/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package postgresql

import (
"cmp"
"context"
"slices"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
Expand Down Expand Up @@ -126,7 +128,26 @@ func (b *backend) ListDatabases(ctx context.Context, params *backends.ListDataba
return nil, err
}

res := &backends.ListDatabasesResult{
var res *backends.ListDatabasesResult

// TODO https://github.com/FerretDB/FerretDB/issues/3601
if params != nil && len(params.Name) > 0 {
res = &backends.ListDatabasesResult{
Databases: make([]backends.DatabaseInfo, 1),
}
_, found := slices.BinarySearchFunc(list, params.Name, func(dbName, t string) int {
return cmp.Compare(dbName, t)
})

if found {
res.Databases[0] = backends.DatabaseInfo{
Name: params.Name,
}
}
return res, nil
}

res = &backends.ListDatabasesResult{
Databases: make([]backends.DatabaseInfo, len(list)),
}

Expand All @@ -135,7 +156,6 @@ func (b *backend) ListDatabases(ctx context.Context, params *backends.ListDataba
Name: dbName,
}
}

return res, nil
}

Expand Down
22 changes: 21 additions & 1 deletion internal/backends/sqlite/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package sqlite

import (
"cmp"
"context"
"slices"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
Expand Down Expand Up @@ -102,7 +104,25 @@ func (b *backend) Database(name string) (backends.Database, error) {
func (b *backend) ListDatabases(ctx context.Context, params *backends.ListDatabasesParams) (*backends.ListDatabasesResult, error) {
list := b.r.DatabaseList(ctx)

res := &backends.ListDatabasesResult{
var res *backends.ListDatabasesResult

// TODO https://github.com/FerretDB/FerretDB/issues/3601
if params != nil && len(params.Name) > 0 {
res = &backends.ListDatabasesResult{
Databases: make([]backends.DatabaseInfo, 1),
}
_, found := slices.BinarySearchFunc(list, params.Name, func(dbName, t string) int {
return cmp.Compare(dbName, t)
})
if found {
res.Databases[0] = backends.DatabaseInfo{
Name: params.Name,
}
}
return res, nil
}

res = &backends.ListDatabasesResult{
Databases: make([]backends.DatabaseInfo, len(list)),
}

Expand Down
31 changes: 22 additions & 9 deletions internal/handler/msg_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,23 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
}

var cList *backends.ListCollectionsResult

if cList, err = db.ListCollections(ctx, nil); err != nil {
// TODO https://github.com/FerretDB/FerretDB/issues/3601
collectionParam := backends.ListCollectionsParams{Name: cName}
if cList, err = db.ListCollections(ctx, &collectionParam); err != nil {
return nil, err
}

if len(cList.Collections) == 0 {
return nil, handlererrors.NewCommandErrorMsgWithArgument(
handlererrors.ErrNamespaceNotFound,
fmt.Sprintf("collection not found: %s", cName),
document.Command(),
)
}

var cInfo backends.CollectionInfo
cInfo = cList.Collections[0]

// TODO https://github.com/FerretDB/FerretDB/issues/3601
if i, found := slices.BinarySearchFunc(cList.Collections, cName, func(e backends.CollectionInfo, t string) int {
return cmp.Compare(e.Name, t)
}); found {
Expand Down Expand Up @@ -464,18 +473,22 @@ func processStagesStats(ctx context.Context, closer *iterator.MultiCloser, p *st
}

var cList *backends.ListCollectionsResult

if cList, err = p.db.ListCollections(ctx, new(backends.ListCollectionsParams)); err != nil {
collectionParam := backends.ListCollectionsParams{Name: p.cName}
if cList, err = p.db.ListCollections(ctx, &collectionParam); err != nil {
return nil, lazyerrors.Error(err)
}

// TODO https://github.com/FerretDB/FerretDB/issues/3601
if i, found := slices.BinarySearchFunc(cList.Collections, p.cName, func(e backends.CollectionInfo, t string) int {
return cmp.Compare(e.Name, t)
}); found {
cInfo = cList.Collections[i]
if len(cList.Collections) == 0 {
return nil, handlererrors.NewCommandErrorMsgWithArgument(
handlererrors.ErrNamespaceNotFound,
fmt.Sprintf("ns not found: %s.%s", p.dbName, p.cName),
"aggregate",
)
}

cInfo = cList.Collections[0]

var iList *backends.ListIndexesResult

iList, err = p.c.ListIndexes(ctx, new(backends.ListIndexesParams))
Expand Down
19 changes: 9 additions & 10 deletions internal/handler/msg_collstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
package handler

import (
"cmp"
"context"
"fmt"
"slices"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/handler/common"
Expand Down Expand Up @@ -78,22 +76,23 @@ func (h *Handler) MsgCollStats(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
return nil, lazyerrors.Error(err)
}

collections, err := db.ListCollections(ctx, new(backends.ListCollectionsParams))
collectionParam := backends.ListCollectionsParams{Name: collection}
collections, err := db.ListCollections(ctx, &collectionParam)
if err != nil {
return nil, lazyerrors.Error(err)
}

var i int
// TODO https://github.com/FerretDB/FerretDB/issues/3601
var found bool
var cInfo backends.CollectionInfo

// TODO https://github.com/FerretDB/FerretDB/issues/3601
if i, found = slices.BinarySearchFunc(collections.Collections, collection, func(e backends.CollectionInfo, t string) int {
return cmp.Compare(e.Name, t)
}); found {
cInfo = collections.Collections[i]
found = len(collections.Collections) > 0
if !found {
msg := fmt.Sprintf("Invalid collection name: %s", collectionParam.Name)
return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command())
}

cInfo = collections.Collections[0]

indexes, err := c.ListIndexes(ctx, new(backends.ListIndexesParams))
if backends.ErrorCodeIs(err, backends.ErrorCodeCollectionDoesNotExist) {
indexes = new(backends.ListIndexesResult)
Expand Down
17 changes: 8 additions & 9 deletions internal/handler/msg_explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package handler

import (
"cmp"
"context"
"errors"
"fmt"
"os"
"slices"

"github.com/FerretDB/FerretDB/build/version"
"github.com/FerretDB/FerretDB/internal/backends"
Expand Down Expand Up @@ -107,18 +105,19 @@ func (h *Handler) MsgExplain(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,

var cList *backends.ListCollectionsResult

if cList, err = db.ListCollections(ctx, nil); err != nil {
collectionParam := backends.ListCollectionsParams{Name: params.Collection}
if cList, err = db.ListCollections(ctx, &collectionParam); err != nil {
return nil, err
}

var cInfo backends.CollectionInfo
if len(cList.Collections) == 0 {
msg := fmt.Sprintf("Invalid collection name: %s", collectionParam.Name)
return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command())
}

// TODO https://github.com/FerretDB/FerretDB/issues/3601
if i, found := slices.BinarySearchFunc(cList.Collections, params.Collection, func(e backends.CollectionInfo, t string) int {
return cmp.Compare(e.Name, t)
}); found {
cInfo = cList.Collections[i]
}
var cInfo backends.CollectionInfo
cInfo = cList.Collections[0]

switch {
case h.DisablePushdown:
Expand Down
17 changes: 8 additions & 9 deletions internal/handler/msg_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
package handler

import (
"cmp"
"context"
"errors"
"fmt"
"slices"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -71,20 +69,21 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er
}

var cList *backends.ListCollectionsResult

if cList, err = db.ListCollections(ctx, nil); err != nil {
collectionParam := backends.ListCollectionsParams{Name: params.Collection}
if cList, err = db.ListCollections(ctx, &collectionParam); err != nil {
return nil, err
}

var cInfo backends.CollectionInfo

// TODO https://github.com/FerretDB/FerretDB/issues/3601
if i, found := slices.BinarySearchFunc(cList.Collections, params.Collection, func(e backends.CollectionInfo, t string) int {
return cmp.Compare(e.Name, t)
}); found {
cInfo = cList.Collections[i]
if len(cList.Collections) == 0 {
// TODO https://github.com/FerretDB/FerretDB/issues/3601
msg := fmt.Sprintf("Invalid collection name: %s", collectionParam.Name)
return nil, handlererrors.NewCommandErrorMsgWithArgument(handlererrors.ErrInvalidNamespace, msg, document.Command())
sachinpuranik marked this conversation as resolved.
Show resolved Hide resolved
}

cInfo = cList.Collections[0]

capped := cInfo.Capped()
if params.Tailable {
if !capped {
Expand Down