private boolean announceHistoricalSegment ( final Handle handle , final DataSegment segment , final boolean used ) throws IOException { try { if ( segmentExists ( handle , segment ) ) { log . info ( "Found [%s] in DB, not updating DB" , segment . getId ( ) ) ; return false ; } // SELECT -> INSERT can fail due to races; callers must be prepared to retry. // Avoiding ON DUPLICATE KEY since it's not portable. // Avoiding try/catch since it may cause inadvertent transaction-splitting. handle . createStatement ( StringUtils . format ( "INSERT INTO %1$s (id, dataSource, created_date, start, %2$send%2$s, partitioned, version, used, payload) " + "VALUES (:id, :dataSource, :created_date, :start, :end, :partitioned, :version, :used, :payload)" , dbTables . getSegmentsTable ( ) , connector . getQuoteString ( ) ) ) . bind ( "id" , segment . getId ( ) . toString ( ) ) . bind ( "dataSource" , segment . getDataSource ( ) ) . bind ( "created_date" , DateTimes . nowUtc ( ) . toString ( ) ) . bind ( "start" , segment . getInterval ( ) . getStart ( ) . toString ( ) ) . bind ( "end" , segment . getInterval ( ) . getEnd ( ) . toString ( ) ) . bind ( "partitioned" , ( segment . getShardSpec ( ) instanceof NoneShardSpec ) ? false : true ) . bind ( "version" , segment . getVersion ( ) ) . bind ( "used" , used ) . bind ( "payload" , jsonMapper . writeValueAsBytes ( segment ) ) . execute ( ) ; log . info ( "Published segment [%s] to DB with used flag [%s]" , segment . getId ( ) , used ) ; } catch ( Exception e ) { log . error ( e , "Exception inserting segment [%s] with used flag [%s] into DB" , segment . getId ( ) , used ) ; throw e ; } return true ; }
Attempts to insert a single segment to the database . If the segment already exists will do nothing ; although this checking is imperfect and callers must be prepared to retry their entire transaction on exceptions .