leerho commented on code in PR #808:
URL: https://github.com/apache/datasketches-java/pull/808#discussion_r4647653267


##########
src/main/java/org/apache/datasketches/req/ReqSketch.java:
##########
@@ -469,15 +474,15 @@ public String toString() {
   public void update(final float item) {
     if (Float.isNaN(item)) { return; }
     ensureCapacity(); //first use allocates, before anything changes
+    final FloatBuffer fbuf = compactors.get(0).getBuffer();
+    fbuf.append(item); //may grow the buffer; nothing else has changed yet

Review Comment:
   Fixed in dacf16d8b: `reqSV` is now cleared right after the append, before 
compression starts.



##########
src/main/java/org/apache/datasketches/req/ReqSketch.java:
##########
@@ -554,26 +559,35 @@ private static void checkK(final int k) {
     }
   }
 
+  //Each compaction moves items to the next level only after the space for 
them has been allocated,
+  //so a failed allocation never loses items. If an error interrupts 
compression, the counters are
+  //recomputed from the compactors, so the sketch stays consistent and the 
next update compresses again.
   private void compress() {
     if (reqDebug != null) { reqDebug.emitStartCompress(); }
-    for (int h = 0; h < compactors.size(); h++) {
-      final ReqCompactor c = compactors.get(h);
-      final int compRetItems = c.getBuffer().getCount();
-      final int compNomCap = c.getNomCapacity();
-
-      if (compRetItems >= compNomCap) {
-        if ((h + 1) >= getNumLevels()) { //at the top?
-          if (reqDebug != null) { reqDebug.emitMustAddCompactor(); }
-          grow(); //add a level, increases maxNomSize
+    try {
+      for (int h = 0; h < compactors.size(); h++) {
+        final ReqCompactor c = compactors.get(h);
+        final int compRetItems = c.getBuffer().getCount();
+        final int compNomCap = c.getNomCapacity();
+
+        if (compRetItems >= compNomCap) {
+          if ((h + 1) >= getNumLevels()) { //at the top?
+            if (reqDebug != null) { reqDebug.emitMustAddCompactor(); }
+            grow(); //add a level, increases maxNomSize
+          }
+          c.compact(cReturn, rand, compactors.get(h + 1).getBuffer());
+          retItems += cReturn.deltaRetItems;
+          maxNomSize += cReturn.deltaNomSize;
+          //we specifically decided not to do lazy compression.
         }
-        final FloatBuffer promoted = c.compact(cReturn, rand);
-        compactors.get(h + 1).getBuffer().mergeSortIn(promoted);
-        retItems += cReturn.deltaRetItems;
-        maxNomSize += cReturn.deltaNomSize;
-        //we specifically decided not to do lazy compression.
       }
+    } catch (final RuntimeException | Error e) {
+      retItems = computeTotalRetainedItems();
+      maxNomSize = computeMaxNomSize();

Review Comment:
   Fixed in dacf16d8b: both recount helpers now use indexed loops, so recovery 
allocates nothing.



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