Skip to content

Commit aea6b7a

Browse files
committed
wip
1 parent fb1c2f6 commit aea6b7a

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

engine/runtime-instrument-common/src/main/java/org/enso/interpreter/service/ProgressAggregator.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ public ProgressAggregator(BiConsumer<Double, String> updateStatus) {
4141
public synchronized void create(Object key, long max) {
4242
Progress p;
4343
if (stack.isEmpty()) {
44-
p = new Progress(max, 0.0, 1.0);
44+
current = 0.0;
45+
message = null;
46+
p = new Progress(key, max, 0.0, 1.0);
4547
} else {
4648
var previous = stack.peek();
4749
var current = previous.from + previous.singleStep() * previous.current;
48-
p = new Progress(max, current, current + previous.singleStep());
50+
p = new Progress(key, max, current, current + previous.singleStep());
4951
}
5052
stack.addFirst(p);
5153
map.put(key, p);
@@ -54,7 +56,7 @@ public synchronized void create(Object key, long max) {
5456
public void closeProgress(Object key) {
5557
if (findBy(key) instanceof Progress p) {
5658
p.advance(p.max);
57-
stack.remove(p);
59+
p.detach();
5860
}
5961
}
6062

@@ -91,12 +93,14 @@ private void advanceTo(double now) {
9193
}
9294

9395
final class Progress implements AutoCloseable {
96+
private final Object key;
9497
private final long max;
9598
private long current;
9699
private final double from;
97100
private final double to;
98101

99-
private Progress(long max, double from, double to) {
102+
private Progress(Object key, long max, double from, double to) {
103+
this.key = key;
100104
assert 0.0 <= from && from <= 1.0;
101105
assert 0.0 <= to && to <= 1.0;
102106
assert from <= to;
@@ -125,19 +129,28 @@ public final void advance(long steps) {
125129
try {
126130
this.current = Math.min(Math.addExact(this.current, steps), this.max);
127131
advanceTo(from + this.current * singleStep());
132+
if (this.current >= this.max) {
133+
detach();
134+
}
128135
} catch (ArithmeticException e) {
129136
// keep unchanged
130137
}
131138
}
132139

140+
private void detach() {
141+
synchronized (ProgressAggregator.this) {
142+
stack.remove(this);
143+
map.remove(key);
144+
}
145+
}
146+
133147
/**
134148
* Closes the progress and removes it from the stack of currently active progresses. Most
135149
* importantly, no new progress will become child of this progress anymore.
136150
*/
137151
@Override
138152
public void close() {
139153
advance(max);
140-
closeProgress(this);
141154
}
142155
}
143156
}

engine/runtime-instrument-common/src/test/java/org/enso/interpreter/service/ProgressAggregatorTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ public void halfAndThird() {
5555
assertEquals("All done", 1.0, current, 0.001);
5656
}
5757

58+
@Test
59+
public void reopeningStartsFromZero() {
60+
var agg = new ProgressAggregator(this);
61+
var first = create(agg, 2);
62+
first.close();
63+
assertEquals("First run completes", 1.0, current, 0.001);
64+
65+
var second = create(agg, 10);
66+
second.advance(1);
67+
assertEquals("Second run starts from zero again", 0.1, current, 0.001);
68+
}
69+
5870
@Override
5971
public void accept(Double t, String msg) {
6072
this.current = t;

std-bits/database/src/main/java/org/enso/database/JDBCBatchInsert.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
import java.time.LocalDate;
1313
import java.time.LocalTime;
1414
import java.time.ZonedDateTime;
15+
16+
import org.enso.base.ProgressReporter;
1517
import org.enso.base.polyglot.EnsoExceptionWrapper;
1618
import org.enso.base.polyglot.EnsoMeta;
1719
import org.enso.table.data.column.builder.Builder;
20+
import static org.enso.table.data.column.operation.StorageIterators.PROGRESS_STEP;
1821
import org.enso.table.data.column.storage.ColumnStorage;
1922
import org.graalvm.polyglot.Value;
2023

@@ -71,6 +74,8 @@ private static void batchInsertImpl(
7174
var localisedStorages =
7275
stream(storages).map(Builder::makeLocal).toArray(ColumnStorage<?>[]::new);
7376

77+
try (var progressReporter =
78+
ProgressReporter.createWithStep("batchInsert", numRows, batchSize)) {
7479
for (int rowId = 0; rowId < numRows; rowId++) {
7580
for (int columnId = 0; columnId < columnCount; columnId++) {
7681
ColumnStorage<?> columnStorage = localisedStorages[columnId];
@@ -92,7 +97,9 @@ private static void batchInsertImpl(
9297
if ((rowId + 1) % batchSize == 0) {
9398
checkRows(stmt.executeBatch(), batchSize);
9499
}
100+
progressReporter.advance();
95101
}
102+
}
96103

97104
int remainingRows = numRows % batchSize;
98105
if (remainingRows != 0) {

0 commit comments

Comments
 (0)