Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions common/include/quantiles_sorted_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,21 @@ class quantiles_sorted_view {
static inline Entry make_dummy_entry(uint64_t weight) { return Entry(nullptr, weight); }

template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value, int>::type = 0>
static inline void check_split_points(const T* items, uint32_t size) {
inline void check_split_points(const T* items, uint32_t size) const {
for (uint32_t i = 0; i < size ; i++) {
if (std::isnan(items[i])) {
throw std::invalid_argument("Values must not be NaN");
}
if ((i < (size - 1)) && !(Comparator()(items[i], items[i + 1]))) {
if ((i < (size - 1)) && !(comparator_(items[i], items[i + 1]))) {
throw std::invalid_argument("Values must be unique and monotonically increasing");
}
}
}

template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value, int>::type = 0>
static inline void check_split_points(const T* items, uint32_t size) {
inline void check_split_points(const T* items, uint32_t size) const {
for (uint32_t i = 0; i < size ; i++) {
if ((i < (size - 1)) && !(Comparator()(items[i], items[i + 1]))) {
if ((i < (size - 1)) && !(comparator_(items[i], items[i + 1]))) {
throw std::invalid_argument("Items must be unique and monotonically increasing");
}
}
Expand Down
1 change: 1 addition & 0 deletions quantiles/include/quantiles_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class quantiles_sketch {
* Merges another sketch into this one.
* If sketches contain strings, callers are responsible for ensuring that
* both sketches were built using compatible string encodings.
* A sketch can be merged into itself.
* If an allocation fails, this sketch is unchanged. If a copy of an item throws, this sketch
* stays valid and usable, but may hold part of other. If the comparator throws while items
* are sorted or merged, the state of this sketch is undefined.
Expand Down
6 changes: 6 additions & 0 deletions quantiles/include/quantiles_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ void quantiles_sketch<T, C, A>::merge(FwdSk&& other) {
if (other.is_empty()) {
return; // nothing to do
}
if (static_cast<const void*>(&other) == static_cast<const void*>(this)) {
// merging reads other while it changes this, so merge a copy
quantiles_sketch copy(other);
merge(std::move(copy));
return;
}
reset_sorted_view();
try {
merge_unchecked(std::forward<FwdSk>(other));
Expand Down
25 changes: 25 additions & 0 deletions quantiles/test/quantiles_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,4 +969,29 @@ TEST_CASE("quantiles sketch: deserialize serial version 1 truncated after min an
REQUIRE_THROWS_AS(quantiles_sketch<int>::deserialize(bytes.data(), bytes.size()), std::out_of_range);
}

TEST_CASE("quantiles sketch: merge into itself", "[quantiles_sketch]") {
// exact and estimation mode, with a partly filled base buffer
for (int n: {5, 100, 1000}) {
CAPTURE(n);
quantiles_sketch<int> sketch(8);
for (int i = 0; i < n; ++i) sketch.update((i * 7919) % 10007);
quantiles_sketch<int> expected(sketch);
const quantiles_sketch<int> copy(sketch);
random_utils::rand.seed(1); random_utils::random_bit.seed(1);
expected.merge(copy);
const auto expected_bytes = expected.serialize();

quantiles_sketch<int> lvalue(sketch);
random_utils::rand.seed(1); random_utils::random_bit.seed(1);
lvalue.merge(lvalue);
REQUIRE(lvalue.get_n() == static_cast<uint64_t>(2 * n));
REQUIRE(lvalue.serialize() == expected_bytes);

quantiles_sketch<int> rvalue(sketch);
random_utils::rand.seed(1); random_utils::random_bit.seed(1);
rvalue.merge(std::move(rvalue));
REQUIRE(rvalue.serialize() == expected_bytes);
}
}

} /* namespace datasketches */
2 changes: 2 additions & 0 deletions req/include/req_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace req_constants {
const uint8_t INIT_NUM_SECTIONS = 3;
/// multiplier for nominal capacity
const unsigned MULTIPLIER = 2;
/// maximum stream length n, the largest signed 64-bit integer, as in datasketches-java
const uint64_t MAX_N = (1ULL << 63) - 1;
}

} /* namespace datasketches */
Expand Down
33 changes: 32 additions & 1 deletion req/include/req_compactor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ class req_compactor {
template<typename FwdC>
void merge(FwdC&& other);

// A merge in two steps, so that a sketch can merge all its levels or none.
// merge_prepare() allocates and copies (or moves) the items of other into the free space of this compactor,
// and changes nothing else; if it throws, it destroys the copies it made.
// Then exactly one of merge_commit() or merge_abort() must follow, with the same other and no change in between.
// merge_commit() allocates and copies nothing. It adds the copies to this compactor before it compares any,
// so a comparator that throws leaks nothing. merge_abort() destroys the copies.
template<typename FwdC>
void merge_prepare(FwdC&& other);
void merge_commit(const req_compactor& other);
void merge_abort(const req_compactor& other);

void sort();

std::pair<uint32_t, uint32_t> compact(req_compactor& next);
Expand Down Expand Up @@ -119,7 +130,27 @@ class req_compactor {
uint32_t capacity_; // 0 while items_ is nullptr
T* items_; // nullptr until first use, see reserve()

bool ensure_enough_sections();
// the section parameters, which change as the state advances
struct sections {
float size_raw;
uint32_t size;
uint8_t num;
uint32_t nom_capacity() const { return req_constants::MULTIPLIER * num * size; }
};
sections get_sections() const;
void set_sections(const sections& s);
// one step of the section schedule for the given state: halves the section size (by sqrt(2)) and doubles
// the number of sections once the state reaches 2^(num - 1); returns false if the sections stay as they are
static bool next_sections(uint64_t state, sections& s);
// the sections once the state of other is combined with this state; returns false if they stay as they are
bool merged_sections(const req_compactor& other, sections& s) const;
// where merge_prepare() puts the items of other
T* merge_destination(uint32_t num);
// destroys the items and frees the storage
void destroy_items();
// allocates storage and copies the items of other, for a constructor; releases both if a copy throws
template<typename O>
void copy_items(const O& other);
std::pair<uint32_t, uint32_t> compute_compaction_range(uint32_t secs_to_compact) const;
void grow(uint32_t new_capacity);
void ensure_space(uint32_t num);
Expand Down
Loading
Loading