SCViewer/plugins/com.minres.scviewer.databas.../src/com/minres/scviewer/database/text/AbstractTxStream.java

160 lines
3.7 KiB
Java
Raw Normal View History

2021-01-09 10:34:22 +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;
import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.TreeMap;
import com.minres.scviewer.database.EventKind;
import com.minres.scviewer.database.HierNode;
import com.minres.scviewer.database.IEvent;
import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.WaveformType;
import com.minres.scviewer.database.tx.ITxEvent;
/**
* The Class AbstractTxStream.
*/
2021-01-09 10:34:22 +01:00
abstract class AbstractTxStream extends HierNode implements IWaveform {
/** The id. */
2021-01-09 10:34:22 +01:00
private Long id;
/** The loader. */
2021-01-09 10:34:22 +01:00
protected TextDbLoader loader;
/** The events. */
2021-01-09 10:34:22 +01:00
TreeMap<Long, IEvent[]> events = new TreeMap<>();
/** The concurrency calculated. */
2021-01-09 10:34:22 +01:00
boolean concurrencyCalculated = false;
/**
* Instantiates a new abstract tx stream.
*
* @param loader the loader
* @param id the id
* @param name the name
*/
2021-01-09 10:34:22 +01:00
public AbstractTxStream(TextDbLoader loader, Long id, String name) {
super(name);
this.loader = loader;
this.id = id;
2021-01-09 10:34:22 +01:00
}
/**
* Adds the event.
*
* @param evt the evt
*/
2021-01-09 10:34:22 +01:00
public void addEvent(ITxEvent evt) {
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());
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);
}
}
/**
* Gets the events.
*
* @return the events
*/
2021-01-09 10:34:22 +01:00
@Override
public NavigableMap<Long, IEvent[]> getEvents() {
if (!concurrencyCalculated)
calculateConcurrency();
2021-01-09 10:34:22 +01:00
return events;
}
/**
* 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) {
if (!concurrencyCalculated)
calculateConcurrency();
2021-01-09 10:34:22 +01:00
return events.get(time);
}
/**
* 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) {
if (!concurrencyCalculated)
2021-01-09 10:34:22 +01:00
calculateConcurrency();
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
}
/**
* Gets the type.
*
* @return the type
*/
2021-01-09 10:34:22 +01:00
@Override
public WaveformType getType() {
return WaveformType.TRANSACTION;
}
/**
* Gets the id.
*
* @return the id
*/
2021-01-09 10:34:22 +01:00
@Override
public Long getId() {
return id;
}
/**
* Calculate concurrency.
*/
2021-01-09 10:34:22 +01:00
synchronized void calculateConcurrency() {
if (concurrencyCalculated)
return;
2021-01-09 10:34:22 +01:00
ArrayList<Long> rowendtime = new ArrayList<>();
events.entrySet().stream().forEach(entry -> {
2021-01-09 10:34:22 +01:00
IEvent[] values = entry.getValue();
Arrays.asList(values).stream().filter(e -> e.getKind() == EventKind.BEGIN).forEach(evt -> {
Tx tx = (Tx) ((TxEvent) evt).getTransaction();
2021-01-09 10:34:22 +01:00
int rowIdx = 0;
for (; rowIdx < rowendtime.size() && rowendtime.get(rowIdx) > tx.getBeginTime(); rowIdx++)
;
if (rowendtime.size() <= rowIdx)
2021-01-09 10:34:22 +01:00
rowendtime.add(tx.getEndTime());
else
rowendtime.set(rowIdx, tx.getEndTime());
tx.setConcurrencyIndex(rowIdx);
});
});
concurrencyCalculated = true;
2021-01-09 10:34:22 +01:00
}
}