Index overlapping transactions for rendering and hit testing

This commit is contained in:
eyck committed 2026-11-14 17:20:35 +01:00
1 parent de66bb2580
commit 182b5bc01e
5 files changed
+194 -54

No files matched your search

@@ -106,11 +106,13 @@ 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;
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);
proj.setLineWidth(1); proj.setLineWidth(1);
IEventList entries = signal.getEvents().subMap(beginTime, true, endTime); IEventList entries = signal.getEvents().subMap(beginTime, true, endTime);
if (entries.isEmpty()) return;
SignalChange left = new SignalChange(entries.firstEntry()); SignalChange left = new SignalChange(entries.firstEntry());
SignalChange right = new SignalChange(entries.size() > 1 ? entries.higherEntry(left.time) : entries.firstEntry()); SignalChange right = new SignalChange(entries.size() > 1 ? entries.higherEntry(left.time) : entries.firstEntry());
maxPosX = area.x + area.width; maxPosX = area.x + area.width;
@@ -10,20 +10,19 @@
*******************************************************************************/ *******************************************************************************/
package com.minres.scviewer.database.ui.swt.internal; package com.minres.scviewer.database.ui.swt.internal;
import java.util.TreeMap;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.graphics.Rectangle;
import com.minres.scviewer.database.EventEntry;
import com.minres.scviewer.database.EventKind; import com.minres.scviewer.database.EventKind;
import com.minres.scviewer.database.IEvent; import com.minres.scviewer.database.IEvent;
import com.minres.scviewer.database.IEventList; import com.minres.scviewer.database.IEventList;
import com.minres.scviewer.database.IWaveform; import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.tx.ITx; import com.minres.scviewer.database.tx.ITx;
import com.minres.scviewer.database.tx.ITxEvent; import com.minres.scviewer.database.tx.ITxEvent;
import com.minres.scviewer.database.tx.TxIntervalIndex;
import com.minres.scviewer.database.ui.TrackEntry; import com.minres.scviewer.database.ui.TrackEntry;
import com.minres.scviewer.database.ui.WaveformColors; import com.minres.scviewer.database.ui.WaveformColors;
@@ -36,14 +35,14 @@ public class StreamPainter extends TrackPainter{
private IWaveform stream; private IWaveform stream;
private int txBase; private int txBase;
private int txHeight; private int txHeight;
// TODO: remove TreeMap usage private TxIntervalIndex intervalIndex;
private TreeMap<ITx, ITxEvent> seenTx; private IEventList indexedEvents;
private int indexedSize = -1;
public StreamPainter(WaveformCanvas waveCanvas, boolean even, TrackEntry trackEntry) { public StreamPainter(WaveformCanvas waveCanvas, boolean even, TrackEntry trackEntry) {
super(trackEntry, even); super(trackEntry, even);
this.waveCanvas = waveCanvas; this.waveCanvas = waveCanvas;
this.stream=trackEntry.waveform; this.stream=trackEntry.waveform;
this.seenTx=new TreeMap<>();
} }
public void paintArea(Projection proj, Rectangle area) { public void paintArea(Projection proj, Rectangle area) {
@@ -72,11 +71,6 @@ public class StreamPainter extends TrackPainter{
long beginTime = beginPos*scaleFactor; long beginTime = beginPos*scaleFactor;
long endTime = beginTime + area.width*scaleFactor; long endTime = beginTime + area.width*scaleFactor;
IEventList events = stream.getEvents();
EventEntry firstTx = events.floorEntry(beginTime);
EventEntry lastTx = events.ceilingEntry(endTime);
if(firstTx==null) firstTx = events.firstEntry();
if(lastTx==null) lastTx = events.lastEntry();
proj.setFillRule(SWT.FILL_EVEN_ODD); proj.setFillRule(SWT.FILL_EVEN_ODD);
proj.setLineStyle(SWT.LINE_SOLID); proj.setLineStyle(SWT.LINE_SOLID);
proj.setLineWidth(1); proj.setLineWidth(1);
@@ -84,43 +78,29 @@ public class StreamPainter extends TrackPainter{
for( int y1=area.y+trackHeight/2; y1<area.y+trackEntry.height; y1+=trackHeight) for( int y1=area.y+trackHeight/2; y1<area.y+trackEntry.height; y1+=trackHeight)
proj.drawLine(area.x, y1, area.x+area.width, y1); proj.drawLine(area.x, y1, area.x+area.width, y1);
if(firstTx==lastTx) { ITxEvent highlighted = null;
for(IEvent txEvent: firstTx.events) for (ITxEvent event : getIntervalIndex().overlapping(beginTime, endTime)) {
drawTx(proj, area, ((ITxEvent)txEvent).getTransaction(), ((ITxEvent)txEvent).getRowIndex(), false); ITx tx = event.getTransaction();
}else{ drawTx(proj, area, tx, event.getRowIndex(), false);
seenTx.clear(); if (waveCanvas.currentSelection != null && waveCanvas.currentSelection.getId() == tx.getId())
ITxEvent highlighed=null; highlighted = event;
proj.setForeground(this.waveCanvas.styleProvider.getColor(WaveformColors.LINE)); }
long selectedId=waveCanvas.currentSelection!=null? waveCanvas.currentSelection.getId():-1; if (highlighted != null) {
for(EventEntry entry: events.subMap(firstTx.timestamp, true, lastTx.timestamp)) proj.setForeground(waveCanvas.styleProvider.getColor(WaveformColors.LINE_HIGHLITE));
for(IEvent e:entry.events){ drawTx(proj, area, highlighted.getTransaction(), highlighted.getRowIndex(), true);
ITxEvent evt = (ITxEvent) e; }
ITx tx = evt.getTransaction(); }
if(selectedId==tx.getId())
highlighed=evt;
switch(evt.getKind()) {
case BEGIN:
seenTx.put(tx, evt);
break;
case END:
drawTx(proj, area, tx, evt.getRowIndex(), false);
seenTx.remove(tx);
break;
case SINGLE:
drawTx(proj, area, tx, evt.getRowIndex(), false);
break;
}
}
seenTx.entrySet().stream().forEach(e -> {
drawTx(proj, area, e.getKey(), e.getValue().getRowIndex(), false);
});
if(highlighed!=null){ private TxIntervalIndex getIntervalIndex() {
proj.setForeground(this.waveCanvas.styleProvider.getColor(WaveformColors.LINE_HIGHLITE)); stream.getRowCount();
drawTx(proj, area, highlighed.getTransaction(), highlighed.getRowIndex(), true); IEventList events = stream.getEvents();
} if (intervalIndex == null || indexedEvents != events || indexedSize != events.size()) {
} intervalIndex = new TxIntervalIndex(events);
} indexedEvents = events;
indexedSize = events.size();
}
return intervalIndex;
}
protected void drawTx(Projection proj, Rectangle area, ITx tx, int concurrencyIndex, boolean highlighted ) { protected void drawTx(Projection proj, Rectangle area, ITx tx, int concurrencyIndex, boolean highlighted ) {
// compute colors // compute colors
@@ -156,14 +136,16 @@ public class StreamPainter extends TrackPainter{
public ITx getClicked(Point point) { public ITx getClicked(Point point) {
int lane=point.y/waveCanvas.styleProvider.getTrackHeight(); int lane=point.y/waveCanvas.styleProvider.getTrackHeight();
EventEntry firstTx=stream.getEvents().floorEntry(point.x*waveCanvas.getScale()); long scale = waveCanvas.getScale();
if(firstTx!=null){ long time = (long) point.x * scale;
do { long low = Math.max(0L, (long) point.x - 5) * scale;
ITx tx = getTxFromEntry(lane, point.x, firstTx.events); long high = ((long) point.x + 5) * scale;
if(tx!=null) return tx; for (ITxEvent event : getIntervalIndex().overlapping(low, high)) {
firstTx=stream.getEvents().lowerEntry(firstTx.timestamp); if (event.getRowIndex() != lane) continue;
}while(firstTx!=null); ITx tx = event.getTransaction();
} if (event.getKind() == EventKind.SINGLE ||
(tx.getBeginTime() <= time && tx.getEndTime() >= time)) return tx;
}
return null; return null;
} }
@@ -173,6 +155,8 @@ public class StreamPainter extends TrackPainter{
public void setStream(IWaveform stream) { public void setStream(IWaveform stream) {
this.stream = stream; this.stream = stream;
intervalIndex = null;
indexedEvents = null;
} }
protected ITx getTxFromEntry(int lane, int offset, IEvent[] firstTx) { protected ITx getTxFromEntry(int lane, int offset, IEvent[] firstTx) {
@@ -0,0 +1,60 @@
package com.minres.scviewer.database.tx;
import java.util.ArrayList;
import java.util.List;
import com.minres.scviewer.database.EventEntry;
import com.minres.scviewer.database.EventKind;
import com.minres.scviewer.database.IEvent;
import com.minres.scviewer.database.IEventList;
/** Immutable balanced interval index; each transaction is represented by its start event. */
public final class TxIntervalIndex {
private static final class Node {
final ITxEvent event;
final long begin, end, maxEnd;
final Node left, right;
Node(List<ITxEvent> events, int from, int to) {
int mid = (from + to) >>> 1;
event = events.get(mid);
ITx tx = event.getTransaction();
begin = tx.getBeginTime();
end = tx.getEndTime();
left = from < mid ? new Node(events, from, mid) : null;
right = mid + 1 < to ? new Node(events, mid + 1, to) : null;
long max = end;
if (left != null && Long.compareUnsigned(left.maxEnd, max) > 0) max = left.maxEnd;
if (right != null && Long.compareUnsigned(right.maxEnd, max) > 0) max = right.maxEnd;
maxEnd = max;
}
void overlapping(long from, long to, List<ITxEvent> result) {
if (Long.compareUnsigned(maxEnd, from) < 0) return;
if (left != null) left.overlapping(from, to, result);
if (Long.compareUnsigned(begin, to) > 0) return;
if (Long.compareUnsigned(end, from) >= 0) result.add(event);
if (right != null) right.overlapping(from, to, result);
}
}
private final Node root;
public TxIntervalIndex(IEventList events) {
List<ITxEvent> starts = new ArrayList<>();
for (EventEntry entry : events)
for (IEvent event : entry.events)
if (event instanceof ITxEvent && event.getKind() != EventKind.END)
starts.add((ITxEvent) event);
// EventList already orders buckets by timestamp, including same-time starts.
root = starts.isEmpty() ? null : new Node(starts, 0, starts.size());
}
/** Inclusive boundaries also include zero-duration transactions. */
public List<ITxEvent> overlapping(long from, long to) {
if (Long.compareUnsigned(from, to) > 0) throw new IllegalArgumentException("Reversed time range");
List<ITxEvent> result = new ArrayList<>();
if (root != null) root.overlapping(from, to, result);
return result;
}
}
@@ -0,0 +1,49 @@
package com.minres.scviewer.database.test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import org.junit.Test;
import com.minres.scviewer.database.EventKind;
import com.minres.scviewer.database.EventList;
import com.minres.scviewer.database.tx.TxIntervalIndex;
public class TxIntervalIndexTest {
@Test
public void spanningViewportAndBoundaries() {
EventList events = new EventList();
TxTestSupport.Tx longTx = new TxTestSupport.Tx(1, 0, 1000);
TxTestSupport.Event begin = new TxTestSupport.Event(longTx, EventKind.BEGIN);
events.put(0, begin);
events.put(1000, new TxTestSupport.Event(longTx, EventKind.END));
TxIntervalIndex index = new TxIntervalIndex(events);
assertEquals(java.util.Arrays.asList(begin), index.overlapping(400, 500));
assertEquals(1, index.overlapping(1000, 1000).size());
assertTrue(index.overlapping(1001, 1100).isEmpty());
assertTrue(new TxIntervalIndex(new EventList()).overlapping(0, 1).isEmpty());
}
@Test
public void randomizedQueriesMatchFullScan() {
Random random = new Random(42);
List<TxTestSupport.Tx> transactions = new ArrayList<>();
EventList events = new EventList();
for (int i = 0; i < 500; ++i) {
int begin = random.nextInt(1000), end = begin + random.nextInt(200);
TxTestSupport.Tx tx = new TxTestSupport.Tx(i, begin, end);
transactions.add(tx);
events.put(begin, new TxTestSupport.Event(tx, begin == end ? EventKind.SINGLE : EventKind.BEGIN));
if (begin != end) events.put(end, new TxTestSupport.Event(tx, EventKind.END));
}
TxIntervalIndex index = new TxIntervalIndex(events);
for (int i = 0; i < 100; ++i) {
int from = random.nextInt(1200), to = from + random.nextInt(200);
List<Long> expected = transactions.stream().filter(t -> t.begin <= to && t.end >= from)
.map(t -> t.id).sorted().collect(Collectors.toList());
assertEquals(expected, index.overlapping(from, to).stream()
.map(e -> e.getTransaction().getId()).sorted().collect(Collectors.toList()));
}
}
}
@@ -0,0 +1,45 @@
package com.minres.scviewer.database.test;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import com.minres.scviewer.database.EventKind;
import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.WaveformType;
import com.minres.scviewer.database.tx.ITx;
import com.minres.scviewer.database.tx.ITxAttribute;
import com.minres.scviewer.database.tx.ITxEvent;
import com.minres.scviewer.database.tx.ITxRelation;
final class TxTestSupport {
static class Tx implements ITx {
final long id, begin, end;
Tx(long id, long begin, long end) { this.id = id; this.begin = begin; this.end = end; }
public long getId() { return id; }
public long getBeginTime() { return begin; }
public long getEndTime() { return end; }
public IWaveform getStream() { return null; }
public IWaveform getGenerator() { return null; }
public List<ITxAttribute> getAttributes() { return Collections.emptyList(); }
public Collection<ITxRelation> getIncomingRelations() { return Collections.emptyList(); }
public Collection<ITxRelation> getOutgoingRelations() { return Collections.emptyList(); }
public int compareTo(ITx other) {
int c = Long.compare(begin, other.getBeginTime());
return c != 0 ? c : Long.compare(id, other.getId());
}
}
static class Event implements ITxEvent {
final Tx tx;
final EventKind kind;
int row = -1;
Event(Tx tx, EventKind kind) { this.tx = tx; this.kind = kind; }
public long getTime() { return kind == EventKind.END ? tx.end : tx.begin; }
public ITx getTransaction() { return tx; }
public EventKind getKind() { return kind; }
public int getRowIndex() { return row; }
public WaveformType getType() { return WaveformType.TRANSACTION; }
public ITxEvent duplicate() { return new Event(tx, kind); }
}
}