aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/yql_udf_index_package_set.cpp
blob: 8d051e121fc35fa2f60400835637ec0168e33a7c (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
#include "yql_udf_index_package_set.h"

#include <yql/essentials/utils/yql_panic.h>

using namespace NYql;

TUdfIndexPackageSet::TUdfIndexPackageSet() {
}

TUdfIndexPackageSet::TUdfIndexPackageSet(const TSet<TString>& knownPackages, const THashMap<TString, ui32>& packageDefaultVersions, const TMap<std::pair<TString, ui32>, TResourceInfo::TPtr>& resources)
    : KnownPackages_(knownPackages)
    , PackageDefaultVersions_(packageDefaultVersions)
    , Resources_(resources)
{
}

void TUdfIndexPackageSet::RegisterPackage(const TString& package) {
    KnownPackages_.insert(package);
}

bool TUdfIndexPackageSet::SetPackageDefaultVersion(const TString& package, ui32 version) {
    if (!KnownPackages_.contains(package)) {
        return false;
    }

    PackageDefaultVersions_[package] = version;
    return true;
}

void TUdfIndexPackageSet::RegisterResource(const TString& package, ui32 version, const TResourceInfo::TPtr& resource) {
    YQL_ENSURE(resource != nullptr);
    YQL_ENSURE(KnownPackages_.contains(package), "Unknown package " << package);
    Resources_.emplace(std::make_pair(package, version), resource);
}

bool TUdfIndexPackageSet::AddResourceTo(const TString& package, ui32 version, const TUdfIndex::TPtr& target) const {
    auto it = Resources_.find(std::make_pair(package, version));
    if (it == Resources_.end()) {
        return KnownPackages_.contains(package);
    }
    target->RegisterResource(it->second, TUdfIndex::EOverrideMode::ReplaceWithNew);
    return true;
}

void TUdfIndexPackageSet::AddResourcesTo(const TUdfIndex::TPtr& target) const {
    YQL_ENSURE(target != nullptr);
    for (auto& package : KnownPackages_) {
        auto vIt = PackageDefaultVersions_.find(package);
        const ui32 version = (vIt != PackageDefaultVersions_.end()) ? vIt->second : 0;
        auto it = Resources_.find(std::make_pair(package, version));
        if (it == Resources_.end()) {
            // todo: consider error?
            continue;
        }
        target->RegisterResource(it->second, TUdfIndex::EOverrideMode::ReplaceWithNew);
    }
}

TUdfIndexPackageSet::TPtr TUdfIndexPackageSet::Clone() const {
    return new TUdfIndexPackageSet(KnownPackages_, PackageDefaultVersions_, Resources_);
}