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

Replace document slices with iterators #2730

Merged
merged 19 commits into from
Jun 2, 2023
Merged
Prev Previous commit
Next Next commit
wip
  • Loading branch information
noisersup committed May 30, 2023
commit 798de5698ac32cb8122297712384bf3adfd9bbc4
17 changes: 3 additions & 14 deletions internal/handlers/common/distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/handlers/commonparams"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

Expand Down Expand Up @@ -66,20 +65,10 @@ func GetDistinctParams(document *types.Document, l *zap.Logger) (*DistinctParams
//
// If the key is found in the document, and the value is an array, each element of the array is added to the result.
// Otherwise, the value itself is added to the result.
func FilterDistinctValues(iter types.DocumentsIterator, key string) (*types.Array, error) {
distinct := types.MakeArray(0)
defer iter.Close()

for {
_, doc, err := iter.Next()
if err != nil {
if err == iterator.ErrIteratorDone {
break
}

return nil, err
}
func FilterDistinctValues(docs []*types.Document, key string) (*types.Array, error) {
distinct := types.MakeArray(len(docs))

for _, doc := range docs {
// docsAtSuffix contains all documents exist at the suffix key.
docsAtSuffix := []*types.Document{doc}
suffix := key
Expand Down
19 changes: 4 additions & 15 deletions internal/handlers/tigris/msg_distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/tigris/tigrisdb"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/iterator"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/wire"
Expand All @@ -46,25 +45,15 @@ func (h *Handler) MsgDistinct(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg
qp := tigrisdb.QueryParams{
DB: dp.DB,
Collection: dp.Collection,
Filter: dp.Filter,
}

if !h.DisableFilterPushdown {
qp.Filter = dp.Filter
}

fp := &fetchParams{dbPool, &qp, h.DisableFilterPushdown}

iter, err := fp.dbPool.QueryDocuments(ctx, fp.qp)
resDocs, err := fetchAndFilterDocs(ctx, &fetchParams{dbPool, &qp, h.DisableFilterPushdown})
if err != nil {
return nil, lazyerrors.Error(err)
return nil, err
}

closer := iterator.NewMultiCloser(iter)
defer closer.Close()

iter = common.FilterIterator(iter, closer, qp.Filter)

distinct, err := common.FilterDistinctValues(iter, dp.Key)
distinct, err := common.FilterDistinctValues(resDocs, dp.Key)
if err != nil {
return nil, err
}
Expand Down