leerho commented on code in PR #575:
URL: https://github.com/apache/datasketches-cpp/pull/575#discussion_r4606772932


##########
req/include/req_sketch_impl.hpp:
##########
@@ -183,33 +184,43 @@ void req_sketch<T, C, A>::update(FwdT&& item) {
   compactors_[0].append(std::forward<FwdT>(item));
   ++num_retained_;
   ++n_;
-  if (num_retained_ == max_nom_size_) compress();
   reset_sorted_view();
+  if (num_retained_ >= max_nom_size_) compress();
 }
 
 template<typename T, typename C, typename A>
 template<typename FwdSk>
 void req_sketch<T, C, A>::merge(FwdSk&& other) {
   if (is_HRA() != other.is_HRA()) throw std::invalid_argument("merging HRA and 
LRA is not valid");
   if (other.is_empty()) return;
+  reset_sorted_view();
   if (is_empty()) {
     min_item_.emplace(conditional_forward<FwdSk>(*other.min_item_));
     max_item_.emplace(conditional_forward<FwdSk>(*other.max_item_));
   } else {
     if (comparator_(*other.min_item_, *min_item_)) *min_item_ = 
conditional_forward<FwdSk>(*other.min_item_);
     if (comparator_(*max_item_, *other.max_item_)) *max_item_ = 
conditional_forward<FwdSk>(*other.max_item_);
   }
-  // grow until this has at least as many compactors as other
-  while (get_num_levels() < other.get_num_levels()) grow();
-  // merge the items in all height compactors
-  for (size_t i = 0; i < other.get_num_levels(); ++i) {
-    compactors_[i].merge(conditional_forward<FwdSk>(other.compactors_[i]));
+  try {
+    // grow until this has at least as many compactors as other
+    while (get_num_levels() < other.get_num_levels()) grow();
+    // merge the items in all height compactors
+    for (size_t i = 0; i < other.get_num_levels(); ++i) {
+      compactors_[i].merge(conditional_forward<FwdSk>(other.compactors_[i]));
+    }
+  } catch (...) {
+    restore_counts(); // some levels may be merged already

Review Comment:
   You're right, thanks. If `grow()` succeeded and a compactor merge then 
failed, the added levels stayed behind empty. The sketch then reported 
estimation mode with nothing above level 0, and its rank bounds used too many 
levels. For example, an empty target merging a 1,000-item source and failing at 
its second allocation was left with n = 0 but in estimation mode.
   
   Fixed in ccbcf6ac: on failure, `merge()` now removes the levels it added 
that received no items. It also grows before it widens min and max, so a 
failure to grow leaves the sketch unchanged. The test "failure at each 
allocation of a merge" now checks that a failed merge which merged no items 
leaves the target's estimation mode as it was; it fails without the change.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to