@Override public byte[] lookup( final String tableName, final String keyColumn, final String valueColumn, final String key ) { return getDBI().withHandle( new HandleCallback<byte[]>() { @Override public byte[] withHandle(Handle handle) { return lookupWithHandle(handle, tableName, keyColumn, valueColumn, key); } } ); }
public <T> T retryWithHandle( HandleCallback<T> callback, Predicate<Throwable> myShouldRetry ) { try { return RetryUtils.retry(() -> getDBI().withHandle(callback), myShouldRetry, MAX_RETRIES); } catch (Exception e) { throw new RuntimeException(e); } }
public <T> T retryWithHandle( final HandleCallback<T> callback, final Predicate<Throwable> myShouldRetry ) { try { return RetryUtils.retry(() -> getDBI().withHandle(callback), myShouldRetry, DEFAULT_MAX_TRIES); } catch (Exception e) { throw Throwables.propagate(e); } }
protected final <T> T inReadOnlyTransaction( final TransactionCallback<T> callback ) { return getDBI().withHandle( new HandleCallback<T>() { @Override public T withHandle(Handle handle) throws Exception { final Connection connection = handle.getConnection(); final boolean readOnly = connection.isReadOnly(); connection.setReadOnly(true); try { return handle.inTransaction(callback); } finally { try { connection.setReadOnly(readOnly); } catch (SQLException e) { // at least try to log it so we don't swallow exceptions log.error(e, "Unable to reset connection read-only state"); } } } } ); }
private <T> T inReadOnlyTransaction(final TransactionCallback<T> callback) { return getDbi().withHandle( new HandleCallback<T>() { @Override public T withHandle(Handle handle) throws Exception { final Connection connection = handle.getConnection(); final boolean readOnly = connection.isReadOnly(); connection.setReadOnly(true); try { return handle.inTransaction(callback); } finally { try { connection.setReadOnly(readOnly); } catch (SQLException e) { // at least try to log it so we don't swallow exceptions LOGGER.error(e, "Unable to reset connection read-only state"); } } } } ); }
return getDBI().withHandle( new HandleCallback<Void>()
@Override public boolean enableSegment(final String segmentId) { try { connector.getDBI().withHandle( new HandleCallback<Void>() { @Override public Void withHandle(Handle handle) { handle.createStatement(StringUtils.format("UPDATE %s SET used=true WHERE id = :id", getSegmentsTable())) .bind("id", segmentId) .execute(); return null; } } ); } catch (Exception e) { log.error(e, "Exception enabling segment %s", segmentId); return false; } return true; }
private void dropTable(final String tableName) { connector.getDBI().withHandle( new HandleCallback<Void>() { @Override public Void withHandle(Handle handle) { handle.createStatement(StringUtils.format("DROP TABLE %s", tableName)) .execute(); return null; } } ); }
private void dropTable(final String tableName) { derbyConnector.getDBI().withHandle( new HandleCallback<Void>() { @Override public Void withHandle(Handle handle) { handle.createStatement(StringUtils.format("DROP TABLE %s", tableName)) .execute(); return null; } } ); }
private void dropTable(final String tableName) { connector.getDBI().withHandle( new HandleCallback<Void>() { @Override public Void withHandle(Handle handle) { handle.createStatement(StringUtils.format("DROP TABLE %s", tableName)) .execute(); return null; } } ); }
private Long lastUpdates(CacheScheduler.EntryImpl<JdbcExtractionNamespace> id, JdbcExtractionNamespace namespace) { final DBI dbi = ensureDBI(id, namespace); final String table = namespace.getTable(); final String tsColumn = namespace.getTsColumn(); if (tsColumn == null) { return null; } final Timestamp update = dbi.withHandle( new HandleCallback<Timestamp>() { @Override public Timestamp withHandle(Handle handle) { final String query = StringUtils.format( "SELECT MAX(%s) FROM %s", tsColumn, table ); return handle .createQuery(query) .map(TimestampMapper.FIRST) .first(); } } ); return update.getTime(); } }
/** * @param connector SQL metadata connector to the metadata storage * @param metadataStorageTablesConfig Table config * * @return all the active data sources in the metadata storage */ static Collection<String> getAllDataSourceNames(SQLMetadataConnector connector, final MetadataStorageTablesConfig metadataStorageTablesConfig) { return connector.getDBI() .withHandle((HandleCallback<List<String>>) handle -> handle.createQuery(String.format( "SELECT DISTINCT(datasource) FROM %s WHERE used = true", metadataStorageTablesConfig.getSegmentsTable())) .fold(Lists.<String>newArrayList(), (druidDataSources, stringObjectMap, foldController, statementContext) -> { druidDataSources.add(MapUtils.getString(stringObjectMap, "datasource")); return druidDataSources; })); }
@Override public Collection<String> getAllDataSourceNames() { return connector.getDBI().withHandle( handle -> handle.createQuery( StringUtils.format("SELECT DISTINCT(datasource) FROM %s", getSegmentsTable()) ) .fold( new ArrayList<>(), new Folder3<List<String>, Map<String, Object>>() { @Override public List<String> fold( List<String> druidDataSources, Map<String, Object> stringObjectMap, FoldController foldController, StatementContext statementContext ) { druidDataSources.add( MapUtils.getString(stringObjectMap, "datasource") ); return druidDataSources; } } ) ); }
private void dropTable(final String tableName) { Assert.assertNull(connector.getDBI().withHandle( new HandleCallback<Void>() { @Override public Void withHandle(Handle handle) { handle.createStatement(StringUtils.format("DROP TABLE %s", tableName)) .execute(); return null; } } )); } }
private boolean removeSegmentFromTable(String segmentId) { final int removed = connector.getDBI().withHandle( handle -> handle .createStatement(StringUtils.format("UPDATE %s SET used=false WHERE id = :segmentID", getSegmentsTable())) .bind("segmentID", segmentId) .execute() ); return removed > 0; }
@After public void cleanup() { connector.getDBI().withHandle( new HandleCallback<Void>() { @Override public Void withHandle(Handle handle) { handle.createStatement(StringUtils.format("DROP TABLE %s", tablesConfig.getSupervisorTable())) .execute(); return null; } } ); } }
@Override public boolean removeDataSource(final String dataSource) { try { final int removed = connector.getDBI().withHandle( handle -> handle.createStatement( StringUtils.format("UPDATE %s SET used=false WHERE dataSource = :dataSource", getSegmentsTable()) ).bind("dataSource", dataSource).execute() ); dataSources.remove(dataSource); if (removed == 0) { return false; } } catch (Exception e) { log.error(e, "Error removing datasource %s", dataSource); return false; } return true; }
private void unUseSegment() { for (final DataSegment segment : SEGMENTS) { Assert.assertEquals( 1, (int) derbyConnector.getDBI().<Integer>withHandle( new HandleCallback<Integer>() { @Override public Integer withHandle(Handle handle) { String request = StringUtils.format( "UPDATE %s SET used = false WHERE id = :id", derbyConnectorRule.metadataTablesConfigSupplier().get().getSegmentsTable() ); return handle.createStatement(request).bind("id", segment.getId().toString()).execute(); } } ) ); } }
private List<DataSegment> getUsedSegmentsList(DerbyConnectorTestUtility connector, final MetadataStorageTablesConfig metadataStorageTablesConfig) { return connector.getDBI() .withHandle(handle -> handle.createQuery(String.format( "SELECT payload FROM %s WHERE used=true ORDER BY created_date ASC", metadataStorageTablesConfig.getSegmentsTable())) .map((i, resultSet, statementContext) -> { try { return DruidStorageHandlerUtils.JSON_MAPPER.readValue(resultSet.getBytes("payload"), DataSegment.class); } catch (IOException e) { throw Throwables.propagate(e); } }).list()); }
derbyConnector.getDBI().withHandle( (handle) -> { Batch batch = handle.createBatch();