public interface StreamerIndex<E,K,V> extends Iterable<StreamerIndexEntry<E,K,V>>
Streamer index can be accessed from StreamerWindow
via any of the following methods:
Indexes are created and provided for streamer windows by StreamerIndexProvider
which is
specified in streamer configuration.
Stock price events are streamed into the system, the stock price event is an object containing stock symbol and price. We need to get minimum price for GOOG instrument.
Here is StreamerIndexUpdater
that maintains index values up to date:
class StockPriceIndexUpdater implements GridStreamerIndexUpdater<StockPriceEvent, String, Double> { @Nullable @Override public String indexKey(StockPriceEvent evt) { return evt.getSymbol(); // Symbol is an index key. } @Nullable @Override public Double initialValue(StockPriceEvent evt, String key) { return evt.getPrice(); // Set first event's price as an initial value. } @Nullable @Override public Double onAdded(GridStreamerIndexEntry<StockPriceEvent, String, Double> entry, StockPriceEvent evt) throws IgniteCheckedException { return Math.min(entry.value(), evt.getPrice()); // Update the minimum on new event. } @Nullable @Override public Double onRemoved(GridStreamerIndexEntry<StockPriceEvent, String, Double> entry, StockPriceEvent evt) { return entry.value(); // Leave minimum unchanged when event is evicted. } }
Here is the code that queries minimum price for GOOG instrument using index:
double minGooglePrice = streamer.context().reduce( // This closure will execute on remote nodes. new GridClosure<GridStreamerContext, Double>() { @Nullable @Override public Double apply(GridStreamerContext ctx) { GridStreamerIndex<StockPriceEvent, String, Double> minIdx = ctx.<StockPriceEvent>window().index("min-price"); return minIdx.entry("GOOG").value(); } }, new GridReducer<Double, Double>() { private double minPrice = Integer.MAX_VALUE; @Override public boolean collect(Double price) { minPrice = Math.min(minPrice, price); // Take minimum price from all nodes. return true; } @Override public Double reduce() { return minPrice; } } );
Modifier and Type | Method and Description |
---|---|
Collection<StreamerIndexEntry<E,K,V>> |
entries(int cnt)
Gets read-only collection of entries in the index.
|
StreamerIndexEntry<E,K,V> |
entry(K key)
Gets index entry for given key.
|
Set<StreamerIndexEntry<E,K,V>> |
entrySet(boolean asc,
V fromVal,
boolean fromIncl,
V toVal,
boolean toIncl)
Gets read-only set of index entries within given value range.
|
Set<StreamerIndexEntry<E,K,V>> |
entrySet(V val)
Gets read-only set of index entries with given value.
|
Collection<E> |
events(boolean asc,
V fromVal,
boolean fromIncl,
V toVal,
boolean toIncl)
Gets read-only collection of index events.
|
Collection<E> |
events(int cnt)
Gets read-only collection of index events.
|
Collection<E> |
events(V val)
Gets read-only collection of index events.
|
StreamerIndexEntry<E,K,V> |
firstEntry()
Gets first entry in the index.
|
Set<K> |
keySet(boolean asc,
V fromVal,
boolean fromIncl,
V toVal,
boolean toIncl)
Gets read-only set of index keys within given value range.
|
Set<K> |
keySet(int cnt)
Gets read-only set of index keys.
|
Set<K> |
keySet(V val)
Gets read-only set of index keys with given value.
|
StreamerIndexEntry<E,K,V> |
lastEntry()
Gets last entry in the index.
|
String |
name()
Index name.
|
StreamerIndexPolicy |
policy()
Gets index policy.
|
int |
size() |
boolean |
sorted()
Returns
true if index supports sorting and therefore can perform range operations. |
boolean |
unique()
Gets index unique flag.
|
Collection<V> |
values(boolean asc,
V fromVal,
boolean fromIncl,
V toVal,
boolean toIncl)
Gets read-only collection of index values within given value range.
|
Collection<V> |
values(int cnt)
Gets read-only collection of index values.
|
@Nullable String name()
boolean unique()
boolean sorted()
true
if index supports sorting and therefore can perform range operations.
Note that sorting happens by value and not by key.
StreamerIndexPolicy policy()
int size()
@Nullable StreamerIndexEntry<E,K,V> entry(K key)
key
- Key for which to retrieve entry.null
if one could not be found.Collection<StreamerIndexEntry<E,K,V>> entries(int cnt)
Returned collection is ordered for sorted indexes.
cnt
- If 0 then all entries are returned,
if positive, then returned collection contains up to cnt
elements
(in ascending order for sorted indexes),
if negative, then returned collection contains up to |cnt|
elements
(in descending order for sorted indexes and not supported for unsorted indexes).Set<K> keySet(int cnt)
Returned collection is ordered for sorted indexes.
cnt
- If 0 then all keys are returned,
if positive, then returned collection contains up to cnt
elements
(in ascending order for sorted indexes),
if negative, then returned collection contains up to |cnt|
elements
(in descending order for sorted indexes and not supported for unsorted indexes).Collection<V> values(int cnt)
Returned collection is ordered for sorted indexes.
cnt
- If 0 then all values are returned,
if positive, then returned collection contains up to cnt
elements
(in ascending order for sorted indexes),
if negative, then returned collection contains up to |cnt|
elements
(in descending order for sorted indexes and not supported for unsorted indexes).Collection<E> events(int cnt)
For sorted indexes events are guaranteed to be grouped by corresponding values, however the order of the events corresponding to the same value is not defined.
cnt
- If 0 then all values are returned,
if positive, then returned collection contains up to cnt
elements
(in ascending order of values for sorted indexes),
if negative, then returned collection contains up to |cnt|
elements
(in descending order of values for sorted indexes and not supported for unsorted indexes).IllegalStateException
- If index is not configured to track events.StreamerIndexPolicy
Set<StreamerIndexEntry<E,K,V>> entrySet(V val)
This operation is only available for sorted indexes.
val
- Value.Set<StreamerIndexEntry<E,K,V>> entrySet(boolean asc, @Nullable V fromVal, boolean fromIncl, @Nullable V toVal, boolean toIncl)
This operation is only available for sorted indexes.
asc
- True
for ascending set.fromVal
- From value, if null
, then start from beginning.toVal
- To value, if null
then include all entries until the end.fromIncl
- Whether or not left value is inclusive (ignored if minVal
is null
).toIncl
- Whether or not right value is inclusive (ignored if maxVal
is null
).Set<K> keySet(V val)
This operation is only available for sorted indexes.
val
- Value.Set<K> keySet(boolean asc, @Nullable V fromVal, boolean fromIncl, @Nullable V toVal, boolean toIncl)
This operation is only available for sorted indexes.
asc
- True
for ascending set.fromVal
- From value, if null
, then start from beginning.toVal
- To value, if null
then include all entries until the end.fromIncl
- Whether or not left value is inclusive (ignored if minVal
is null
).toIncl
- Whether or not right value is inclusive (ignored if maxVal
is null
).Collection<V> values(boolean asc, @Nullable V fromVal, boolean fromIncl, @Nullable V toVal, boolean toIncl)
This operation is only available for sorted indexes.
asc
- True
for ascending set.fromVal
- From value, if null
, then start from beginning.toVal
- To value, if null
then include all entries until the end.fromIncl
- Whether or not left value is inclusive (ignored if minVal
is null
).toIncl
- Whether or not right value is inclusive (ignored if maxVal
is null
).Collection<E> events(@Nullable V val)
This operation is only available for sorted indexes.
val
- From value, if null
, then start from beginning.Collection<E> events(boolean asc, @Nullable V fromVal, boolean fromIncl, @Nullable V toVal, boolean toIncl)
Events are guaranteed to be sorted by corresponding values, however the order of the events corresponding to the same value is not defined.
This operation is only available for sorted indexes.
asc
- True
for ascending set.fromVal
- From value, if null
, then start from beginning.toVal
- To value, if null
then include all entries until the end.fromIncl
- Whether or not left value is inclusive (ignored if minVal
is null
).toIncl
- Whether or not right value is inclusive (ignored if maxVal
is null
).@Nullable StreamerIndexEntry<E,K,V> firstEntry()
This operation is only available for sorted indexes.
null
if index is empty.@Nullable StreamerIndexEntry<E,K,V> lastEntry()
This operation is only available for sorted indexes.
null
if index is empty.
Follow @ApacheIgnite
Apache Ignite Fabric : ver. 1.0.0-RC1 Release Date : February 16 2015