summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/ide/completion/name/object/simple/static/schema.cpp
blob: 96ce885cdb1e314afd722e7ec949bc37d969a101 (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
87
88
89
90
#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 = "", .NameHint = path};
        }

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

    NThreading::TFuture<TVector<TFolderEntry>> List(TString cluster, TString folder) const override {
        TVector<TFolderEntry> entries;

        const THashMap<TString, TVector<TFolderEntry>>* tables = nullptr;
        const TVector<TFolderEntry>* items = nullptr;
        if ((tables = Folders_.FindPtr(cluster)) &&
            (items = tables->FindPtr(Qualified(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(Qualified(path));
        if (details == nullptr) {
            return NThreading::MakeFuture<TMaybe<TTableDetails>>(Nothing());
        }

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

private:
    TString Qualified(TString path) const {
        if (!path.StartsWith('/')) {
            path.prepend('/');
        }
        return path;
    }

    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