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
+195 -55

No files matched your search

@@ -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); }
}
}