filebuffer

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2023 License: MIT Imports: 6 Imported by: 1

README

filebuffer PkgGoDev

filebuffer is a Go module which provides a buffer implmentation for random access of fixed size files with io.ReadAt and io.WriteAt interfaces.

License

MIT

Credits

CREDITS

Documentation

Overview

Package filebuffer provides a file buffer implementation for random access of fixed size files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileBuffer

type FileBuffer struct {
	// contains filtered or unexported fields
}

FileBuffer is a file buffer which can read the file partialy in page size units.

FileBuffer assumes the file size never changes.

FileBuffer uses vector I/O (preadv and pwritev) on Linux for reading and writing successive pages in the file.

FileBuffer implements ReadWriterAt interface.

func New

func New(file *os.File, fileSize, pageSize int64) *FileBuffer

New creates a new whole file buffer.

func (*FileBuffer) Flush

func (b *FileBuffer) Flush() error

Flush writes dirty pages to the file.

func (*FileBuffer) Preread

func (b *FileBuffer) Preread(off, length int64) error

Preread reads a bytes of the specified length starting at offset off data from the underlying file.

It reads the file in page size units and skips pages which were already read and kept in the buffer.

func (*FileBuffer) ReadAt

func (b *FileBuffer) ReadAt(p []byte, off int64) (n int, err error)

ReadAt reads len(p) bytes into p starting at offset off in the buffer. It returns the number of bytes read (n == len(p)) and any error encountered.

ReadAt reads necessary pages using Preread method which are not already read.

If you are going to call ReadAt multiple times to get data spanning to multiple pages, you may want to call Preread for the total range beforehand to reduce file I/O system calls.

ReadAt implements io.ReaderAt interface.

func (*FileBuffer) WriteAt

func (b *FileBuffer) WriteAt(p []byte, off int64) (n int, err error)

WriteAt writes len(p) bytes from p to the buffer at offset off. It returns the number of bytes written from p (n == len(p)) and any error encountered that caused the write to stop early.

Pages for the corresponding range will be read first if not already read.

The caller must call Flush later to write dirty pages to the file.

WriteAt implements io.WriterAt interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL