views:

29

answers:

2

(I'm using C# but this isn't necessarily platform-specific)

Say I have a table of values, which are indexed by a triplet of keys (A, B, C). The values in the table are streaming in from an outside source at sporadic intervals. I want to publish aggregates over various sets of keys, e.g. calculate a sum over all values where B = b1.

This is trivial to do if I just iterate over the entire table each time, but obviously that's not efficient. What I'm wondering is, is there a particularly good way to design such a thing so that I only update sum(B = b1) when a value in the table changes that would affect this sum? It seems like I would need to create some sort of Aggregation object that maintains a list of every value that is included in that aggregation, but I feel like there might be a more elegant way that's escaping me. Something like a "real time" LINQ query...

A: 

Well, for the simple example that you presented, why not have an event, say, OnValueChanging that signals the value that is about to change? Then you can record the current value, say, x. Then fire another event OnValueChanged and record the new value, say, y. Then the sum that you want to update is currentSum - x + y.

Jason
that would work, but the values are coming from another machine. I guess the message that's sent out could include both the new value and the old value, I hadn't thought of that.
toasteroven
+1  A: 

Use a Dictionary<TypeOfB, int>. Everytime you add a new b, do

dictionary[b] += value;

If a value changes, do

dictionary[b] += (newValue - oldValue)
BlueRaja - Danny Pflughoeft
You stated below the machine is only sending the new value. I was under the impression it was sending four things: `(a,b,c)` and `value`, so that you could keep track of the old value on the receiving end. Otherwise, if the new value is not actually needed for anything on the receiving end, you could simply send `(newValue - oldValue)` (and `b`, of course) to update the sum.
BlueRaja - Danny Pflughoeft