private < T extends BaseResourceIndex > void tryToReuseIndexEntities ( List < T > theIndexesToRemove , List < T > theIndexesToAdd ) { for ( int addIndex = 0 ; addIndex < theIndexesToAdd . size ( ) ; addIndex ++ ) { // If there are no more rows to remove, there's nothing we can reuse if ( theIndexesToRemove . isEmpty ( ) ) { break ; } T targetEntity = theIndexesToAdd . get ( addIndex ) ; if ( targetEntity . getId ( ) != null ) { continue ; } // Take a row we were going to remove, and repurpose its ID T entityToReuse = theIndexesToRemove . remove ( theIndexesToRemove . size ( ) - 1 ) ; targetEntity . setId ( entityToReuse . getId ( ) ) ; } }
The logic here is that often times when we update a resource we are dropping one index row and adding another . This method tries to reuse rows that would otherwise have been deleted by updating them with the contents of rows that would have otherwise been added . In other words we re trying to replace one delete + one insert with one update