This is an automated email from the ASF dual-hosted git repository.

fortino pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 392ede19fe OAK-10547 - Fix: Indexing job fails at the end of 
reindexing if it took more than 24h to run (#1203)
392ede19fe is described below

commit 392ede19fee32af40817ff5dee154b6d7f323427
Author: Nuno Santos <[email protected]>
AuthorDate: Mon Nov 13 17:33:06 2023 +0100

    OAK-10547 - Fix: Indexing job fails at the end of reindexing if it took 
more than 24h to run (#1203)
    
    * - Fix support formatting durations longer than 24h.
    
    * Add missing license
    
    * Show negative time spans instead of absolute time.
    Reformat to use Java utils to do some of the time conversions and simplify 
logic.
    
    * Remove wildcard import.
---
 .../oak/plugins/index/FormattingUtils.java         | 21 ++++--
 .../oak/plugins/index/FormattingUtilsTest.java     | 88 ++++++++++++++++++++++
 2 files changed, 103 insertions(+), 6 deletions(-)

diff --git 
a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtils.java
 
b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtils.java
index 4c1a640b1a..ed8cc2c45a 100644
--- 
a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtils.java
+++ 
b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtils.java
@@ -18,18 +18,27 @@ package org.apache.jackrabbit.oak.plugins.index;
 
 import org.apache.jackrabbit.guava.common.base.Stopwatch;
 
-import java.time.LocalTime;
-import java.time.format.DateTimeFormatter;
 import java.util.concurrent.TimeUnit;
 
 public class FormattingUtils {
     public static String formatToSeconds(Stopwatch stopwatch) {
-        LocalTime seconds = 
LocalTime.ofSecondOfDay(stopwatch.elapsed(TimeUnit.SECONDS));
-        return DateTimeFormatter.ISO_TIME.format(seconds);
+        long seconds = stopwatch.elapsed(TimeUnit.SECONDS);
+        long absSeconds = Math.abs(seconds);
+        long hoursPart = TimeUnit.SECONDS.toHours(absSeconds);
+        long minutesPart = TimeUnit.SECONDS.toMinutes(absSeconds) % 60;
+        long secondsPart = TimeUnit.SECONDS.toSeconds(absSeconds) % 60;
+        String sign = seconds < 0 ? "-" : "";
+        return String.format("%s%02d:%02d:%02d", sign, hoursPart, minutesPart, 
secondsPart);
     }
 
     public static String formatToMillis(Stopwatch stopwatch) {
-        LocalTime nanoSeconds = 
LocalTime.ofNanoOfDay(stopwatch.elapsed(TimeUnit.MILLISECONDS)*1000000);
-        return DateTimeFormatter.ISO_TIME.format(nanoSeconds);
+        long millis = stopwatch.elapsed(TimeUnit.MILLISECONDS);
+        long absMillis = Math.abs(millis);
+        long hoursPart = TimeUnit.MILLISECONDS.toHours(absMillis);
+        long minutesPart = TimeUnit.MILLISECONDS.toMinutes(absMillis) % 60;
+        long secondsPart = TimeUnit.MILLISECONDS.toSeconds(absMillis) % 60;
+        long millisPart = TimeUnit.MILLISECONDS.toMillis(absMillis) % 1000;
+        String sign = millis < 0 ? "-" : "";
+        return String.format("%s%02d:%02d:%02d.%03d", sign, hoursPart, 
minutesPart, secondsPart, millisPart);
     }
 }
diff --git 
a/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtilsTest.java
 
b/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtilsTest.java
new file mode 100644
index 0000000000..eb494dbb45
--- /dev/null
+++ 
b/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/FormattingUtilsTest.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.oak.plugins.index;
+
+import org.apache.jackrabbit.guava.common.base.Stopwatch;
+import org.apache.jackrabbit.guava.common.base.Ticker;
+import org.junit.Test;
+
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertEquals;
+
+
+public class FormattingUtilsTest {
+
+    private static class TestTicker extends Ticker {
+        private long time = 0;
+        @Override
+        public long read() {
+            return time;
+        }
+        public void set(long nanos) {
+            time = nanos;
+        }
+    }
+    private final TestTicker ticker = new TestTicker();
+    private final Stopwatch sw = Stopwatch.createStarted(ticker);
+
+    @Test
+    public void formatToSeconds() {
+        testFormatToSeconds("00:00:00", 0);
+        testFormatToSeconds("00:00:59", TimeUnit.MILLISECONDS.toNanos(59_567));
+        testFormatToSeconds("00:01:00", TimeUnit.MILLISECONDS.toNanos(60_567));
+        testFormatToSeconds("00:59:00", TimeUnit.MINUTES.toNanos(59));
+        testFormatToSeconds("01:00:00", TimeUnit.MINUTES.toNanos(60));
+        testFormatToSeconds("23:00:00", TimeUnit.HOURS.toNanos(23));
+        testFormatToSeconds("24:00:00", TimeUnit.HOURS.toNanos(24));
+        testFormatToSeconds("48:00:00", TimeUnit.HOURS.toNanos(48));
+        testFormatToSeconds("23:59:59", TimeUnit.HOURS.toNanos(23) +
+                TimeUnit.MINUTES.toNanos(59) +
+                TimeUnit.SECONDS.toNanos(59) +
+                TimeUnit.MILLISECONDS.toNanos(999)
+        );
+        testFormatToSeconds("-00:01:00", -TimeUnit.SECONDS.toNanos(60));
+    }
+
+    private void testFormatToSeconds(String expected, long nanos) {
+        ticker.set(nanos);
+        assertEquals(expected, FormattingUtils.formatToSeconds(sw));
+    }
+
+    @Test
+    public void formatToMillis() {
+        testFormatToMillis("00:00:00.000", 0);
+        testFormatToMillis("00:00:59.567", 
TimeUnit.MILLISECONDS.toNanos(59_567));
+        testFormatToMillis("00:01:00.567", 
TimeUnit.MILLISECONDS.toNanos(60_567));
+        testFormatToMillis("00:59:00.000", TimeUnit.MINUTES.toNanos(59));
+        testFormatToMillis("01:00:00.000", TimeUnit.MINUTES.toNanos(60));
+        testFormatToMillis("23:00:00.000", TimeUnit.HOURS.toNanos(23));
+        testFormatToMillis("24:00:00.000", TimeUnit.HOURS.toNanos(24));
+        testFormatToMillis("48:00:00.000", TimeUnit.HOURS.toNanos(48));
+        testFormatToMillis("23:59:59.999", TimeUnit.HOURS.toNanos(23) +
+                TimeUnit.MINUTES.toNanos(59) +
+                TimeUnit.SECONDS.toNanos(59) +
+                TimeUnit.MILLISECONDS.toNanos(999)
+        );
+        testFormatToMillis("-00:01:00.000", -TimeUnit.SECONDS.toNanos(60));
+    }
+
+    private void testFormatToMillis(String expected, long nanos) {
+        ticker.set(nanos);
+        assertEquals(expected, FormattingUtils.formatToMillis(sw));
+    }
+}
\ No newline at end of file

Reply via email to