diff --git a/kll/include/kll_helper.hpp b/kll/include/kll_helper.hpp index e0d82c27..3a223d9a 100644 --- a/kll/include/kll_helper.hpp +++ b/kll/include/kll_helper.hpp @@ -57,14 +57,16 @@ class kll_helper { // assumes that destination has initialized objects // does not destroy the originals after the move template - static void merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, uint32_t start_b, uint32_t len_b, uint32_t start_c); + static void merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, uint32_t start_b, uint32_t len_b, uint32_t start_c, + const C& comparator); - // this version is to merge from two different buffers into a third buffer - // initializes objects is the destination buffer - // moves objects from buf_a and destroys the originals - // copies objects from buf_b + // Merges the sorted items buf_a[start_a, start_a + len_a) and the sorted items + // buf_c[start_c + len_a, start_c + len_a + len_b) into buf_c[start_c, start_c + len_a + len_b). + // The first len_a slots of the destination must hold no objects. + // Moves the items and destroys the originals; copies nothing. template - static void merge_sorted_arrays(const T* buf_a, uint32_t start_a, uint32_t len_a, const T* buf_b, uint32_t start_b, uint32_t len_b, T* buf_c, uint32_t start_c); + static void merge_sorted_arrays_into_gap(T* buf_a, uint32_t start_a, uint32_t len_a, T* buf_c, uint32_t start_c, uint32_t len_b, + const C& comparator); struct compress_result { uint8_t final_num_levels; @@ -101,7 +103,7 @@ class kll_helper { template static compress_result general_compress(uint16_t k, uint8_t m, uint8_t num_levels_in, T* items, - uint32_t* in_levels, uint32_t* out_levels, bool is_level_zero_sorted); + uint32_t* in_levels, uint32_t* out_levels, bool is_level_zero_sorted, const C& comparator); template static void copy_construct(const T* src, size_t src_first, size_t src_last, T* dst, size_t dst_first); diff --git a/kll/include/kll_helper_impl.hpp b/kll/include/kll_helper_impl.hpp index e8dc073e..1325c345 100644 --- a/kll/include/kll_helper_impl.hpp +++ b/kll/include/kll_helper_impl.hpp @@ -129,7 +129,8 @@ void kll_helper::randomly_halve_up(T* buf, uint32_t start, uint32_t length) { // assumes that destination has initialized objects // does not destroy the originals after the move template -void kll_helper::merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, uint32_t start_b, uint32_t len_b, uint32_t start_c) { +void kll_helper::merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, uint32_t start_b, uint32_t len_b, uint32_t start_c, + const C& comparator) { const uint32_t len_c = len_a + len_b; const uint32_t lim_a = start_a + len_a; const uint32_t lim_b = start_b + len_b; @@ -145,7 +146,7 @@ void kll_helper::merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, u } else if (b == lim_b) { if (a != c) buf[c] = std::move(buf[a]); a++; - } else if (C()(buf[a], buf[b])) { + } else if (comparator(buf[a], buf[b])) { if (a != c) buf[c] = std::move(buf[a]); a++; } else { @@ -156,34 +157,26 @@ void kll_helper::merge_sorted_arrays(T* buf, uint32_t start_a, uint32_t len_a, u if (a != lim_a || b != lim_b) throw std::logic_error("inconsistent state"); } -// this version is to merge from two different buffers into a third buffer -// initializes objects is the destination buffer -// moves objects from buf_a and destroys the originals -// copies objects from buf_b +// Merges into the gap in front of the items of b, which are already in the destination. +// Every destination slot before the next item of b holds no object: it was either empty, +// or its item was moved out and destroyed. Once a runs out, the rest of b is in place. template -void kll_helper::merge_sorted_arrays(const T* buf_a, uint32_t start_a, uint32_t len_a, const T* buf_b, uint32_t start_b, uint32_t len_b, T* buf_c, uint32_t start_c) { - const uint32_t len_c = len_a + len_b; +void kll_helper::merge_sorted_arrays_into_gap(T* buf_a, uint32_t start_a, uint32_t len_a, T* buf_c, uint32_t start_c, uint32_t len_b, + const C& comparator) { const uint32_t lim_a = start_a + len_a; - const uint32_t lim_b = start_b + len_b; - const uint32_t lim_c = start_c + len_c; - + const uint32_t lim_b = start_c + len_a + len_b; uint32_t a = start_a; - uint32_t b = start_b; - - for (uint32_t c = start_c; c < lim_c; c++) { - if (a == lim_a) { - new (&buf_c[c]) T(buf_b[b++]); - } else if (b == lim_b) { - new (&buf_c[c]) T(std::move(buf_a[a])); - buf_a[a++].~T(); - } else if (C()(buf_a[a], buf_b[b])) { - new (&buf_c[c]) T(std::move(buf_a[a])); + uint32_t b = start_c + len_a; + uint32_t c = start_c; + while (a < lim_a) { + if (b == lim_b || comparator(buf_a[a], buf_c[b])) { + new (&buf_c[c++]) T(std::move(buf_a[a])); buf_a[a++].~T(); } else { - new (&buf_c[c]) T(buf_b[b++]); + new (&buf_c[c++]) T(std::move(buf_c[b])); + buf_c[b++].~T(); } } - if (a != lim_a || b != lim_b) throw std::logic_error("inconsistent state"); } // Mirrors the level loop of general_compress() on the level sizes only. @@ -240,7 +233,7 @@ kll_helper::compress_result kll_helper::predict_compress(uint16_t k, uint8_t m, */ template kll_helper::compress_result kll_helper::general_compress(uint16_t k, uint8_t m, uint8_t num_levels_in, T* items, - uint32_t* in_levels, uint32_t* out_levels, bool is_level_zero_sorted) + uint32_t* in_levels, uint32_t* out_levels, bool is_level_zero_sorted, const C& comparator) { if (num_levels_in == 0) { throw std::invalid_argument("num_levels_in == 0"); } // things are too weird if zero levels are allowed const uint32_t starting_item_count = in_levels[num_levels_in] - in_levels[0]; @@ -289,14 +282,14 @@ kll_helper::compress_result kll_helper::general_compress(uint16_t k, uint8_t m, // level zero might not be sorted, so we must sort it if we wish to compact it if ((current_level == 0) && !is_level_zero_sorted) { - std::sort(items + adj_beg, items + adj_beg + adj_pop, C()); + std::sort(items + adj_beg, items + adj_beg + adj_pop, comparator); } if (pop_above == 0) { // Level above is empty, so halve up randomly_halve_up(items, adj_beg, adj_pop); } else { // Level above is nonempty, so halve down, then merge up randomly_halve_down(items, adj_beg, adj_pop); - merge_sorted_arrays(items, adj_beg, half_adj_pop, raw_lim, pop_above, adj_beg + half_adj_pop); + merge_sorted_arrays(items, adj_beg, half_adj_pop, raw_lim, pop_above, adj_beg + half_adj_pop, comparator); } // track the fact that we just eliminated some data diff --git a/kll/include/kll_sketch.hpp b/kll/include/kll_sketch.hpp index b721bde8..c28e56b7 100644 --- a/kll/include/kll_sketch.hpp +++ b/kll/include/kll_sketch.hpp @@ -41,6 +41,8 @@ namespace kll_constants { const uint16_t MAX_K = (1 << 16) - 1; /// maximum weight of a weighted update, the same as in datasketches-java const uint64_t MAX_WEIGHT = (1ULL << 61) - 1; + /// maximum stream length n, the largest signed 64-bit integer, as in datasketches-java + const uint64_t MAX_N = (1ULL << 63) - 1; } /** @@ -246,13 +248,16 @@ class kll_sketch { * the input string uses a compatible encoding (valid UTF-8). * The first update allocates storage for items, unless reserve() or a merge already did. * @param item from a stream of items + * @throw std::overflow_error if n would exceed kll_constants::MAX_N, in which case the sketch is unchanged */ template void update(FwdT&& item); /** * Updates this sketch with the given data item repeated the given number of times. - * The result is equivalent to calling update(item) weight times, at a cost that grows + * The result is equivalent to calling update(item) weight times. + * If the weight is less than the free space in level zero, the item is copied into level zero + * weight times. Otherwise a sketch of the weighted item is merged in, at a cost that grows * with the logarithm of the weight rather than with the weight itself. * If cross-language portability is required, callers should ensure that * the input string uses a compatible encoding (valid UTF-8). @@ -260,6 +265,7 @@ class kll_sketch { * @param item from a stream of items * @param weight number of times the item is repeated, from 1 to kll_constants::MAX_WEIGHT (2^61 - 1) * @throw std::invalid_argument if the weight is 0 or greater than kll_constants::MAX_WEIGHT + * @throw std::overflow_error if n would exceed kll_constants::MAX_N, in which case the sketch is unchanged */ template void update(FwdT&& item, uint64_t weight); @@ -271,7 +277,10 @@ class kll_sketch { * If other is this sketch, this throws std::invalid_argument. * The first merge of a non-empty sketch allocates storage for items, * unless reserve() or an update already did. + * If copying an item throws, this sketch stays valid and usable, but may hold part of other. + * The comparator must not throw: if it does, the state of this sketch is undefined. * @param other sketch to merge into this one + * @throw std::overflow_error if n would exceed kll_constants::MAX_N, in which case the sketch is unchanged */ template void merge(FwdSk&& other); @@ -633,6 +642,12 @@ class kll_sketch { // &items_[index], or nullptr without item storage, which makes every item range empty inline T* item_ptr(uint32_t index) const; inline uint32_t internal_update(); + // inserts an item into level zero; if constructing it throws, the sketch is unchanged + template void insert_level_zero(FwdT&& item); + // throws std::overflow_error if n_ + increase would exceed kll_constants::MAX_N + inline void check_n_increase(uint64_t increase) const; + // destroys the items and frees items_, for a constructor that throws + void destroy_items(); // The following code is only valid in the special case of exactly reaching capacity while updating. // It cannot be used while merging, while reducing k, or anything else. @@ -646,7 +661,7 @@ class kll_sketch { template void merge_higher_levels(O&& other, uint64_t final_n); template - void populate_work_arrays(FwdSk&& other, T* workbuf, uint32_t* worklevels, uint8_t provisional_num_levels); + void populate_work_arrays(FwdSk&& other, T* workbuf, const uint32_t* worklevels, uint8_t provisional_num_levels); void assert_correct_total_weight() const; uint32_t safe_level_size(uint8_t level) const; diff --git a/kll/include/kll_sketch_impl.hpp b/kll/include/kll_sketch_impl.hpp index 2fb47f22..e1e8e57b 100644 --- a/kll/include/kll_sketch_impl.hpp +++ b/kll/include/kll_sketch_impl.hpp @@ -140,13 +140,19 @@ kll_sketch& kll_sketch::operator=(kll_sketch&& other) { template kll_sketch::~kll_sketch() { + destroy_items(); + reset_sorted_view(); +} + +template +void kll_sketch::destroy_items() { if (items_ != nullptr) { const uint32_t begin = levels_[0]; const uint32_t end = levels_[num_levels_]; for (uint32_t i = begin; i < end; i++) items_[i].~T(); allocator_.deallocate(items_, items_size_); + items_ = nullptr; } - reset_sorted_view(); } template @@ -172,25 +178,45 @@ sorted_view_(nullptr) "Type converting constructor requires new type to be constructible from existing type" ); copy_items(other); - check_sorting(); + try { + check_sorting(); + } catch (...) { + destroy_items(); // the destructor doesn't run if a constructor throws + throw; + } } template template void kll_sketch::update(FwdT&& item) { if (!check_update_item(item)) { return; } + check_n_increase(1); allocate_items(); // first, so that a failure leaves the sketch unchanged + reset_sorted_view(); // before any change, so that a failed copy can't leave a stale view update_min_max(static_cast(item)); // min and max are always copies + insert_level_zero(std::forward(item)); +} + +template +template +void kll_sketch::insert_level_zero(FwdT&& item) { if (levels_[0] == 0) { // item can refer into items_, and internal_update() can destroy it T copy(std::forward(item)); const uint32_t index = internal_update(); new (&items_[index]) T(std::move(copy)); } else { - const uint32_t index = internal_update(); - new (&items_[index]) T(std::forward(item)); + // construct the item before counting it, so that a failed copy leaves the sketch unchanged + new (&items_[levels_[0] - 1]) T(std::forward(item)); + internal_update(); + } +} + +template +void kll_sketch::check_n_increase(uint64_t increase) const { + if (n_ > kll_constants::MAX_N || increase > kll_constants::MAX_N - n_) { + throw std::overflow_error("n would exceed 2^63 - 1"); } - reset_sorted_view(); } template @@ -201,6 +227,7 @@ void kll_sketch::update(FwdT&& item, uint64_t weight) { if (weight > kll_constants::MAX_WEIGHT) { throw std::invalid_argument("weight must be less than 2^61: " + std::to_string(weight)); } + check_n_increase(weight); if (weight == 1) { update(std::forward(item)); return; @@ -209,13 +236,9 @@ void kll_sketch::update(FwdT&& item, uint64_t weight) { reset_sorted_view(); // before any change, so that a failed copy can't leave a stale view if (weight < levels_[0]) { // fits into level zero without compaction update_min_max(static_cast(item)); - // construct each copy before counting it, so that a failed copy leaves a valid sketch - for (uint64_t i = 1; i < weight; ++i) { - new (&items_[levels_[0] - 1]) T(static_cast(item)); - internal_update(); - } - new (&items_[levels_[0] - 1]) T(std::forward(item)); - internal_update(); + // each copy is counted only once it is constructed, so a failed copy leaves a valid sketch + for (uint64_t i = 1; i < weight; ++i) insert_level_zero(static_cast(item)); + insert_level_zero(std::forward(item)); } else { merge(kll_sketch(k_, static_cast(item), weight, comparator_, allocator_)); } @@ -291,7 +314,16 @@ template void kll_sketch::copy_items(const O& other) { if (other.items_ == nullptr) return; allocate_items(); - for (auto i = levels_[0]; i < levels_[num_levels_]; ++i) new (&items_[i]) T(other.items_[i]); + auto i = levels_[0]; + try { + for (; i < levels_[num_levels_]; ++i) new (&items_[i]) T(other.items_[i]); + } catch (...) { + // the destructor doesn't run if a constructor throws, so release the items here + for (auto j = levels_[0]; j < i; ++j) items_[j].~T(); + allocator_.deallocate(items_, items_size_); + items_ = nullptr; + throw; + } } template @@ -322,7 +354,9 @@ void kll_sketch::merge(FwdSk&& other) { if (m_ != other.m_) { throw std::invalid_argument("incompatible M: " + std::to_string(m_) + " and " + std::to_string(other.m_)); } + check_n_increase(other.n_); allocate_items(); // the higher levels are merged into items_ even if other's level zero is empty + reset_sorted_view(); // before any change, so that a failed copy can't leave a stale view if (is_empty()) { min_item_.emplace(conditional_forward(*other.min_item_)); max_item_.emplace(conditional_forward(*other.max_item_)); @@ -332,14 +366,12 @@ void kll_sketch::merge(FwdSk&& other) { } const uint64_t final_n = n_ + other.n_; for (uint32_t i = other.levels_[0]; i < other.levels_[1]; i++) { - const uint32_t index = internal_update(); - new (&items_[index]) T(conditional_forward(other.items_[i])); + insert_level_zero(conditional_forward(other.items_[i])); } if (other.num_levels_ >= 2) { merge_higher_levels(std::forward(other), final_n); } n_ = final_n; if (other.is_estimation_mode()) { min_k_ = std::min(min_k_, other.min_k_); } assert_correct_total_weight(); - reset_sorted_view(); } template @@ -800,7 +832,7 @@ void kll_sketch::compress_while_updating(void) { kll_helper::randomly_halve_up(items_, adj_beg, adj_pop); } else { kll_helper::randomly_halve_down(items_, adj_beg, adj_pop); - kll_helper::merge_sorted_arrays(items_, adj_beg, half_adj_pop, raw_lim, pop_above, adj_beg + half_adj_pop); + kll_helper::merge_sorted_arrays(items_, adj_beg, half_adj_pop, raw_lim, pop_above, adj_beg + half_adj_pop, comparator_); } levels_[level + 1] -= half_adj_pop; // adjust boundaries of the level above if (odd_pop) { @@ -956,7 +988,7 @@ void kll_sketch::merge_higher_levels(O&& other, uint64_t final_n) { populate_work_arrays(std::forward(other), workbuf.get(), worklevels.data(), provisional_num_levels); const kll_helper::compress_result result = kll_helper::general_compress(k_, m_, provisional_num_levels, workbuf.get(), - worklevels.data(), outlevels.data(), is_level_zero_sorted_); + worklevels.data(), outlevels.data(), is_level_zero_sorted_, comparator_); if (result.final_num_levels != predicted.final_num_levels || result.final_capacity != predicted.final_capacity || result.final_num_items != predicted.final_num_items) { throw std::logic_error("merge error: compression does not match the prediction"); @@ -981,29 +1013,41 @@ void kll_sketch::merge_higher_levels(O&& other, uint64_t final_n) { num_levels_ = result.final_num_levels; } -// this leaves items_ uninitialized (all objects moved out and destroyed) +// This leaves items_ uninitialized (all objects moved out and destroyed). +// worklevels must already hold the level boundaries in workbuf. +// The items of other are copied (or moved) first, the only step that can fail on a throwing copy: +// if one throws, the copies made are destroyed and this sketch is unchanged. +// After that, the items of this sketch are only moved. template template -void kll_sketch::populate_work_arrays(FwdSk&& other, T* workbuf, uint32_t* worklevels, uint8_t provisional_num_levels) { - worklevels[0] = 0; +void kll_sketch::populate_work_arrays(FwdSk&& other, T* workbuf, const uint32_t* worklevels, uint8_t provisional_num_levels) { + // the level zero items of other were already inserted into this + // each level of other goes after the items of this sketch at that level + uint8_t lvl = 1; + uint32_t num_constructed = 0; // at level lvl + try { + for (; lvl < provisional_num_levels; ++lvl) { + const uint32_t other_pop = other.safe_level_size(lvl); + const uint32_t dst = worklevels[lvl] + safe_level_size(lvl); + for (num_constructed = 0; num_constructed < other_pop; ++num_constructed) { + new (&workbuf[dst + num_constructed]) T(conditional_forward(other.items_[other.levels_[lvl] + num_constructed])); + } + } + } catch (...) { + for (uint8_t l = 1; l <= lvl && l < provisional_num_levels; ++l) { + const uint32_t dst = worklevels[l] + safe_level_size(l); + const uint32_t count = l < lvl ? other.safe_level_size(l) : num_constructed; + for (uint32_t i = 0; i < count; ++i) workbuf[dst + i].~T(); + } + throw; + } - // the level zero data from "other" was already inserted into "this" kll_helper::move_construct(items_, levels_[0], levels_[1], workbuf, 0, true); - worklevels[1] = safe_level_size(0); - - for (uint8_t lvl = 1; lvl < provisional_num_levels; lvl++) { + for (lvl = 1; lvl < provisional_num_levels; ++lvl) { const uint32_t self_pop = safe_level_size(lvl); - const uint32_t other_pop = other.safe_level_size(lvl); - worklevels[lvl + 1] = worklevels[lvl] + self_pop + other_pop; - - if ((self_pop > 0) && (other_pop == 0)) { - kll_helper::move_construct(items_, levels_[lvl], levels_[lvl] + self_pop, workbuf, worklevels[lvl], true); - } else if ((self_pop == 0) && (other_pop > 0)) { - for (auto i = other.levels_[lvl], j = worklevels[lvl]; i < other.levels_[lvl] + other_pop; ++i, ++j) { - new (&workbuf[j]) T(conditional_forward(other.items_[i])); - } - } else if ((self_pop > 0) && (other_pop > 0)) { - kll_helper::merge_sorted_arrays(items_, levels_[lvl], self_pop, other.items_, other.levels_[lvl], other_pop, workbuf, worklevels[lvl]); + if (self_pop > 0) { + kll_helper::merge_sorted_arrays_into_gap(items_, levels_[lvl], self_pop, workbuf, worklevels[lvl], + other.safe_level_size(lvl), comparator_); } } } diff --git a/kll/test/kll_sketch_allocation_test.cpp b/kll/test/kll_sketch_allocation_test.cpp index a0ac1cf0..ee0d81fe 100644 --- a/kll/test/kll_sketch_allocation_test.cpp +++ b/kll/test/kll_sketch_allocation_test.cpp @@ -306,29 +306,47 @@ using kll_fragile_sketch = kll_sketch +uint64_t total_weight(const S& sketch) { + uint64_t total = 0; + for (const auto pair: sketch) total += pair.second; + return total; +} + +} // namespace + // Fail each item copy made by a weighted update in turn: nothing may leak, // and the sketch must stay usable, with no stale sorted view. TEST_CASE("kll sketch allocation: failure at each item copy of a weighted update", "[kll_sketch]") { test_allocator_total_bytes = 0; test_allocator_net_allocations = 0; - for (const uint64_t weight: {uint64_t(10), uint64_t(1000)}) { // copies into level zero, and a merge - CAPTURE(weight); + struct config { uint16_t k; int num_updates; uint64_t weight; }; + for (const config c: { + config{200, 5, 10}, // copies into level zero + config{200, 5, 1000}, // a merge into a sketch with one level + config{8, 9, 18}, // a merge into a sketch with items at the same levels + }) { + CAPTURE(c.k, c.num_updates, c.weight); bool completed = false; for (long fail_at = 0; !completed; ++fail_at) { CAPTURE(fail_at); - kll_fragile_sketch sketch(200, fragile_less(), test_allocator(0)); - for (int i = 0; i < 5; ++i) sketch.update(fragile_item(i)); + kll_fragile_sketch sketch(c.k, fragile_less(), test_allocator(0)); + for (int i = 0; i < c.num_updates; ++i) sketch.update(fragile_item(i)); const fragile_item probe(2); const double rank_before = sketch.get_rank(probe); // caches a sorted view REQUIRE(rank_before > 0); copies_allowed = fail_at; try { - sketch.update(fragile_item(100), weight); + sketch.update(fragile_item(100), c.weight); completed = true; } catch (const std::runtime_error&) {} copies_allowed = -1; - REQUIRE(sketch.get_n() >= 5); - REQUIRE(sketch.get_n() <= 5 + weight); + REQUIRE(sketch.get_n() >= static_cast(c.num_updates)); + REQUIRE(sketch.get_n() <= c.num_updates + c.weight); + REQUIRE(total_weight(sketch) == sketch.get_n()); // a copy of the sketch builds its sorted view from the current items const kll_fragile_sketch copy(sketch); REQUIRE(sketch.get_rank(probe) == copy.get_rank(probe)); @@ -340,4 +358,86 @@ TEST_CASE("kll sketch allocation: failure at each item copy of a weighted update REQUIRE(test_allocator_net_allocations == 0); } +// Fail each item copy made by merging from a const sketch in turn, with both sketches +// in estimation mode and holding items at the same levels. +TEST_CASE("kll sketch allocation: failure at each item copy of a merge", "[kll_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + kll_fragile_sketch other(8, fragile_less(), test_allocator(0)); + for (int i = 0; i < 50; ++i) other.update(fragile_item(2 * i + 1)); + REQUIRE(other.is_estimation_mode()); + const fragile_item probe(50); + const double other_rank = other.get_rank(probe); + bool completed = false; + for (long fail_at = 0; !completed; ++fail_at) { + CAPTURE(fail_at); + kll_fragile_sketch sketch(8, fragile_less(), test_allocator(0)); + for (int i = 0; i < 50; ++i) sketch.update(fragile_item(2 * i)); + const uint64_t n_before = sketch.get_n(); + const double rank_before = sketch.get_rank(probe); // caches a sorted view + REQUIRE(rank_before > 0); + copies_allowed = fail_at; + try { + sketch.merge(other); + completed = true; + } catch (const std::runtime_error&) {} + copies_allowed = -1; + REQUIRE(sketch.get_n() >= n_before); + REQUIRE(sketch.get_n() <= n_before + other.get_n()); + if (completed) REQUIRE(sketch.get_n() == n_before + other.get_n()); + REQUIRE(total_weight(sketch) == sketch.get_n()); + const kll_fragile_sketch copy(sketch); + REQUIRE(sketch.get_rank(probe) == copy.get_rank(probe)); + REQUIRE(other.get_n() == 50); + REQUIRE(other.get_rank(probe) == other_rank); + // the sketch stays usable + sketch.update(fragile_item(-1)); + sketch.merge(other); + REQUIRE(total_weight(sketch) == sketch.get_n()); + REQUIRE(*sketch.get_min_item().value == -1); + } + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +TEST_CASE("kll sketch allocation: failure at each item copy of the copy constructor", "[kll_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + kll_fragile_sketch sketch(8, fragile_less(), test_allocator(0)); + for (int i = 0; i < 50; ++i) sketch.update(fragile_item(i)); + bool completed = false; + for (long fail_at = 0; !completed; ++fail_at) { + CAPTURE(fail_at); + copies_allowed = fail_at; + try { + const kll_fragile_sketch copy(sketch); + completed = true; + copies_allowed = -1; + REQUIRE(copy.get_n() == sketch.get_n()); + REQUIRE(copy.get_num_retained() == sketch.get_num_retained()); + } catch (const std::runtime_error&) {} + copies_allowed = -1; + } + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + +TEST_CASE("kll sketch allocation: converting constructor that rejects the order releases the items", "[kll_sketch]") { + test_allocator_total_bytes = 0; + test_allocator_net_allocations = 0; + { + kll_sketch, test_allocator> sketch(8, std::less(), test_allocator(0)); + for (int i = 0; i < 100; ++i) sketch.update(i); + REQUIRE(sketch.is_estimation_mode()); + using descending_sketch = kll_sketch, test_allocator>; + REQUIRE_THROWS_AS(descending_sketch(sketch, std::greater(), test_allocator(0)), std::logic_error); + } + REQUIRE(test_allocator_total_bytes == 0); + REQUIRE(test_allocator_net_allocations == 0); +} + } /* namespace datasketches */ diff --git a/kll/test/kll_sketch_test.cpp b/kll/test/kll_sketch_test.cpp index 7086cc33..eaec784e 100644 --- a/kll/test/kll_sketch_test.cpp +++ b/kll/test/kll_sketch_test.cpp @@ -1059,6 +1059,7 @@ TEST_CASE("kll sketch", "[kll_sketch]") { } SECTION("weighted update: estimation mode") { + random_utils::random_bit.seed(11); // deterministic when this section runs alone kll_float_sketch sketch(200, std::less(), 0); const int n = 10000; std::vector weights(n); @@ -1087,6 +1088,41 @@ TEST_CASE("kll sketch", "[kll_sketch]") { REQUIRE(sketch2.get_quantile(0.5) == sketch.get_quantile(0.5)); } + SECTION("weighted update: n is capped at 2^63 - 1") { + kll_float_sketch sketch(8, std::less(), 0); + for (int i = 0; i < 4; ++i) sketch.update(static_cast(i), kll_constants::MAX_WEIGHT); + const uint64_t n = sketch.get_n(); + REQUIRE(n == 4 * kll_constants::MAX_WEIGHT); + const uint32_t num_retained = sketch.get_num_retained(); + REQUIRE_THROWS_AS(sketch.update(10.0f, kll_constants::MAX_WEIGHT), std::overflow_error); + REQUIRE(sketch.get_n() == n); + REQUIRE(sketch.get_num_retained() == num_retained); + REQUIRE(sketch.get_max_item() == 3); + sketch.update(10.0f, kll_constants::MAX_N - n); + REQUIRE(sketch.get_n() == kll_constants::MAX_N); + REQUIRE(sketch.get_max_item() == 10); + REQUIRE_THROWS_AS(sketch.update(11.0f), std::overflow_error); + REQUIRE_THROWS_AS(sketch.update(11.0f, 2), std::overflow_error); + REQUIRE(sketch.get_n() == kll_constants::MAX_N); + REQUIRE(sketch.get_max_item() == 10); + } + + SECTION("merge: n is capped at 2^63 - 1") { + kll_float_sketch sketch(8, std::less(), 0); + for (int i = 0; i < 4; ++i) sketch.update(static_cast(i), kll_constants::MAX_WEIGHT); + const uint64_t n = sketch.get_n(); + kll_float_sketch too_big(8, std::less(), 0); + too_big.update(10.0f, kll_constants::MAX_WEIGHT); + REQUIRE_THROWS_AS(sketch.merge(too_big), std::overflow_error); + REQUIRE(sketch.get_n() == n); + REQUIRE(sketch.get_max_item() == 3); + kll_float_sketch fits(8, std::less(), 0); + fits.update(10.0f, kll_constants::MAX_N - n); + sketch.merge(fits); + REQUIRE(sketch.get_n() == kll_constants::MAX_N); + REQUIRE(sketch.get_max_item() == 10); + } + SECTION("weighted update: strings") { kll_string_sketch sketch(200, std::less(), 0); sketch.update(std::string("b"), 3); @@ -1520,4 +1556,32 @@ TEST_CASE("kll sketch: update with aliased item during buffer growth", "[kll_ske REQUIRE(count == 2); } +namespace { + +// a comparator that can't be default-constructed: the sketch must use its own comparator everywhere +struct ordered_by { + bool descending; + explicit ordered_by(bool d): descending(d) {} + bool operator()(int a, int b) const { return descending ? b < a : a < b; } +}; + +} // namespace + +TEST_CASE("kll sketch: compaction and merge use the sketch's comparator", "[kll_sketch]") { + kll_sketch sketch(8, ordered_by(true)); + kll_sketch other(8, ordered_by(true)); + for (int i = 0; i < 1000; ++i) { + sketch.update(i); + other.update(i + 1000); + } + sketch.merge(other); + REQUIRE(sketch.get_n() == 2000); + REQUIRE(sketch.get_min_item() == 1999); // first in descending order + REQUIRE(sketch.get_max_item() == 0); + // the converting constructor checks that every level above zero is sorted by the comparator + const kll_sketch copy(sketch, ordered_by(true)); + REQUIRE(copy.get_n() == 2000); + REQUIRE(sketch.get_rank(1000) == Approx(0.5).margin(RANK_EPS_FOR_K_200 * 10)); +} + } /* namespace datasketches */