/** * Returns a {@link PreparedStatement} to select all segments ids for a given processorName from the underlying * storage. * * @param connection the connection to the underlying database * @param processorName the name of the processor to fetch the segments for * @return a {@link PreparedStatement} that will fetch segments when executed * * @throws SQLException when an exception occurs while creating the prepared statement */ protected PreparedStatement selectForSegments(Connection connection, String processorName) throws SQLException { final String sql = "SELECT " + schema.segmentColumn() + " FROM " + schema.tokenTable() + " WHERE " + schema.processorNameColumn() + " = ? ORDER BY " + schema.segmentColumn() + " ASC"; PreparedStatement preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); preparedStatement.setString(1, processorName); return preparedStatement; }
/** * Creates a new {@link PreparedStatement} to release the current claim this node has on a token belonging to a * processor with given {@code processorName} and {@code segment}. * * @param connection the connection that should be used to create a {@link PreparedStatement} * @param processorName the name of the processor for which to release this node's claim * @param segment the segment of the processor for which to release this node's claim * @return a {@link PreparedStatement} that will release the claim this node has on the token entry * * @throws SQLException if the statement to release a claim cannot be created */ protected PreparedStatement releaseClaim(Connection connection, String processorName, int segment) throws SQLException { final String sql = "UPDATE " + schema.tokenTable() + " SET " + schema.ownerColum() + " = ?, " + schema.timestampColumn() + " = ? WHERE " + schema.processorNameColumn() + " = ? AND " + schema.segmentColumn() + " = ? AND " + schema.ownerColum() + " = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, null); preparedStatement.setString(2, formatInstant(AbstractTokenEntry.clock.instant())); preparedStatement.setString(3, processorName); preparedStatement.setInt(4, segment); preparedStatement.setString(5, nodeId); return preparedStatement; }
/** * Returns a {@link PreparedStatement} to select a token entry from the underlying storage. The {@link ResultSet} * that is returned when this statement is executed should be updatable. * * @param connection the connection to the underlying database * @param processorName the name of the processor to fetch the entry for * @param segment the segment of the processor to fetch the entry for * @return a {@link PreparedStatement} that will fetch an updatable token entry when executed * * @throws SQLException when an exception occurs while creating the prepared statement */ protected PreparedStatement selectForUpdate(Connection connection, String processorName, int segment) throws SQLException { final String sql = "SELECT " + String.join(", ", schema.processorNameColumn(), schema.segmentColumn(), schema.tokenColumn(), schema.tokenTypeColumn(), schema.timestampColumn(), schema.ownerColum()) + " FROM " + schema.tokenTable() + " WHERE " + schema.processorNameColumn() + " = ? AND " + schema.segmentColumn() + " = ? FOR UPDATE"; PreparedStatement preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); preparedStatement.setString(1, processorName); preparedStatement.setInt(2, segment); return preparedStatement; }
@Override public PreparedStatement createTable(Connection connection, TokenSchema schema) throws SQLException { String sql = "CREATE TABLE " + schema.tokenTable() + " (\n" + schema.processorNameColumn() + " VARCHAR(255) NOT NULL,\n" + schema.segmentColumn() + " INTEGER NOT NULL,\n" + schema.tokenColumn() + " " + " BLOB NULL,\n" + schema.tokenTypeColumn() + " VARCHAR(255) NULL,\n" + schema.timestampColumn() + " VARCHAR(255) NULL,\n" + schema.ownerColum() + " VARCHAR(255) NULL,\n" + "PRIMARY KEY (" + schema.processorNameColumn() + "," + schema.segmentColumn() + ")\n" + ")"; return connection.prepareStatement(sql); } }
@Override public PreparedStatement createTable(Connection connection, TokenSchema schema) throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS " + schema.tokenTable() + " (\n" + schema.processorNameColumn() + " VARCHAR(255) NOT NULL,\n" + schema.segmentColumn() + " INTEGER NOT NULL,\n" + schema.tokenColumn() + " " + tokenType() + " NULL,\n" + schema.tokenTypeColumn() + " VARCHAR(255) NULL,\n" + schema.timestampColumn() + " VARCHAR(255) NULL,\n" + schema.ownerColum() + " VARCHAR(255) NULL,\n" + "PRIMARY KEY (" + schema.processorNameColumn() + "," + schema.segmentColumn() + ")\n" + ")"; return connection.prepareStatement(sql); }
/** * Returns a {@link PreparedStatement} to select all segments ids for a given processorName from the underlying storage. * * @param connection the connection to the underlying database * @param processorName the name of the processor to fetch the segments for * @return a {@link PreparedStatement} that will fetch segments when executed * @throws SQLException when an exception occurs while creating the prepared statement */ protected PreparedStatement selectForSegments(Connection connection, String processorName) throws SQLException { final String sql = "SELECT " + schema.segmentColumn() + " FROM " + schema.tokenTable() + " WHERE " + schema.processorNameColumn() + " = ? ORDER BY " + schema.segmentColumn() + " ASC"; PreparedStatement preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); preparedStatement.setString(1, processorName); return preparedStatement; }
/** * Returns a {@link PreparedStatement} to select all segments ids for a given processorName from the underlying * storage. * * @param connection the connection to the underlying database * @param processorName the name of the processor to fetch the segments for * @return a {@link PreparedStatement} that will fetch segments when executed * * @throws SQLException when an exception occurs while creating the prepared statement */ protected PreparedStatement selectForSegments(Connection connection, String processorName) throws SQLException { final String sql = "SELECT " + schema.segmentColumn() + " FROM " + schema.tokenTable() + " WHERE " + schema.processorNameColumn() + " = ? ORDER BY " + schema.segmentColumn() + " ASC"; PreparedStatement preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); preparedStatement.setString(1, processorName); return preparedStatement; }
/** * Creates a new {@link PreparedStatement} to release the current claim this node has on a token belonging to a * processor with given {@code processorName} and {@code segment}. * * @param connection the connection that should be used to create a {@link PreparedStatement} * @param processorName the name of the processor for which to release this node's claim * @param segment the segment of the processor for which to release this node's claim * @return a {@link PreparedStatement} that will release the claim this node has on the token entry * @throws SQLException if the statement to release a claim cannot be created */ protected PreparedStatement releaseClaim(Connection connection, String processorName, int segment) throws SQLException { final String sql = "UPDATE " + schema.tokenTable() + " SET " + schema.ownerColum() + " = ?, " + schema.timestampColumn() + " = ? WHERE " + schema.processorNameColumn() + " = ? AND " + schema.segmentColumn() + " = ? AND " + schema.ownerColum() + " = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, null); preparedStatement.setString(2, formatInstant(AbstractTokenEntry.clock.instant())); preparedStatement.setString(3, processorName); preparedStatement.setInt(4, segment); preparedStatement.setString(5, nodeId); return preparedStatement; }
/** * Creates a new {@link PreparedStatement} to release the current claim this node has on a token belonging to a * processor with given {@code processorName} and {@code segment}. * * @param connection the connection that should be used to create a {@link PreparedStatement} * @param processorName the name of the processor for which to release this node's claim * @param segment the segment of the processor for which to release this node's claim * @return a {@link PreparedStatement} that will release the claim this node has on the token entry * * @throws SQLException if the statement to release a claim cannot be created */ protected PreparedStatement releaseClaim(Connection connection, String processorName, int segment) throws SQLException { final String sql = "UPDATE " + schema.tokenTable() + " SET " + schema.ownerColum() + " = ?, " + schema.timestampColumn() + " = ? WHERE " + schema.processorNameColumn() + " = ? AND " + schema.segmentColumn() + " = ? AND " + schema.ownerColum() + " = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, null); preparedStatement.setString(2, formatInstant(AbstractTokenEntry.clock.instant())); preparedStatement.setString(3, processorName); preparedStatement.setInt(4, segment); preparedStatement.setString(5, nodeId); return preparedStatement; }
/** * Returns a {@link PreparedStatement} to select a token entry from the underlying storage. The {@link ResultSet} * that is returned when this statement is executed should be updatable. * * @param connection the connection to the underlying database * @param processorName the name of the processor to fetch the entry for * @param segment the segment of the processor to fetch the entry for * @return a {@link PreparedStatement} that will fetch an updatable token entry when executed * @throws SQLException when an exception occurs while creating the prepared statement */ protected PreparedStatement selectForUpdate(Connection connection, String processorName, int segment) throws SQLException { final String sql = "SELECT " + String.join(", ", schema.processorNameColumn(), schema.segmentColumn(), schema.tokenColumn(), schema.tokenTypeColumn(), schema.timestampColumn(), schema.ownerColum()) + " FROM " + schema.tokenTable() + " WHERE " + schema.processorNameColumn() + " = ? AND " + schema.segmentColumn() + " = ? FOR UPDATE"; PreparedStatement preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); preparedStatement.setString(1, processorName); preparedStatement.setInt(2, segment); return preparedStatement; }
/** * Returns a {@link PreparedStatement} to select a token entry from the underlying storage. The {@link ResultSet} * that is returned when this statement is executed should be updatable. * * @param connection the connection to the underlying database * @param processorName the name of the processor to fetch the entry for * @param segment the segment of the processor to fetch the entry for * @return a {@link PreparedStatement} that will fetch an updatable token entry when executed * * @throws SQLException when an exception occurs while creating the prepared statement */ protected PreparedStatement selectForUpdate(Connection connection, String processorName, int segment) throws SQLException { final String sql = "SELECT " + String.join(", ", schema.processorNameColumn(), schema.segmentColumn(), schema.tokenColumn(), schema.tokenTypeColumn(), schema.timestampColumn(), schema.ownerColum()) + " FROM " + schema.tokenTable() + " WHERE " + schema.processorNameColumn() + " = ? AND " + schema.segmentColumn() + " = ? FOR UPDATE"; PreparedStatement preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE); preparedStatement.setString(1, processorName); preparedStatement.setInt(2, segment); return preparedStatement; }
@Override public PreparedStatement createTable(Connection connection, TokenSchema schema) throws SQLException { String sql = "CREATE TABLE " + schema.tokenTable() + " (\n" + schema.processorNameColumn() + " VARCHAR(255) NOT NULL,\n" + schema.segmentColumn() + " INTEGER NOT NULL,\n" + schema.tokenColumn() + " " + " BLOB NULL,\n" + schema.tokenTypeColumn() + " VARCHAR(255) NULL,\n" + schema.timestampColumn() + " VARCHAR(255) NULL,\n" + schema.ownerColum() + " VARCHAR(255) NULL,\n" + "PRIMARY KEY (" + schema.processorNameColumn() + "," + schema.segmentColumn() + ")\n" + ")"; return connection.prepareStatement(sql); } }
@Override public PreparedStatement createTable(Connection connection, TokenSchema schema) throws SQLException { String sql = "CREATE TABLE " + schema.tokenTable() + " (\n" + schema.processorNameColumn() + " VARCHAR(255) NOT NULL,\n" + schema.segmentColumn() + " INTEGER NOT NULL,\n" + schema.tokenColumn() + " " + " BLOB NULL,\n" + schema.tokenTypeColumn() + " VARCHAR(255) NULL,\n" + schema.timestampColumn() + " VARCHAR(255) NULL,\n" + schema.ownerColum() + " VARCHAR(255) NULL,\n" + "PRIMARY KEY (" + schema.processorNameColumn() + "," + schema.segmentColumn() + ")\n" + ")"; return connection.prepareStatement(sql); } }
@Override public PreparedStatement createTable(Connection connection, TokenSchema schema) throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS " + schema.tokenTable() + " (\n" + schema.processorNameColumn() + " VARCHAR(255) NOT NULL,\n" + schema.segmentColumn() + " INTEGER NOT NULL,\n" + schema.tokenColumn() + " " + tokenType() + " NULL,\n" + schema.tokenTypeColumn() + " VARCHAR(255) NULL,\n" + schema.timestampColumn() + " VARCHAR(255) NULL,\n" + schema.ownerColum() + " VARCHAR(255) NULL,\n" + "PRIMARY KEY (" + schema.processorNameColumn() + "," + schema.segmentColumn() + ")\n" + ")"; return connection.prepareStatement(sql); }
@Override public PreparedStatement createTable(Connection connection, TokenSchema schema) throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS " + schema.tokenTable() + " (\n" + schema.processorNameColumn() + " VARCHAR(255) NOT NULL,\n" + schema.segmentColumn() + " INTEGER NOT NULL,\n" + schema.tokenColumn() + " " + tokenType() + " NULL,\n" + schema.tokenTypeColumn() + " VARCHAR(255) NULL,\n" + schema.timestampColumn() + " VARCHAR(255) NULL,\n" + schema.ownerColum() + " VARCHAR(255) NULL,\n" + "PRIMARY KEY (" + schema.processorNameColumn() + "," + schema.segmentColumn() + ")\n" + ")"; return connection.prepareStatement(sql); }