aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/yql_user_data.h
blob: cf825e8c7627c3a30f3f99aad52ff1e58fd279bf (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once

#include <library/cpp/enumbitset/enumbitset.h>

#include <yql/essentials/core/file_storage/file_storage.h>

#include <util/generic/hash.h>
#include <util/generic/hash_set.h>

#include <functional>

namespace NYql {

// -- user files --

enum class EUserDataType {
    URL = 1, // indicates that provided user data represents URL which can be used to obtain table data
    PATH = 2, // indicates that provided user data represents file path which can be used to obtain table data
    RAW_INLINE_DATA = 3, // table data is inside the provided data block
};

enum class EUserDataBlockUsage {
    Begin,
    Path = Begin,
    Content,
    Udf,
    Library,
    PgExt,
    End,
};
typedef TEnumBitSet<EUserDataBlockUsage, static_cast<int>(EUserDataBlockUsage::Begin),
    static_cast<int>(EUserDataBlockUsage::End)> TUserDataBlockUsage;

struct TUserDataBlock {
    EUserDataType Type = EUserDataType::PATH;
    TString UrlToken;
    TString Data;
    TUserDataBlockUsage Usage;

    TFileLinkPtr FrozenFile;
    // Prefix added to all UDF module names
    TString CustomUdfPrefix = {};

    THashMap<TString, TString> Options = {};
};

class TUserDataKey {
public:
    enum class EDataType {
        FILE, UDF
    };

    static TUserDataKey File(const TString& alias) {
        return { EDataType::FILE, alias };
    }

    static TUserDataKey File(const TStringBuf& alias) {
        return { EDataType::FILE, TString(alias) };
    }

    static TUserDataKey Udf(const TString& alias) {
        return { EDataType::UDF, alias };
    }

    static TUserDataKey Udf(const TStringBuf& alias) {
        return { EDataType::UDF, TString(alias) };
    }

    inline bool IsFile() const { return Type_ == EDataType::FILE; }
    inline bool IsUdf() const { return Type_ == EDataType::UDF; }
    inline const TString& Alias() const { return Alias_; }
    inline EDataType Type() const { return Type_; }

    bool operator<(const TUserDataKey& other) const {
        return std::tie(Type_, Alias_) < std::tie(other.Type_, other.Alias_);
    }

    struct THash {
        size_t operator()(const TUserDataKey& key) {
            auto type = static_cast<size_t>(key.Type_);
            return CombineHashes(type, ComputeHash(key.Alias_));
        }
    };

    struct TEqualTo {
        bool operator()(const TUserDataKey& k1, const TUserDataKey& k2) const {
            return k1.Type_ == k2.Type_ && k1.Alias_ == k2.Alias_;
        }
    };

private:
    inline TUserDataKey(EDataType type, TString alias)
        : Type_(type)
        , Alias_(std::move(alias))
    {
    }

private:
    const EDataType Type_;
    const TString Alias_;
};

inline IOutputStream& operator<<(IOutputStream& os, const TUserDataKey& key) {
    os << "Type: " << key.Type() << ", alias: " << key.Alias();
    return os;
}

using TUserDataTable = THashMap<TUserDataKey, TUserDataBlock, TUserDataKey::THash, TUserDataKey::TEqualTo>;

using TTokenResolver = std::function<TString(const TString&, const TString&)>;

} // namespace NYql