aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/error/error_attributes.cpp
diff options
context:
space:
mode:
authorarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-10-22 18:15:25 +0300
committerarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-10-22 19:13:37 +0300
commit477bb1cd2d7d44f218454a8a4137a849eb7e64fa (patch)
treea9a2acf38b06ab47ecc6a377c82e5ec494ab588b /library/cpp/yt/error/error_attributes.cpp
parent0bd28574f1df329ac2b07bf9081184f739bcb483 (diff)
downloadydb-477bb1cd2d7d44f218454a8a4137a849eb7e64fa.tar.gz
YT-21233: Introduce TErrorAttributes as an opaque type for attributes to hide dependencies on IAttributeDictionary's API
Plan: 1) Remove `IAttributedDictionary` type from the public API. \+ 2) Remove `Set` method from public API in favor of `operator<<=`. \+ 3) Adopt `ConvertTo<T>` (or other name) CPO with proper extension into `NYT::NYson::ConvertTo` from `yt/core`. 4) Use CPO from (3) to eliminate direct dependency on `yt/core` of `Get/Find` methods from attributes API. 5) Adopt `ConvertToYsonString` (or other name) CPO with proper extension into `yt/core` customisations. 6) Use CPO from (5) to eliminate direct dependency on `yt/core` of `TErrorAttribute` ctor. 7) Swap attributes implementation to the one which doesn’t use `IAttributeDictionary`. 8) At this point `stripped_error*` can be moved to library/cpp/yt and so can recursively dependant on THROW macro methods `Get/Find/…`. 9) Adjust CPO’s to work with `std::string` instead of `TYsonString` assuming text format to be used (maybe `TString` for now). 10) Remove dep of `library/cpp/yt/error` on `yson` entirely. This pull request covers steps 1 and 2 laying foundation to 3-4 being implemented in the next one commit_hash:e899bccdef4ebed321ea2eb93215282694c261ac
Diffstat (limited to 'library/cpp/yt/error/error_attributes.cpp')
-rw-r--r--library/cpp/yt/error/error_attributes.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/library/cpp/yt/error/error_attributes.cpp b/library/cpp/yt/error/error_attributes.cpp
new file mode 100644
index 0000000000..2c1b542314
--- /dev/null
+++ b/library/cpp/yt/error/error_attributes.cpp
@@ -0,0 +1,62 @@
+#include "error_attributes.h"
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+TErrorAttributes::TErrorAttributes(void* attributes)
+ : Attributes_(attributes)
+{ }
+
+void TErrorAttributes::Clear()
+{
+ for (const auto& key : ListKeys()) {
+ Remove(key);
+ }
+}
+
+NYson::TYsonString TErrorAttributes::GetYsonAndRemove(const TString& key)
+{
+ auto result = GetYson(key);
+ Remove(key);
+ return result;
+}
+
+bool TErrorAttributes::Contains(TStringBuf key) const
+{
+ return FindYson(key).operator bool();
+}
+
+bool operator == (const TErrorAttributes& lhs, const TErrorAttributes& rhs)
+{
+ auto lhsPairs = lhs.ListPairs();
+ auto rhsPairs = rhs.ListPairs();
+ if (lhsPairs.size() != rhsPairs.size()) {
+ return false;
+ }
+
+ std::sort(lhsPairs.begin(), lhsPairs.end(), [] (const auto& lhs, const auto& rhs) {
+ return lhs.first < rhs.first;
+ });
+ std::sort(rhsPairs.begin(), rhsPairs.end(), [] (const auto& lhs, const auto& rhs) {
+ return lhs.first < rhs.first;
+ });
+
+ for (auto index = 0; index < std::ssize(lhsPairs); ++index) {
+ if (lhsPairs[index].first != rhsPairs[index].first) {
+ return false;
+ }
+ }
+
+ for (auto index = 0; index < std::ssize(lhsPairs); ++index) {
+ if (lhsPairs[index].second != rhsPairs[index].second) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT