mkql_computation_node_graph_saveload.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "mkql_computation_node_graph_saveload.h"
  2. #include "mkql_computation_node_holders.h"
  3. #include <yql/essentials/minikql/pack_num.h>
  4. #include <yql/essentials/minikql/comp_nodes/mkql_saveload.h>
  5. namespace NKikimr {
  6. namespace NMiniKQL {
  7. namespace {
  8. void TraverseGraph(const NUdf::TUnboxedValue* roots, ui32 rootCount, TVector<NUdf::TUnboxedValue>& values) {
  9. THashSet<NUdf::IBoxedValue*> dedup;
  10. for (ui32 i = 0; i < rootCount; ++i) {
  11. const auto& value = roots[i];
  12. if (!value.IsBoxed()) {
  13. continue;
  14. }
  15. auto* ptr = value.AsBoxed().Get();
  16. if (dedup.contains(ptr)) {
  17. continue;
  18. }
  19. dedup.insert(ptr);
  20. values.push_back(value);
  21. }
  22. for (ui32 from = 0, to = values.size(); from != to; ++from) {
  23. auto current = values[from];
  24. auto count = current.GetTraverseCount();
  25. for (ui32 i = 0; i < count; ++i) {
  26. auto value = current.GetTraverseItem(i);
  27. if (!value.IsBoxed()) {
  28. continue;
  29. }
  30. auto* ptr = value.AsBoxed().Get();
  31. if (dedup.contains(ptr)) {
  32. continue;
  33. }
  34. dedup.insert(ptr);
  35. values.push_back(value);
  36. ++to;
  37. }
  38. }
  39. }
  40. }
  41. void SaveGraphState(const NUdf::TUnboxedValue* roots, ui32 rootCount, ui64 hash, TString& out) {
  42. out.clear();
  43. out.AppendNoAlias((const char*)&hash, sizeof(hash));
  44. TVector<NUdf::TUnboxedValue> values;
  45. TraverseGraph(roots, rootCount, values);
  46. for (ui32 i = 0; i < values.size(); ++i) {
  47. auto state = values[i].Save();
  48. if (state.IsString() || state.IsEmbedded()) {
  49. auto strRef = state.AsStringRef();
  50. auto size = strRef.Size();
  51. WriteUi64(out, size);
  52. if (size) {
  53. out.AppendNoAlias(strRef.Data(), size);
  54. }
  55. }
  56. else if (state.IsBoxed()) {
  57. TString taskState;
  58. auto listIt = state.GetListIterator();
  59. NUdf::TUnboxedValue str;
  60. while (listIt.Next(str)) {
  61. const TStringBuf strRef = str.AsStringRef();
  62. taskState.AppendNoAlias(strRef.Data(), strRef.Size());
  63. }
  64. WriteUi64(out, taskState.size());
  65. if (!taskState.empty()) {
  66. out.AppendNoAlias(taskState.data(), taskState.size());
  67. }
  68. }
  69. }
  70. }
  71. void LoadGraphState(const NUdf::TUnboxedValue* roots, ui32 rootCount, ui64 hash, const TStringBuf& in) {
  72. TStringBuf state(in);
  73. MKQL_ENSURE(state.size() >= sizeof(ui64), "Serialized state is corrupted - no hash");
  74. ui64 storedHash = *(ui64*)state.data();
  75. state.Skip(sizeof(storedHash));
  76. MKQL_ENSURE(hash == storedHash, "Unable to load graph state, different hashes");
  77. TVector<NUdf::TUnboxedValue> values;
  78. TraverseGraph(roots, rootCount, values);
  79. for (ui32 i = 0; i < values.size(); ++i) {
  80. auto size = ReadUi64(state);
  81. if (size) {
  82. MKQL_ENSURE(size <= state.size(), "Serialized state is corrupted");
  83. values[i].Load(NUdf::TStringRef(state.data(), size));
  84. state.Skip(size);
  85. }
  86. }
  87. MKQL_ENSURE(state.size() == 0, "State was not loaded correctly");
  88. }
  89. } // namespace NMiniKQL
  90. } // namespace NKikimr