diff --git a/CHANGELOG.md b/CHANGELOG.md index 56b69955..269d4fab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### New features - [PR #1563](https://github.com/rqlite/rqlite/pull/1563): Support S3-compatible storage systems for auto-backups and auto-restores. Fixes issue [#1560](https://github.com/rqlite/rqlite/issues/1560). Thanks @jtackaberry ### Implementation changes and bug fixes +- [PR #1567](https://github.com/rqlite/rqlite/pull/1567): Refactor to make more use of `progress` module. - [Commit d1cc802](https://github.com/rqlite/rqlite/commit/d1cc80229221ff51cd4741cc7a2e05e87f0337cb): Fix return codes in `HasData()`. ## 8.15.0 (January 4th 2024) diff --git a/command/chunking/chunker.go b/command/chunking/chunker.go index 7347a210..d9177279 100644 --- a/command/chunking/chunker.go +++ b/command/chunking/chunker.go @@ -10,6 +10,7 @@ import ( "sync" "github.com/rqlite/rqlite/v8/command/proto" + "github.com/rqlite/rqlite/v8/progress" ) const ( @@ -37,7 +38,7 @@ var gzipWriterPool = sync.Pool{ // Chunker is a reader that reads from an underlying io.Reader and returns // LoadChunkRequests of a given size. type Chunker struct { - r *CountingReader + r *progress.CountingReader chunkSize int64 streamID string @@ -52,7 +53,7 @@ type Chunker struct { // LoadChunkRequests of size chunkSize. func NewChunker(r io.Reader, chunkSize int64) *Chunker { return &Chunker{ - r: NewCountingReader(r), + r: progress.NewCountingReader(r), chunkSize: chunkSize, streamID: generateStreamID(), } diff --git a/command/chunking/counting_io.go b/command/chunking/counting_io.go deleted file mode 100644 index 748e0503..00000000 --- a/command/chunking/counting_io.go +++ /dev/null @@ -1,57 +0,0 @@ -package chunking - -import ( - "io" -) - -// CountingReader is a wrapper around an io.Reader that counts the number of -// bytes read. -type CountingReader struct { - r io.Reader - n int64 -} - -// Read reads from the underlying io.Reader. -func (cr *CountingReader) Read(p []byte) (int, error) { - n, err := cr.r.Read(p) - cr.n += int64(n) - return n, err -} - -// Count returns the number of bytes read. -func (cr *CountingReader) Count() int64 { - return cr.n -} - -// NewCountingReader returns a new CountingReader that reads from r. -func NewCountingReader(r io.Reader) *CountingReader { - return &CountingReader{ - r: r, - } -} - -// CountingWriter is a wrapper around an io.Writer that counts the number of -// bytes written. -type CountingWriter struct { - w io.Writer - n int64 -} - -// Write writes to the underlying io.Writer. -func (cw *CountingWriter) Write(p []byte) (int, error) { - n, err := cw.w.Write(p) - cw.n += int64(n) - return n, err -} - -// Count returns the number of bytes written. -func (cw *CountingWriter) Count() int64 { - return cw.n -} - -// NewCountingWriter returns a new CountingWriter that writes to w. -func NewCountingWriter(w io.Writer) *CountingWriter { - return &CountingWriter{ - w: w, - } -} diff --git a/store/gzip/decompressor.go b/store/gzip/decompressor.go index cb72aded..96c50100 100644 --- a/store/gzip/decompressor.go +++ b/store/gzip/decompressor.go @@ -3,12 +3,14 @@ package gzip import ( "compress/gzip" "io" + + "github.com/rqlite/rqlite/v8/progress" ) // Decompressor is a wrapper around a gzip.Reader that reads from an io.Reader // and decompresses the data. type Decompressor struct { - cr *CountingReader + cr *progress.CountingReader gzr *gzip.Reader nRx int64 @@ -19,7 +21,7 @@ type Decompressor struct { // decompresses the data using gzip. func NewDecompressor(r io.Reader) *Decompressor { return &Decompressor{ - cr: NewCountingReader(r), + cr: progress.NewCountingReader(r), } } @@ -56,24 +58,3 @@ func (c *Decompressor) Read(p []byte) (nn int, err error) { } return n, err } - -// CountingReader is a wrapper around io.Reader that counts the number of bytes -// read. -type CountingReader struct { - r io.Reader - n int64 -} - -// NewCountingReader returns an instantiated CountingReader that reads from r. -func NewCountingReader(r io.Reader) *CountingReader { - return &CountingReader{ - r: r, - } -} - -// Read reads data. -func (c *CountingReader) Read(p []byte) (n int, err error) { - n, err = c.r.Read(p) - c.n += int64(n) - return n, err -}