Class ChildKeyComponentGenerator

java.lang.Object
io.bloviate.gen.AbstractDataGenerator<Integer>
io.bloviate.gen.ChildKeyComponentGenerator
All Implemented Interfaces:
DataGenerator<Integer>, IndexedDataGenerator

public class ChildKeyComponentGenerator extends AbstractDataGenerator<Integer> implements IndexedDataGenerator
Emits one component of a child table's composite key when each parent has a variable number of children (see ChildCardinality), which the fixed-cardinality CompositeKeyComponentGenerator cannot express.

The child key is (parent-key-components..., sequence): every child row repeats its parent's key and adds a 1-based sequence number within the parent. A generator instance emits a single one of those components, in one of two modes:

  • parent component — the value of one dimension of the parent's key. Computed from the parent's global index with the same start + ((parentIndex / repeat) % cycle) formula used by the parent's own CompositeKeyComponentGenerator, so the values line up exactly.
  • sequence — the child's position within its parent, as start + positionWithinParent (so start = 1 yields 1, 2, 3, ...).

Each instance walks the parents incrementally with a private cursor (parentIndex, childrenRemaining, position), advancing by one child on every generate() and re-reading the next parent's child count from the shared ChildCardinality when the current parent is exhausted. Because the cardinality is deterministic and each key column is generated exactly once per row, the separate generators for the different key components advance in lockstep and produce a consistent child-key tuple per row, with no shared mutable state. Parents with a zero child count are skipped.

The produced values do not depend on the random source.

  • Method Details

    • generate

      public Integer generate()
      Description copied from interface: DataGenerator
      Generates a random value of type T.
      Specified by:
      generate in interface DataGenerator<Integer>
      Returns:
      a generated value, may be null if the generator supports nullable columns
    • seek

      public void seek(long rowIndex)
      Positions this generator so the next DataGenerator.generate() returns the value for the given absolute (0-based) row index.

      Positions the cursor on the parent that owns the rowIndex-th child and the offset within it, so the next generate() returns exactly what a sequential walk would. When the cardinality is fixed this is O(1); otherwise it is a single O(parentCount) scan that accumulates child counts (done once per partition, then generation walks sequentially).

      Specified by:
      seek in interface IndexedDataGenerator
      Parameters:
      rowIndex - the absolute, 0-based row index to seek to; must be >= 0
    • set

      public void set(Connection connection, PreparedStatement statement, int parameterIndex, Integer value) throws SQLException
      Description copied from interface: DataGenerator
      Sets a specific value on a PreparedStatement parameter.
      Specified by:
      set in interface DataGenerator<Integer>
      Overrides:
      set in class AbstractDataGenerator<Integer>
      Parameters:
      connection - the database connection
      statement - the prepared statement to set the parameter on
      parameterIndex - the 1-based parameter index
      value - the value to set
      Throws:
      SQLException - if the database operation fails
    • get

      public Integer get(ResultSet resultSet, int columnIndex) throws SQLException
      Description copied from interface: DataGenerator
      Retrieves a value from a ResultSet column.
      Specified by:
      get in interface DataGenerator<Integer>
      Parameters:
      resultSet - the result set to read from
      columnIndex - the 1-based column index
      Returns:
      the value from the result set, properly cast to type T
      Throws:
      SQLException - if the database operation fails