/******************************************************************************* * Copyright (c) 2026 MINRES Technologies GmbH and others. * * SPDX-License-Identifier: EPL-1.0 * * Contributors: * MINRES Technologies GmbH - initial API and implementation *******************************************************************************/ package com.minres.scviewer.database.ui; import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import com.minres.scviewer.database.ReadCancellation; import com.minres.scviewer.database.WaveformAccess; import com.minres.scviewer.database.tx.ITx; import com.minres.scviewer.database.tx.ITxAttribute; import com.minres.scviewer.database.tx.ITxRelation; /** UI-owned, bounded snapshots. Accessors never call recording code on the UI thread. */ public final class AsyncTransactionData implements AutoCloseable { public record Snapshot(List attributes, List incoming, List outgoing) { } private static final Snapshot EMPTY = new Snapshot(List.of(), List.of(), List.of()); private final Map values = new IdentityHashMap<>(); private final Map jobs = new IdentityHashMap<>(); private final java.util.ArrayDeque order = new java.util.ArrayDeque<>(); private final boolean relations; private final Consumer dispatch; private final Consumer changed; private volatile boolean closed; public AsyncTransactionData(Consumer dispatch, Consumer changed) { this(dispatch, changed, true); } public AsyncTransactionData(Consumer dispatch, Consumer changed, boolean relations) { this.relations = relations; this.dispatch = dispatch; this.changed = changed; } public Snapshot get(ITx tx) { if (closed || tx == null) return EMPTY; Snapshot value = values.get(tx); if (value != null) return value; if (!jobs.containsKey(tx)) { Job job = new Job("Load transaction details") { @Override protected IStatus run(org.eclipse.core.runtime.IProgressMonitor monitor) { try { Snapshot loaded = ReadCancellation.with(() -> closed || monitor.isCanceled(), () -> WaveformAccess.read(tx.getStream(), () -> new Snapshot(List.copyOf(tx.getAttributes()), relations ? List.copyOf(tx.getIncomingRelations()) : List.of(), relations ? List.copyOf(tx.getOutgoingRelations()) : List.of()))); dispatch.accept(() -> { if (closed || jobs.get(tx) != this) return; jobs.remove(tx); if (values.size() >= 512) values.remove(order.removeFirst()); values.put(tx, loaded); order.addLast(tx); changed.accept(tx); }); return Status.OK_STATUS; } catch (java.util.concurrent.CancellationException e) { dispatch.accept(() -> { if (!closed && jobs.get(tx) == this) jobs.remove(tx); }); return Status.CANCEL_STATUS; } catch (RuntimeException e) { dispatch.accept(() -> { if (!closed && jobs.get(tx) == this) { jobs.remove(tx); values.put(tx, EMPTY); order.addLast(tx); changed.accept(tx); } }); return new Status(IStatus.ERROR, "com.minres.scviewer.database.ui.swt", "Cannot load transaction details", e); } finally { monitor.done(); } } }; jobs.put(tx, job); job.setSystem(true); job.schedule(); } return EMPTY; } public boolean isReady(ITx tx) { return values.containsKey(tx); } @Override public void close() { closed = true; jobs.values().forEach(Job::cancel); jobs.clear(); values.clear(); order.clear(); } }