Make lazy waveform and attribute loading explicit and thread safe

This commit is contained in:
eyck committed 2026-11-14 17:25:34 +01:00
1 parent 182b5bc01e
commit 82c4dcf552
12 files changed
+162 -71

No files matched your search

@@ -17,6 +17,7 @@ import com.minres.scviewer.database.HierNode;
import com.minres.scviewer.database.IEvent; import com.minres.scviewer.database.IEvent;
import com.minres.scviewer.database.IEventList; import com.minres.scviewer.database.IEventList;
import com.minres.scviewer.database.IWaveform; import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.LazyValue;
import com.minres.scviewer.database.WaveformType; import com.minres.scviewer.database.WaveformType;
public class FstSignal<T extends IEvent> extends HierNode implements IWaveform { public class FstSignal<T extends IEvent> extends HierNode implements IWaveform {
@@ -31,7 +32,7 @@ public class FstSignal<T extends IEvent> extends HierNode implements IWaveform {
private final int width; private final int width;
private final IEventList values; private final LazyValue<IEventList> values;
public FstSignal(FstDbLoader loader, String name) { public FstSignal(FstDbLoader loader, String name) {
this(loader, 0, name, 0, 1); this(loader, 0, name, 0, 1);
@@ -48,7 +49,7 @@ public class FstSignal<T extends IEvent> extends HierNode implements IWaveform {
this.id=id; this.id=id;
this.direction = direction; this.direction = direction;
this.width=width; this.width=width;
this.values=new EventList(); this.values=new LazyValue<>();
} }
public FstSignal(FstSignal<T> o, int id, String name) { public FstSignal(FstSignal<T> o, int id, String name) {
@@ -73,9 +74,11 @@ public class FstSignal<T extends IEvent> extends HierNode implements IWaveform {
@Override @Override
public IEventList getEvents() { public IEventList getEvents() {
if(values.size()==0) return values.get(() -> {
loader.getEvents(id, width, values); EventList loaded = new EventList();
return values; loader.getEvents(id, width, loaded);
return loaded;
});
} }
@Override @Override
@@ -89,7 +92,7 @@ public class FstSignal<T extends IEvent> extends HierNode implements IWaveform {
if(e==null) if(e==null)
return new IEvent[] {}; return new IEvent[] {};
else else
return getEvents().floorEntry(time).events; return e.events;
} }
@Override @Override
@@ -16,3 +16,4 @@ Service-Component: OSGI-INF/component.xml
Bundle-ActivationPolicy: lazy Bundle-ActivationPolicy: lazy
Automatic-Module-Name: com.minres.scviewer.database.ftr Automatic-Module-Name: com.minres.scviewer.database.ftr
Bundle-ClassPath: . Bundle-ClassPath: .
Export-Package: com.minres.scviewer.database.ftr;x-friends:="com.minres.scviewer.database.test"
@@ -82,7 +82,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
*/ */
@Override @Override
public IEvent[] getEventsAtTime(long time) { public IEvent[] getEventsAtTime(long time) {
return events.get(time); return getEvents().get(time);
} }
/** /**
@@ -93,11 +93,11 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
*/ */
@Override @Override
public IEvent[] getEventsBeforeTime(long time) { public IEvent[] getEventsBeforeTime(long time) {
EventEntry e = events.floorEntry(time); EventEntry e = getEvents().floorEntry(time);
if (e == null) if (e == null)
return new IEvent[] {}; return new IEvent[] {};
else else
return events.floorEntry(time).events; return e.events;
} }
/** /**
@@ -344,7 +344,7 @@ public class FtrDbLoader implements IWaveformDbLoader {
} }
} }
} catch (IOException e) { } catch (IOException e) {
LOG.error("Error parsing file "+file.getName(), e); throw new IllegalStateException("Cannot parse attributes in " + file.getName(), e);
} }
return ret; return ret;
} }
@@ -11,10 +11,10 @@
package com.minres.scviewer.database.ftr; package com.minres.scviewer.database.ftr;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.minres.scviewer.database.InputFormatException; import com.minres.scviewer.database.InputFormatException;
import com.minres.scviewer.database.LazyValue;
import com.minres.scviewer.database.tx.ITxAttribute; import com.minres.scviewer.database.tx.ITxAttribute;
/** /**
@@ -41,7 +41,7 @@ class FtrTx implements Serializable {
long endTime; long endTime;
/** The attributes. */ /** The attributes. */
final List<ITxAttribute> attributes = new ArrayList<>(); private final LazyValue<List<ITxAttribute>> attributes = new LazyValue<>();
final long blockId; final long blockId;
@@ -75,15 +75,14 @@ class FtrTx implements Serializable {
} }
public List<ITxAttribute> getAttributes(FtrDbLoader loader) { public List<ITxAttribute> getAttributes(FtrDbLoader loader) {
if(attributes.size()==0) return attributes.get(() -> {
try { try {
final TxStream stream = loader.txStreams.get(streamId); TxStream stream = loader.txStreams.get(streamId);
final byte[] chunk = stream.getChunks().get((int)blockId); byte[] chunk = stream.getChunks().get((int) blockId);
attributes.addAll(loader.parseAtrributes(chunk, blockOffset)); return List.copyOf(loader.parseAtrributes(chunk, blockOffset));
} catch (InputFormatException e) { } catch (InputFormatException e) {
// TODO Auto-generated catch block throw new IllegalStateException("Cannot read attributes for transaction " + id, e);
e.printStackTrace(); }
} });
return attributes; }
}
} }
@@ -10,14 +10,12 @@
*******************************************************************************/ *******************************************************************************/
package com.minres.scviewer.database.ftr; package com.minres.scviewer.database.ftr;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.minres.scviewer.database.DirectionType; import com.minres.scviewer.database.DirectionType;
import com.minres.scviewer.database.IEventList; import com.minres.scviewer.database.IEventList;
import com.minres.scviewer.database.IWaveform; import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.InputFormatException;
/** /**
* The Class TxGenerator. * The Class TxGenerator.
@@ -65,21 +63,7 @@ class TxGenerator extends AbstractTxStream {
*/ */
@Override @Override
public IEventList getEvents() { public IEventList getEvents() {
if(stream.events.size()==0) { stream.loadStream();
try {
List<byte[]> chunks = stream.getChunks();
int blockid = 0;
for (byte[] bs : chunks) {
loader.parseTx(stream, blockid++, bs);
}
} catch (InputFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return events; return events;
} }
/** /**
@@ -17,6 +17,7 @@ import java.util.List;
import com.minres.scviewer.database.DirectionType; import com.minres.scviewer.database.DirectionType;
import com.minres.scviewer.database.IEventList; import com.minres.scviewer.database.IEventList;
import com.minres.scviewer.database.IWaveform; import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.LazyValue;
import com.minres.scviewer.database.InputFormatException; import com.minres.scviewer.database.InputFormatException;
/** /**
@@ -29,7 +30,8 @@ class TxStream extends AbstractTxStream {
final ArrayList<Long> fileOffsets = new ArrayList<>(); final ArrayList<Long> fileOffsets = new ArrayList<>();
final ArrayList<byte[]> chunks = new ArrayList<>(); private final LazyValue<List<byte[]>> chunks = new LazyValue<>();
private final LazyValue<Boolean> parsed = new LazyValue<>();
/** /**
* Instantiates a new tx stream. * Instantiates a new tx stream.
@@ -66,37 +68,32 @@ class TxStream extends AbstractTxStream {
} }
public List<byte[]> getChunks() throws InputFormatException { public List<byte[]> getChunks() throws InputFormatException {
if(chunks.size()==0) { return chunks.get(() -> {
chunks.addAll(loader.getChunksAtOffsets(fileOffsets)); try {
} return List.copyOf(loader.getChunksAtOffsets(fileOffsets));
return chunks; } catch (InputFormatException e) {
} throw new IllegalStateException("Cannot read stream " + getFullName(), e);
}
});
}
public void loadStream() { public void loadStream() {
if(chunks.size()>0) return; parsed.get(() -> {
try { try {
List<byte[]> chunks = getChunks(); int blockId = 0;
int blockid = 0; for (byte[] chunk : getChunks()) loader.parseTx(this, blockId++, chunk);
for (byte[] bs : chunks) { return Boolean.TRUE;
loader.parseTx(this, blockid, bs); } catch (InputFormatException | IOException e) {
blockid++; throw new IllegalStateException("Cannot parse stream " + getFullName(), e);
} }
} catch (InputFormatException e) { });
} catch (IOException e) { }
}
} @Override
/** public IEventList getEvents() {
* Gets the events. loadStream();
* return events;
* @return the events }
*/
@Override
public IEventList getEvents() {
if(events.size()==0) {
loadStream();
}
return events;
}
@Override @Override
public DirectionType getDirection() { public DirectionType getDirection() {
@@ -0,0 +1,31 @@
package com.minres.scviewer.database;
import java.util.function.Supplier;
/** Publishes one complete value, including empty values, and preserves loading failures. */
public final class LazyValue<T> {
private enum State { UNLOADED, LOADING, LOADED, FAILED }
private State state = State.UNLOADED;
private T value;
private Throwable failure;
public synchronized T get(Supplier<T> supplier) {
if (state == State.LOADED) return value;
if (state == State.FAILED) {
if (failure instanceof Error) throw (Error) failure;
throw (RuntimeException) failure;
}
if (state == State.LOADING) throw new IllegalStateException("Recursive lazy loading");
state = State.LOADING;
try {
T loaded = supplier.get();
value = loaded;
state = State.LOADED;
return loaded;
} catch (RuntimeException | Error e) {
failure = e;
state = State.FAILED;
throw e;
}
}
}
@@ -9,6 +9,7 @@ Require-Bundle: com.minres.scviewer.database,
com.minres.scviewer.database.sqlite;bundle-version="1.0.0", com.minres.scviewer.database.sqlite;bundle-version="1.0.0",
com.minres.scviewer.database.text;bundle-version="1.0.0", com.minres.scviewer.database.text;bundle-version="1.0.0",
com.minres.scviewer.database.vcd;bundle-version="1.0.0", com.minres.scviewer.database.vcd;bundle-version="1.0.0",
com.minres.scviewer.database.ftr,
org.junit org.junit
Bundle-ActivationPolicy: lazy Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/component.xml Service-Component: OSGI-INF/component.xml
@@ -0,0 +1,33 @@
package com.minres.scviewer.database.test;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Test;
import com.minres.scviewer.database.IEventList;
import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.ftr.FtrDbLoader;
public class FtrLoadingTest {
@Test
public void cachedChunksStillParseEvents() throws Exception {
for (String name : new String[] {"my_db.ftr", "my_db_c.ftr"}) {
FtrDbLoader loader = new FtrDbLoader();
try {
loader.load(TxTestSupport.input(name));
IWaveform stream = loader.getAllWaves().stream()
.filter(w -> w.getClass().getSimpleName().equals("TxStream") && w.getId() == 1)
.findFirst().get();
Method chunks = stream.getClass().getMethod("getChunks");
chunks.setAccessible(true);
chunks.invoke(stream);
assertNotNull(stream.getEventsAtTime(0));
IEventList events = stream.getEvents();
assertEquals(27, events.size());
assertSame(events, stream.getEvents());
assertEquals(2, stream.getRowCount());
for (IWaveform wave : loader.getAllWaves()) wave.getEvents();
assertEquals(27, stream.getEvents().size());
} finally { loader.dispose(); }
}
}
}
@@ -0,0 +1,37 @@
package com.minres.scviewer.database.test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import com.minres.scviewer.database.LazyValue;
public class LazyValueTest {
@Test
public void concurrentEmptyValueLoadsOnce() throws Exception {
LazyValue<List<String>> lazy = new LazyValue<>();
AtomicInteger calls = new AtomicInteger();
ExecutorService executor = Executors.newFixedThreadPool(8);
try {
List<Callable<List<String>>> tasks = new ArrayList<>();
for (int i = 0; i < 100; ++i) tasks.add(() -> lazy.get(() -> {
calls.incrementAndGet();
return java.util.Collections.emptyList();
}));
for (Future<List<String>> result : executor.invokeAll(tasks)) assertTrue(result.get().isEmpty());
assertEquals(1, calls.get());
} finally { executor.shutdownNow(); }
}
@Test
public void preservesFailureWithoutRetry() {
LazyValue<String> lazy = new LazyValue<>();
IllegalStateException failure = new IllegalStateException("Invalid input");
for (int i = 0; i < 2; ++i) {
try { lazy.get(() -> { throw failure; }); fail("Expected failure"); }
catch (IllegalStateException e) { assertSame(failure, e); }
}
}
}
@@ -13,6 +13,11 @@ import com.minres.scviewer.database.tx.ITxEvent;
import com.minres.scviewer.database.tx.ITxRelation; import com.minres.scviewer.database.tx.ITxRelation;
final class TxTestSupport { final class TxTestSupport {
static java.io.File input(String name) {
java.io.File local = new java.io.File("inputs/" + name);
return local.exists() ? local : new java.io.File("tests/com.minres.scviewer.database.test/inputs/" + name);
}
static class Tx implements ITx { static class Tx implements ITx {
final long id, begin, end; final long id, begin, end;
Tx(long id, long begin, long end) { this.id = id; this.begin = begin; this.end = end; } Tx(long id, long begin, long end) { this.id = id; this.begin = begin; this.end = end; }