diff --git a/lib/filestream/filestream.go b/lib/filestream/filestream.go index 6b5b3d233..4be2fa82e 100644 --- a/lib/filestream/filestream.go +++ b/lib/filestream/filestream.go @@ -64,10 +64,7 @@ func (r *Reader) Path() string { // // If nocache is set, then the reader doesn't pollute OS page cache. func OpenReaderAt(path string, offset int64, nocache bool) (*Reader, error) { - r, err := Open(path, nocache) - if err != nil { - return nil, err - } + r := MustOpen(path, nocache) n, err := r.f.Seek(offset, io.SeekStart) if err != nil { r.MustClose() @@ -80,13 +77,13 @@ func OpenReaderAt(path string, offset int64, nocache bool) (*Reader, error) { return r, nil } -// Open opens the file from the given path in nocache mode. +// MustOpen opens the file from the given path in nocache mode. // // If nocache is set, then the reader doesn't pollute OS page cache. -func Open(path string, nocache bool) (*Reader, error) { +func MustOpen(path string, nocache bool) *Reader { f, err := os.Open(path) if err != nil { - return nil, err + logger.Panicf("FATAL: cannot open file: %s", err) } r := &Reader{ f: f, @@ -96,10 +93,10 @@ func Open(path string, nocache bool) (*Reader, error) { r.st.fd = f.Fd() } readersCount.Inc() - return r, nil + return r } -// MustClose closes the underlying file passed to Open. +// MustClose closes the underlying file passed to MustOpen. func (r *Reader) MustClose() { if err := r.st.close(); err != nil { logger.Panicf("FATAL: cannot close streamTracker for file %q: %s", r.f.Name(), err) diff --git a/lib/filestream/filestream_test.go b/lib/filestream/filestream_test.go index a0d56973b..cf3223ac1 100644 --- a/lib/filestream/filestream_test.go +++ b/lib/filestream/filestream_test.go @@ -42,10 +42,7 @@ func testWriteRead(t *testing.T, nocache bool, testStr string) { } w.MustClose() - r, err := Open("./nocache_test.txt", nocache) - if err != nil { - t.Fatalf("cannot open file: %s", err) - } + r := MustOpen("./nocache_test.txt", nocache) buf := make([]byte, len(testStr)) if _, err := io.ReadFull(r, buf); err != nil { t.Fatalf("unexpected error when reading: %s", err) diff --git a/lib/mergeset/block_stream_reader.go b/lib/mergeset/block_stream_reader.go index c2d4a3d3d..8a4f729fe 100644 --- a/lib/mergeset/block_stream_reader.go +++ b/lib/mergeset/block_stream_reader.go @@ -148,36 +148,23 @@ func (bsr *blockStreamReader) InitFromFilePart(path string) error { } metaindexPath := filepath.Join(path, metaindexFilename) - metaindexFile, err := filestream.Open(metaindexPath, true) - if err != nil { - return fmt.Errorf("cannot open metaindex file in stream mode: %w", err) - } + metaindexFile := filestream.MustOpen(metaindexPath, true) + + var err error bsr.mrs, err = unmarshalMetaindexRows(bsr.mrs[:0], metaindexFile) metaindexFile.MustClose() if err != nil { - return fmt.Errorf("cannot unmarshal metaindex rows from file %q: %w", metaindexPath, err) + logger.Panicf("FATAL: cannot unmarshal metaindex rows from file %q: %s", metaindexPath, err) } indexPath := filepath.Join(path, indexFilename) - indexFile, err := filestream.Open(indexPath, true) - if err != nil { - return fmt.Errorf("cannot open index file in stream mode: %w", err) - } + indexFile := filestream.MustOpen(indexPath, true) itemsPath := filepath.Join(path, itemsFilename) - itemsFile, err := filestream.Open(itemsPath, true) - if err != nil { - indexFile.MustClose() - return fmt.Errorf("cannot open items file in stream mode: %w", err) - } + itemsFile := filestream.MustOpen(itemsPath, true) lensPath := filepath.Join(path, lensFilename) - lensFile, err := filestream.Open(lensPath, true) - if err != nil { - indexFile.MustClose() - itemsFile.MustClose() - return fmt.Errorf("cannot open lens file in stream mode: %w", err) - } + lensFile := filestream.MustOpen(lensPath, true) bsr.path = path bsr.indexReader = indexFile diff --git a/lib/mergeset/part.go b/lib/mergeset/part.go index 194f11b60..7ee86dffc 100644 --- a/lib/mergeset/part.go +++ b/lib/mergeset/part.go @@ -74,10 +74,7 @@ func openFilePart(path string) (*part, error) { } metaindexPath := filepath.Join(path, metaindexFilename) - metaindexFile, err := filestream.Open(metaindexPath, true) - if err != nil { - return nil, fmt.Errorf("cannot open %q: %w", metaindexPath, err) - } + metaindexFile := filestream.MustOpen(metaindexPath, true) metaindexSize := fs.MustFileSize(metaindexPath) indexPath := filepath.Join(path, indexFilename) diff --git a/lib/persistentqueue/persistentqueue.go b/lib/persistentqueue/persistentqueue.go index 6ce06fd29..7a548f94e 100644 --- a/lib/persistentqueue/persistentqueue.go +++ b/lib/persistentqueue/persistentqueue.go @@ -100,10 +100,7 @@ func (q *queue) mustResetFiles() { q.writer = w q.readerPath = q.writerPath - r, err := filestream.Open(q.readerPath, true) - if err != nil { - logger.Panicf("FATAL: cannot open chunk file %q: %s", q.readerPath, err) - } + r := filestream.MustOpen(q.readerPath, true) q.reader = r if err := q.flushMetainfo(); err != nil { @@ -559,10 +556,7 @@ func (q *queue) nextChunkFileForRead() error { } q.readerLocalOffset = 0 q.readerPath = q.chunkFilePath(q.readerOffset) - r, err := filestream.Open(q.readerPath, true) - if err != nil { - return fmt.Errorf("cannot open chunk file %q: %w", q.readerPath, err) - } + r := filestream.MustOpen(q.readerPath, true) q.reader = r if err := q.flushMetainfo(); err != nil { return fmt.Errorf("cannot flush metainfo: %w", err) diff --git a/lib/storage/block_stream_reader.go b/lib/storage/block_stream_reader.go index 58e24c6c1..8087c2388 100644 --- a/lib/storage/block_stream_reader.go +++ b/lib/storage/block_stream_reader.go @@ -135,41 +135,20 @@ func (bsr *blockStreamReader) InitFromFilePart(path string) error { } timestampsPath := filepath.Join(path, timestampsFilename) - timestampsFile, err := filestream.Open(timestampsPath, true) - if err != nil { - return fmt.Errorf("cannot open timestamps file in stream mode: %w", err) - } + timestampsFile := filestream.MustOpen(timestampsPath, true) valuesPath := filepath.Join(path, valuesFilename) - valuesFile, err := filestream.Open(valuesPath, true) - if err != nil { - timestampsFile.MustClose() - return fmt.Errorf("cannot open values file in stream mode: %w", err) - } + valuesFile := filestream.MustOpen(valuesPath, true) indexPath := filepath.Join(path, indexFilename) - indexFile, err := filestream.Open(indexPath, true) - if err != nil { - timestampsFile.MustClose() - valuesFile.MustClose() - return fmt.Errorf("cannot open index file in stream mode: %w", err) - } + indexFile := filestream.MustOpen(indexPath, true) metaindexPath := filepath.Join(path, metaindexFilename) - metaindexFile, err := filestream.Open(metaindexPath, true) - if err != nil { - timestampsFile.MustClose() - valuesFile.MustClose() - indexFile.MustClose() - return fmt.Errorf("cannot open metaindex file in stream mode: %w", err) - } + metaindexFile := filestream.MustOpen(metaindexPath, true) mrs, err := unmarshalMetaindexRows(bsr.mrs[:0], metaindexFile) metaindexFile.MustClose() if err != nil { - timestampsFile.MustClose() - valuesFile.MustClose() - indexFile.MustClose() - return fmt.Errorf("cannot unmarshal metaindex rows from file part %q: %w", metaindexPath, err) + logger.Panicf("FATAL: cannot unmarshal metaindex rows from file part %q: %s", metaindexPath, err) } bsr.path = path diff --git a/lib/storage/part.go b/lib/storage/part.go index de917b427..3882bc79f 100644 --- a/lib/storage/part.go +++ b/lib/storage/part.go @@ -67,13 +67,7 @@ func openFilePart(path string) (*part, error) { indexSize := fs.MustFileSize(indexPath) metaindexPath := filepath.Join(path, metaindexFilename) - metaindexFile, err := filestream.Open(metaindexPath, true) - if err != nil { - timestampsFile.MustClose() - valuesFile.MustClose() - indexFile.MustClose() - return nil, fmt.Errorf("cannot open metaindex file: %w", err) - } + metaindexFile := filestream.MustOpen(metaindexPath, true) metaindexSize := fs.MustFileSize(metaindexPath) size := timestampsSize + valuesSize + indexSize + metaindexSize