private long getAvgSizePerGranularity ( String datasource ) { return connector . retryWithHandle ( new HandleCallback < Long > ( ) { Set < Interval > intervals = new HashSet <> ( ) ; long totalSize = 0 ; @ Override public Long withHandle ( Handle handle ) { handle . createQuery ( StringUtils . format ( "SELECT start,%1$send%1$s,payload FROM %2$s WHERE used = true AND dataSource = :dataSource" , connector . getQuoteString ( ) , dbTables . get ( ) . getSegmentsTable ( ) ) ) . bind ( "dataSource" , datasource ) . map ( new ResultSetMapper < Object > ( ) { @ Override public Object map ( int index , ResultSet r , StatementContext ctx ) throws SQLException { try { intervals . add ( Intervals . utc ( DateTimes . of ( r . getString ( "start" ) ) . getMillis ( ) , DateTimes . of ( r . getString ( "end" ) ) . getMillis ( ) ) ) ; DataSegment segment = objectMapper . readValue ( r . getBytes ( "payload" ) , DataSegment . class ) ; totalSize += segment . getSize ( ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } return null ; } } ) . first ( ) ; return intervals . isEmpty ( ) ? 0L : totalSize / intervals . size ( ) ; } } ) ; }
calculate the average data size per segment granularity for a given datasource .