Build and freeze transaction events in bulk
This commit is contained in:
1 parent
e78c65a36c
commit
75fa1a4f31
10 files changed
+141
-17
No files matched your search
+9
-1
@@ -36,6 +36,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
|||||||
|
|
||||||
/** The events. */
|
/** The events. */
|
||||||
protected IEventList events = new EventList();
|
protected IEventList events = new EventList();
|
||||||
|
private EventList.Builder eventBuilder = new EventList.Builder();
|
||||||
|
|
||||||
/** The max concurrency. */
|
/** The max concurrency. */
|
||||||
private int rowCount = -1;
|
private int rowCount = -1;
|
||||||
@@ -69,7 +70,14 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
|||||||
* @param evt the evt
|
* @param evt the evt
|
||||||
*/
|
*/
|
||||||
public void addEvent(ITxEvent evt) {
|
public void addEvent(ITxEvent evt) {
|
||||||
events.put(evt.getTime(), evt);
|
eventBuilder.add(evt.getTime(), evt);
|
||||||
|
}
|
||||||
|
|
||||||
|
void finishEvents() {
|
||||||
|
if (eventBuilder != null) {
|
||||||
|
events = eventBuilder.build();
|
||||||
|
eventBuilder = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+3
@@ -82,6 +82,9 @@ class TxStream extends AbstractTxStream {
|
|||||||
try {
|
try {
|
||||||
int blockId = 0;
|
int blockId = 0;
|
||||||
for (byte[] chunk : getChunks()) loader.parseTx(this, blockId++, chunk);
|
for (byte[] chunk : getChunks()) loader.parseTx(this, blockId++, chunk);
|
||||||
|
finishEvents();
|
||||||
|
for (com.minres.scviewer.database.IHierNode child : getChildNodes())
|
||||||
|
((TxGenerator) child).finishEvents();
|
||||||
return Boolean.TRUE;
|
return Boolean.TRUE;
|
||||||
} catch (InputFormatException | IOException e) {
|
} catch (InputFormatException | IOException e) {
|
||||||
throw new IllegalStateException("Cannot parse stream " + getFullName(), e);
|
throw new IllegalStateException("Cannot parse stream " + getFullName(), e);
|
||||||
|
|||||||
+14
-11
@@ -13,7 +13,6 @@ package com.minres.scviewer.database.sqlite;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
|
||||||
|
|
||||||
import com.minres.scviewer.database.EventEntry;
|
import com.minres.scviewer.database.EventEntry;
|
||||||
import com.minres.scviewer.database.EventKind;
|
import com.minres.scviewer.database.EventKind;
|
||||||
@@ -77,21 +76,25 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IEventList getEvents(){
|
public synchronized IEventList getEvents() {
|
||||||
if(events==null){
|
if (events == null) {
|
||||||
events=new EventList();
|
EventList.Builder builder = new EventList.Builder();
|
||||||
for(Entry<Integer, ITx> entry:getTransactions().entrySet()){
|
for (ITx tx : getTransactions().values()) {
|
||||||
putEvent(new TxEvent(EventKind.BEGIN, entry.getValue()));
|
if (tx.getBeginTime() == tx.getEndTime()) {
|
||||||
putEvent(new TxEvent(EventKind.END, entry.getValue()));
|
TxEvent single = new TxEvent(EventKind.SINGLE, tx);
|
||||||
|
builder.add(single.getTime(), single);
|
||||||
|
} else {
|
||||||
|
TxEvent begin = new TxEvent(EventKind.BEGIN, tx);
|
||||||
|
TxEvent end = new TxEvent(EventKind.END, tx);
|
||||||
|
builder.add(begin.getTime(), begin);
|
||||||
|
builder.add(end.getTime(), end);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
events = builder.build();
|
||||||
|
}
|
||||||
return events;
|
return events;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void putEvent(TxEvent ev){
|
|
||||||
events.put(ev.getTime(), ev);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract Map<Integer, ITx> getTransactions();
|
protected abstract Map<Integer, ITx> getTransactions();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+9
-1
@@ -37,6 +37,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
|||||||
|
|
||||||
/** The events. */
|
/** The events. */
|
||||||
IEventList events = new EventList();
|
IEventList events = new EventList();
|
||||||
|
private EventList.Builder eventBuilder = new EventList.Builder();
|
||||||
|
|
||||||
/** The max concurrency. */
|
/** The max concurrency. */
|
||||||
private int rowCount = -1;
|
private int rowCount = -1;
|
||||||
@@ -70,7 +71,14 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
|||||||
* @param evt the evt
|
* @param evt the evt
|
||||||
*/
|
*/
|
||||||
public void addEvent(ITxEvent evt) {
|
public void addEvent(ITxEvent evt) {
|
||||||
events.put(evt.getTime(), evt);
|
eventBuilder.add(evt.getTime(), evt);
|
||||||
|
}
|
||||||
|
|
||||||
|
void finishEvents() {
|
||||||
|
if (eventBuilder != null) {
|
||||||
|
events = eventBuilder.build();
|
||||||
|
eventBuilder = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+2
@@ -252,6 +252,8 @@ public class TextDbLoader implements IWaveformDbLoader {
|
|||||||
} finally {
|
} finally {
|
||||||
transactions = parser.txSink;
|
transactions = parser.txSink;
|
||||||
}
|
}
|
||||||
|
txStreams.values().forEach(TxStream::finishEvents);
|
||||||
|
txGenerators.values().forEach(TxGenerator::finishEvents);
|
||||||
txStreams.values().parallelStream().forEach(TxStream::calculateConcurrency);
|
txStreams.values().parallelStream().forEach(TxStream::calculateConcurrency);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -106,7 +106,7 @@ public class SignalPainter extends TrackPainter {
|
|||||||
EventEntry first = signal.getEvents().floorEntry(beginTime);
|
EventEntry first = signal.getEvents().floorEntry(beginTime);
|
||||||
if (first == null)
|
if (first == null)
|
||||||
first = signal.getEvents().firstEntry();
|
first = signal.getEvents().firstEntry();
|
||||||
if (first == null) return;
|
if (first == null || Long.compareUnsigned(first.timestamp, endTime) > 0) return;
|
||||||
beginTime = first.timestamp;
|
beginTime = first.timestamp;
|
||||||
proj.setForeground(this.waveCanvas.styleProvider.getColor(WaveformColors.LINE));
|
proj.setForeground(this.waveCanvas.styleProvider.getColor(WaveformColors.LINE));
|
||||||
proj.setLineStyle(SWT.LINE_SOLID);
|
proj.setLineStyle(SWT.LINE_SOLID);
|
||||||
|
|||||||
@@ -7,6 +7,36 @@ import java.util.NoSuchElementException;
|
|||||||
|
|
||||||
/** Sorted timestamp buckets. Range views are read-only and bounded by their parent. */
|
/** Sorted timestamp buckets. Range views are read-only and bounded by their parent. */
|
||||||
public class EventList implements IEventList {
|
public class EventList implements IEventList {
|
||||||
|
/** Accumulates unsorted events and freezes timestamp buckets with one sort and one allocation per bucket. */
|
||||||
|
public static final class Builder {
|
||||||
|
private record TimedEvent(long time, IEvent event) { }
|
||||||
|
private final ArrayList<TimedEvent> pending = new ArrayList<>();
|
||||||
|
private boolean built;
|
||||||
|
|
||||||
|
public void add(long time, IEvent event) {
|
||||||
|
if (built) throw new IllegalStateException("Event list has already been built");
|
||||||
|
pending.add(new TimedEvent(time, event));
|
||||||
|
}
|
||||||
|
|
||||||
|
public EventList build() {
|
||||||
|
if (built) throw new IllegalStateException("Event list has already been built");
|
||||||
|
pending.sort((a, b) -> Long.compareUnsigned(a.time(), b.time()));
|
||||||
|
ArrayList<EventEntry> entries = new ArrayList<>();
|
||||||
|
for (int first = 0; first < pending.size();) {
|
||||||
|
long time = pending.get(first).time();
|
||||||
|
int next = first + 1;
|
||||||
|
while (next < pending.size() && pending.get(next).time() == time) next++;
|
||||||
|
IEvent[] events = new IEvent[next - first];
|
||||||
|
for (int i = first; i < next; ++i) events[i - first] = pending.get(i).event();
|
||||||
|
entries.add(new EventEntry(time, events));
|
||||||
|
first = next;
|
||||||
|
}
|
||||||
|
built = true;
|
||||||
|
pending.clear();
|
||||||
|
return new EventList(entries, 0, entries.size(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private final ArrayList<EventEntry> store;
|
private final ArrayList<EventEntry> store;
|
||||||
private final int start;
|
private final int start;
|
||||||
private int end;
|
private int end;
|
||||||
|
|||||||
+3
-3
@@ -70,9 +70,9 @@ public interface IEventList extends Iterable<EventEntry> {
|
|||||||
* {@code fromKey} to {@code toKey}. If {@code fromKey} and
|
* {@code fromKey} to {@code toKey}. If {@code fromKey} and
|
||||||
* {@code toKey} are equal, the returned map is empty unless
|
* {@code toKey} are equal, the returned map is empty unless
|
||||||
* {@code fromInclusive} is true. The
|
* {@code fromInclusive} is true. The
|
||||||
* returned map is backed by this map, so changes in the returned map are
|
* returned view is read-only. Do not structurally modify a mutable source
|
||||||
* reflected in this map, and vice-versa. The returned map supports all
|
* while a view is in use. EventList.Builder produces frozen sources suitable
|
||||||
* optional map operations that this map supports.
|
* for sharing range views.
|
||||||
*
|
*
|
||||||
* @param fromKey low endpoint of the keys in the returned map
|
* @param fromKey low endpoint of the keys in the returned map
|
||||||
* @param fromInclusive {@code true} if the low endpoint
|
* @param fromInclusive {@code true} if the low endpoint
|
||||||
|
|||||||
+32
@@ -0,0 +1,32 @@
|
|||||||
|
package com.minres.scviewer.database.test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
import com.minres.scviewer.database.*;
|
||||||
|
|
||||||
|
public class EventListBuilderTest {
|
||||||
|
@Test
|
||||||
|
public void sortsAndPreservesSameTimeInsertionOrder() {
|
||||||
|
EventList.Builder builder = new EventList.Builder();
|
||||||
|
TxTestSupport.Tx tx = new TxTestSupport.Tx(1, 0, 10);
|
||||||
|
IEvent begin = new TxTestSupport.Event(tx, EventKind.BEGIN);
|
||||||
|
IEvent end = new TxTestSupport.Event(tx, EventKind.END);
|
||||||
|
builder.add(10, begin);builder.add(2, end);builder.add(10, end);
|
||||||
|
builder.add(Long.MIN_VALUE, begin);builder.add(Long.MAX_VALUE, end);
|
||||||
|
EventList list = builder.build();
|
||||||
|
assertEquals(4, list.size());
|
||||||
|
assertEquals(2, list.firstKey());
|
||||||
|
assertArrayEquals(new IEvent[] {begin, end}, list.get(10));
|
||||||
|
assertEquals(Long.MIN_VALUE, list.lastKey());
|
||||||
|
try { list.put(5, begin); fail(); } catch (UnsupportedOperationException expected) { }
|
||||||
|
try { builder.add(5, begin); fail(); } catch (IllegalStateException expected) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handlesManyEventsAtOneTimestampAndEmptyInput() {
|
||||||
|
EventList.Builder builder = new EventList.Builder();
|
||||||
|
for (int i = 0; i < 10000; ++i) builder.add(5, null);
|
||||||
|
assertEquals(10000, builder.build().get(5).length);
|
||||||
|
assertNull(new EventList.Builder().build().firstEntry());
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package com.minres.scviewer.database.test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.junit.Test;
|
||||||
|
import com.minres.scviewer.database.*;
|
||||||
|
import com.minres.scviewer.database.text.TextDbLoader;
|
||||||
|
import com.minres.scviewer.database.tx.ITxEvent;
|
||||||
|
|
||||||
|
public class TransactionReaderTest {
|
||||||
|
@Test
|
||||||
|
public void textAndTruncatedRecordingsHaveConsistentRows() throws Exception {
|
||||||
|
for (String input : new String[] {"my_db.txlog", "my_db_truncated.txlog"}) {
|
||||||
|
TextDbLoader loader = new TextDbLoader();
|
||||||
|
try {
|
||||||
|
loader.load(TxTestSupport.input(input));
|
||||||
|
assertFalse(loader.getAllWaves().isEmpty());
|
||||||
|
for (IWaveform wave : loader.getAllWaves()) {
|
||||||
|
int rows = wave.getRowCount();
|
||||||
|
Map<Long, Integer> begun = new HashMap<>();
|
||||||
|
for (EventEntry entry : wave.getEvents()) for (IEvent raw : entry.events) {
|
||||||
|
ITxEvent event = (ITxEvent) raw;
|
||||||
|
assertTrue(event.getRowIndex() >= 0 && event.getRowIndex() < rows);
|
||||||
|
assertTrue(event.getTransaction().getEndTime() >= event.getTransaction().getBeginTime());
|
||||||
|
if (event.getKind() == EventKind.BEGIN)
|
||||||
|
begun.put(event.getTransaction().getId(), event.getRowIndex());
|
||||||
|
else if (event.getKind() == EventKind.END)
|
||||||
|
assertEquals(begun.remove(event.getTransaction().getId()), Integer.valueOf(event.getRowIndex()));
|
||||||
|
}
|
||||||
|
assertTrue(begun.isEmpty());
|
||||||
|
try { wave.getEvents().put(1, null); fail("Frozen event list"); }
|
||||||
|
catch (UnsupportedOperationException expected) { }
|
||||||
|
}
|
||||||
|
} finally { loader.dispose(); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user