summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/ide/completion/name/service/union/name_service.cpp
blob: 00771c8c7eacb01b7b6749c0f1647c916e1b7b5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "name_service.h"

#include <library/cpp/threading/future/wait/wait.h>

namespace NSQLComplete {

namespace {

class TNameService: public INameService {
public:
    TNameService(
        TVector<INameService::TPtr> children,
        IRanking::TPtr ranking)
        : Children_(std::move(children))
        , Ranking_(std::move(ranking))
    {
    }

    NThreading::TFuture<TNameResponse> Lookup(const TNameRequest& request) const override {
        TVector<NThreading::TFuture<TNameResponse>> fs;
        for (const auto& c : Children_) {
            fs.emplace_back(c->Lookup(request));
        }

        return NThreading::WaitAll(fs)
            // TODO(YQL-20095): Explore real problem to fix this.
            // NOLINTNEXTLINE(bugprone-exception-escape)
            .Apply([fs, ranking = Ranking_, request](auto) {
                return Union(fs, ranking, request.Constraints, request.Limit);
            });
    }

private:
    static TNameResponse Union(
        TVector<NThreading::TFuture<TNameResponse>> fs,
        IRanking::TPtr ranking,
        const TNameConstraints& constraints,
        size_t limit) {
        TNameResponse united;
        for (auto f : fs) {
            TNameResponse response = f.ExtractValue();

            std::ranges::move(
                response.RankedNames,
                std::back_inserter(united.RankedNames));

            if (!response.IsEmpty() && response.NameHintLength) {
                Y_ENSURE(
                    united.NameHintLength.Empty() ||
                    united.NameHintLength == response.NameHintLength);
                united.NameHintLength = response.NameHintLength;
            }
        }
        ranking->CropToSortedPrefix(united.RankedNames, constraints, limit);
        return united;
    }

    TVector<INameService::TPtr> Children_;
    IRanking::TPtr Ranking_;
};

} // namespace

INameService::TPtr MakeUnionNameService(
    TVector<INameService::TPtr> children,
    IRanking::TPtr ranking) {
    return INameService::TPtr(new TNameService(
        std::move(children),
        std::move(ranking)));
}

} // namespace NSQLComplete