You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/08/28 18:42:43 UTC

[GitHub] [beam] youngoli commented on a change in pull request #12683: [BEAM-10812] fix(beam/sdks/go): fix buffer limit on textio

youngoli commented on a change in pull request #12683:
URL: https://github.com/apache/beam/pull/12683#discussion_r479454010



##########
File path: sdks/go/pkg/beam/io/textio/textio.go
##########
@@ -79,6 +81,7 @@ func expandFn(ctx context.Context, glob string, emit func(string)) error {
 }
 
 func readFn(ctx context.Context, filename string, emit func(string)) error {
+	fmt.Println(filename)

Review comment:
       This looks like a debug printf got left in, but if it's intentional, there shouldn't be direct printing to stdout in the SDK, use logs instead.

##########
File path: sdks/go/pkg/beam/io/textio/textio.go
##########
@@ -93,11 +96,18 @@ func readFn(ctx context.Context, filename string, emit func(string)) error {
 	}
 	defer fd.Close()
 
-	scanner := bufio.NewScanner(fd)
-	for scanner.Scan() {
-		emit(scanner.Text())
+	rd := bufio.NewReader(fd)
+	for {
+		line, err := rd.ReadString('\n')
+		if err == io.EOF {

Review comment:
       The documentation for ReadString states:
   
   > If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF)
   
   So it sounds like you'd want to emit the line here once before breaking, maybe with a quick check `if len(line) != 0`.

##########
File path: sdks/go/pkg/beam/io/textio/textio.go
##########
@@ -93,11 +96,18 @@ func readFn(ctx context.Context, filename string, emit func(string)) error {
 	}
 	defer fd.Close()
 
-	scanner := bufio.NewScanner(fd)
-	for scanner.Scan() {
-		emit(scanner.Text())
+	rd := bufio.NewReader(fd)
+	for {
+		line, err := rd.ReadString('\n')

Review comment:
       One thing that caught my eye on the ReadString description was that the string returned includes the delimiter, which differs from the old scanner implementation which strips the delimiter. To keep parity between the old and new behavior you'll probably want to strip the newline before emitting the string.

##########
File path: sdks/go/pkg/beam/io/textio/textio_test.go
##########
@@ -0,0 +1,41 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package textio contains transforms for reading and writing text files.
+package textio
+
+import (
+	"testing"
+
+	_ "github.com/apache/beam/sdks/go/pkg/beam/io/filesystem/local"
+)
+
+func TestRead(t *testing.T) {
+	f := "testfile.txt"
+
+	receivedLines := []string{}
+	getLines := func(line string) {
+		receivedLines = append(receivedLines, line)
+	}
+
+	err := readFn(nil, f, getLines)
+	if err != nil {
+		t.Fatalf("failed with %v", err)
+	}
+	if len(receivedLines) != 1 {
+		t.Fatalf("received %v lines", len(receivedLines))
+	}

Review comment:
       You should mention what the expected number of lines is, for example:
   ```suggestion
   	want := 1
   	if len(receivedLines) != want {
   		t.Fatalf("received %v lines, wanted %v", len(receivedLines), want)
   	}
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org