Expose transaction concurrency indexes in streams and generators

This commit is contained in:
eyck committed 2026-11-14 18:13:22 +01:00
1 parent 40d402ce8a
commit 1f861cefb8
13 files changed
+264 -16

No files matched your search

@@ -148,7 +148,12 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
*/ */
synchronized void calculateConcurrency() { synchronized void calculateConcurrency() {
if (rowCount >= 0) return; if (rowCount >= 0) return;
int count = TxRowAllocator.assign(getEvents(), (event, row) -> ((TxEvent) event).setConcurrencyIndex(row)); int count = TxRowAllocator.assign(getEvents(), (event, row) -> {
((TxEvent) event).setConcurrencyIndex(row);
Tx tx = (Tx) event.getTransaction();
if (this instanceof TxGenerator) tx.setConcurrencyIdxInGen(row);
else tx.setConcurrencyIndxInStream(row);
});
for (com.minres.scviewer.database.IHierNode child : getChildNodes()) for (com.minres.scviewer.database.IHierNode child : getChildNodes())
((TxGenerator) child).calculateConcurrency(); ((TxGenerator) child).calculateConcurrency();
rowCount = count; rowCount = count;
@@ -38,6 +38,9 @@ class Tx implements ITx {
private final long streamId; private final long streamId;
private volatile int concurrencyIndxInStream = -1;
private volatile int concurrencyIdxInGen = -1;
/** The begin time. */ /** The begin time. */
long beginTime = -1; long beginTime = -1;
@@ -175,6 +178,26 @@ class Tx implements ITx {
return loader.txGenerators.get(generatorId); return loader.txGenerators.get(generatorId);
} }
@Override
public int getConcurrencyIndxInStream() {
if (concurrencyIndxInStream < 0) getStream().getRowCount();
return concurrencyIndxInStream;
}
@Override
public int getConcurrencyIdxInGen() {
if (concurrencyIdxInGen < 0) getGenerator().getRowCount();
return concurrencyIdxInGen;
}
void setConcurrencyIndxInStream(int index) {
concurrencyIndxInStream = index;
}
void setConcurrencyIdxInGen(int index) {
concurrencyIdxInGen = index;
}
/** /**
* Gets the begin time. * Gets the begin time.
* *
@@ -133,6 +133,16 @@ class TxRelation implements ITxRelation {
return resolve().getGenerator(); return resolve().getGenerator();
} }
@Override
public int getConcurrencyIndxInStream() {
return resolve().getConcurrencyIndxInStream();
}
@Override
public int getConcurrencyIdxInGen() {
return resolve().getConcurrencyIdxInGen();
}
@Override @Override
public long getBeginTime() { public long getBeginTime() {
return resolve().getBeginTime(); return resolve().getBeginTime();
@@ -26,6 +26,7 @@ import com.minres.scviewer.database.RelationTypeFactory;
import com.minres.scviewer.database.WaveformType; import com.minres.scviewer.database.WaveformType;
import com.minres.scviewer.database.sqlite.db.IDatabase; import com.minres.scviewer.database.sqlite.db.IDatabase;
import com.minres.scviewer.database.tx.ITx; import com.minres.scviewer.database.tx.ITx;
import com.minres.scviewer.database.tx.TxRowAllocator;
abstract class AbstractTxStream extends HierNode implements IWaveform { abstract class AbstractTxStream extends HierNode implements IWaveform {
@@ -46,7 +47,11 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
} }
@Override @Override
public int getRowCount() { public synchronized int getRowCount() {
if (this instanceof TxGenerator) {
getEvents();
return maxConcurrency;
}
if(maxConcurrency==null){ if(maxConcurrency==null){
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("SELECT MAX(concurrencyLevel) as concurrencyLevel FROM ScvTx where stream="); sb.append("SELECT MAX(concurrencyLevel) as concurrencyLevel FROM ScvTx where stream=");
@@ -81,16 +86,20 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
EventList.Builder builder = new EventList.Builder(); EventList.Builder builder = new EventList.Builder();
for (ITx tx : getTransactions().values()) { for (ITx tx : getTransactions().values()) {
if (tx.getBeginTime() == tx.getEndTime()) { if (tx.getBeginTime() == tx.getEndTime()) {
TxEvent single = new TxEvent(EventKind.SINGLE, tx); TxEvent single = new TxEvent(EventKind.SINGLE, tx, this instanceof TxGenerator);
builder.add(single.getTime(), single); builder.add(single.getTime(), single);
} else { } else {
TxEvent begin = new TxEvent(EventKind.BEGIN, tx); TxEvent begin = new TxEvent(EventKind.BEGIN, tx, this instanceof TxGenerator);
TxEvent end = new TxEvent(EventKind.END, tx); TxEvent end = new TxEvent(EventKind.END, tx, this instanceof TxGenerator);
builder.add(begin.getTime(), begin); builder.add(begin.getTime(), begin);
builder.add(end.getTime(), end); builder.add(end.getTime(), end);
} }
} }
events = builder.build(); IEventList loaded = builder.build();
if (this instanceof TxGenerator)
maxConcurrency = TxRowAllocator.assign(loaded,
(event, row) -> ((Tx) event.getTransaction()).setConcurrencyIdxInGen(row));
events = loaded;
} }
return events; return events;
} }
@@ -34,6 +34,7 @@ public class Tx implements ITx {
private TxStream trStream; private TxStream trStream;
private TxGenerator trGenerator; private TxGenerator trGenerator;
private ScvTx scvTx; private ScvTx scvTx;
private volatile int concurrencyIdxInGen = -1;
private List<ITxAttribute> attributes; private List<ITxAttribute> attributes;
private final long begin; private final long begin;
private final long end; private final long end;
@@ -65,9 +66,20 @@ public class Tx implements ITx {
return trGenerator; return trGenerator;
} }
int getConcurrencyIndex() { @Override
return scvTx.getConcurrencyLevel(); public int getConcurrencyIndxInStream() {
} return scvTx.getConcurrencyLevel();
}
@Override
public int getConcurrencyIdxInGen() {
if (concurrencyIdxInGen < 0) trGenerator.getRowCount();
return concurrencyIdxInGen;
}
void setConcurrencyIdxInGen(int index) {
concurrencyIdxInGen = index;
}
@Override @Override
public long getBeginTime() { return begin; } public long getBeginTime() { return begin; }
@@ -19,12 +19,17 @@ import com.minres.scviewer.database.tx.ITxEvent;
public class TxEvent implements ITxEvent { public class TxEvent implements ITxEvent {
private final EventKind type; private final EventKind type;
private final boolean inGenerator;
private ITx tx; private ITx tx;
public TxEvent(EventKind type, ITx tx) { public TxEvent(EventKind type, ITx tx) {
super(); this(type, tx, false);
this.type = type; }
this.tx = tx;
TxEvent(EventKind type, ITx tx, boolean inGenerator) {
this.type = type;
this.tx = tx;
this.inGenerator = inGenerator;
} }
@Override @Override
@@ -34,7 +39,7 @@ public class TxEvent implements ITxEvent {
@Override @Override
public IEvent duplicate() throws CloneNotSupportedException { public IEvent duplicate() throws CloneNotSupportedException {
return new TxEvent(type, tx); return new TxEvent(type, tx, inGenerator);
} }
@Override @Override
@@ -59,7 +64,7 @@ public class TxEvent implements ITxEvent {
@Override @Override
public int getRowIndex() { public int getRowIndex() {
return ((Tx)tx).getConcurrencyIndex(); return inGenerator ? tx.getConcurrencyIdxInGen() : tx.getConcurrencyIndxInStream();
} }
} }
@@ -158,7 +158,12 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
*/ */
synchronized void calculateConcurrency() { synchronized void calculateConcurrency() {
if (rowCount >= 0) return; if (rowCount >= 0) return;
int count = TxRowAllocator.assign(getEvents(), (event, row) -> ((TxEvent) event).setConcurrencyIndex(row)); int count = TxRowAllocator.assign(getEvents(), (event, row) -> {
((TxEvent) event).setConcurrencyIndex(row);
Tx tx = (Tx) event.getTransaction();
if (this instanceof TxGenerator) tx.setConcurrencyIdxInGen(row);
else tx.setConcurrencyIndxInStream(row);
});
for (com.minres.scviewer.database.IHierNode child : getChildNodes()) for (com.minres.scviewer.database.IHierNode child : getChildNodes())
((TxGenerator) child).calculateConcurrency(); ((TxGenerator) child).calculateConcurrency();
rowCount = count; rowCount = count;
@@ -144,7 +144,7 @@ public class TextDbLoader implements IWaveformDbLoader {
* @param txId the tx id * @param txId the tx id
* @return the transaction * @return the transaction
*/ */
public ITx getTransaction(long txId) { public synchronized ITx getTransaction(long txId) {
if (txCache.containsKey(txId)) if (txCache.containsKey(txId))
return txCache.get(txId); return txCache.get(txId);
if(transactions.containsKey(txId)) { if(transactions.containsKey(txId)) {
@@ -267,6 +267,7 @@ public class TextDbLoader implements IWaveformDbLoader {
txStreams.clear(); txStreams.clear();
txGenerators.clear(); txGenerators.clear();
transactions = null; transactions = null;
txCache.clear();
attributeTypes.clear(); attributeTypes.clear();
relationsIn.clear(); relationsIn.clear();
relationsOut.clear(); relationsOut.clear();
@@ -38,6 +38,9 @@ class Tx implements ITx {
private final long streamId; private final long streamId;
private volatile int concurrencyIndxInStream = -1;
private volatile int concurrencyIdxInGen = -1;
/** The begin time. */ /** The begin time. */
long beginTime = -1; long beginTime = -1;
@@ -174,6 +177,26 @@ class Tx implements ITx {
return loader.txGenerators.get(generatorId); return loader.txGenerators.get(generatorId);
} }
@Override
public int getConcurrencyIndxInStream() {
if (concurrencyIndxInStream < 0) getStream().getRowCount();
return concurrencyIndxInStream;
}
@Override
public int getConcurrencyIdxInGen() {
if (concurrencyIdxInGen < 0) getGenerator().getRowCount();
return concurrencyIdxInGen;
}
void setConcurrencyIndxInStream(int index) {
concurrencyIndxInStream = index;
}
void setConcurrencyIdxInGen(int index) {
concurrencyIdxInGen = index;
}
/** /**
* Gets the begin time. * Gets the begin time.
* *
@@ -41,6 +41,22 @@ public interface ITx extends Comparable<ITx> {
*/ */
public IWaveform getGenerator(); public IWaveform getGenerator();
/**
* Gets the zero-based row index within the transaction's stream.
* The stream layout is calculated on demand.
*
* @return the concurrency index in the stream
*/
public int getConcurrencyIndxInStream();
/**
* Gets the zero-based row index within the transaction's generator.
* The generator layout is calculated on demand, independently of the stream layout.
*
* @return the concurrency index in the generator
*/
public int getConcurrencyIdxInGen();
/** /**
* Gets the begin time. * Gets the begin time.
* *
@@ -32,8 +32,12 @@ public class FtrRelationTest {
Loader loader = new Loader(); Loader loader = new Loader();
ITx proxy = proxy(loader); ITx proxy = proxy(loader);
TxTestSupport.Tx real = new TxTestSupport.Tx(1, 10, 50); TxTestSupport.Tx real = new TxTestSupport.Tx(1, 10, 50);
real.streamRow = 2;
real.generatorRow = 1;
loader.available = real; loader.available = real;
assertEquals(50, proxy.getEndTime()); assertEquals(50, proxy.getEndTime());
assertEquals(2, proxy.getConcurrencyIndxInStream());
assertEquals(1, proxy.getConcurrencyIdxInGen());
assertEquals(10, proxy.getBeginTime()); assertEquals(10, proxy.getBeginTime());
assertSame(real.getAttributes(), proxy.getAttributes()); assertSame(real.getAttributes(), proxy.getAttributes());
assertEquals(0, proxy.compareTo(proxy)); assertEquals(0, proxy.compareTo(proxy));
@@ -43,6 +47,18 @@ public class FtrRelationTest {
assertEquals(-Integer.signum(later.compareTo(proxy)), Integer.signum(proxy.compareTo(later))); assertEquals(-Integer.signum(later.compareTo(proxy)), Integer.signum(proxy.compareTo(later)));
} }
@Test
public void resolvesFromConcurrencyGetter() throws Exception {
Loader loader = new Loader();
ITx proxy = proxy(loader);
TxTestSupport.Tx real = new TxTestSupport.Tx(1, 10, 50);
real.streamRow = 3;
real.generatorRow = 1;
loader.available = real;
assertEquals(1, proxy.getConcurrencyIdxInGen());
assertEquals(3, proxy.getConcurrencyIndxInStream());
}
@Test @Test
public void missingEndpointFailsExplicitly() throws Exception { public void missingEndpointFailsExplicitly() throws Exception {
ITx proxy = proxy(new Loader()); ITx proxy = proxy(new Loader());
@@ -0,0 +1,120 @@
package com.minres.scviewer.database.test;
import static org.junit.Assert.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import com.minres.scviewer.database.EventEntry;
import com.minres.scviewer.database.IEvent;
import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.IWaveformDbLoader;
import com.minres.scviewer.database.ftr.FtrDbLoader;
import com.minres.scviewer.database.sqlite.TxStream;
import com.minres.scviewer.database.sqlite.db.SQLiteDatabase;
import com.minres.scviewer.database.sqlite.tables.ScvStream;
import com.minres.scviewer.database.text.TextDbLoader;
import com.minres.scviewer.database.tx.ITx;
import com.minres.scviewer.database.tx.ITxEvent;
public class TxConcurrencyTest {
private void checkRows(IWaveform wave) throws Exception {
for (EventEntry entry : wave.getEvents()) for (IEvent raw : entry.events) {
ITxEvent event = (ITxEvent) raw;
ITx tx = event.getTransaction();
// The property getters must initialize lazy layouts without a prior getRowCount().
int streamIndex = tx.getConcurrencyIndxInStream();
int generatorIndex = tx.getConcurrencyIdxInGen();
assertTrue(streamIndex >= 0 && streamIndex < tx.getStream().getRowCount());
assertTrue(generatorIndex >= 0 && generatorIndex < tx.getGenerator().getRowCount());
assertEquals(wave == tx.getStream() ? streamIndex : generatorIndex, event.getRowIndex());
if (event instanceof com.minres.scviewer.database.sqlite.TxEvent)
assertEquals(event.getRowIndex(), ((ITxEvent) event.duplicate()).getRowIndex());
}
}
@Test
public void ftrAndTextPropertiesMatchBothEventLayouts() throws Exception {
for (String input : new String[] {"my_db.ftr", "my_db_c.ftr", "my_db.txlog"}) {
IWaveformDbLoader loader = input.endsWith("txlog") ? new TextDbLoader() : new FtrDbLoader();
try {
loader.load(TxTestSupport.input(input));
for (IWaveform wave : loader.getAllWaves()) checkRows(wave);
} finally { loader.dispose(); }
}
}
private Map<Long, ITx> transactions(IWaveform stream) {
Map<Long, ITx> transactions = new HashMap<>();
for (EventEntry entry : stream.getEvents()) for (IEvent raw : entry.events) {
ITx tx = ((ITxEvent) raw).getTransaction();
transactions.put(tx.getId(), tx);
}
return transactions;
}
private void checkIndependentRows(IWaveform stream) throws Exception {
Map<Long, ITx> tx = transactions(stream);
assertEquals(0, tx.get(10L).getConcurrencyIndxInStream());
assertEquals(0, tx.get(10L).getConcurrencyIdxInGen());
assertEquals(0, tx.get(11L).getConcurrencyIdxInGen());
assertEquals(1, tx.get(11L).getConcurrencyIndxInStream());
assertEquals(2, tx.get(12L).getConcurrencyIndxInStream());
assertEquals(1, tx.get(12L).getConcurrencyIdxInGen());
assertEquals(1, tx.get(13L).getConcurrencyIndxInStream());
assertEquals(0, tx.get(13L).getConcurrencyIdxInGen());
assertEquals(2, tx.get(11L).getGenerator().getRowCount());
checkRows(stream);
for (com.minres.scviewer.database.IHierNode child : stream.getChildNodes()) checkRows((IWaveform) child);
}
@Test
public void textRowsAreIndependentAcrossGeneratorsAndSurviveReload() throws Exception {
Path file = Files.createTempFile("tx-concurrency-", ".txlog");
TextDbLoader loader = new TextDbLoader();
try {
Files.writeString(file, "scv_tr_stream (ID 1, name \"stream\", kind \"test\")\n"
+ "scv_tr_generator (ID 4, name \"a\", scv_tr_stream 1,\n)\n"
+ "scv_tr_generator (ID 5, name \"b\", scv_tr_stream 1,\n)\n"
+ "tx_begin 10 4 0 ns\ntx_begin 11 5 1 ns\ntx_begin 12 5 2 ns\n"
+ "tx_end 12 5 3 ns\ntx_end 11 5 4 ns\ntx_begin 13 5 4 ns\n"
+ "tx_end 13 5 4 ns\ntx_end 10 4 10 ns\n");
loader.load(file.toFile());
IWaveform stream = loader.getAllWaves().stream().filter(w -> w.getId() == 1).findFirst().get();
ITx before = transactions(stream).get(10L);
checkIndependentRows(stream);
loader.load(file.toFile());
stream = loader.getAllWaves().stream().filter(w -> w.getId() == 1).findFirst().get();
assertNotSame(before, transactions(stream).get(10L));
checkIndependentRows(stream);
} finally { loader.dispose();Files.deleteIfExists(file); }
}
@Test
public void sqliteRowsAreIndependentAcrossGenerators() throws Exception {
Path file = Files.createTempFile("tx-concurrency-", ".txdb");
try {
Files.copy(TxTestSupport.input("my_sqldb.txdb").toPath(), file,
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
SQLiteDatabase database = new SQLiteDatabase(file.toString());
database.setData("TIMERESOLUTION", 1L);
try (Connection connection = database.createConnection();Statement statement = connection.createStatement()) {
statement.executeUpdate("DELETE FROM ScvTxRelation");
statement.executeUpdate("DELETE FROM ScvTxAttribute");
statement.executeUpdate("DELETE FROM ScvTxEvent");
statement.executeUpdate("DELETE FROM ScvTx");
statement.executeUpdate("INSERT INTO ScvTx VALUES (10,4,1,0),(11,5,1,1),(12,5,1,2),(13,5,1,1)");
statement.executeUpdate("INSERT INTO ScvTxEvent VALUES (10,0,0),(10,2,10),(11,0,1),(11,2,4),"
+ "(12,0,2),(12,2,3),(13,0,4),(13,2,4)");
}
ScvStream row = new ScvStream();row.setId(1);row.setName("stream");row.setKind("test");
checkIndependentRows(new TxStream(database, row));
} finally { Files.deleteIfExists(file); }
}
}
@@ -20,8 +20,11 @@ final class TxTestSupport {
static class Tx implements ITx { static class Tx implements ITx {
final long id, begin, end; final long id, begin, end;
int streamRow, generatorRow;
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; }
public long getId() { return id; } public long getId() { return id; }
public int getConcurrencyIndxInStream() { return streamRow; }
public int getConcurrencyIdxInGen() { return generatorRow; }
public long getBeginTime() { return begin; } public long getBeginTime() { return begin; }
public long getEndTime() { return end; } public long getEndTime() { return end; }
public IWaveform getStream() { return null; } public IWaveform getStream() { return null; }