SCViewer/plugins/com.minres.scviewer.databas.../src/com/minres/scviewer/database/sqlite/Tx.java

193 lines
6.7 KiB
Java
Raw Normal View History

/*******************************************************************************
2021-01-09 14:26:49 +01:00
* Copyright (c) 2015-2021 MINRES Technologies GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* MINRES Technologies GmbH - initial API and implementation
*******************************************************************************/
package com.minres.scviewer.database.sqlite;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.minres.scviewer.database.AssociationType;
2020-11-28 10:22:22 +01:00
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;
2020-11-28 14:47:43 +01:00
import com.minres.scviewer.database.tx.ITx;
import com.minres.scviewer.database.tx.ITxAttribute;
import com.minres.scviewer.database.tx.ITxRelation;
2015-01-06 17:14:16 +01:00
public class Tx implements ITx {
private IDatabase database;
2015-01-06 17:14:16 +01:00
private TxStream trStream;
private TxGenerator trGenerator;
private ScvTx scvTx;
2015-01-06 17:14:16 +01:00
private List<ITxAttribute> attributes;
2021-02-27 14:59:00 +01:00
private long begin=-1;
private long end=-1;
private List<ITxRelation> incoming;
private List<ITxRelation> outgoing;
public Tx(IDatabase database, TxStream trStream, TxGenerator trGenerator, ScvTx scvTx) {
this.database=database;
this.trStream=trStream;
this.trGenerator=trGenerator;
this.scvTx=scvTx;
}
@Override
2021-02-27 14:59:00 +01:00
public long getId() {
return (long) scvTx.getId();
}
@Override
2020-11-28 10:22:22 +01:00
public IWaveform getStream() {
return trStream;
}
@Override
2021-01-14 23:55:47 +01:00
public IWaveform getGenerator() {
return trGenerator;
}
2021-01-14 23:13:11 +01:00
int getConcurrencyIndex() {
2015-01-20 18:50:15 +01:00
return scvTx.getConcurrencyLevel();
}
@Override
2021-02-27 14:59:00 +01:00
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()){
2015-01-20 18:50:15 +01:00
begin= scvEvent.getTime()*(Long)database.getData("TIMERESOLUTION");
}
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
2021-02-18 07:32:14 +01:00
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
}
}
return begin;
}
@Override
2021-02-27 14:59:00 +01:00
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()){
2015-01-20 18:50:15 +01:00
end = scvEvent.getTime()*(Long)database.getData("TIMERESOLUTION");
}
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
2021-02-18 07:32:14 +01:00
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
}
}
return end;
}
@Override
2015-01-06 17:14:16 +01:00
public List<ITxAttribute> getAttributes() {
if(attributes==null){
SQLiteDatabaseSelectHandler<ScvTxAttribute> handler = new SQLiteDatabaseSelectHandler<>(
ScvTxAttribute.class, database, "tx="+scvTx.getId());
try {
attributes = new ArrayList<>();
for(ScvTxAttribute scvAttribute:handler.selectObjects()){
2015-01-06 17:14:16 +01:00
attributes.add(new TxAttribute(this, scvAttribute));
}
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
2021-02-18 07:32:14 +01:00
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
}
}
return attributes;
}
@Override
2015-01-06 17:14:16 +01:00
public Collection<ITxRelation> getIncomingRelations() {
if(incoming==null){
SQLiteDatabaseSelectHandler<ScvTxRelation> handler = new SQLiteDatabaseSelectHandler<>(
ScvTxRelation.class, database, "sink="+scvTx.getId());
try {
incoming = new ArrayList<>();
for(ScvTxRelation scvRelation:handler.selectObjects()){
incoming.add(createRelation(scvRelation, false));
}
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
2021-02-18 07:32:14 +01:00
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
}
}
return incoming;
}
@Override
2015-01-06 17:14:16 +01:00
public Collection<ITxRelation> getOutgoingRelations() {
if(outgoing==null){
SQLiteDatabaseSelectHandler<ScvTxRelation> handler = new SQLiteDatabaseSelectHandler<>(
ScvTxRelation.class, database, "src="+scvTx.getId());
try {
outgoing = new ArrayList<>();
for(ScvTxRelation scvRelation:handler.selectObjects()){
outgoing.add(createRelation(scvRelation, true));
}
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
2021-02-18 07:32:14 +01:00
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
}
}
return outgoing;
}
2015-01-06 17:14:16 +01:00
private ITxRelation createRelation(ScvTxRelation rel, boolean outgoing) {
int otherId = outgoing?rel.getSink():rel.getSrc();
SQLiteDatabaseSelectHandler<ScvTx> handler = new SQLiteDatabaseSelectHandler<>(ScvTx.class, database,
"id="+otherId);
try {
List<ScvTx> res = handler.selectObjects();
if(res.size()!=1) return null;
List<ScvStream> streams = new SQLiteDatabaseSelectHandler<ScvStream>(ScvStream.class, database,
"id="+res.get(0).getStream()).selectObjects();
if(streams.size()!=1) return null;
2021-01-02 17:02:05 +01:00
TxStream tgtStream = (TxStream) database.getWaveformDb().getStreamByName(streams.get(0).getName());
Tx that = (Tx) tgtStream.getTransactions().get(otherId);
if(outgoing)
return new TxRelation(trStream.getRelationType(rel.getName()), this, that);
else
return new TxRelation(trStream.getRelationType(rel.getName()), that, this);
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
2021-02-18 07:32:14 +01:00
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
2015-01-09 09:16:40 +01:00
@Override
public int compareTo(ITx o) {
2021-02-27 14:59:00 +01:00
int res = Long.compare(this.getBeginTime(), o.getBeginTime());
if(res!=0)
return res;
else
2021-02-27 14:59:00 +01:00
return Long.compare(this.getId(), o.getId());
2015-01-09 09:16:40 +01:00
}
@Override
public String toString() {
return "tx#"+getId()+"["+getBeginTime()/1000000+"ns - "+getEndTime()/1000000+"ns]";
}
}