aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/complete/name/object/static/schema_gateway.cpp
blob: e325b908f7a582b58242e3b9cddd95d199dc65b9 (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 "schema_gateway.h"

#include <yql/essentials/sql/v1/complete/text/case.h>

#include <util/charset/utf8.h>

namespace NSQLComplete {

    namespace {

        class TSchemaGateway: public ISchemaGateway {
            static constexpr size_t MaxLimit = 4 * 1024;

        public:
            explicit TSchemaGateway(THashMap<TString, TVector<TFolderEntry>> data)
                : Data_(std::move(data))
            {
                for (const auto& [k, _] : Data_) {
                    Y_ENSURE(k.StartsWith("/"), k << " must start with the '/'");
                    Y_ENSURE(k.EndsWith("/"), k << " must end with the '/'");
                }
            }

            NThreading::TFuture<TListResponse> List(const TListRequest& request) const override {
                auto [path, prefix] = ParsePath(request.Path);

                TVector<TFolderEntry> entries;
                if (const auto* data = Data_.FindPtr(path)) {
                    entries = *data;
                }

                EraseIf(entries, [prefix = ToLowerUTF8(prefix)](const TFolderEntry& entry) {
                    return !entry.Name.StartsWith(prefix);
                });

                EraseIf(entries, [types = std::move(request.Filter.Types)](const TFolderEntry& entry) {
                    return types && !types->contains(entry.Type);
                });

                Y_ENSURE(request.Limit <= MaxLimit);
                entries.crop(request.Limit);

                TListResponse response = {
                    .NameHintLength = prefix.size(),
                    .Entries = std::move(entries),
                };

                return NThreading::MakeFuture(std::move(response));
            }

        private:
            static std::tuple<TStringBuf, TStringBuf> ParsePath(TString path Y_LIFETIME_BOUND) {
                size_t pos = path.find_last_of('/');
                if (pos == TString::npos) {
                    return {"", path};
                }

                TStringBuf head, tail;
                TStringBuf(path).SplitAt(pos + 1, head, tail);
                return {head, tail};
            }

            THashMap<TString, TVector<TFolderEntry>> Data_;
        };

    } // namespace

    ISchemaGateway::TPtr MakeStaticSchemaGateway(THashMap<TString, TVector<TFolderEntry>> fs) {
        return MakeIntrusive<TSchemaGateway>(std::move(fs));
    }

} // namespace NSQLComplete