dispatcher.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "registrator.h"
  2. #include "re.h"
  3. #include <util/generic/fwd.h>
  4. #include <util/generic/vector.h>
  5. #include <util/generic/singleton.h>
  6. #include <util/generic/yexception.h>
  7. #include <yql/essentials/minikql/jsonpath/rewrapper/proto/serialization.pb.h>
  8. namespace NReWrapper {
  9. namespace NRegistrator {
  10. struct TLib {
  11. ui64 Id = 0;
  12. TCompiler Compiler;
  13. TDeserializer Deserializer;
  14. };
  15. using TModules = TVector<TLib>;
  16. TModules* GetModules() {
  17. return Singleton<TModules>();
  18. }
  19. void AddLibrary(ui32 id, TCompiler compiler, TDeserializer deserializer) {
  20. Y_ABORT_UNLESS(id > 0);
  21. if (GetModules()->size() < id) {
  22. GetModules()->resize(id);
  23. }
  24. GetModules()->at(id - 1) = TLib{id, compiler, deserializer};
  25. }
  26. }
  27. namespace NDispatcher {
  28. void ThrowOnOutOfRange(ui32 id) {
  29. if (NRegistrator::GetModules()->size() < id || id == 0) {
  30. ythrow yexception()
  31. << "Libs with id: " << id
  32. << " was not found. Total added libs: " << NRegistrator::GetModules()->size();
  33. }
  34. }
  35. bool Has(ui32 id) {
  36. if (id == 0 || id > NRegistrator::GetModules()->size()) {
  37. return false;
  38. }
  39. return NRegistrator::GetModules()->at(id).Id == id;
  40. }
  41. IRePtr Deserialize(const TStringBuf& serializedRegex) {
  42. TSerialization proto;
  43. TString str(serializedRegex);
  44. auto res = proto.ParseFromString(str);
  45. if (!res) {
  46. proto.SetHyperscan(str);
  47. }
  48. ui64 id = (ui64)proto.GetDataCase();;
  49. ThrowOnOutOfRange(id);
  50. return NRegistrator::GetModules()->at(id - 1).Deserializer(proto);
  51. }
  52. IRePtr Compile(const TStringBuf& regex, unsigned int flags, ui32 id) {
  53. ThrowOnOutOfRange(id);
  54. return NRegistrator::GetModules()->at(id - 1).Compiler(regex, flags);
  55. }
  56. }
  57. }