registry.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "registry.h"
  2. #include <library/cpp/containers/absl_flat_hash/flat_hash_map.h>
  3. #include <library/cpp/blockcodecs/core/codecs.h>
  4. #include <util/system/yassert.h>
  5. #include <util/generic/hash.h>
  6. #include <util/generic/deque.h>
  7. #include <util/generic/singleton.h>
  8. #include <util/system/env.h>
  9. using namespace NResource;
  10. using namespace NBlockCodecs;
  11. namespace {
  12. inline const ICodec* GetCodec() noexcept {
  13. static const ICodec* ret = Codec("zstd08_5");
  14. return ret;
  15. }
  16. typedef std::pair<TStringBuf, TStringBuf> TDescriptor;
  17. [[noreturn]]
  18. void ReportRedefinitionError(const TStringBuf key, const TStringBuf data, const ui64 kk, const TDescriptor& prev) noexcept {
  19. const auto& [prevKey, value] = prev;
  20. Y_ABORT_UNLESS(key == prevKey, "Internal hash collision:"
  21. " old key: %s,"
  22. " new key: %s,"
  23. " hash: %" PRIx64 ".",
  24. TString{prevKey}.Quote().c_str(),
  25. TString{key}.Quote().c_str(),
  26. kk);
  27. size_t vsize = GetCodec()->DecompressedLength(value);
  28. size_t dsize = GetCodec()->DecompressedLength(data);
  29. if (vsize + dsize < 1000) {
  30. Y_ABORT_UNLESS(false, "Redefinition of key %s:\n"
  31. " old value: %s,\n"
  32. " new value: %s.",
  33. TString{key}.Quote().c_str(),
  34. Decompress(value).Quote().c_str(),
  35. Decompress(data).Quote().c_str());
  36. } else {
  37. Y_ABORT_UNLESS(false, "Redefinition of key %s,"
  38. " old size: %zu,"
  39. " new size: %zu.",
  40. TString{key}.Quote().c_str(), vsize, dsize);
  41. }
  42. }
  43. struct TStore final: public IStore, public absl::flat_hash_map<ui64, TDescriptor*> {
  44. static inline ui64 ToK(TStringBuf k) {
  45. return NHashPrivate::ComputeStringHash(k.data(), k.size());
  46. }
  47. void Store(const TStringBuf key, const TStringBuf data) override {
  48. auto kk = ToK(key);
  49. const auto [it, unique] = try_emplace(kk, nullptr);
  50. if (!unique) {
  51. const auto& [_, value] = *it->second;
  52. if (value != data) {
  53. ReportRedefinitionError(key, data, kk, *it->second);
  54. Y_UNREACHABLE();
  55. }
  56. } else {
  57. D_.push_back(TDescriptor(key, data));
  58. it->second = &D_.back();
  59. }
  60. Y_ABORT_UNLESS(size() == Count(), "size mismatch");
  61. }
  62. bool Has(const TStringBuf key) const override {
  63. return contains(ToK(key));
  64. }
  65. bool FindExact(const TStringBuf key, TString* out) const override {
  66. if (auto res = find(ToK(key)); res != end()) {
  67. // temporary
  68. // https://st.yandex-team.ru/DEVTOOLS-3985
  69. try {
  70. *out = Decompress(res->second->second);
  71. } catch (const yexception& e) {
  72. if (GetEnv("RESOURCE_DECOMPRESS_DIAG")) {
  73. Cerr << "Can't decompress resource " << key << Endl << e.what() << Endl;
  74. }
  75. throw e;
  76. }
  77. return true;
  78. }
  79. return false;
  80. }
  81. void FindMatch(const TStringBuf subkey, IMatch& cb) const override {
  82. for (const auto& it : D_) {
  83. if (it.first.StartsWith(subkey)) {
  84. // temporary
  85. // https://st.yandex-team.ru/DEVTOOLS-3985
  86. try {
  87. const TResource res = {
  88. it.first, Decompress(it.second)};
  89. cb.OnMatch(res);
  90. } catch (const yexception& e) {
  91. if (GetEnv("RESOURCE_DECOMPRESS_DIAG")) {
  92. Cerr << "Can't decompress resource " << it.first << Endl << e.what() << Endl;
  93. }
  94. throw e;
  95. }
  96. }
  97. }
  98. }
  99. size_t Count() const noexcept override {
  100. return D_.size();
  101. }
  102. TStringBuf KeyByIndex(size_t idx) const override {
  103. return D_.at(idx).first;
  104. }
  105. typedef TDeque<TDescriptor> TDescriptors;
  106. TDescriptors D_;
  107. };
  108. }
  109. TString NResource::Compress(const TStringBuf data) {
  110. return GetCodec()->Encode(data);
  111. }
  112. TString NResource::Decompress(const TStringBuf data) {
  113. return GetCodec()->Decode(data);
  114. }
  115. IStore* NResource::CommonStore() {
  116. return SingletonWithPriority<TStore, 0>();
  117. }
  118. NResource::TRegHelper::TRegHelper(const TStringBuf key, const TStringBuf data) {
  119. CommonStore()->Store(key, data);
  120. }