-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matthias Bertschy <[email protected]>
- Loading branch information
Showing
4 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package file | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/ncw/directio" | ||
) | ||
|
||
// DirectIOReader is a reader that reads data from the underlying reader using direct I/O. | ||
type DirectIOReader struct { | ||
buf []byte | ||
bufSize int | ||
off int | ||
rd io.Reader | ||
} | ||
|
||
var _ io.ByteReader = (*DirectIOReader)(nil) | ||
|
||
var _ io.Reader = (*DirectIOReader)(nil) | ||
|
||
func NewDirectIOReader(rd io.Reader) *DirectIOReader { | ||
return &DirectIOReader{ | ||
buf: directio.AlignedBlock(directio.BlockSize), | ||
rd: rd, | ||
} | ||
} | ||
|
||
func (d *DirectIOReader) Read(p []byte) (int, error) { | ||
// read data from the underlying reader if the buffer is empty | ||
if d.off == d.bufSize { | ||
var err error | ||
d.bufSize, err = io.ReadFull(d.rd, d.buf) | ||
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) { | ||
return 0, err | ||
} | ||
d.off = 0 | ||
} | ||
// copy data to the buffer | ||
n := copy(p, d.buf[d.off:]) | ||
d.off += n | ||
return n, nil | ||
} | ||
|
||
func (d *DirectIOReader) ReadByte() (byte, error) { | ||
panic("ReadByte not implemented, gob.Decode should not be using this") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters