summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/complete/name/object/simple/static/schema.cpp
blob: 576de4dfdc52375ec9789da0f575b88e7054ed42 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "schema.h"

#include <library/cpp/iterator/concatenate.h>

namespace NSQLComplete {

    namespace {

        class TSimpleSchema: public ISimpleSchema {
        public:
            explicit TSimpleSchema(
                THashMap<TString, THashMap<TString, TVector<TFolderEntry>>> folders,
                THashMap<TString, THashMap<TString, TTableDetails>> tables)
                : Folders_(std::move(folders))
                , Tables_(std::move(tables))
            {
                for (const auto& [_, paths] : Folders_) {
                    for (const auto& [k, _] : paths) {
                        Y_ENSURE(k.StartsWith("/"), k << " must start with the '/'");
                        Y_ENSURE(k.EndsWith("/"), k << " must end with the '/'");
                    }
                }

                for (const auto& [_, paths] : Tables_) {
                    for (const auto& [k, _] : paths) {
                        Y_ENSURE(k.StartsWith("/"), k << " must start with the '/'");
                        Y_ENSURE(!k.EndsWith("/"), k << " must not end with the '/'");
                    }
                }
            }

            TSplittedPath Split(TStringBuf path) const override {
                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};
            }

            NThreading::TFuture<TVector<TFolderEntry>> List(TString cluster, TString folder) const override {
                if (!folder.StartsWith('/')) {
                    folder.prepend('/');
                }

                TVector<TFolderEntry> entries;

                const THashMap<TString, TVector<TFolderEntry>>* tables = nullptr;
                const TVector<TFolderEntry>* items = nullptr;
                if ((tables = Folders_.FindPtr(cluster)) &&
                    (items = tables->FindPtr(folder))) {
                    entries = *items;
                }

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

            NThreading::TFuture<TMaybe<TTableDetails>>
            DescribeTable(const TString& cluster, const TString& path) const override {
                auto* tables = Tables_.FindPtr(cluster);
                if (tables == nullptr) {
                    return NThreading::MakeFuture<TMaybe<TTableDetails>>(Nothing());
                }

                auto* details = tables->FindPtr(path);
                if (details == nullptr) {
                    return NThreading::MakeFuture<TMaybe<TTableDetails>>(Nothing());
                }

                return NThreading::MakeFuture<TMaybe<TTableDetails>>(*details);
            }

        private:
            THashMap<TString, THashMap<TString, TVector<TFolderEntry>>> Folders_;
            THashMap<TString, THashMap<TString, TTableDetails>> Tables_;
        };

    } // namespace

    ISimpleSchema::TPtr MakeStaticSimpleSchema(TSchemaData data) {
        return new TSimpleSchema(std::move(data.Folders), std::move(data.Tables));
    }

} // namespace NSQLComplete