Batch SQLite transaction timestamps and share generator records
This commit is contained in:
1 parent
8d4ec071e5
commit
e78c65a36c
5 files changed
+102
-50
No files matched your search
+2
-2
@@ -111,11 +111,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;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+8
-33
@@ -17,14 +17,12 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import com.minres.scviewer.database.AssociationType;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
||||
import com.minres.scviewer.database.sqlite.db.SQLiteDatabaseSelectHandler;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTx;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTxAttribute;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTxEvent;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTxRelation;
|
||||
import com.minres.scviewer.database.tx.ITx;
|
||||
import com.minres.scviewer.database.tx.ITxAttribute;
|
||||
@@ -37,8 +35,8 @@ public class Tx implements ITx {
|
||||
private TxGenerator trGenerator;
|
||||
private ScvTx scvTx;
|
||||
private List<ITxAttribute> attributes;
|
||||
private long begin=-1;
|
||||
private long end=-1;
|
||||
private final long begin;
|
||||
private final long end;
|
||||
private List<ITxRelation> incoming;
|
||||
private List<ITxRelation> outgoing;
|
||||
|
||||
@@ -47,6 +45,9 @@ public class Tx implements ITx {
|
||||
this.trStream=trStream;
|
||||
this.trGenerator=trGenerator;
|
||||
this.scvTx=scvTx;
|
||||
long[] times = trStream.getTransactionTimes(scvTx.getId());
|
||||
begin = times[0];
|
||||
end = times[1];
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,36 +70,10 @@ public class Tx implements ITx {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getBeginTime() {
|
||||
if(begin<0){
|
||||
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<>(ScvTxEvent.class,
|
||||
database, "tx="+scvTx.getId()+" AND type="+ AssociationType.BEGIN.ordinal());
|
||||
try {
|
||||
for(ScvTxEvent scvEvent:handler.selectObjects()){
|
||||
begin= scvEvent.getTime()*(Long)database.getData("TIMERESOLUTION");
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
|
||||
}
|
||||
}
|
||||
return begin;
|
||||
}
|
||||
public long getBeginTime() { return begin; }
|
||||
|
||||
@Override
|
||||
public long getEndTime() {
|
||||
if(end<0){
|
||||
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<>(ScvTxEvent.class,
|
||||
database, "tx="+scvTx.getId()+" AND type="+ AssociationType.END.ordinal());
|
||||
try {
|
||||
for(ScvTxEvent scvEvent:handler.selectObjects()){
|
||||
end = scvEvent.getTime()*(Long)database.getData("TIMERESOLUTION");
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
|
||||
}
|
||||
}
|
||||
return end;
|
||||
}
|
||||
@Override
|
||||
public long getEndTime() { return end; }
|
||||
|
||||
@Override
|
||||
public List<ITxAttribute> getAttributes() {
|
||||
|
||||
+7
-14
@@ -60,20 +60,13 @@ public class TxGenerator extends AbstractTxStream {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<Integer, ITx> getTransactions() {
|
||||
if(transactions==null){
|
||||
transactions = new TreeMap<>();
|
||||
SQLiteDatabaseSelectHandler<ScvTx> handler = new SQLiteDatabaseSelectHandler<>(ScvTx.class, database,
|
||||
"stream="+stream.getId()+" and generator="+scvGenerator.getId());
|
||||
try {
|
||||
for(ScvTx scvTx:handler.selectObjects()){
|
||||
transactions.put(scvTx.getId(), new Tx(database, (TxStream) stream, this, scvTx));
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
protected synchronized Map<Integer, ITx> getTransactions() {
|
||||
if (transactions == null) {
|
||||
TreeMap<Integer, ITx> loaded = new TreeMap<>();
|
||||
for (Map.Entry<Integer, ITx> entry : stream.getTransactions().entrySet())
|
||||
if (entry.getValue().getGenerator() == this) loaded.put(entry.getKey(), entry.getValue());
|
||||
transactions = loaded;
|
||||
}
|
||||
return transactions;
|
||||
}
|
||||
|
||||
|
||||
+39
-1
@@ -17,6 +17,12 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.HashMap;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import com.minres.scviewer.database.AssociationType;
|
||||
import com.minres.scviewer.database.LazyValue;
|
||||
|
||||
import com.minres.scviewer.database.DirectionType;
|
||||
import com.minres.scviewer.database.IEvent;
|
||||
@@ -37,6 +43,38 @@ public class TxStream extends AbstractTxStream {
|
||||
private TreeMap<Integer, TxGenerator> generators;
|
||||
|
||||
private TreeMap<Integer, ITx> transactions;
|
||||
private final LazyValue<Map<Integer, long[]>> transactionTimes = new LazyValue<>();
|
||||
|
||||
long[] getTransactionTimes(int id) {
|
||||
Map<Integer, long[]> times = transactionTimes.get(() -> {
|
||||
String sql = "SELECT e.tx, MIN(CASE WHEN e.type=? THEN e.time END) AS beginTime, "
|
||||
+ "MAX(CASE WHEN e.type=? THEN e.time END) AS endTime "
|
||||
+ "FROM ScvTxEvent e JOIN ScvTx t ON t.id=e.tx WHERE t.stream=? GROUP BY e.tx";
|
||||
Map<Integer, long[]> loaded = new HashMap<>();
|
||||
long scale = ((Number) database.getData("TIMERESOLUTION")).longValue();
|
||||
try (Connection connection = database.createConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)) {
|
||||
statement.setInt(1, AssociationType.BEGIN.ordinal());
|
||||
statement.setInt(2, AssociationType.END.ordinal());
|
||||
statement.setLong(3, getId());
|
||||
try (ResultSet rows = statement.executeQuery()) {
|
||||
while (rows.next()) {
|
||||
if (rows.getObject("beginTime") == null || rows.getObject("endTime") == null)
|
||||
throw new SQLException("Missing timestamps for transaction " + rows.getInt("tx"));
|
||||
loaded.put(rows.getInt("tx"), new long[] {
|
||||
Math.multiplyExact(rows.getLong("beginTime"), scale),
|
||||
Math.multiplyExact(rows.getLong("endTime"), scale)});
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("Cannot load timestamps for stream " + getFullName(), e);
|
||||
}
|
||||
return loaded;
|
||||
});
|
||||
long[] result = times.get(id);
|
||||
if (result == null) throw new IllegalStateException("Missing timestamps for transaction " + id);
|
||||
return result;
|
||||
}
|
||||
|
||||
public TxStream(IDatabase database, ScvStream scvStream) {
|
||||
super(database, scvStream.getName(), scvStream.getId());
|
||||
@@ -71,7 +109,7 @@ public class TxStream extends AbstractTxStream {
|
||||
return new ArrayList<>(generators.values());
|
||||
}
|
||||
|
||||
protected Map<Integer, ITx> getTransactions() {
|
||||
protected synchronized Map<Integer, ITx> getTransactions() {
|
||||
if(transactions==null){
|
||||
if(generators==null) getGenerators();
|
||||
transactions = new TreeMap<>();
|
||||
|
||||
Reference in new issue
Block a user