Event Caches

An event cache is a special SPLASH data structure for grouping and storing events from an input stream. Events are grouped into buckets. You can run aggregate operations like count, sum, and max over a bucket.

You declare an event cache in the Local block of a stream using the name of the input stream. For example, this declares an event cache for the input stream Trades:
eventCache(Trades) events;
You can have as many event caches as you wish per input stream, so you can declare this in the same stream:
eventCache(Trades) moreEvents;
By default, the buckets are determined by the keys of the input stream. For example, if you have an input stream Trades with two buckets, one for events with Symbol "T" and one with symbol "CSCO":
[ Symbol='T'; | Shares=10; Price=22.88; ]
[ Symbol='CSCO'; | Shares=50; Price=15.66; ]
Each event—whether an insert, update, or delete—is put into the corresponding bucket. For instance, if a delete event carrying this bucket comes into the stream, then there are two events in the bucket for "CSCO":
[ Symbol='CSCO'; | Shares=50; Price=15.66; ]
You can change that behavior by declaring the event cache to coalesce events:
eventCache(Trades, coalesce) events;
In this case, the bucket for "CSCO" then has no events.

You can compute over buckets through aggregate operations. For instance, if you want to compute the total number of shares in a bucket, write sum(events.Shares). Which bucket gets selected? By default, it's the bucket associated with the current event that came from the input stream. You can change the bucket with the keyCache operation.

There are ways to change the way buckets are stored. For example, you can group events into buckets by specifying columns, say by Trades that have the same number of shares:
eventCache(Trades[Shares]) eventsByShares;
Or group the same number of shares and the same symbol:
eventCache(Trades[Symbol, Shares]) eventsBySymbolShares;
Or group in one large bucket:
eventCache(Trades[]) eventsAll;
You can also order the events in the bucket by field:
eventCache(Trades, Price desc) eventsOrderByPrice;
This orders the events by descending order of Price. You can use the nth operation to get the individual elements in that order.
If the input stream has many updates, buckets can become very big. You can control the size of buckets either by specifying a maximum number of events or a maximum amount of time or both. For example, you can set a maximum number of 10 events per bucket:
eventCache(Trades[Symbol], 10 events) eventsBySymbol10Events;
Or set a maximum age of 20 seconds:
eventCache(Trades[Symbol], 20 seconds) eventsBySymbol20Seconds;
Or both:
eventCache(Trades[Symbol], 10 events, 20 seconds) eventsSmall;