diff --git a/oviewer/document_test.go b/oviewer/document_test.go index bc44157..a606c59 100644 --- a/oviewer/document_test.go +++ b/oviewer/document_test.go @@ -14,7 +14,7 @@ func docHelper(t *testing.T, str string) *Document { if err != nil { t.Fatal(err) } - if err := m.ReadAll(bytes.NewBufferString(str)); err != nil { + if err := m.ControlReader(bytes.NewBufferString(str), nil); err != nil { t.Fatal(err) } for !m.BufEOF() { diff --git a/oviewer/example_test.go b/oviewer/example_test.go index 24da5f1..ee2cd65 100644 --- a/oviewer/example_test.go +++ b/oviewer/example_test.go @@ -35,7 +35,7 @@ func ExampleNewOviewer() { panic(err) } s := "Hello, World!" - if err := doc.ReadAll(bytes.NewBufferString(s)); err != nil { + if err := doc.ControlReader(bytes.NewBufferString(s), nil); err != nil { panic(err) } @@ -65,7 +65,7 @@ func ExampleSearch() { panic(err) } s := "Hello, World!" - if err := doc.ReadAll(bytes.NewBufferString(s)); err != nil { + if err := doc.ControlReader(bytes.NewBufferString(s), nil); err != nil { panic(err) } diff --git a/oviewer/reader.go b/oviewer/reader.go index b26d5ed..ace4646 100644 --- a/oviewer/reader.go +++ b/oviewer/reader.go @@ -2,7 +2,6 @@ package oviewer import ( "bufio" - "context" "errors" "fmt" "io" @@ -404,72 +403,3 @@ func (m *Document) close() error { atomic.StoreInt32(&m.store.changed, 1) return nil } - -// ReadFile reads file. -// If the file name is empty, read from standard input. -// -// Deprecated: Use ControlFile instead. -func (m *Document) ReadFile(fileName string) error { - r, err := m.openFileReader(fileName) - if err != nil { - return err - } - return m.ReadAll(r) -} - -// ContinueReadAll continues to read even if it reaches EOF. -// -// Deprecated: Use ControlFile instead. -func (m *Document) ContinueReadAll(_ context.Context, r io.Reader) error { - return m.ReadAll(r) -} - -// ReadReader reads reader. -// A wrapper for ReadAll. -// -// Deprecated: Use ControlReader instead. -func (m *Document) ReadReader(r io.Reader) error { - return m.ReadAll(r) -} - -// ReadAll reads all from the reader. -// And store it in the lines of the Document. -// ReadAll needs to be notified on eofCh. -// -// Deprecated: Use ControlReader instead. -func (m *Document) ReadAll(r io.Reader) error { - reader := bufio.NewReader(r) - go func() { - if m.checkClose() { - return - } - - if err := m.readAll(reader); err != nil { - if errors.Is(err, io.EOF) || errors.Is(err, io.ErrClosedPipe) || errors.Is(err, os.ErrClosed) { - m.store.offset = m.store.size - atomic.StoreInt32(&m.store.eof, 1) - return - } - log.Printf("error: %v\n", err) - return - } - }() - return nil -} - -// readAll reads to the end. -// The read lines are stored in the lines of the Document. -func (m *Document) readAll(reader *bufio.Reader) error { - chunk := m.store.chunkForAdd(m.seekable, m.store.size) - start := len(chunk.lines) - for { - if err := m.store.readLines(chunk, reader, start, ChunkSize, true); err != nil { - return err - } - chunk = NewChunk(m.store.size) - m.store.mu.Lock() - m.store.chunks = append(m.store.chunks, chunk) - m.store.mu.Unlock() - start = 0 - } -} diff --git a/oviewer/reader_test.go b/oviewer/reader_test.go index e18fe06..0cb3962 100644 --- a/oviewer/reader_test.go +++ b/oviewer/reader_test.go @@ -3,98 +3,11 @@ package oviewer import ( "bufio" "bytes" - "io" "path/filepath" - "strings" "sync/atomic" "testing" ) -func TestDocument_ReadFile(t *testing.T) { - t.Parallel() - type args struct { - args string - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "testNoFile", - args: args{ - "noFile", - }, - wantErr: true, - }, - { - name: "testSTDIN", - args: args{ - "", - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - m, err := NewDocument() - if err != nil { - t.Fatal(err) - } - if err := m.ReadFile(tt.args.args); (err != nil) != tt.wantErr { - t.Errorf("Document.ReadFile() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - -func TestDocument_ReadAll(t *testing.T) { - t.Parallel() - type args struct { - r io.Reader - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "test1", - args: args{ - r: bytes.NewBufferString("foo\nbar\n"), - }, - wantErr: false, - }, - { - name: "testOverChunkSize", - args: args{ - r: bytes.NewBufferString(strings.Repeat("a\n", ChunkSize+1)), - }, - wantErr: false, - }, - { - name: "testLongLine", - args: args{ - r: bytes.NewBufferString(strings.Repeat("a", 4097)), - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - m, err := NewDocument() - if err != nil { - t.Fatal(err) - } - if err := m.ReadAll(tt.args.r); (err != nil) != tt.wantErr { - t.Errorf("Document.ReadAll() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -} - func TestDocument_reset(t *testing.T) { t.Parallel() type fields struct {