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

Implement MsgCount for Tigris #928

Merged
merged 22 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
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
53 changes: 53 additions & 0 deletions integration/tigris/smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package tigris

import (
"math"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"

Expand All @@ -34,3 +36,54 @@ func TestSmoke(t *testing.T) {
require.NoError(t, err)
integration.AssertEqualDocuments(t, bson.D{{"_id", "fixed_double"}, {"double_value", 42.13}}, doc)
}

// TestSmokeMsgCount implements simple smoke tests for MsgCount.
// TODO Implement proper testing: https://github.com/FerretDB/FerretDB/issues/931.
func TestSmokeMsgCount(t *testing.T) {
t.Parallel()

// As Tigris require different fields for different types,
// for this smoke test we only use the fixed scalars.
ctx, collection := setup.Setup(t, shareddata.FixedScalars)

for name, tc := range map[string]struct {
command any
response int32
}{
"CountAllFixedScalars": {
command: bson.D{{"count", collection.Name()}},
response: 6,
},
"CountExactlyOneDocument": {
command: bson.D{
{"count", collection.Name()},
{"query", bson.D{{"double_value", math.MaxFloat64}}},
},
response: 1,
},
"CountNonExistingCollection": {
command: bson.D{
{"count", "doesnotexist"},
{"query", bson.D{{"v", true}}},
},
response: 0,
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

var actual bson.D
err := collection.Database().RunCommand(ctx, tc.command).Decode(&actual)
require.NoError(t, err)

m := actual.Map()

assert.Equal(t, float64(1), m["ok"])

keys := integration.CollectKeys(t, actual)
assert.Contains(t, keys, "n")
assert.Equal(t, tc.response, m["n"])
})
}
}
11 changes: 11 additions & 0 deletions internal/handlers/tigris/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"context"

"github.com/tigrisdata/tigris-client-go/driver"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/FerretDB/FerretDB/internal/tjson"
"github.com/FerretDB/FerretDB/internal/types"
Expand All @@ -38,6 +41,14 @@ func (h *Handler) fetch(ctx context.Context, param fetchParam) ([]*types.Documen

collection, err := db.DescribeCollection(ctx, param.collection)
if err != nil {
if status.Code(err) == codes.NotFound {
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
h.L.Debug(
"Collection doesn't exist, handling a case to deal with a non-existing collection (return empty list)",
zap.String("db", param.db), zap.String("collection", param.collection),
)
return []*types.Document{}, nil
}

return nil, lazyerrors.Error(err)
}

Expand Down
101 changes: 99 additions & 2 deletions internal/handlers/tigris/msg_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,110 @@ package tigris

import (
"context"
"fmt"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/wire"
)

// MsgCount implements HandlerInterface.
func (h *Handler) MsgCount(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
// TODO https://github.com/FerretDB/FerretDB/issues/771
return nil, notImplemented(must.NotFail(msg.Document()).Command())
document, err := msg.Document()
if err != nil {
return nil, lazyerrors.Error(err)
}

unimplementedFields := []string{
"skip",
"returnKey",
"showRecordId",
"tailable",
"oplogReplay",
"noCursorTimeout",
"awaitData",
"allowPartialResults",
"collation",
"allowDiskUse",
"let",
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
}
if err := common.Unimplemented(document, unimplementedFields...); err != nil {
return nil, err
}
ignoredFields := []string{
"hint",
"batchSize",
"singleBatch",
"maxTimeMS",
"readConcern",
"max",
"min",
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
}
common.Ignored(document, h.L, ignoredFields...)

var filter *types.Document
if filter, err = common.GetOptionalParam(document, "query", filter); err != nil {
return nil, err
}

var limit int64
if l, _ := document.Get("limit"); l != nil {
if limit, err = common.GetWholeNumberParam(l); err != nil {
return nil, err
}
}

var fp fetchParam
if fp.db, err = common.GetRequiredParam[string](document, "$db"); err != nil {
return nil, err
}
collectionParam, err := document.Get(document.Command())
if err != nil {
return nil, err
}
var ok bool
if fp.collection, ok = collectionParam.(string); !ok {
return nil, common.NewErrorMsg(
common.ErrBadValue,
fmt.Sprintf("collection name has invalid type %s", common.AliasFromType(collectionParam)),
)
}

fetchedDocs, err := h.fetch(ctx, fp)
if err != nil {
return nil, err
}

resDocs := make([]*types.Document, 0, 16)
for _, doc := range fetchedDocs {
matches, err := common.FilterDocument(doc, filter)
if err != nil {
return nil, err
}

if !matches {
continue
}

resDocs = append(resDocs, doc)
}

if resDocs, err = common.LimitDocuments(resDocs, limit); err != nil {
return nil, err
}

var reply wire.OpMsg
err = reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{must.NotFail(types.NewDocument(
"n", int32(len(resDocs)),
"ok", float64(1),
))},
})
if err != nil {
return nil, lazyerrors.Error(err)
}

return &reply, nil
}