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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add lazy implementation
  • Loading branch information
Elena Grahovac committed Jul 21, 2022
commit 49238aaae22fcca033e1cc1a87813f087cf952af
102 changes: 99 additions & 3 deletions internal/handlers/tigris/msg_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +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
// todo
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
}