Unified databases so that signals and transactions can be loaded into
same database
This commit is contained in:
@ -1,36 +0,0 @@
|
||||
package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
import com.minres.scviewer.database.IWaveformDb;
|
||||
import com.minres.scviewer.database.IWaveformDbFactory;
|
||||
|
||||
public class SQLiteDbFactory implements IWaveformDbFactory {
|
||||
|
||||
private byte[] x = "SQLite format 3".getBytes();
|
||||
|
||||
public SQLiteDbFactory() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IWaveformDb createDatabase(File file) {
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
byte[] buffer = new byte[x.length];
|
||||
int read = fis.read(buffer, 0, x.length);
|
||||
fis.close();
|
||||
if (read == x.length)
|
||||
for (int i = 0; i < x.length; i++)
|
||||
if (buffer[i] != x[i])
|
||||
return null;
|
||||
SQLiteDb db = new SQLiteDb();
|
||||
db.load(file);
|
||||
return db;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -2,6 +2,7 @@ package com.minres.scviewer.database.sqlite;
|
||||
|
||||
import java.beans.IntrospectionException;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
@ -9,12 +10,9 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.minres.scviewer.database.EventTime;
|
||||
import com.minres.scviewer.database.HierNode;
|
||||
import com.minres.scviewer.database.IWaveformDb;
|
||||
import com.minres.scviewer.database.IHierNode;
|
||||
import com.minres.scviewer.database.ITxStream;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.InputFormatException;
|
||||
import com.minres.scviewer.database.IWaveformDb;
|
||||
import com.minres.scviewer.database.IWaveformDbLoader;
|
||||
import com.minres.scviewer.database.RelationType;
|
||||
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
||||
import com.minres.scviewer.database.sqlite.db.SQLiteDatabase;
|
||||
@ -23,7 +21,7 @@ import com.minres.scviewer.database.sqlite.tables.ScvSimProps;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTxEvent;
|
||||
|
||||
public class SQLiteDb extends HierNode implements IWaveformDb {
|
||||
public class SQLiteDbLoader implements IWaveformDbLoader {
|
||||
|
||||
protected IDatabase database;
|
||||
|
||||
@ -32,13 +30,10 @@ public class SQLiteDb extends HierNode implements IWaveformDb {
|
||||
long timeResolution=1;
|
||||
|
||||
private HashMap<String, RelationType> relationMap = new HashMap<String, RelationType>();
|
||||
|
||||
private IWaveformDb db;
|
||||
|
||||
IDatabase getDb(){
|
||||
return database;
|
||||
}
|
||||
|
||||
public SQLiteDb() {
|
||||
super("SQLiteDb");
|
||||
public SQLiteDbLoader() {
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -63,7 +58,7 @@ public class SQLiteDb extends HierNode implements IWaveformDb {
|
||||
streams=new ArrayList<IWaveform>();
|
||||
try {
|
||||
for(ScvStream scvStream:handler.selectObjects()){
|
||||
streams.add(new TxStream(this, scvStream));
|
||||
streams.add(new TxStream(database, db, scvStream));
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
@ -73,77 +68,34 @@ public class SQLiteDb extends HierNode implements IWaveformDb {
|
||||
return streams;
|
||||
}
|
||||
|
||||
private byte[] x = "SQLite format 3".getBytes();
|
||||
|
||||
@Override
|
||||
public void load(File file) throws InputFormatException {
|
||||
public boolean load(IWaveformDb db, File file) throws Exception {
|
||||
this.db=db;
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
byte[] buffer = new byte[x.length];
|
||||
int read = fis.read(buffer, 0, x.length);
|
||||
fis.close();
|
||||
if (read == x.length)
|
||||
for (int i = 0; i < x.length; i++)
|
||||
if (buffer[i] != x[i]) return false;
|
||||
|
||||
database=new SQLiteDatabase(file.getAbsolutePath());
|
||||
database.setData("TIMERESOLUTION", 1L);
|
||||
SQLiteDatabaseSelectHandler<ScvSimProps> handler = new SQLiteDatabaseSelectHandler<ScvSimProps>(ScvSimProps.class, database);
|
||||
try {
|
||||
for(ScvSimProps scvSimProps:handler.selectObjects()){
|
||||
timeResolution=scvSimProps.getTime_resolution();
|
||||
database.setData("TIMERESOLUTION", scvSimProps.getTime_resolution());
|
||||
}
|
||||
return true;
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
buildHierarchyNodes();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
database=null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IWaveform getStreamByName(String name) {
|
||||
for (IWaveform n : getAllWaves())
|
||||
if (n.getFullName().equals(name))
|
||||
return n;
|
||||
return null;
|
||||
}
|
||||
|
||||
public ITxStream getStreamById(long id) {
|
||||
for (IWaveform n : getAllWaves())
|
||||
if (n.getId().equals(id))
|
||||
return (ITxStream) n;
|
||||
return null;
|
||||
}
|
||||
|
||||
private void buildHierarchyNodes() throws InputFormatException{
|
||||
for(IWaveform stream:getAllWaves()){
|
||||
String[] hier = stream.getFullName().split("\\.");
|
||||
IHierNode node = this;
|
||||
for(String name:hier){
|
||||
IHierNode n1 = null;
|
||||
for (IHierNode n : node.getChildNodes()) {
|
||||
if (n.getName().equals(name)) {
|
||||
n1=n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(name == hier[hier.length-1]){ //leaf
|
||||
if(n1!=null) {
|
||||
if(n1 instanceof HierNode){
|
||||
node.getChildNodes().remove(n1);
|
||||
stream.getChildNodes().addAll(n1.getChildNodes());
|
||||
} else {
|
||||
throw new InputFormatException();
|
||||
}
|
||||
}
|
||||
stream.setName(name);
|
||||
node.getChildNodes().add(stream);
|
||||
node=stream;
|
||||
} else { // intermediate
|
||||
if(n1 != null) {
|
||||
node=n1;
|
||||
} else {
|
||||
HierNode newNode = new HierNode(name);
|
||||
node.getChildNodes().add(newNode);
|
||||
node=newNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public RelationType getRelationType(String relationName) {
|
||||
if(relationMap.containsKey(relationName)) return relationMap.get(relationName);
|
@ -14,6 +14,7 @@ import com.minres.scviewer.database.ITxGenerator;
|
||||
import com.minres.scviewer.database.ITxRelation;
|
||||
import com.minres.scviewer.database.ITxStream;
|
||||
import com.minres.scviewer.database.ITx;
|
||||
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
||||
import com.minres.scviewer.database.sqlite.db.SQLiteDatabaseSelectHandler;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTx;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvTxAttribute;
|
||||
@ -22,6 +23,7 @@ import com.minres.scviewer.database.sqlite.tables.ScvTxRelation;
|
||||
|
||||
public class Tx implements ITx {
|
||||
|
||||
private IDatabase database;
|
||||
private TxStream trStream;
|
||||
private TxGenerator trGenerator;
|
||||
private ScvTx scvTx;
|
||||
@ -29,7 +31,8 @@ public class Tx implements ITx {
|
||||
private EventTime begin, end;
|
||||
private List<ITxRelation> incoming, outgoing;
|
||||
|
||||
public Tx(TxStream trStream, TxGenerator trGenerator, ScvTx scvTx) {
|
||||
public Tx(IDatabase database, TxStream trStream, TxGenerator trGenerator, ScvTx scvTx) {
|
||||
this.database=database;
|
||||
this.trStream=trStream;
|
||||
this.trGenerator=trGenerator;
|
||||
this.scvTx=scvTx;
|
||||
@ -54,10 +57,10 @@ public class Tx implements ITx {
|
||||
public EventTime getBeginTime() {
|
||||
if(begin==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<ScvTxEvent>(ScvTxEvent.class,
|
||||
trStream.getDb().getDb(), "tx="+scvTx.getId()+" AND type="+ AssociationType.BEGIN.ordinal());
|
||||
database, "tx="+scvTx.getId()+" AND type="+ AssociationType.BEGIN.ordinal());
|
||||
try {
|
||||
for(ScvTxEvent scvEvent:handler.selectObjects()){
|
||||
begin= new EventTime(scvEvent.getTime()*trStream.getDb().timeResolution);
|
||||
begin= new EventTime(scvEvent.getTime()*(Long)database.getData("TIMERESOLUTION"));
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
@ -70,10 +73,10 @@ public class Tx implements ITx {
|
||||
public EventTime getEndTime() {
|
||||
if(end==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxEvent> handler = new SQLiteDatabaseSelectHandler<ScvTxEvent>(ScvTxEvent.class,
|
||||
trStream.getDb().getDb(), "tx="+scvTx.getId()+" AND type="+ AssociationType.END.ordinal());
|
||||
database, "tx="+scvTx.getId()+" AND type="+ AssociationType.END.ordinal());
|
||||
try {
|
||||
for(ScvTxEvent scvEvent:handler.selectObjects()){
|
||||
end = new EventTime(scvEvent.getTime()*trStream.getDb().timeResolution);
|
||||
end = new EventTime(scvEvent.getTime()*(Long)database.getData("TIMERESOLUTION"));
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
@ -86,7 +89,7 @@ public class Tx implements ITx {
|
||||
public List<ITxAttribute> getAttributes() {
|
||||
if(attributes==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxAttribute> handler = new SQLiteDatabaseSelectHandler<ScvTxAttribute>(
|
||||
ScvTxAttribute.class, trStream.getDb().getDb(), "tx="+scvTx.getId());
|
||||
ScvTxAttribute.class, database, "tx="+scvTx.getId());
|
||||
try {
|
||||
attributes = new ArrayList<ITxAttribute>();
|
||||
for(ScvTxAttribute scvAttribute:handler.selectObjects()){
|
||||
@ -104,7 +107,7 @@ public class Tx implements ITx {
|
||||
public Collection<ITxRelation> getIncomingRelations() {
|
||||
if(incoming==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxRelation> handler = new SQLiteDatabaseSelectHandler<ScvTxRelation>(
|
||||
ScvTxRelation.class, trStream.getDb().getDb(), "sink="+scvTx.getId());
|
||||
ScvTxRelation.class, database, "sink="+scvTx.getId());
|
||||
try {
|
||||
incoming = new ArrayList<ITxRelation>();
|
||||
for(ScvTxRelation scvRelation:handler.selectObjects()){
|
||||
@ -121,7 +124,7 @@ public class Tx implements ITx {
|
||||
public Collection<ITxRelation> getOutgoingRelations() {
|
||||
if(outgoing==null){
|
||||
SQLiteDatabaseSelectHandler<ScvTxRelation> handler = new SQLiteDatabaseSelectHandler<ScvTxRelation>(
|
||||
ScvTxRelation.class, trStream.getDb().getDb(), "src="+scvTx.getId());
|
||||
ScvTxRelation.class, database, "src="+scvTx.getId());
|
||||
try {
|
||||
outgoing = new ArrayList<ITxRelation>();
|
||||
for(ScvTxRelation scvRelation:handler.selectObjects()){
|
||||
@ -136,8 +139,9 @@ public class Tx implements ITx {
|
||||
|
||||
private ITxRelation createRelation(ScvTxRelation rel, boolean outgoing) {
|
||||
long otherId = outgoing?rel.getSink():rel.getSrc();
|
||||
/*FIXME:
|
||||
try {
|
||||
List<ScvTx> scvTx=new SQLiteDatabaseSelectHandler<ScvTx>(ScvTx.class, trStream.getDb().getDb(), "id="+otherId).selectObjects();
|
||||
List<ScvTx> scvTx=new SQLiteDatabaseSelectHandler<ScvTx>(ScvTx.class, database, "id="+otherId).selectObjects();
|
||||
assert(scvTx.size()==1);
|
||||
ITxStream stream = trStream.getDb().getStreamById(scvTx.get(0).getStream());
|
||||
Tx that=(Tx) stream.getTransactionById(otherId);
|
||||
@ -147,7 +151,7 @@ public class Tx implements ITx {
|
||||
return new TxRelation(trStream.getDb().getRelationType(rel.getName()), that, this);
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
}
|
||||
}*/
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,8 @@ import com.minres.scviewer.database.HierNode;
|
||||
import com.minres.scviewer.database.ITxGenerator;
|
||||
import com.minres.scviewer.database.ITxStream;
|
||||
import com.minres.scviewer.database.ITx;
|
||||
import com.minres.scviewer.database.IWaveformDb;
|
||||
import com.minres.scviewer.database.sqlite.db.IDatabase;
|
||||
import com.minres.scviewer.database.sqlite.db.SQLiteDatabaseSelectHandler;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvGenerator;
|
||||
import com.minres.scviewer.database.sqlite.tables.ScvStream;
|
||||
@ -20,9 +22,11 @@ import com.minres.scviewer.database.sqlite.tables.ScvTx;
|
||||
|
||||
public class TxStream extends HierNode implements ITxStream {
|
||||
|
||||
private IDatabase database;
|
||||
|
||||
private String fullName;
|
||||
|
||||
private SQLiteDb db;
|
||||
private IWaveformDb db;
|
||||
|
||||
private ScvStream scvStream;
|
||||
|
||||
@ -30,11 +34,17 @@ public class TxStream extends HierNode implements ITxStream {
|
||||
|
||||
private NavigableSet<ITx> transactions;
|
||||
|
||||
public TxStream(SQLiteDb trSQLiteDb, ScvStream scvStream) {
|
||||
public TxStream(IDatabase database, IWaveformDb waveformDb, ScvStream scvStream) {
|
||||
super(scvStream.getName());
|
||||
this.database=database;
|
||||
fullName=scvStream.getName();
|
||||
this.scvStream=scvStream;
|
||||
db=trSQLiteDb;
|
||||
db=waveformDb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IWaveformDb getDb() {
|
||||
return db;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -52,16 +62,11 @@ public class TxStream extends HierNode implements ITxStream {
|
||||
return scvStream.getKind();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLiteDb getDb() {
|
||||
return db;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ITxGenerator> getGenerators() {
|
||||
if(generators==null){
|
||||
SQLiteDatabaseSelectHandler<ScvGenerator> handler = new SQLiteDatabaseSelectHandler<ScvGenerator>(
|
||||
ScvGenerator.class, db.getDb(), "stream="+scvStream.getId());
|
||||
ScvGenerator.class, database, "stream="+scvStream.getId());
|
||||
generators=new HashMap<Integer, TxGenerator>();
|
||||
try {
|
||||
for(ScvGenerator scvGenerator:handler.selectObjects()){
|
||||
@ -94,12 +99,12 @@ public class TxStream extends HierNode implements ITxStream {
|
||||
protected void checkTransactions() {
|
||||
if(transactions==null){
|
||||
if(generators==null) getGenerators();
|
||||
SQLiteDatabaseSelectHandler<ScvTx> handler = new SQLiteDatabaseSelectHandler<ScvTx>(ScvTx.class, db.getDb(),
|
||||
SQLiteDatabaseSelectHandler<ScvTx> handler = new SQLiteDatabaseSelectHandler<ScvTx>(ScvTx.class, database,
|
||||
"stream="+scvStream.getId());
|
||||
transactions=new TreeSet<ITx>();
|
||||
try {
|
||||
for(ScvTx scvTx:handler.selectObjects()){
|
||||
transactions.add(new Tx(this, generators.get(scvTx.getGenerator()), scvTx));
|
||||
transactions.add(new Tx(database, this, generators.get(scvTx.getGenerator()), scvTx));
|
||||
}
|
||||
} catch (SecurityException | IllegalArgumentException | InstantiationException | IllegalAccessException
|
||||
| InvocationTargetException | SQLException | IntrospectionException e) {
|
||||
|
@ -45,4 +45,7 @@ public interface IDatabase {
|
||||
*/
|
||||
public void close(PreparedStatement preparedStatement, Connection connection);
|
||||
|
||||
public void setData(String name, Object value);
|
||||
|
||||
public Object getData(String name);
|
||||
}
|
@ -6,14 +6,18 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class SQLiteDatabase implements IDatabase {
|
||||
|
||||
protected String dbFileName;
|
||||
|
||||
protected HashMap<String, Object> props;
|
||||
|
||||
public SQLiteDatabase(String dbFileName) {
|
||||
super();
|
||||
this.dbFileName = dbFileName;
|
||||
props = new HashMap<String, Object>();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -50,4 +54,14 @@ public class SQLiteDatabase implements IDatabase {
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setData(String name, Object value){
|
||||
props.put(name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getData(String name){
|
||||
return props.get(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user