Build and freeze transaction events in bulk

This commit is contained in:
eyck committed 2026-11-14 17:38:02 +01:00
1 parent e78c65a36c
commit 75fa1a4f31
10 files changed
+146 -22

No files matched your search

@@ -7,6 +7,36 @@ import java.util.NoSuchElementException;
/** Sorted timestamp buckets. Range views are read-only and bounded by their parent. */
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 int start;
private int end;
@@ -70,9 +70,9 @@ public interface IEventList extends Iterable<EventEntry> {
* {@code fromKey} to {@code toKey}. If {@code fromKey} and
* {@code toKey} are equal, the returned map is empty unless
* {@code fromInclusive} is true. The
* returned map is backed by this map, so changes in the returned map are
* reflected in this map, and vice-versa. The returned map supports all
* optional map operations that this map supports.
* returned view is read-only. Do not structurally modify a mutable source
* while a view is in use. EventList.Builder produces frozen sources suitable
* for sharing range views.
*
* @param fromKey low endpoint of the keys in the returned map
* @param fromInclusive {@code true} if the low endpoint