2021-01-09 10:34:22 +01:00
|
|
|
/*******************************************************************************
|
2021-01-09 12:43:02 +01:00
|
|
|
* Copyright (c) 2012 IT Just working
|
|
|
|
* Copyright (c) 2020 MINRES Technologies GmbH
|
2021-01-09 10:34:22 +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:
|
|
|
|
* IT Just working - initial API and implementation
|
|
|
|
*******************************************************************************/
|
|
|
|
package com.minres.scviewer.database.text;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2021-01-14 23:13:11 +01:00
|
|
|
import java.util.HashMap;
|
2021-01-09 10:34:22 +01:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
import java.util.NavigableMap;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
|
|
import com.minres.scviewer.database.HierNode;
|
|
|
|
import com.minres.scviewer.database.IEvent;
|
|
|
|
import com.minres.scviewer.database.IWaveform;
|
|
|
|
import com.minres.scviewer.database.WaveformType;
|
2021-01-14 23:13:11 +01:00
|
|
|
import com.minres.scviewer.database.tx.ITx;
|
2021-01-09 10:34:22 +01:00
|
|
|
import com.minres.scviewer.database.tx.ITxEvent;
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* The Class AbstractTxStream.
|
|
|
|
*/
|
2021-01-09 10:34:22 +01:00
|
|
|
abstract class AbstractTxStream extends HierNode implements IWaveform {
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/** The id. */
|
2021-01-09 10:34:22 +01:00
|
|
|
private Long id;
|
2021-01-09 12:43:02 +01:00
|
|
|
|
|
|
|
/** The loader. */
|
2021-01-09 10:34:22 +01:00
|
|
|
protected TextDbLoader loader;
|
2021-01-09 12:43:02 +01:00
|
|
|
|
|
|
|
/** The events. */
|
2021-01-09 10:34:22 +01:00
|
|
|
TreeMap<Long, IEvent[]> events = new TreeMap<>();
|
|
|
|
|
2021-01-14 23:13:11 +01:00
|
|
|
/** The max concurrency. */
|
|
|
|
private int maxConcurrency = 0;
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/** The concurrency calculated. */
|
2021-01-09 10:34:22 +01:00
|
|
|
boolean concurrencyCalculated = false;
|
2021-01-09 12:43:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates a new abstract tx stream.
|
|
|
|
*
|
|
|
|
* @param loader the loader
|
|
|
|
* @param id the id
|
|
|
|
* @param name the name
|
|
|
|
*/
|
2021-01-14 23:13:11 +01:00
|
|
|
protected AbstractTxStream(TextDbLoader loader, Long id, String name) {
|
2021-01-09 10:34:22 +01:00
|
|
|
super(name);
|
2021-01-09 12:43:02 +01:00
|
|
|
this.loader = loader;
|
|
|
|
this.id = id;
|
2021-01-09 10:34:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Adds the event.
|
|
|
|
*
|
|
|
|
* @param evt the evt
|
|
|
|
*/
|
2021-01-09 10:34:22 +01:00
|
|
|
public void addEvent(ITxEvent evt) {
|
2021-01-09 12:43:02 +01:00
|
|
|
if (!events.containsKey(evt.getTime()))
|
|
|
|
events.put(evt.getTime(), new IEvent[] { evt });
|
2021-01-09 10:34:22 +01:00
|
|
|
else {
|
|
|
|
IEvent[] evts = events.get(evt.getTime());
|
2021-01-09 12:43:02 +01:00
|
|
|
IEvent[] newEvts = Arrays.copyOf(evts, evts.length + 1);
|
|
|
|
newEvts[evts.length] = evt;
|
2021-01-09 10:34:22 +01:00
|
|
|
events.put(evt.getTime(), newEvts);
|
|
|
|
}
|
|
|
|
}
|
2021-01-09 12:43:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the events.
|
|
|
|
*
|
|
|
|
* @return the events
|
|
|
|
*/
|
2021-01-09 10:34:22 +01:00
|
|
|
@Override
|
|
|
|
public NavigableMap<Long, IEvent[]> getEvents() {
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Gets the events at time.
|
|
|
|
*
|
|
|
|
* @param time the time
|
|
|
|
* @return the events at time
|
|
|
|
*/
|
2021-01-09 10:34:22 +01:00
|
|
|
@Override
|
|
|
|
public IEvent[] getEventsAtTime(Long time) {
|
|
|
|
return events.get(time);
|
|
|
|
}
|
2021-01-09 12:43:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the events before time.
|
|
|
|
*
|
|
|
|
* @param time the time
|
|
|
|
* @return the events before time
|
|
|
|
*/
|
2021-01-09 10:34:22 +01:00
|
|
|
@Override
|
|
|
|
public IEvent[] getEventsBeforeTime(Long time) {
|
2021-01-09 12:43:02 +01:00
|
|
|
Entry<Long, IEvent[]> e = events.floorEntry(time);
|
|
|
|
if (e == null)
|
|
|
|
return new IEvent[] {};
|
|
|
|
else
|
|
|
|
return events.floorEntry(time).getValue();
|
2021-01-09 10:34:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Gets the type.
|
|
|
|
*
|
|
|
|
* @return the type
|
|
|
|
*/
|
2021-01-09 10:34:22 +01:00
|
|
|
@Override
|
|
|
|
public WaveformType getType() {
|
|
|
|
return WaveformType.TRANSACTION;
|
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Gets the id.
|
|
|
|
*
|
|
|
|
* @return the id
|
|
|
|
*/
|
2021-01-09 10:34:22 +01:00
|
|
|
@Override
|
|
|
|
public Long getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2021-01-14 23:13:11 +01:00
|
|
|
/**
|
|
|
|
* Gets the width.
|
|
|
|
*
|
|
|
|
* @return the width
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int getRowCount() {
|
|
|
|
if (!concurrencyCalculated)
|
|
|
|
calculateConcurrency();
|
|
|
|
return maxConcurrency;
|
|
|
|
}
|
|
|
|
|
2021-01-09 12:43:02 +01:00
|
|
|
/**
|
|
|
|
* Calculate concurrency.
|
|
|
|
*/
|
2021-01-14 23:13:11 +01:00
|
|
|
public void calculateConcurrency() {
|
2021-01-09 12:43:02 +01:00
|
|
|
if (concurrencyCalculated)
|
|
|
|
return;
|
2021-01-14 23:13:11 +01:00
|
|
|
ArrayList<Long> rowEndTime = new ArrayList<>();
|
|
|
|
HashMap<Long, Integer> rowByTxId = new HashMap<>();
|
2021-01-09 12:43:02 +01:00
|
|
|
events.entrySet().stream().forEach(entry -> {
|
2021-01-14 23:13:11 +01:00
|
|
|
Arrays.asList(entry.getValue()).stream().forEach(evt -> {
|
|
|
|
TxEvent txEvt = (TxEvent) evt;
|
|
|
|
ITx tx = txEvt.getTransaction();
|
2021-01-09 10:34:22 +01:00
|
|
|
int rowIdx = 0;
|
2021-01-14 23:13:11 +01:00
|
|
|
switch(evt.getKind()) {
|
|
|
|
case END:
|
|
|
|
Long txId = txEvt.getTransaction().getId();
|
|
|
|
txEvt.setConcurrencyIndex(rowByTxId.get(txId));
|
|
|
|
rowByTxId.remove(txId);
|
|
|
|
break;
|
|
|
|
case SINGLE:
|
|
|
|
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx) > tx.getBeginTime(); rowIdx++);
|
|
|
|
if (rowEndTime.size() <= rowIdx)
|
|
|
|
rowEndTime.add(tx.getEndTime());
|
|
|
|
else
|
|
|
|
rowEndTime.set(rowIdx, tx.getEndTime());
|
|
|
|
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
|
|
|
break;
|
|
|
|
case BEGIN:
|
|
|
|
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx) > tx.getBeginTime(); rowIdx++);
|
|
|
|
if (rowEndTime.size() <= rowIdx)
|
|
|
|
rowEndTime.add(tx.getEndTime());
|
|
|
|
else
|
|
|
|
rowEndTime.set(rowIdx, tx.getEndTime());
|
|
|
|
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
|
|
|
rowByTxId.put(tx.getId(), rowIdx);
|
|
|
|
break;
|
|
|
|
}
|
2021-01-09 10:34:22 +01:00
|
|
|
});
|
|
|
|
});
|
2021-01-14 23:13:11 +01:00
|
|
|
maxConcurrency=rowEndTime.size();
|
2021-01-09 12:43:02 +01:00
|
|
|
concurrencyCalculated = true;
|
2021-01-14 23:13:11 +01:00
|
|
|
getChildNodes().parallelStream().forEach(c -> ((TxGenerator)c).calculateConcurrency());
|
2021-01-09 10:34:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|