2015-01-21 21:58:35 +01:00
|
|
|
/*******************************************************************************
|
2021-01-09 14:26:49 +01:00
|
|
|
* Copyright (c) 2015-2021 MINRES Technologies GmbH and others.
|
2015-01-21 21:58:35 +01:00
|
|
|
* 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
|
|
|
|
*******************************************************************************/
|
2015-01-03 16:34:32 +01:00
|
|
|
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;
|
2015-01-10 00:23:46 +01:00
|
|
|
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
2015-01-03 16:34:32 +01:00
|
|
|
import com.minres.scviewer.database.sqlite.db.SQLiteDatabaseSelectHandler;
|
2015-01-21 21:58:35 +01:00
|
|
|
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
2015-01-03 16:34:32 +01:00
|
|
|
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-03 16:34:32 +01:00
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public class Tx implements ITx {
|
2015-01-03 16:34:32 +01:00
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
private IDatabase database;
|
2015-01-06 17:14:16 +01:00
|
|
|
private TxStream trStream;
|
|
|
|
private TxGenerator trGenerator;
|
2015-01-03 16:34:32 +01:00
|
|
|
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;
|
2020-11-29 10:25:48 +01:00
|
|
|
private List<ITxRelation> incoming;
|
|
|
|
private List<ITxRelation> outgoing;
|
2015-01-03 16:34:32 +01:00
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
public Tx(IDatabase database, TxStream trStream, TxGenerator trGenerator, ScvTx scvTx) {
|
|
|
|
this.database=database;
|
2015-01-03 16:34:32 +01:00
|
|
|
this.trStream=trStream;
|
|
|
|
this.trGenerator=trGenerator;
|
|
|
|
this.scvTx=scvTx;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 14:59:00 +01:00
|
|
|
public long getId() {
|
2015-01-03 16:34:32 +01:00
|
|
|
return (long) scvTx.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-11-28 10:22:22 +01:00
|
|
|
public IWaveform getStream() {
|
2015-01-03 16:34:32 +01:00
|
|
|
return trStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-14 23:55:47 +01:00
|
|
|
public IWaveform getGenerator() {
|
2015-01-03 16:34:32 +01:00
|
|
|
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){
|
2020-11-29 10:25:48 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<>(ScvTxEvent.class,
|
2015-01-10 00:23:46 +01:00
|
|
|
database, "tx="+scvTx.getId()+" AND type="+ AssociationType.BEGIN.ordinal());
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
|
|
|
for(ScvTxEvent scvEvent:handler.selectObjects()){
|
2015-01-20 18:50:15 +01:00
|
|
|
begin= scvEvent.getTime()*(Long)database.getData("TIMERESOLUTION");
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
2021-02-18 07:32:14 +01:00
|
|
|
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-02-27 14:59:00 +01:00
|
|
|
public long getEndTime() {
|
|
|
|
if(end<0){
|
2020-11-29 10:25:48 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<>(ScvTxEvent.class,
|
2015-01-10 00:23:46 +01:00
|
|
|
database, "tx="+scvTx.getId()+" AND type="+ AssociationType.END.ordinal());
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
|
|
|
for(ScvTxEvent scvEvent:handler.selectObjects()){
|
2015-01-20 18:50:15 +01:00
|
|
|
end = scvEvent.getTime()*(Long)database.getData("TIMERESOLUTION");
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
2021-02-18 07:32:14 +01:00
|
|
|
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-06 17:14:16 +01:00
|
|
|
public List<ITxAttribute> getAttributes() {
|
2015-01-03 16:34:32 +01:00
|
|
|
if(attributes==null){
|
2020-11-29 10:25:48 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvTxAttribute> handler = new SQLiteDatabaseSelectHandler<>(
|
2015-01-10 00:23:46 +01:00
|
|
|
ScvTxAttribute.class, database, "tx="+scvTx.getId());
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
2020-11-29 10:25:48 +01:00
|
|
|
attributes = new ArrayList<>();
|
2015-01-03 16:34:32 +01:00
|
|
|
for(ScvTxAttribute scvAttribute:handler.selectObjects()){
|
2015-01-06 17:14:16 +01:00
|
|
|
attributes.add(new TxAttribute(this, scvAttribute));
|
2015-01-03 16:34:32 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
2021-02-18 07:32:14 +01:00
|
|
|
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-06 17:14:16 +01:00
|
|
|
public Collection<ITxRelation> getIncomingRelations() {
|
2015-01-03 16:34:32 +01:00
|
|
|
if(incoming==null){
|
2020-11-29 10:25:48 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvTxRelation> handler = new SQLiteDatabaseSelectHandler<>(
|
2015-01-10 00:23:46 +01:00
|
|
|
ScvTxRelation.class, database, "sink="+scvTx.getId());
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
2020-11-29 10:25:48 +01:00
|
|
|
incoming = new ArrayList<>();
|
2015-01-03 16:34:32 +01:00
|
|
|
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) {
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return incoming;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-06 17:14:16 +01:00
|
|
|
public Collection<ITxRelation> getOutgoingRelations() {
|
2015-01-03 16:34:32 +01:00
|
|
|
if(outgoing==null){
|
2020-11-29 10:25:48 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvTxRelation> handler = new SQLiteDatabaseSelectHandler<>(
|
2015-01-10 00:23:46 +01:00
|
|
|
ScvTxRelation.class, database, "src="+scvTx.getId());
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
2020-11-29 10:25:48 +01:00
|
|
|
outgoing = new ArrayList<>();
|
2015-01-03 16:34:32 +01:00
|
|
|
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) {
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return outgoing;
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
private ITxRelation createRelation(ScvTxRelation rel, boolean outgoing) {
|
2015-01-21 21:58:35 +01:00
|
|
|
int otherId = outgoing?rel.getSink():rel.getSrc();
|
2020-11-29 10:25:48 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvTx> handler = new SQLiteDatabaseSelectHandler<>(ScvTx.class, database,
|
2015-01-21 21:58:35 +01:00
|
|
|
"id="+otherId);
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
2015-01-21 21:58:35 +01:00
|
|
|
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());
|
2015-01-21 21:58:35 +01:00
|
|
|
Tx that = (Tx) tgtStream.getTransactions().get(otherId);
|
2015-01-03 16:34:32 +01:00
|
|
|
if(outgoing)
|
2015-01-21 21:58:35 +01:00
|
|
|
return new TxRelation(trStream.getRelationType(rel.getName()), this, that);
|
2015-01-03 16:34:32 +01:00
|
|
|
else
|
2015-01-21 21:58:35 +01:00
|
|
|
return new TxRelation(trStream.getRelationType(rel.getName()), that, this);
|
2015-01-03 16:34:32 +01:00
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
2021-02-18 07:32:14 +01:00
|
|
|
| InvocationTargetException | SQLException | IntrospectionException | NoSuchMethodException e) {
|
2015-01-21 21:58:35 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
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());
|
2015-02-04 16:20:59 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-21 21:58:35 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2015-10-27 23:39:33 +01:00
|
|
|
return "tx#"+getId()+"["+getBeginTime()/1000000+"ns - "+getEndTime()/1000000+"ns]";
|
2015-01-21 21:58:35 +01:00
|
|
|
}
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|