aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVitalii Gridnev <gridnevvvit@gmail.com>2024-12-10 12:38:02 +0300
committerGitHub <noreply@github.com>2024-12-10 12:38:02 +0300
commit5dde9124e4b679724c462d07aca7a88ae553a82c (patch)
tree32005cb6c2d7b928ba90c87cfac7295a10de36b0
parente5f6c1258e5be95a4e10f320962b0a6eaf628c23 (diff)
downloadydb-5dde9124e4b679724c462d07aca7a88ae553a82c.tar.gz
avoid code duplication of cell vectors hascode and equals (#12437)
-rw-r--r--ydb/core/kqp/runtime/kqp_stream_lookup_worker.cpp65
-rw-r--r--ydb/core/scheme/scheme_tablecell.cpp59
-rw-r--r--ydb/core/scheme/scheme_tablecell.h16
-rw-r--r--ydb/core/scheme/ya.make1
-rw-r--r--ydb/core/tx/datashard/conflicts_cache.h68
5 files changed, 77 insertions, 132 deletions
diff --git a/ydb/core/kqp/runtime/kqp_stream_lookup_worker.cpp b/ydb/core/kqp/runtime/kqp_stream_lookup_worker.cpp
index 78dc47933b7..b1d56e60dc8 100644
--- a/ydb/core/kqp/runtime/kqp_stream_lookup_worker.cpp
+++ b/ydb/core/kqp/runtime/kqp_stream_lookup_worker.cpp
@@ -62,70 +62,7 @@ NScheme::TTypeInfo UnpackTypeInfo(NKikimr::NMiniKQL::TType* type) {
return NScheme::TypeInfoFromMiniKQLType(type);
}
-struct THashableKey {
- TConstArrayRef<TCell> Cells;
-
- template <typename H>
- friend H AbslHashValue(H h, const THashableKey& key) {
- h = H::combine(std::move(h), key.Cells.size());
- for (const TCell& cell : key.Cells) {
- h = H::combine(std::move(h), cell.IsNull());
- if (!cell.IsNull()) {
- h = H::combine(std::move(h), cell.Size());
- h = H::combine_contiguous(std::move(h), cell.Data(), cell.Size());
- }
- }
- return h;
- }
-};
-
-struct TKeyHash {
- using is_transparent = void;
-
- size_t operator()(TConstArrayRef<TCell> key) const {
- return absl::Hash<THashableKey>()(THashableKey{ key });
- }
-};
-
-struct TKeyEq {
- using is_transparent = void;
-
- bool operator()(TConstArrayRef<TCell> a, TConstArrayRef<TCell> b) const {
- if (a.size() != b.size()) {
- return false;
- }
-
- const TCell* pa = a.data();
- const TCell* pb = b.data();
- if (pa == pb) {
- return true;
- }
-
- size_t left = a.size();
- while (left > 0) {
- if (pa->IsNull()) {
- if (!pb->IsNull()) {
- return false;
- }
- } else {
- if (pb->IsNull()) {
- return false;
- }
- if (pa->Size() != pb->Size()) {
- return false;
- }
- if (pa->Size() > 0 && ::memcmp(pa->Data(), pb->Data(), pa->Size()) != 0) {
- return false;
- }
- }
- ++pa;
- ++pb;
- --left;
- }
- return true;
- }
-};
} // !namespace
TKqpStreamLookupWorker::TKqpStreamLookupWorker(NKikimrKqp::TKqpStreamLookupSettings&& settings,
@@ -1001,7 +938,7 @@ private:
std::deque<std::pair<TOwnedCellVec, NUdf::TUnboxedValue>> UnprocessedRows;
std::deque<TOwnedTableRange> UnprocessedKeys;
std::unordered_map<ui64, std::vector<TOwnedTableRange>> PendingKeysByReadId;
- absl::flat_hash_map<TOwnedCellVec, TLeftRowInfo, TKeyHash, TKeyEq> PendingLeftRowsByKey;
+ absl::flat_hash_map<TOwnedCellVec, TLeftRowInfo, NKikimr::TCellVectorsHash, NKikimr::TCellVectorsEquals> PendingLeftRowsByKey;
std::unordered_map<ui64, TResultBatch> ResultRowsBySeqNo;
ui64 InputRowSeqNo = 0;
ui64 CurrentResultSeqNo = 0;
diff --git a/ydb/core/scheme/scheme_tablecell.cpp b/ydb/core/scheme/scheme_tablecell.cpp
index 53448bd16c6..768ee84e647 100644
--- a/ydb/core/scheme/scheme_tablecell.cpp
+++ b/ydb/core/scheme/scheme_tablecell.cpp
@@ -4,6 +4,8 @@
#include <ydb/library/actors/core/actorid.h>
#include <util/string/escape.h>
+#include <library/cpp/containers/absl_flat_hash/flat_hash_map.h>
+
namespace NKikimr {
void TOwnedCellVec::TData::operator delete(void* mem) noexcept {
@@ -64,6 +66,63 @@ TOwnedCellVec::TInit TOwnedCellVec::Allocate(TOwnedCellVec::TCellVec cells) {
};
}
+struct THashableKey {
+ TConstArrayRef<TCell> Cells;
+
+ template <typename H>
+ friend H AbslHashValue(H h, const THashableKey& key) {
+ h = H::combine(std::move(h), key.Cells.size());
+ for (const TCell& cell : key.Cells) {
+ h = H::combine(std::move(h), cell.IsNull());
+ if (!cell.IsNull()) {
+ h = H::combine(std::move(h), cell.Size());
+ h = H::combine_contiguous(std::move(h), cell.Data(), cell.Size());
+ }
+ }
+ return h;
+ }
+};
+
+size_t TCellVectorsHash::operator()(TConstArrayRef<TCell> key) const {
+ return absl::Hash<THashableKey>()(THashableKey{ key });
+}
+
+bool TCellVectorsEquals::operator() (TConstArrayRef<TCell> a, TConstArrayRef<TCell> b) const {
+ if (a.size() != b.size()) {
+ return false;
+ }
+
+ const TCell* pa = a.data();
+ const TCell* pb = b.data();
+ if (pa == pb) {
+ return true;
+ }
+
+ size_t left = a.size();
+ while (left > 0) {
+ if (pa->IsNull()) {
+ if (!pb->IsNull()) {
+ return false;
+ }
+ } else {
+ if (pb->IsNull()) {
+ return false;
+ }
+ if (pa->Size() != pb->Size()) {
+ return false;
+ }
+ if (pa->Size() > 0 && ::memcmp(pa->Data(), pb->Data(), pa->Size()) != 0) {
+ return false;
+ }
+ }
+ ++pa;
+ ++pb;
+ --left;
+ }
+
+ return true;
+}
+
namespace {
struct TCellHeader {
diff --git a/ydb/core/scheme/scheme_tablecell.h b/ydb/core/scheme/scheme_tablecell.h
index 93d6ab9446d..f0bf5f7fd82 100644
--- a/ydb/core/scheme/scheme_tablecell.h
+++ b/ydb/core/scheme/scheme_tablecell.h
@@ -179,10 +179,22 @@ inline size_t EstimateSize(TCellsRef cells) {
size += AlignUp(cellSize);
}
}
-
+
return size;
}
+struct TCellVectorsHash {
+ using is_transparent = void;
+
+ size_t operator()(TConstArrayRef<TCell> key) const;
+};
+
+struct TCellVectorsEquals {
+ using is_transparent = void;
+
+ bool operator()(TConstArrayRef<TCell> a, TConstArrayRef<TCell> b) const;
+};
+
inline int CompareCellsAsByteString(const TCell& a, const TCell& b, bool isDescending) {
const char* pa = (const char*)a.Data();
const char* pb = (const char*)b.Data();
@@ -558,7 +570,7 @@ public:
explicit operator bool() const
{
return !Cells.empty();
- }
+ }
// read headers, assuming the buf is correct and append additional cells at the end
static bool UnsafeAppendCells(TConstArrayRef<TCell> cells, TString& serializedCellVec);
diff --git a/ydb/core/scheme/ya.make b/ydb/core/scheme/ya.make
index e61ea8acf3d..ef87c4b7c34 100644
--- a/ydb/core/scheme/ya.make
+++ b/ydb/core/scheme/ya.make
@@ -25,6 +25,7 @@ PEERDIR(
# temporary.
ydb/library/pretty_types_print/protobuf
library/cpp/lwtrace/mon
+ library/cpp/containers/absl_flat_hash
)
END()
diff --git a/ydb/core/tx/datashard/conflicts_cache.h b/ydb/core/tx/datashard/conflicts_cache.h
index 2c14e5d24ef..30e866def32 100644
--- a/ydb/core/tx/datashard/conflicts_cache.h
+++ b/ydb/core/tx/datashard/conflicts_cache.h
@@ -23,71 +23,6 @@ class TDataShard;
* for.
*/
class TTableConflictsCache {
- struct THashableKey {
- TConstArrayRef<TCell> Cells;
-
- template <typename H>
- friend H AbslHashValue(H h, const THashableKey& key) {
- h = H::combine(std::move(h), key.Cells.size());
- for (const TCell& cell : key.Cells) {
- h = H::combine(std::move(h), cell.IsNull());
- if (!cell.IsNull()) {
- h = H::combine(std::move(h), cell.Size());
- h = H::combine_contiguous(std::move(h), cell.Data(), cell.Size());
- }
- }
- return h;
- }
- };
-
- struct TKeyHash {
- using is_transparent = void;
-
- size_t operator()(TConstArrayRef<TCell> key) const {
- return absl::Hash<THashableKey>()(THashableKey{ key });
- }
- };
-
- struct TKeyEq {
- using is_transparent = void;
-
- bool operator()(TConstArrayRef<TCell> a, TConstArrayRef<TCell> b) const {
- if (a.size() != b.size()) {
- return false;
- }
-
- const TCell* pa = a.data();
- const TCell* pb = b.data();
- if (pa == pb) {
- return true;
- }
-
- size_t left = a.size();
- while (left > 0) {
- if (pa->IsNull()) {
- if (!pb->IsNull()) {
- return false;
- }
- } else {
- if (pb->IsNull()) {
- return false;
- }
- if (pa->Size() != pb->Size()) {
- return false;
- }
- if (pa->Size() > 0 && ::memcmp(pa->Data(), pb->Data(), pa->Size()) != 0) {
- return false;
- }
- }
- ++pa;
- ++pb;
- --left;
- }
-
- return true;
- }
- };
-
struct TWriteKey {
TOwnedCellVec Key;
absl::flat_hash_set<ui64> UncommittedWrites;
@@ -102,7 +37,8 @@ class TTableConflictsCache {
absl::flat_hash_set<TWriteKey*> WriteKeys;
};
- using TWriteKeys = absl::flat_hash_map<TOwnedCellVec, std::unique_ptr<TWriteKey>, TKeyHash, TKeyEq>;
+ using TWriteKeys = absl::flat_hash_map<TOwnedCellVec, std::unique_ptr<TWriteKey>,
+ NKikimr::TCellVectorsHash, NKikimr::TCellVectorsEquals>;
using TUncommittedWrites = absl::flat_hash_map<ui64, std::unique_ptr<TUncommittedWrite>>;
using TDistributedWrites = absl::flat_hash_map<ui64, std::unique_ptr<TDistributedWrite>>;