diff --git a/com.minres.scviewer.database.leveldb/.classpath b/com.minres.scviewer.database.leveldb/.classpath new file mode 100644 index 0000000..76e4599 --- /dev/null +++ b/com.minres.scviewer.database.leveldb/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/com.minres.scviewer.database.leveldb/.gitignore b/com.minres.scviewer.database.leveldb/.gitignore new file mode 100644 index 0000000..00a0994 --- /dev/null +++ b/com.minres.scviewer.database.leveldb/.gitignore @@ -0,0 +1,4 @@ +/json-20180813-sources.jar +/leveldb-0.11-SNAPSHOT-sources.jar +/bin/ +/target/ diff --git a/com.minres.scviewer.database.leveldb/.project b/com.minres.scviewer.database.leveldb/.project new file mode 100644 index 0000000..9764d8d --- /dev/null +++ b/com.minres.scviewer.database.leveldb/.project @@ -0,0 +1,33 @@ + + + com.minres.scviewer.database.leveldb + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + org.eclipse.pde.ds.core.builder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/com.minres.scviewer.database.leveldb/.settings/org.eclipse.jdt.core.prefs b/com.minres.scviewer.database.leveldb/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..0c68a61 --- /dev/null +++ b/com.minres.scviewer.database.leveldb/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,7 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/com.minres.scviewer.database.leveldb/.settings/org.eclipse.pde.core.prefs b/com.minres.scviewer.database.leveldb/.settings/org.eclipse.pde.core.prefs new file mode 100644 index 0000000..e8ff8be --- /dev/null +++ b/com.minres.scviewer.database.leveldb/.settings/org.eclipse.pde.core.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +pluginProject.equinox=false +pluginProject.extensions=false +resolve.requirebundle=false diff --git a/com.minres.scviewer.database.leveldb/.settings/org.eclipse.pde.ds.annotations.prefs b/com.minres.scviewer.database.leveldb/.settings/org.eclipse.pde.ds.annotations.prefs new file mode 100644 index 0000000..c61da6e --- /dev/null +++ b/com.minres.scviewer.database.leveldb/.settings/org.eclipse.pde.ds.annotations.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +enabled=true +path=OSGI-INF +validationErrorLevel=error +validationErrorLevel.missingImplicitUnbindMethod=error diff --git a/com.minres.scviewer.database.leveldb/META-INF/MANIFEST.MF b/com.minres.scviewer.database.leveldb/META-INF/MANIFEST.MF new file mode 100644 index 0000000..7e68178 --- /dev/null +++ b/com.minres.scviewer.database.leveldb/META-INF/MANIFEST.MF @@ -0,0 +1,18 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Leveldb +Bundle-SymbolicName: com.minres.scviewer.database.leveldb +Bundle-Version: 1.0.0.qualifier +Bundle-Vendor: MINRES Technologies GmbH +Bundle-RequiredExecutionEnvironment: JavaSE-1.8 +Import-Package: org.osgi.framework;version="1.3.0" +Automatic-Module-Name: com.minres.scviewer.database.leveldb +Service-Component: OSGI-INF/*.xml +Require-Bundle: com.minres.scviewer.database;bundle-version="1.0.0", + org.eclipse.equinox.util;bundle-version="1.0.500", + org.eclipse.equinox.ds;bundle-version="1.4.200", + org.eclipse.osgi.services;bundle-version="3.4.0" +Bundle-ActivationPolicy: lazy +Bundle-ClassPath: leveldb-0.11-SNAPSHOT-uber.jar, + ., + json-20180813.jar diff --git a/com.minres.scviewer.database.leveldb/OSGI-INF/component.xml b/com.minres.scviewer.database.leveldb/OSGI-INF/component.xml new file mode 100644 index 0000000..d05f80b --- /dev/null +++ b/com.minres.scviewer.database.leveldb/OSGI-INF/component.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/com.minres.scviewer.database.leveldb/build.properties b/com.minres.scviewer.database.leveldb/build.properties new file mode 100644 index 0000000..7c7792b --- /dev/null +++ b/com.minres.scviewer.database.leveldb/build.properties @@ -0,0 +1,7 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + OSGI-INF/,\ + leveldb-0.11-SNAPSHOT-uber.jar,\ + json-20180813.jar diff --git a/com.minres.scviewer.database.leveldb/json-20180813-sources.jar b/com.minres.scviewer.database.leveldb/json-20180813-sources.jar new file mode 100644 index 0000000..59c52db Binary files /dev/null and b/com.minres.scviewer.database.leveldb/json-20180813-sources.jar differ diff --git a/com.minres.scviewer.database.leveldb/json-20180813.jar b/com.minres.scviewer.database.leveldb/json-20180813.jar new file mode 100644 index 0000000..592f08c Binary files /dev/null and b/com.minres.scviewer.database.leveldb/json-20180813.jar differ diff --git a/com.minres.scviewer.database.leveldb/leveldb-0.11-SNAPSHOT-sources.jar b/com.minres.scviewer.database.leveldb/leveldb-0.11-SNAPSHOT-sources.jar new file mode 100644 index 0000000..f9a10ee Binary files /dev/null and b/com.minres.scviewer.database.leveldb/leveldb-0.11-SNAPSHOT-sources.jar differ diff --git a/com.minres.scviewer.database.leveldb/leveldb-0.11-SNAPSHOT-uber.jar b/com.minres.scviewer.database.leveldb/leveldb-0.11-SNAPSHOT-uber.jar new file mode 100644 index 0000000..2bb4528 Binary files /dev/null and b/com.minres.scviewer.database.leveldb/leveldb-0.11-SNAPSHOT-uber.jar differ diff --git a/com.minres.scviewer.database.leveldb/pom.xml b/com.minres.scviewer.database.leveldb/pom.xml new file mode 100644 index 0000000..88fc3e4 --- /dev/null +++ b/com.minres.scviewer.database.leveldb/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + com.minres.scviewer.database.leveldb + + com.minres.scviewer + com.minres.scviewer.parent + 1.0.0-SNAPSHOT + ../com.minres.scviewer.parent + + eclipse-plugin + + + + org.eclipse.tycho + tycho-compiler-plugin + 0.23.1 + + groovy-eclipse-compiler + + + 1.7 + 1.7 + + + + org.codehaus.groovy + groovy-eclipse-compiler + ${groovy-eclipse-compiler-version} + + + org.codehaus.groovy + groovy-eclipse-batch + ${groovy-eclipse-batch-version} + + + + + + + + 1.0.1-SNAPSHOT + \ No newline at end of file diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/LevelDBLoader.java b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/LevelDBLoader.java new file mode 100644 index 0000000..4e7986e --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/LevelDBLoader.java @@ -0,0 +1,85 @@ +package com.minres.scviewer.database.leveldb; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map.Entry; + +import org.iq80.leveldb.Options; +import org.iq80.leveldb.impl.SeekingIterator; +import org.json.JSONObject; + +import com.minres.scviewer.database.IWaveform; +import com.minres.scviewer.database.IWaveformDb; +import com.minres.scviewer.database.IWaveformDbLoader; +import com.minres.scviewer.database.IWaveformEvent; +import com.minres.scviewer.database.RelationType; + +public class LevelDBLoader implements IWaveformDbLoader { + + static byte[] toByteArray(String value) { + return value.getBytes(UTF_8); + } + + private StringDBWrapper levelDb; + private IWaveformDb db; + private Long maxTime=null; + private List usedRelationsList = new ArrayList<>(); + + @Override + public boolean load(IWaveformDb db, File inp) throws Exception { + try { + this.db=db; + levelDb = new StringDBWrapper(new Options(), inp); + } catch(Exception e) { + return false; + } + return true; + } + + @Override + public Long getMaxTime() { + if(maxTime==null) { + SeekingIterator it = levelDb.iterator(); + it.seek("st~"); + Entry val = null; + while(it.hasNext()) { + Entry v = it.next(); + if(!v.getKey().startsWith("st~")) continue; + val=v; + } + if(val==null) + maxTime = 0L; + else { + String[] token = val.getKey().split("~"); + maxTime = Long.parseLong(token[2], 16); + } + } + return maxTime; + } + + @Override + public List> getAllWaves() { + List> streams=new ArrayList>(); + SeekingIterator it = levelDb.iterator(); + it.seek("s~"); + while(it.hasNext()) { + Entry val = it.next(); + if(!val.getKey().startsWith("s~")) break; + TxStream stream = new TxStream(levelDb, db, new JSONObject(val.getValue())); + stream.setRelationTypeList(usedRelationsList); + streams.add(stream); + } + return streams; + } + + @Override + public Collection getAllRelationTypes() { +// return Collections.emptyList(); + return usedRelationsList; + } + +} diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/StringDBWrapper.java b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/StringDBWrapper.java new file mode 100644 index 0000000..bc742de --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/StringDBWrapper.java @@ -0,0 +1,79 @@ +package com.minres.scviewer.database.leveldb; + +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.File; +import java.io.IOException; + +import org.iq80.leveldb.Options; +import org.iq80.leveldb.Range; +import org.iq80.leveldb.ReadOptions; +import org.iq80.leveldb.Snapshot; +import org.iq80.leveldb.impl.DbImpl; +import org.iq80.leveldb.impl.SeekingIterator; +import org.json.*; + +class StringDBWrapper { + private final Options options; + private final File databaseDir; + private DbImpl db; + + StringDBWrapper(Options options, File databaseDir) throws IOException { + this.options = options.verifyChecksums(true).createIfMissing(false).errorIfExists(false); + this.databaseDir = databaseDir; + this.db = new DbImpl(options, databaseDir); + } + + public String get(String key) { + byte[] slice = db.get(LevelDBLoader.toByteArray(key)); + if (slice == null) { + return null; + } + return new String(slice, UTF_8); + } + + public String get(String key, Snapshot snapshot) { + byte[] slice = db.get(LevelDBLoader.toByteArray(key), new ReadOptions().snapshot(snapshot)); + if (slice == null) { + return null; + } + return new String(slice, UTF_8); + } + + public void put(String key, String value) { + db.put(LevelDBLoader.toByteArray(key), LevelDBLoader.toByteArray(value)); + } + + public void delete(String key) { + db.delete(LevelDBLoader.toByteArray(key)); + } + + public SeekingIterator iterator() { + return new StringDbIterator(db.iterator()); + } + + public Snapshot getSnapshot() { + return db.getSnapshot(); + } + + public void close() { + db.close(); + } + + public long size(String start, String limit) { + return db.getApproximateSizes(new Range(LevelDBLoader.toByteArray(start), LevelDBLoader.toByteArray(limit))); + } + + public long getMaxNextLevelOverlappingBytes() { + return db.getMaxNextLevelOverlappingBytes(); + } + + public void reopen() throws IOException { + reopen(options); + } + + public void reopen(Options options) throws IOException { + db.close(); + db = new DbImpl(options.verifyChecksums(true).createIfMissing(false).errorIfExists(false), databaseDir); + } +} \ No newline at end of file diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/StringDbIterator.java b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/StringDbIterator.java new file mode 100644 index 0000000..b5c1b27 --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/StringDbIterator.java @@ -0,0 +1,51 @@ +package com.minres.scviewer.database.leveldb; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.iq80.leveldb.shaded.guava.collect.Maps.immutableEntry; + +import java.util.Map.Entry; + +import org.iq80.leveldb.DBIterator; +import org.iq80.leveldb.impl.SeekingIterator; + +class StringDbIterator implements SeekingIterator { + private final DBIterator iterator; + + StringDbIterator(DBIterator iterator) { + this.iterator = iterator; + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public void seekToFirst() { + iterator.seekToFirst(); + } + + @Override + public void seek(String targetKey) { + iterator.seek(targetKey.getBytes(UTF_8)); + } + + @Override + public Entry peek() { + return adapt(iterator.peekNext()); + } + + @Override + public Entry next() { + return adapt(iterator.next()); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + private Entry adapt(Entry next) { + return immutableEntry(new String(next.getKey(), UTF_8), new String(next.getValue(), UTF_8)); + } +} \ No newline at end of file diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/Tx.java b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/Tx.java new file mode 100644 index 0000000..2a583e9 --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/Tx.java @@ -0,0 +1,122 @@ +/******************************************************************************* + * Copyright (c) 2015 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.leveldb; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import com.minres.scviewer.database.ITx; +import com.minres.scviewer.database.ITxAttribute; +import com.minres.scviewer.database.ITxEvent; +import com.minres.scviewer.database.ITxGenerator; +import com.minres.scviewer.database.ITxRelation; +import com.minres.scviewer.database.ITxStream; + +public class Tx implements ITx { + + private StringDBWrapper levelDb; + private TxStream trStream; + private TxGenerator trGenerator; + private long id; + private JSONObject dbVal; + private List attributes; + private List incoming, outgoing; + + public Tx(StringDBWrapper levelDb, TxStream trStream, TxGenerator trGenerator, long id) { + this.levelDb=levelDb; + this.trStream=trStream; + this.trGenerator=trGenerator; + this.id=id; + this.dbVal=null; + } + + @Override + public Long getId() { + return id; + } + + @Override + public ITxStream getStream() { + return trStream; + } + + @Override + public ITxGenerator getGenerator() { + return trGenerator; + } + + @Override + public int getConcurrencyIndex() { + checkDb(); + return dbVal.getInt("conc"); + } + + private void checkDb() throws JSONException { + if(dbVal==null) { + dbVal = new JSONObject(levelDb.get("x~"+ String.format("%016x", id))); + } + } + + @Override + public Long getBeginTime() { + checkDb(); + return dbVal.getLong("START_TIME"); + } + + @Override + public Long getEndTime() { + checkDb(); + return dbVal.getLong("END_TIME"); + } + + @Override + public List getAttributes() { + if(attributes==null) { + checkDb(); + attributes=new ArrayList<>(); + JSONArray arr = dbVal.getJSONArray("attr"); + arr.forEach(entry -> { + TxAttribute attr = new TxAttribute(this, (JSONObject) entry); + attributes.add(attr); + }); + } + return attributes; + } + + @Override + public Collection getIncomingRelations() { + return incoming; + } + + @Override + public Collection getOutgoingRelations() { + return outgoing; + } + + @Override + public int compareTo(ITx o) { + int res = this.getBeginTime().compareTo(o.getBeginTime()); + if(res!=0) + return res; + else + return this.getId().compareTo(o.getId()); + } + + @Override + public String toString() { + return "tx#"+getId()+"["+getBeginTime()/1000000+"ns - "+getEndTime()/1000000+"ns]"; + } +} diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxAttribute.java b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxAttribute.java new file mode 100644 index 0000000..8384658 --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxAttribute.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2015 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.leveldb; + +import org.json.JSONObject; + +import com.minres.scviewer.database.AssociationType; +import com.minres.scviewer.database.DataType; +import com.minres.scviewer.database.ITxAttribute; + +public class TxAttribute implements ITxAttribute{ + + private Tx trTransaction; + + private String name; + + private DataType dataType; + + private AssociationType associationType; + + private Object value; + + public TxAttribute(Tx trTransaction, JSONObject attribute) { + this.trTransaction=trTransaction; + this.name=attribute.getString("name"); + this.dataType=DataType.values()[attribute.getInt("type")]; + this.associationType=AssociationType.values()[attribute.getInt("assoc")]; + this.value=attribute.get("value"); + } + + @Override + public String getName() { + return name; + } + + @Override + public DataType getDataType() { + return dataType; + } + + @Override + public AssociationType getType() { + return associationType; + } + + @Override + public Object getValue() { + return value; + } + +} diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxEvent.java b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxEvent.java new file mode 100644 index 0000000..0599ddc --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxEvent.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2015 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.leveldb; + +import com.minres.scviewer.database.ITx; +import com.minres.scviewer.database.ITxEvent; +import com.minres.scviewer.database.IWaveformEvent; + +public class TxEvent implements ITxEvent { + + private final Type type; + private ITx tx; + + public TxEvent(Type type, ITx tx) { + super(); + this.type = type; + this.tx = tx; + } + + @Override + public Long getTime() { + return type==Type.BEGIN?tx.getBeginTime():tx.getEndTime(); + } + + @Override + public IWaveformEvent duplicate() throws CloneNotSupportedException { + return new TxEvent(type, tx); + } + + @Override + public int compareTo(IWaveformEvent o) { + return getTime().compareTo(o.getTime()); + } + + @Override + public ITx getTransaction() { + return tx; + } + + @Override + public Type getType() { + return type; + } + + @Override + public String toString() { + return type.toString()+"@"+getTime()+" of tx #"+tx.getId(); + } +} diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxGenerator.java b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxGenerator.java new file mode 100644 index 0000000..87e4a88 --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxGenerator.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2015 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.leveldb; + +import java.util.List; + +import org.json.JSONObject; + +import com.minres.scviewer.database.ITx; +import com.minres.scviewer.database.ITxEvent; +import com.minres.scviewer.database.ITxGenerator; +import com.minres.scviewer.database.ITxStream; + +public class TxGenerator implements ITxGenerator { + + private ITxStream stream; + + private long id; + + private String name; + + public TxGenerator(ITxStream stream, JSONObject object) { + this.stream=stream; + this.id=object.getLong("id"); + this.name=object.getString("name"); + } + + @Override + public Long getId() { + return id; + } + + @Override + public ITxStream getStream() { + return stream; + } + + @Override + public String getName() { + return name; + } + + @Override + public List getTransactions() { + return null; + } + +} diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxRelation.java b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxRelation.java new file mode 100644 index 0000000..090a8ab --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxRelation.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2015 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.leveldb; + +import com.minres.scviewer.database.ITxRelation; +import com.minres.scviewer.database.ITx; +import com.minres.scviewer.database.RelationType; + +public class TxRelation implements ITxRelation { + + RelationType relationType; + Tx source, target; + + public TxRelation(RelationType relationType, Tx source, Tx target) { + this.source = source; + this.target = target; + this.relationType = relationType; + } + + @Override + public RelationType getRelationType() { + return relationType; + } + + @Override + public ITx getSource() { + return source; + } + + @Override + public ITx getTarget() { + return target; + } + +} diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxStream.java b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxStream.java new file mode 100644 index 0000000..c585e2d --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/TxStream.java @@ -0,0 +1,176 @@ +/******************************************************************************* + * Copyright (c) 2015 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.leveldb; + +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 java.util.Map; +import java.util.Map.Entry; + +import org.iq80.leveldb.impl.SeekingIterator; +import org.json.JSONObject; + +import java.util.NavigableMap; +import java.util.StringTokenizer; +import java.util.TreeMap; +import java.util.Vector; + +import com.minres.scviewer.database.HierNode; +import com.minres.scviewer.database.ITx; +import com.minres.scviewer.database.ITxEvent; +import com.minres.scviewer.database.ITxGenerator; +import com.minres.scviewer.database.ITxStream; +import com.minres.scviewer.database.IWaveform; +import com.minres.scviewer.database.IWaveformDb; +import com.minres.scviewer.database.IWaveformEvent; +import com.minres.scviewer.database.RelationType; + +public class TxStream extends HierNode implements ITxStream { + + private StringDBWrapper levelDb; + + private String fullName; + + private String kind; + + private IWaveformDb db; + + private long id; + + private TreeMap generators; + + private TreeMap transactions; + + private Integer maxConcurrency; + + private TreeMap> events; + + private List usedRelationsList; + + public TxStream(StringDBWrapper database, IWaveformDb waveformDb, JSONObject object) { + super(object.get("name").toString()); + this.levelDb=database; + this.db=waveformDb; + this.fullName=object.getString("name"); + this.kind=object.getString("kind"); + this.id = object.getLong("id"); + } + + @Override + public IWaveformDb getDb() { + return db; + } + + @Override + public String getFullName() { + return fullName; + } + + @Override + public Long getId() { + return id; + } + + @Override + public String getKind() { + return kind; + } + + @Override + public List getGenerators() { + if(generators==null){ + generators=new TreeMap(); + SeekingIterator it = levelDb.iterator(); + it.seek("sg~"+String.format("%016x", id)); + while(it.hasNext()) { + Entry val = it.next(); + if(!val.getKey().startsWith("sg~")) break; + JSONObject jVal = new JSONObject(val.getValue()); + generators.put(jVal.getLong("id"), new TxGenerator(this, jVal)); + } + } + return new ArrayList(generators.values()); + } + + @Override + public int getMaxConcurrency() { + if(maxConcurrency==null){ + maxConcurrency=1; + } + return maxConcurrency; + } + + @Override + public NavigableMap> getEvents(){ + if(events==null){ + events=new TreeMap>(); + for(Entry entry:getTransactions().entrySet()){ + putEvent(new TxEvent(TxEvent.Type.BEGIN, entry.getValue())); + putEvent(new TxEvent(TxEvent.Type.END, entry.getValue())); + } + } + return events; + } + + private void putEvent(TxEvent ev){ + Long time = ev.getTime(); + if(!events.containsKey(time)){ + Vector vector=new Vector(); + vector.add(ev); + events.put(time, vector); + } else { + events.get(time).add(ev); + } + } + + protected Map getTransactions() { + if(transactions==null){ + if(generators==null) getGenerators(); + transactions = new TreeMap(); + SeekingIterator it = levelDb.iterator(); + it.seek("sgx~"+String.format("%016x", id)); + while(it.hasNext()) { + Entry val = it.next(); + if(!val.getKey().startsWith("sgx~")) break; + String[] token = val.getKey().split("~"); + long gid = Long.parseLong(token[2], 16); // gen id + long id = Long.parseLong(token[3], 16); // tx id + transactions.put(id, new Tx(levelDb, this, generators.get(gid), id)); + } + } + return transactions; + } + + @Override + public Collection getWaveformEventsAtTime(Long time) { + return getEvents().get(time); + } + + public void setRelationTypeList(List usedRelationsList){ + this.usedRelationsList=usedRelationsList; + } + + public RelationType getRelationType(String name) { + RelationType relType=RelationType.create(name); + if(!usedRelationsList.contains(relType)) usedRelationsList.add(relType); + return relType; + } + + @Override + public Boolean equals(IWaveform other) { + return(other instanceof TxStream && this.getId()==other.getId()); + } + +} diff --git a/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/pom.xml b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/pom.xml new file mode 100644 index 0000000..94123fe --- /dev/null +++ b/com.minres.scviewer.database.leveldb/src/com/minres/scviewer/database/leveldb/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + com.minres.scviewer.database.text + + com.minres.scviewer + com.minres.scviewer.parent + 2.0.0-SNAPSHOT + ../com.minres.scviewer.parent + + eclipse-plugin + + + + org.eclipse.tycho + tycho-compiler-plugin + 0.23.1 + + groovy-eclipse-compiler + + + 1.7 + 1.7 + + + + org.codehaus.groovy + groovy-eclipse-compiler + ${groovy-eclipse-compiler-version} + + + org.codehaus.groovy + groovy-eclipse-batch + ${groovy-eclipse-batch-version} + + + + + + + + 1.0.1-SNAPSHOT + \ No newline at end of file