bfile

package
v0.0.0-...-642df0c Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: BSD-3-Clause, MIT Imports: 6 Imported by: 0

README

bfile

GoDoc

A buffer pool file I/O library for Go.

The purpose of the library is to provide I/O mechanisms for copying data to and from a buffer in user space and maintain complete control over how and when it transfers pages.

This is an alternative to using mmap on large DBMS like files.

Install

go get github.com/tidwall/bfile

Usage

Use the NewPager function to create a new Pager, which includes three functions: ReadAt, WriteAt, and Flush. The Pager reading and writing works like an os.File, but with an automatically maintained pool of buffered pages.

// Open a file for read/write
f, err := os.OpenFile("bigfile.dat", os.O_RDWR, 0)

// Create a new Pager for accessing the data in opened file.
p := bfile.NewPager(f)

// Read some data at a specific offset.
data := make([]byte, 50)
n, err := p.ReadAt(data, 827364)

// Write some data at the same offset.
n, err := p.WriteAt([]byte("hello"), c)

// Flush unwritten data to the underlying os.File
err = p.Flush()

There's also a Stream object for sequentially reading and writing data, which includes the three functions: Read, Write, and Flush.


// Create a new Pager
p := bfile.NewPager(f)

// Create a Stream that is backed by the Pager starting at a specific offset
s := p.Stream(827364)

// Read 50 bytes data at the offset 827364.
data := make([]byte, 50)
n, err := s.Read(data)

// Write the string "hello" at 827414, which is after the previous read.
n, err := s.Write([]byte("hello"))

// Flush unwritten data to the underlying os.File
err = p.Flush()

The default page size is 4096 and the default size of all buffered pages will not exceed 8 MB. These defaults can be changed by using the NewPagerSize function.

All operations are thread-safe.

Contact

Josh Baker @tidwall

License

Source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pager

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

Pager is a page buffer that is backed by an os.File

func NewPager

func NewPager(file *os.File) *Pager

NewPager returns a new Pager that is backed by the provided file.

func NewPagerSize

func NewPagerSize(file *os.File, pageSize, bufferSize int) *Pager

NewPagerSize returns a new Pager with a custom page size and buffer size. The bufferSize is the maximum amount of memory dedicated to individual pages. Setting pageSize and bufferSize to zero will use their defaults, which are 4096 and 8 MB respectively. Custom values are rounded up to the nearest power of 2.

func (*Pager) Flush

func (f *Pager) Flush() error

Flush writes any unwritten buffered data to the underlying file.

func (*Pager) ReadAt

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

The byte offset off and len(b) must fall within the range of the size of the underlying file from Open or Create, otherwise an error is returned. ReadAt reads len(b) bytes from the File starting at byte offset off. It returns the number of bytes read and the error, if any. ReadAt returns a non-nil error when n < len(b).

func (*Pager) Stream

func (f *Pager) Stream(off int64) *Stream

Stream returns a new Stream for sequentially reading and writing data that is backed by a Pager.

func (*Pager) WriteAt

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

WriteAt writes len(b) bytes to the File starting at byte offset off. It returns the number of bytes written and an error, if any. WriteAt returns a non-nil error when n != len(b).

type Stream

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

Stream is a sequential read/writer that is backed by a Pager.

func (*Stream) Flush

func (s *Stream) Flush() error

func (*Stream) Read

func (s *Stream) Read(p []byte) (n int, err error)

func (*Stream) Write

func (s *Stream) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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