Skip to content

Commit 89ce243

Browse files
committed
fdsdump: add comparison method to View
1 parent 7e3ca59 commit 89ce243

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/tools/fdsdump/src/aggregator/view.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,24 @@ View::ordered_before(uint8_t *key1, uint8_t *key2) const
135135
return false;
136136
}
137137

138+
CmpResult
139+
View::compare(const uint8_t *rec1, const uint8_t *rec2) const
140+
{
141+
assert(!m_order_fields.empty());
142+
for (const auto &item : m_order_fields) {
143+
const Value &value1 = access_field(*item.field, rec1);
144+
const Value &value2 = access_field(*item.field, rec2);
145+
CmpResult res = item.field->compare(value1, value2);
146+
if (res != CmpResult::Eq) {
147+
if (item.dir == OrderDirection::Ascending) {
148+
return res;
149+
} else {
150+
return res == CmpResult::Lt ? CmpResult::Gt : CmpResult::Lt;
151+
}
152+
}
153+
}
154+
return CmpResult::Eq;
155+
}
156+
138157
} // aggregator
139158
} // fdsdump

src/tools/fdsdump/src/aggregator/view.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99

1010
#pragma once
1111

12+
#define XXH_INLINE_ALL
13+
14+
#include <3rd_party/xxhash/xxhash.h>
1215
#include <aggregator/field.hpp>
16+
#include <common/common.hpp>
1317

1418
#include <cstddef>
19+
#include <functional>
1520
#include <memory>
1621
#include <vector>
1722

@@ -28,6 +33,10 @@ class View {
2833
/**
2934
* @brief The ordering/sorting direction
3035
*/
36+
using KeyHashFnType = std::function<uint64_t(const uint8_t *)>;
37+
using KeyEqualsFnType = std::function<bool(const uint8_t *, const uint8_t *)>;
38+
using RecOrdFnType = std::function<bool(const uint8_t *, const uint8_t *)>;
39+
3140
enum class OrderDirection {
3241
Ascending,
3342
Descending
@@ -399,6 +408,45 @@ class View {
399408
*/
400409
bool is_fixed_size() const { return m_is_fixed_size; }
401410

411+
uint64_t key_hash(const uint8_t *key) const
412+
{
413+
return XXH3_64bits(key, key_size(key));
414+
}
415+
416+
bool key_equals(const uint8_t *key1, const uint8_t *key2) const
417+
{
418+
std::size_t key1_size = key_size(key1);
419+
std::size_t key2_size = key_size(key2);
420+
return key1_size == key2_size && std::memcmp(key1, key2, key1_size) == 0;
421+
}
422+
423+
KeyHashFnType key_hasher() const
424+
{
425+
return [this](const uint8_t *key) { return key_hash(key); };
426+
}
427+
428+
KeyEqualsFnType key_equaler() const
429+
{
430+
return [this](const uint8_t *key1, const uint8_t *key2) { return key_equals(key1, key2); };
431+
}
432+
433+
RecOrdFnType rec_orderer() const
434+
{
435+
return [this](const uint8_t *rec1, const uint8_t *rec2) {
436+
return compare(rec1, rec2) == CmpResult::Lt;
437+
};
438+
}
439+
440+
RecOrdFnType rec_reverse_orderer() const
441+
{
442+
return [this](const uint8_t *rec1, const uint8_t *rec2) {
443+
return compare(rec1, rec2) == CmpResult::Gt;
444+
};
445+
}
446+
447+
CmpResult
448+
compare(const uint8_t *rec1, const uint8_t *rec2) const;
449+
402450
private:
403451
View() = default;
404452

0 commit comments

Comments
 (0)