Tables from functions
There are two factories that allow you to create a table from the result of a function:
- The
HashSetBackedTableFactory
creates a table with one row for each SmartKey in a hash set (order is not preserved). - The
FunctionGeneratedTableFactory
allows you to assemble a table in your function, and the contents of the output table are replaced with the new values.
HashSetBackedTableFactory
An abstract table that represents a HashSet of SmartKeys. Since we are representing a set, we are not defining an order to our output. Whatever order the table happens to end up in, is fine. The table will refresh by regenerating the full HashSet (using the setGenerator function passed in); and then comparing that to the existing HashSet.
/**
* Create a ticking table based on a setGenerator.
*
* @param setGenerator a function that returns a HashSet of SmartKeys; each SmartKey is a row in the output.
* @param refreshIntervalMs how often to refresh the table, if <= 0 the table does not tick.
* @param colNames the column names for the output table, must match the number of elements in each SmartKey.
* @return a table representing the Set returned by the setGenerator
*/
com.illumon.iris.db.v2.utils.HashSetBackedTableFactory.create(Function.Nullary<HashSet<SmartKey>> setGenerator, int refreshIntervalMs, String... colNames)
FunctionGeneratedTableFactory
This factory allows you to create an arbitrary table using operations like pulling data out of another table and manipulating it using Java code. The table will refresh by regenerating the full values (using the tableGenerator function passed in). The resultant table's values are copied into the result table and appropriate listener notifications are fired.
All of the rows in the output table are modified on every tick, even if no actual changes occurred. The output table also has a contiguous index.
The generator function must produce a table, and the table definition must not change between invocations.
If you are transforming a table, you should generally use the regular table operations as opposed to this factory because table operations are capable of performing some operations incrementally. However, for small tables, this might prove to require less development effort.
There are two creation methods, and each has a different refresh policy. One is time-based and the other is based on a set of ticking tables.
Time-based
public static Table create(Function.Nullary<Table> tableGenerator, int refreshIntervalMs);
Ticking tables-based
public static Table create(Function.Nullary<Table> tableGenerator, DynamicTable ...sourceTables);
The query engine does not know the details of your function inputs. If you are dependent on a ticking table in your tableGenerator
function, you can add it to the sourceTables
list so the function will be recomputed on each tick.
Note
If you have an expensive computation, keep in mind it will be recomputed - from scratch - each time any of the tables listed in sourceTables
tick, even if your result would not change.
An appropriate use of these methods would be to reformat data that you've used other table operations to create, without the need to cast things like transposition into a table operation.