summaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/jsonpath/rewrapper/dispatcher.cpp
blob: 5c534431864b90371f46aa20162171aa18755040 (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
#include "registrator.h"
#include "re.h"

#include <util/generic/fwd.h>
#include <util/generic/vector.h>
#include <util/generic/singleton.h>
#include <util/generic/yexception.h>

#include <yql/essentials/minikql/jsonpath/rewrapper/proto/serialization.pb.h>

namespace NReWrapper {

namespace NRegistrator {

struct TLib {
    ui64 Id = 0;
    TCompiler Compiler;
    TDeserializer Deserializer;
};

using TModules = TVector<TLib>;

TModules* GetModules() {
    return Singleton<TModules>();
}

void AddLibrary(ui32 id, TCompiler compiler, TDeserializer deserializer) {
    Y_ABORT_UNLESS(id > 0);
    if (GetModules()->size() < id) {
        GetModules()->resize(id);
    }
    GetModules()->at(id - 1) = TLib{id, compiler, deserializer};
}

} // namespace NRegistrator

namespace NDispatcher {

void ThrowOnOutOfRange(ui32 id) {
    if (NRegistrator::GetModules()->size() < id || id == 0) {
        ythrow yexception()
            << "Libs with id: " << id
            << " was not found. Total added libs: " << NRegistrator::GetModules()->size();
    }
}

bool Has(ui32 id) {
    if (id == 0 || id > NRegistrator::GetModules()->size()) {
        return false;
    }

    return NRegistrator::GetModules()->at(id).Id == id;
}

IRePtr Deserialize(const TStringBuf& serializedRegex) {
    TSerialization proto;
    TString str(serializedRegex);
    auto res = proto.ParseFromString(str);
    if (!res) {
        proto.SetHyperscan(str);
    }

    ui64 id = (ui64)proto.GetDataCase();
    ThrowOnOutOfRange(id);
    return NRegistrator::GetModules()->at(id - 1).Deserializer(proto);
}

IRePtr Compile(const TStringBuf& regex, unsigned int flags, ui32 id) {
    ThrowOnOutOfRange(id);
    return NRegistrator::GetModules()->at(id - 1).Compiler(regex, flags);
}

} // namespace NDispatcher

} // namespace NReWrapper