@@ -919,12 +919,19 @@ protected function addTokensToDb($tokens, $context = '')
919919
920920 $ query = clone $ this ->addTokensToDbQueryTemplate ;
921921
922+ // Check if a single FinderIndexerToken object was given and make it to be an array of FinderIndexerToken objects
923+ $ tokens = is_array ($ tokens ) ? $ tokens : array ($ tokens );
924+
922925 // Count the number of token values.
923926 $ values = 0 ;
924927
925- // Iterate through the tokens to create SQL value sets.
926- if (!is_a ($ tokens , Token::class))
928+ // Break into chunks of no more than 128 items
929+ $ chunks = array_chunk ($ tokens , 128 );
930+
931+ foreach ($ chunks as $ tokens )
927932 {
933+ $ query ->clear ('values ' );
934+
928935 foreach ($ tokens as $ token )
929936 {
930937 if ($ filterCommon && $ token ->common )
@@ -946,38 +953,16 @@ protected function addTokensToDb($tokens, $context = '')
946953 . (int ) $ context . ', '
947954 . $ db ->quote ($ token ->language )
948955 );
949- $ values ++;
950-
951- if ($ values > 0 && ($ values % 128 ) == 0 )
952- {
953- $ db ->setQuery ($ query )->execute ();
954- $ query ->clear ('values ' );
955-
956- // Check if we're approaching the memory limit of the token table.
957- if ($ values > static ::$ state ->options ->get ('memory_table_limit ' , 10000 ))
958- {
959- $ this ->toggleTables (false );
960- }
961- }
956+ ++$ values ;
962957 }
963- }
964- else
965- {
966- $ query ->values (
967- $ db ->quote ($ tokens ->term ) . ', '
968- . $ db ->quote ($ tokens ->stem ) . ', '
969- . (int ) $ tokens ->common . ', '
970- . (int ) $ tokens ->phrase . ', '
971- . $ db ->escape ((float ) $ tokens ->weight ) . ', '
972- . (int ) $ context . ', '
973- . $ db ->quote ($ tokens ->language )
974- );
975- $ values ++;
976- }
977958
978- if ($ query ->values )
979- {
980959 $ db ->setQuery ($ query )->execute ();
960+
961+ // Check if we're approaching the memory limit of the token table.
962+ if ($ values > static ::$ state ->options ->get ('memory_table_limit ' , 10000 ))
963+ {
964+ $ this ->toggleTables (false );
965+ }
981966 }
982967
983968 return $ values ;
0 commit comments