Batch SQLite transaction timestamps and share generator records
This commit is contained in:
1 parent
8d4ec071e5
commit
e78c65a36c
5 files changed
+100
-48
No files matched your search
+2
-2
@@ -111,11 +111,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+7
-32
@@ -17,14 +17,12 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.minres.scviewer.database.AssociationType;
|
|
||||||
import com.minres.scviewer.database.IWaveform;
|
import com.minres.scviewer.database.IWaveform;
|
||||||
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
||||||
import com.minres.scviewer.database.sqlite.db.SQLiteDatabaseSelectHandler;
|
import com.minres.scviewer.database.sqlite.db.SQLiteDatabaseSelectHandler;
|
||||||
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
||||||
import com.minres.scviewer.database.sqlite.tables.ScvTx;
|
import com.minres.scviewer.database.sqlite.tables.ScvTx;
|
||||||
import com.minres.scviewer.database.sqlite.tables.ScvTxAttribute;
|
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.sqlite.tables.ScvTxRelation;
|
||||||
import com.minres.scviewer.database.tx.ITx;
|
import com.minres.scviewer.database.tx.ITx;
|
||||||
import com.minres.scviewer.database.tx.ITxAttribute;
|
import com.minres.scviewer.database.tx.ITxAttribute;
|
||||||
@@ -37,8 +35,8 @@ public class Tx implements ITx {
|
|||||||
private TxGenerator trGenerator;
|
private TxGenerator trGenerator;
|
||||||
private ScvTx scvTx;
|
private ScvTx scvTx;
|
||||||
private List<ITxAttribute> attributes;
|
private List<ITxAttribute> attributes;
|
||||||
private long begin=-1;
|
private final long begin;
|
||||||
private long end=-1;
|
private final long end;
|
||||||
private List<ITxRelation> incoming;
|
private List<ITxRelation> incoming;
|
||||||
private List<ITxRelation> outgoing;
|
private List<ITxRelation> outgoing;
|
||||||
|
|
||||||
@@ -47,6 +45,9 @@ public class Tx implements ITx {
|
|||||||
this.trStream=trStream;
|
this.trStream=trStream;
|
||||||
this.trGenerator=trGenerator;
|
this.trGenerator=trGenerator;
|
||||||
this.scvTx=scvTx;
|
this.scvTx=scvTx;
|
||||||
|
long[] times = trStream.getTransactionTimes(scvTx.getId());
|
||||||
|
begin = times[0];
|
||||||
|
end = times[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -69,36 +70,10 @@ public class Tx implements ITx {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getBeginTime() {
|
public long getBeginTime() { return begin; }
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getEndTime() {
|
public long getEndTime() { return end; }
|
||||||
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
|
@Override
|
||||||
public List<ITxAttribute> getAttributes() {
|
public List<ITxAttribute> getAttributes() {
|
||||||
|
|||||||
+6
-13
@@ -60,19 +60,12 @@ public class TxGenerator extends AbstractTxStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Map<Integer, ITx> getTransactions() {
|
protected synchronized Map<Integer, ITx> getTransactions() {
|
||||||
if(transactions==null){
|
if (transactions == null) {
|
||||||
transactions = new TreeMap<>();
|
TreeMap<Integer, ITx> loaded = new TreeMap<>();
|
||||||
SQLiteDatabaseSelectHandler<ScvTx> handler = new SQLiteDatabaseSelectHandler<>(ScvTx.class, database,
|
for (Map.Entry<Integer, ITx> entry : stream.getTransactions().entrySet())
|
||||||
"stream="+stream.getId()+" and generator="+scvGenerator.getId());
|
if (entry.getValue().getGenerator() == this) loaded.put(entry.getKey(), entry.getValue());
|
||||||
try {
|
transactions = loaded;
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return transactions;
|
return transactions;
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-1
@@ -17,6 +17,12 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
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.DirectionType;
|
||||||
import com.minres.scviewer.database.IEvent;
|
import com.minres.scviewer.database.IEvent;
|
||||||
@@ -37,6 +43,38 @@ public class TxStream extends AbstractTxStream {
|
|||||||
private TreeMap<Integer, TxGenerator> generators;
|
private TreeMap<Integer, TxGenerator> generators;
|
||||||
|
|
||||||
private TreeMap<Integer, ITx> transactions;
|
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) {
|
public TxStream(IDatabase database, ScvStream scvStream) {
|
||||||
super(database, scvStream.getName(), scvStream.getId());
|
super(database, scvStream.getName(), scvStream.getId());
|
||||||
@@ -71,7 +109,7 @@ public class TxStream extends AbstractTxStream {
|
|||||||
return new ArrayList<>(generators.values());
|
return new ArrayList<>(generators.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Map<Integer, ITx> getTransactions() {
|
protected synchronized Map<Integer, ITx> getTransactions() {
|
||||||
if(transactions==null){
|
if(transactions==null){
|
||||||
if(generators==null) getGenerators();
|
if(generators==null) getGenerators();
|
||||||
transactions = new TreeMap<>();
|
transactions = new TreeMap<>();
|
||||||
|
|||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
package com.minres.scviewer.database.test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
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.sqlite.TxStream;
|
||||||
|
import com.minres.scviewer.database.sqlite.db.SQLiteDatabase;
|
||||||
|
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
||||||
|
import com.minres.scviewer.database.tx.ITx;
|
||||||
|
import com.minres.scviewer.database.tx.ITxEvent;
|
||||||
|
|
||||||
|
public class SQLiteBatchTest {
|
||||||
|
private static class Database extends SQLiteDatabase {
|
||||||
|
int connections;
|
||||||
|
Database() { super(TxTestSupport.input("my_sqldb.txdb").getAbsolutePath()); setData("TIMERESOLUTION", 1000L); }
|
||||||
|
@Override public Connection createConnection() throws SQLException { connections++;return super.createConnection(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loadsAllTimesInOneQueryAndSharesGeneratorTransactions() {
|
||||||
|
Database database = new Database();
|
||||||
|
ScvStream row = new ScvStream();row.setId(1);row.setName("test");row.setKind("test");
|
||||||
|
TxStream stream = new TxStream(database, row);
|
||||||
|
assertNotNull(stream.getEventsBeforeTime(0));
|
||||||
|
assertEquals(26, stream.getEvents().size());
|
||||||
|
// One query each for generators, transaction records, and all timestamps.
|
||||||
|
assertEquals(3, database.connections);
|
||||||
|
int count = database.connections;
|
||||||
|
ITx first = ((ITxEvent)stream.getEventsAtTime(0)[0]).getTransaction();
|
||||||
|
assertEquals(0, first.getBeginTime());
|
||||||
|
assertEquals(420000000, first.getEndTime());
|
||||||
|
for (EventEntry entry : stream.getEvents()) for (IEvent event : entry.events) {
|
||||||
|
ITx tx = ((ITxEvent)event).getTransaction();
|
||||||
|
assertTrue(tx.getEndTime() >= tx.getBeginTime());
|
||||||
|
}
|
||||||
|
for (IWaveform generator : stream.getGenerators())
|
||||||
|
for (EventEntry entry : generator.getEvents()) for (IEvent event : entry.events)
|
||||||
|
if (((ITxEvent)event).getTransaction().getId() == first.getId())
|
||||||
|
assertSame(first, ((ITxEvent)event).getTransaction());
|
||||||
|
assertEquals(count, database.connections);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user