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.ITxGenerator;
|
|
|
|
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;
|
2020-11-29 10:25:48 +01:00
|
|
|
private Long begin;
|
|
|
|
private Long end;
|
|
|
|
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
|
|
|
|
public Long getId() {
|
|
|
|
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
|
2015-01-06 17:14:16 +01:00
|
|
|
public ITxGenerator getGenerator() {
|
2015-01-03 16:34:32 +01:00
|
|
|
return trGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-20 18:50:15 +01:00
|
|
|
public int getConcurrencyIndex() {
|
|
|
|
return scvTx.getConcurrencyLevel();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Long getBeginTime() {
|
2015-01-03 16:34:32 +01:00
|
|
|
if(begin==null){
|
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
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-20 18:50:15 +01:00
|
|
|
public Long getEndTime() {
|
2015-01-03 16:34:32 +01:00
|
|
|
if(end==null){
|
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
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException 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) {
|
2015-02-04 16:20:59 +01:00
|
|
|
int res = this.getBeginTime().compareTo(o.getBeginTime());
|
|
|
|
if(res!=0)
|
|
|
|
return res;
|
|
|
|
else
|
|
|
|
return this.getId().compareTo(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
|
|
|
}
|