snapshotWhen
The snapshotWhen
method produces an in-memory copy of a source table that adds a new snapshot when another table, the trigger table, changes.
The trigger table is often a time table, a special type of table that adds new rows at a regular, user-defined interval.
When snapshotWhen
stores table history, it stores a copy of the source table for every trigger event. This means large source tables or rapidly changing trigger tables can result in large memory usage.
Syntax
result = source.snapshotWhen(trigger)
result = source.snapshotWhen(trigger, features...)
result = source.snapshotWhen(trigger, features, stampColumns...)
result = source.snapshotWhen(trigger, options)
Parameters
Parameter | Type | Description |
---|---|---|
trigger | Table | The table that triggers the snapshot. This should be a ticking table, as changes in this table trigger the snapshot. |
features | SnapshotWhenOptions.Flag... | The snapshot features. |
features | Collection<SnapshotWhenOptions.Flag> | The snapshot features. |
stampColumns | String... | The stamp columns. |
options | SnapshotWhenOptions | The options dictate whether to take an initial snapshot, do incremental snapshots, keep history, and set stamp columns. |
The trigger table's stamp column(s) appear in the result table. If the source table has a column with the same name as the stamp column, an error will be raised. To avoid this problem, rename the stamp column in the trigger table using renameColumns
.
Returns
An in-memory history of a source table that adds a new snapshot when the trigger table updates.
SnapshotWhenOptions
The snapshotWhen
method's options
parameter takes a SnapshotWhenOptions
object. These are constructed using SnapshotWhenOptions.of(...)
. The syntax is given below.
import io.deephaven.api.snapshot.SnapshotWhenOptions
myOpts = SnapshotWhenOptions.of(INITIAL, INCREMENTAL, HISTORY, stampColumns)
Where INITIAL
, INCREMENTAL
, and HISTORY
are booleans and stampColumns
is one or more string values corresponding to columns in the trigger
table.
INITIAL
(Boolean): Whether or not to take an initial snapshot upon creating the result table. The default isfalse
.INCREMENTAL
(Boolean): Determines whether the resulting table should be incremental. The default value isfalse
. Whenfalse
, the stamp column in the resulting table will always contain the latest value from the stamp column in the trigger table. This means that every row in the resulting table will be updated each cycle. Whentrue
, only rows that have been added or updated to the source table since the last snapshot will have the latest value from the stamp column.HISTORY
(Boolean): Determines whether the resulting table should keep history. The default value isfalse
. Whentrue
, a full snapshot of the source table and the stamp column is appended to the resulting table every time the trigger table changes. This means that the resulting table will grow very fast. Whenfalse
, only rows from the source table that have changed since the last snapshot will be appended to the resulting table.stampColumns
(String...): One or more column names to act as stamp columns. Each stamp column will be included in the final result and will contain the value of the stamp column from the trigger table at the time of the snapshot. The default value isNone
, meaning all trigger table columns will be appended to the source table.
If HISTORY
is true
, INITIAL
and INCREMENTAL
must be false
.
Examples
In the following example, the source
table updates once every second. The trigger
table updates once every five seconds. Thus, the result
table updates once every five seconds. To avoid a name conflict error, the Timestamp
column in the trigger
is renamed.
source = timeTable("PT00:00:01").updateView("X = i")
trigger = timeTable("PT00:00:05").renameColumns("TriggerTimestamp = Timestamp").updateView("Y = Math.sin(0.1 * i)")
result = source.snapshotWhen(trigger)
Notice three things:
stamp_cols
is left blank, so every column fromtrigger
is included inresult
.incremental
isfalse
, so the entireTriggerTimestamp
column inresult
is updated every cycle and always contains the latest value from theTriggerTimestamp
column intrigger
.historical
isfalse
, so only updated rows fromsource
get appended toresult
on each snapshot.
In the following example, the code is nearly identical to the one above it. However, in this case, the Y
column is given as the stamp key. Thus, the Timestamp
column in the trigger
table is omitted from the result
table, which avoids a name conflict error. This is an alternative to renaming the column in the trigger table.
import io.deephaven.api.snapshot.SnapshotWhenOptions
myOpts = SnapshotWhenOptions.of(false, false, false, "Y")
source = timeTable("PT00:00:01").updateView("X = i")
trigger = timeTable("PT00:00:05").updateView("Y = Math.sin(0.1 * i)")
result = source.snapshotWhen(trigger, myOpts)
In the following example, HISTORY
is set to true
. Therefore, every row in source
gets snapshotted and appended to result
when trigger
changes, regardless of whether source
has changed.
import io.deephaven.api.snapshot.SnapshotWhenOptions
myOpts = SnapshotWhenOptions.of(false, false, true, "Y")
source = timeTable("PT00:00:01").updateView("X = i")
trigger = timeTable("PT00:00:05").updateView("Y = Math.sin(0.1 * i)")
result = source.snapshotWhen(trigger, myOpts)
In the following example, INCREMENTAL
is set to true
. Thus, the Y
column in result
only updates when corresponding rows in trigger
have changed. Contrast this with the first and second examples given above.
import io.deephaven.api.snapshot.SnapshotWhenOptions
myOpts = SnapshotWhenOptions.of(false, true, false, "Y")
source = timeTable("PT00:00:01").updateView("X = i")
trigger = timeTable("PT00:00:05").updateView("Y = Math.sin(0.1 * i)")
result = source.snapshotWhen(trigger, myOpts)