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


##########
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:
   In here, we should remove last compactor. When grow is success and merge 
failed, then sketch's state is invalid.



-- 
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