Only strip newline in OrderedStreamingForkController
authorErik Bernhardson <ebernhardson@wikimedia.org>
Fri, 28 Sep 2018 17:58:00 +0000 (10:58 -0700)
committerErik Bernhardson <ebernhardson@wikimedia.org>
Fri, 28 Sep 2018 19:00:32 +0000 (12:00 -0700)
The controller should pass through lines of input exactly as
they were provided, only stripping the trailing newline that
delimits items. The trim was making `door` and `door ` equivilant
but for the use case in search the distinction is important.

Additionally check that the line is actually empty, don't throw
away inputs like '0' which are falsy.

Change-Id: Ifac910543fdb46a27da021e831e3e18befefcfc5

includes/OrderedStreamingForkController.php

index ff29cb5..11abc81 100644 (file)
@@ -134,9 +134,12 @@ class OrderedStreamingForkController extends ForkController {
         */
        protected function consumeNoFork() {
                while ( !feof( $this->input ) ) {
-                       $line = trim( fgets( $this->input ) );
-                       if ( $line ) {
-                               $result = call_user_func( $this->workCallback, $line );
+                       $data = fgets( $this->input );
+                       if ( $data[ strlen( $data ) - 1 ] == "\n" ) {
+                               $data = substr( $data, 0, -1 );
+                       }
+                       if ( strlen( $data ) !== 0 ) {
+                               $result = call_user_func( $this->workCallback, $data );
                                fwrite( $this->output, "$result\n" );
                        }
                }
@@ -160,8 +163,12 @@ class OrderedStreamingForkController extends ForkController {
                                        $this->updateAvailableSockets( $sockets, $used, $sockets ? 0 : 5 );
                                } while ( !$sockets );
                        }
-                       $data = trim( $data );
-                       if ( !$data ) {
+                       // Strip the trailing \n. The last line of a file might not have a trailing
+                       // \n though
+                       if ( $data[ strlen( $data ) - 1 ] == "\n" ) {
+                               $data = substr( $data, 0, -1 );
+                       }
+                       if ( strlen( $data ) === 0 ) {
                                continue;
                        }
                        $socket = array_pop( $sockets );