private void computeLinkFanouts ( TableDefinition tableDef , Map < String , MutableFloat > tableLinkFanoutMap ) { m_logger . info ( "Computing link field fanouts for table: {}" , tableDef . getTableName ( ) ) ; StringBuilder buffer = new StringBuilder ( ) ; for ( FieldDefinition fieldDef : tableDef . getFieldDefinitions ( ) ) { if ( fieldDef . isLinkField ( ) ) { if ( buffer . length ( ) > 0 ) { buffer . append ( "," ) ; } buffer . append ( fieldDef . getName ( ) ) ; } } if ( buffer . length ( ) == 0 ) { return ; } Map < String , String > queryParams = new HashMap <> ( ) ; queryParams . put ( "q" , "*" ) ; queryParams . put ( "f" , buffer . toString ( ) ) ; queryParams . put ( "s" , Integer . toString ( LINK_FANOUT_SAMPLE_SIZE ) ) ; if ( m_session instanceof OLAPSession ) { queryParams . put ( "shards" , m_config . shard ) ; } QueryResult qResult = m_session . objectQuery ( tableDef . getTableName ( ) , queryParams ) ; Collection < DBObject > objectSet = qResult . getResultObjects ( ) ; if ( objectSet . size ( ) == 0 ) { return ; } Map < String , AtomicInteger > linkValueCounts = new HashMap < String , AtomicInteger > ( ) ; int totalObjs = 0 ; for ( DBObject dbObj : objectSet ) { totalObjs ++ ; for ( String fieldName : dbObj . getFieldNames ( ) ) { if ( tableDef . isLinkField ( fieldName ) ) { Collection < String > linkValues = dbObj . getFieldValues ( fieldName ) ; AtomicInteger totalLinkValues = linkValueCounts . get ( fieldName ) ; if ( totalLinkValues == null ) { linkValueCounts . put ( fieldName , new AtomicInteger ( linkValues . size ( ) ) ) ; } else { totalLinkValues . addAndGet ( linkValues . size ( ) ) ; } } } } for ( String fieldName : linkValueCounts . keySet ( ) ) { AtomicInteger totalLinkValues = linkValueCounts . get ( fieldName ) ; float linkFanout = totalLinkValues . get ( ) / ( float ) totalObjs ; // may round to 0 m_logger . info ( "Average fanout for link {}: {}" , fieldName , linkFanout ) ; tableLinkFanoutMap . put ( fieldName , new MutableFloat ( linkFanout ) ) ; } }
Compute link fanouts for the given table