Make lazy waveform and attribute loading explicit and thread safe
This commit is contained in:
1 parent
182b5bc01e
commit
82c4dcf552
12 files changed
+162
-71
No files matched your search
+9
-6
@@ -17,6 +17,7 @@ import com.minres.scviewer.database.HierNode;
|
||||
import com.minres.scviewer.database.IEvent;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.LazyValue;
|
||||
import com.minres.scviewer.database.WaveformType;
|
||||
|
||||
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 IEventList values;
|
||||
private final LazyValue<IEventList> values;
|
||||
|
||||
public FstSignal(FstDbLoader loader, String name) {
|
||||
this(loader, 0, name, 0, 1);
|
||||
@@ -48,7 +49,7 @@ public class FstSignal<T extends IEvent> extends HierNode implements IWaveform {
|
||||
this.id=id;
|
||||
this.direction = direction;
|
||||
this.width=width;
|
||||
this.values=new EventList();
|
||||
this.values=new LazyValue<>();
|
||||
}
|
||||
|
||||
public FstSignal(FstSignal<T> o, int id, String name) {
|
||||
@@ -73,9 +74,11 @@ public class FstSignal<T extends IEvent> extends HierNode implements IWaveform {
|
||||
|
||||
@Override
|
||||
public IEventList getEvents() {
|
||||
if(values.size()==0)
|
||||
loader.getEvents(id, width, values);
|
||||
return values;
|
||||
return values.get(() -> {
|
||||
EventList loaded = new EventList();
|
||||
loader.getEvents(id, width, loaded);
|
||||
return loaded;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,7 +92,7 @@ public class FstSignal<T extends IEvent> extends HierNode implements IWaveform {
|
||||
if(e==null)
|
||||
return new IEvent[] {};
|
||||
else
|
||||
return getEvents().floorEntry(time).events;
|
||||
return e.events;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,3 +16,4 @@ Service-Component: OSGI-INF/component.xml
|
||||
Bundle-ActivationPolicy: lazy
|
||||
Automatic-Module-Name: com.minres.scviewer.database.ftr
|
||||
Bundle-ClassPath: .
|
||||
Export-Package: com.minres.scviewer.database.ftr;x-friends:="com.minres.scviewer.database.test"
|
||||
+3
-3
@@ -82,7 +82,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
||||
*/
|
||||
@Override
|
||||
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
|
||||
public IEvent[] getEventsBeforeTime(long time) {
|
||||
EventEntry e = events.floorEntry(time);
|
||||
EventEntry e = getEvents().floorEntry(time);
|
||||
if (e == null)
|
||||
return new IEvent[] {};
|
||||
else
|
||||
return events.floorEntry(time).events;
|
||||
return e.events;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -344,7 +344,7 @@ public class FtrDbLoader implements IWaveformDbLoader {
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.error("Error parsing file "+file.getName(), e);
|
||||
throw new IllegalStateException("Cannot parse attributes in " + file.getName(), e);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
+12
-13
@@ -11,10 +11,10 @@
|
||||
package com.minres.scviewer.database.ftr;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.minres.scviewer.database.InputFormatException;
|
||||
import com.minres.scviewer.database.LazyValue;
|
||||
import com.minres.scviewer.database.tx.ITxAttribute;
|
||||
|
||||
/**
|
||||
@@ -41,7 +41,7 @@ class FtrTx implements Serializable {
|
||||
long endTime;
|
||||
|
||||
/** The attributes. */
|
||||
final List<ITxAttribute> attributes = new ArrayList<>();
|
||||
private final LazyValue<List<ITxAttribute>> attributes = new LazyValue<>();
|
||||
|
||||
final long blockId;
|
||||
|
||||
@@ -75,15 +75,14 @@ class FtrTx implements Serializable {
|
||||
}
|
||||
|
||||
public List<ITxAttribute> getAttributes(FtrDbLoader loader) {
|
||||
if(attributes.size()==0)
|
||||
try {
|
||||
final TxStream stream = loader.txStreams.get(streamId);
|
||||
final byte[] chunk = stream.getChunks().get((int)blockId);
|
||||
attributes.addAll(loader.parseAtrributes(chunk, blockOffset));
|
||||
} catch (InputFormatException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return attributes;
|
||||
}
|
||||
return attributes.get(() -> {
|
||||
try {
|
||||
TxStream stream = loader.txStreams.get(streamId);
|
||||
byte[] chunk = stream.getChunks().get((int) blockId);
|
||||
return List.copyOf(loader.parseAtrributes(chunk, blockOffset));
|
||||
} catch (InputFormatException e) {
|
||||
throw new IllegalStateException("Cannot read attributes for transaction " + id, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+1
-17
@@ -10,14 +10,12 @@
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.ftr;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.minres.scviewer.database.DirectionType;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.InputFormatException;
|
||||
|
||||
/**
|
||||
* The Class TxGenerator.
|
||||
@@ -65,21 +63,7 @@ class TxGenerator extends AbstractTxStream {
|
||||
*/
|
||||
@Override
|
||||
public IEventList getEvents() {
|
||||
if(stream.events.size()==0) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
stream.loadStream();
|
||||
return events;
|
||||
}
|
||||
/**
|
||||
|
||||
+28
-31
@@ -17,6 +17,7 @@ import java.util.List;
|
||||
import com.minres.scviewer.database.DirectionType;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.LazyValue;
|
||||
import com.minres.scviewer.database.InputFormatException;
|
||||
|
||||
/**
|
||||
@@ -29,7 +30,8 @@ class TxStream extends AbstractTxStream {
|
||||
|
||||
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.
|
||||
@@ -66,37 +68,32 @@ class TxStream extends AbstractTxStream {
|
||||
}
|
||||
|
||||
public List<byte[]> getChunks() throws InputFormatException {
|
||||
if(chunks.size()==0) {
|
||||
chunks.addAll(loader.getChunksAtOffsets(fileOffsets));
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
return chunks.get(() -> {
|
||||
try {
|
||||
return List.copyOf(loader.getChunksAtOffsets(fileOffsets));
|
||||
} catch (InputFormatException e) {
|
||||
throw new IllegalStateException("Cannot read stream " + getFullName(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void loadStream() {
|
||||
if(chunks.size()>0) return;
|
||||
try {
|
||||
List<byte[]> chunks = getChunks();
|
||||
int blockid = 0;
|
||||
for (byte[] bs : chunks) {
|
||||
loader.parseTx(this, blockid, bs);
|
||||
blockid++;
|
||||
}
|
||||
} catch (InputFormatException e) {
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Gets the events.
|
||||
*
|
||||
* @return the events
|
||||
*/
|
||||
@Override
|
||||
public IEventList getEvents() {
|
||||
if(events.size()==0) {
|
||||
loadStream();
|
||||
}
|
||||
return events;
|
||||
}
|
||||
public void loadStream() {
|
||||
parsed.get(() -> {
|
||||
try {
|
||||
int blockId = 0;
|
||||
for (byte[] chunk : getChunks()) loader.parseTx(this, blockId++, chunk);
|
||||
return Boolean.TRUE;
|
||||
} catch (InputFormatException | IOException e) {
|
||||
throw new IllegalStateException("Cannot parse stream " + getFullName(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEventList getEvents() {
|
||||
loadStream();
|
||||
return events;
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user