2015-01-21 21:58:35 +01:00
|
|
|
/*******************************************************************************
|
2015-10-22 00:25:12 +02:00
|
|
|
* Copyright (c) 2015 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.List;
|
2015-01-21 21:58:35 +01:00
|
|
|
import java.util.Map;
|
2015-01-20 18:50:15 +01:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.NavigableMap;
|
2015-01-21 21:58:35 +01:00
|
|
|
import java.util.TreeMap;
|
2015-01-03 16:34:32 +01:00
|
|
|
|
2020-11-28 10:22:22 +01:00
|
|
|
import com.minres.scviewer.database.EventKind;
|
2015-01-06 17:14:16 +01:00
|
|
|
import com.minres.scviewer.database.HierNode;
|
2020-11-28 10:22:22 +01:00
|
|
|
import com.minres.scviewer.database.IEvent;
|
2015-01-20 18:50:15 +01:00
|
|
|
import com.minres.scviewer.database.ITx;
|
2015-01-06 17:14:16 +01:00
|
|
|
import com.minres.scviewer.database.ITxGenerator;
|
2015-10-22 00:02:58 +02:00
|
|
|
import com.minres.scviewer.database.IWaveform;
|
2015-01-10 00:23:46 +01:00
|
|
|
import com.minres.scviewer.database.IWaveformDb;
|
2015-01-21 21:58:35 +01:00
|
|
|
import com.minres.scviewer.database.RelationType;
|
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;
|
|
|
|
import com.minres.scviewer.database.sqlite.tables.ScvGenerator;
|
|
|
|
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
|
|
|
import com.minres.scviewer.database.sqlite.tables.ScvTx;
|
|
|
|
|
2020-11-28 10:22:22 +01:00
|
|
|
public class TxStream extends HierNode implements IWaveform {
|
2015-01-03 16:34:32 +01:00
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
private IDatabase database;
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
private String fullName;
|
2015-01-06 17:14:16 +01:00
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
private IWaveformDb db;
|
2015-01-06 17:14:16 +01:00
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
private ScvStream scvStream;
|
|
|
|
|
2015-01-21 21:58:35 +01:00
|
|
|
private TreeMap<Integer, TxGenerator> generators;
|
2015-01-06 17:14:16 +01:00
|
|
|
|
2015-01-21 21:58:35 +01:00
|
|
|
private TreeMap<Integer, ITx> transactions;
|
2015-01-20 18:50:15 +01:00
|
|
|
|
|
|
|
private Integer maxConcurrency;
|
|
|
|
|
2020-11-28 10:22:22 +01:00
|
|
|
private TreeMap<Long, IEvent[]> events;
|
2015-01-21 21:58:35 +01:00
|
|
|
|
2015-11-15 22:15:37 +01:00
|
|
|
private List<RelationType> usedRelationsList;
|
2015-01-03 16:34:32 +01:00
|
|
|
|
2015-01-10 00:23:46 +01:00
|
|
|
public TxStream(IDatabase database, IWaveformDb waveformDb, ScvStream scvStream) {
|
2015-01-03 16:34:32 +01:00
|
|
|
super(scvStream.getName());
|
2015-01-10 00:23:46 +01:00
|
|
|
this.database=database;
|
2015-01-03 16:34:32 +01:00
|
|
|
fullName=scvStream.getName();
|
|
|
|
this.scvStream=scvStream;
|
2015-01-10 00:23:46 +01:00
|
|
|
db=waveformDb;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IWaveformDb getDb() {
|
|
|
|
return db;
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getFullName() {
|
|
|
|
return fullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Long getId() {
|
|
|
|
return (long) scvStream.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getKind() {
|
|
|
|
return scvStream.getKind();
|
|
|
|
}
|
|
|
|
|
2015-01-06 17:14:16 +01:00
|
|
|
public List<ITxGenerator> getGenerators() {
|
2015-01-03 16:34:32 +01:00
|
|
|
if(generators==null){
|
|
|
|
SQLiteDatabaseSelectHandler<ScvGenerator> handler = new SQLiteDatabaseSelectHandler<ScvGenerator>(
|
2015-01-10 00:23:46 +01:00
|
|
|
ScvGenerator.class, database, "stream="+scvStream.getId());
|
2015-01-21 21:58:35 +01:00
|
|
|
generators=new TreeMap<Integer, TxGenerator>();
|
2015-01-03 16:34:32 +01:00
|
|
|
try {
|
|
|
|
for(ScvGenerator scvGenerator:handler.selectObjects()){
|
2015-01-06 17:14:16 +01:00
|
|
|
generators.put(scvGenerator.getId(), new TxGenerator(this, scvGenerator));
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 17:14:16 +01:00
|
|
|
return new ArrayList<ITxGenerator>(generators.values());
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-01-20 18:50:15 +01:00
|
|
|
public int getMaxConcurrency() {
|
|
|
|
if(maxConcurrency==null){
|
|
|
|
java.sql.Connection connection=null;
|
|
|
|
java.sql.Statement statement=null;
|
|
|
|
java.sql.ResultSet resultSet=null;
|
|
|
|
try {
|
|
|
|
connection = database.createConnection();
|
|
|
|
statement = connection.createStatement();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append("SELECT MAX(concurrencyLevel) as concurrencyLevel FROM ScvTx where stream=");
|
|
|
|
sb.append(scvStream.getId());
|
|
|
|
resultSet = statement.executeQuery(sb.toString());
|
|
|
|
while (resultSet.next()) {
|
2018-09-26 21:25:02 +02:00
|
|
|
if(maxConcurrency==null) maxConcurrency=0;
|
2015-01-20 18:50:15 +01:00
|
|
|
Object value = resultSet.getObject("concurrencyLevel");
|
|
|
|
if(value!=null)
|
|
|
|
maxConcurrency=(Integer) value;
|
|
|
|
}
|
|
|
|
} catch (SQLException e) {
|
|
|
|
if(maxConcurrency==null) maxConcurrency=0;
|
|
|
|
} finally {
|
|
|
|
try{
|
|
|
|
if(resultSet!=null) resultSet.close();
|
|
|
|
if(statement!=null) statement.close();
|
|
|
|
if(connection!=null) connection.close();
|
|
|
|
} catch (SQLException e) { }
|
|
|
|
}
|
|
|
|
maxConcurrency+=1;
|
|
|
|
}
|
|
|
|
return maxConcurrency;
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-11-28 10:22:22 +01:00
|
|
|
public NavigableMap<Long, IEvent[]> getEvents(){
|
2015-01-20 18:50:15 +01:00
|
|
|
if(events==null){
|
2020-11-28 10:22:22 +01:00
|
|
|
events=new TreeMap<Long, IEvent[]>();
|
2015-01-20 18:50:15 +01:00
|
|
|
for(Entry<Integer, ITx> entry:getTransactions().entrySet()){
|
2020-11-28 10:22:22 +01:00
|
|
|
putEvent(new TxEvent(EventKind.BEGIN, entry.getValue()));
|
|
|
|
putEvent(new TxEvent(EventKind.END, entry.getValue()));
|
2015-01-20 18:50:15 +01:00
|
|
|
}
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
2015-01-21 21:58:35 +01:00
|
|
|
return events;
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 21:58:35 +01:00
|
|
|
private void putEvent(TxEvent ev){
|
|
|
|
Long time = ev.getTime();
|
2020-11-28 10:22:22 +01:00
|
|
|
if(events.containsKey(time)) {
|
|
|
|
IEvent[] oldV = events.get(time);
|
|
|
|
IEvent[] newV = new IEvent[oldV.length+1];
|
|
|
|
System.arraycopy(oldV, 0, newV, 0, oldV.length);
|
|
|
|
newV[oldV.length]=ev;
|
|
|
|
events.put(time, newV);
|
2015-01-21 21:58:35 +01:00
|
|
|
} else {
|
2020-11-28 10:22:22 +01:00
|
|
|
events.put(time, new IEvent[] {ev});
|
2015-01-21 21:58:35 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-20 18:50:15 +01:00
|
|
|
|
2015-01-21 21:58:35 +01:00
|
|
|
protected Map<Integer, ITx> getTransactions() {
|
2015-01-03 16:34:32 +01:00
|
|
|
if(transactions==null){
|
|
|
|
if(generators==null) getGenerators();
|
2015-01-21 21:58:35 +01:00
|
|
|
transactions = new TreeMap<Integer, ITx>();
|
2015-01-10 00:23:46 +01:00
|
|
|
SQLiteDatabaseSelectHandler<ScvTx> handler = new SQLiteDatabaseSelectHandler<ScvTx>(ScvTx.class, database,
|
2015-01-03 16:34:32 +01:00
|
|
|
"stream="+scvStream.getId());
|
|
|
|
try {
|
|
|
|
for(ScvTx scvTx:handler.selectObjects()){
|
2015-01-20 18:50:15 +01:00
|
|
|
transactions.put(scvTx.getId(), new Tx(database, this, generators.get(scvTx.getGenerator()), scvTx));
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
|
|
|
| InvocationTargetException | SQLException | IntrospectionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 18:50:15 +01:00
|
|
|
return transactions;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-11-28 10:22:22 +01:00
|
|
|
public IEvent[] getEventsAtTime(Long time) {
|
2015-01-20 18:50:15 +01:00
|
|
|
return getEvents().get(time);
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|
|
|
|
|
2015-11-15 22:15:37 +01:00
|
|
|
public void setRelationTypeList(List<RelationType> usedRelationsList){
|
|
|
|
this.usedRelationsList=usedRelationsList;
|
2015-01-21 21:58:35 +01:00
|
|
|
}
|
2015-11-15 22:15:37 +01:00
|
|
|
|
2015-01-21 21:58:35 +01:00
|
|
|
public RelationType getRelationType(String name) {
|
2015-11-15 22:15:37 +01:00
|
|
|
RelationType relType=RelationType.create(name);
|
|
|
|
if(!usedRelationsList.contains(relType)) usedRelationsList.add(relType);
|
|
|
|
return relType;
|
2015-01-21 21:58:35 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:02:58 +02:00
|
|
|
@Override
|
2018-11-05 18:21:54 +01:00
|
|
|
public Boolean equals(IWaveform other) {
|
2020-03-13 16:01:16 +01:00
|
|
|
return(other instanceof TxStream && this.getId().equals(other.getId()));
|
2015-10-22 00:02:58 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 10:22:22 +01:00
|
|
|
@Override
|
|
|
|
public IEvent[] getEventsBeforeTime(Long time) {
|
|
|
|
Entry<Long, IEvent[]> e = events.floorEntry(time);
|
|
|
|
if(e==null)
|
|
|
|
return null;
|
|
|
|
else
|
|
|
|
return events.floorEntry(time).getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Class<?> getType() {
|
|
|
|
return TxEvent.class;
|
|
|
|
}
|
|
|
|
|
2015-01-03 16:34:32 +01:00
|
|
|
}
|