buffer

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2019 License: MIT Imports: 9 Imported by: 1

README

Go Disk Buffer

Package buffer helps to work with huge amount of data, which cannot be stored in RAM. Instead of keeping all data in RAM buffer.Buffer can store the data on a disk in a temporary file.

Features:

  • buffer.Buffer is compatible with io.Reader and io.Writer interfaces
  • buffer.Buffer can replace bytes.Buffer (except some methods – check Unavailable methods)
  • You can encrypt data on a disk. Just use Buffer.EnableEncryption method

Notes:

  • It is not recommended to use zero value of buffer.Buffer. Use buffer.NewBuffer() or buffer.NewBufferWithMaxMemorySize() instead
  • buffer.Buffer is not thread-safe!
  • buffer.Buffer uses a directory returned by os.TempDir() to store temp files. You can change the directory with Buffer.ChangeTempDir method

Example

With bytes.Buffer

package main

import (
    "bytes"
    "fmt"
)

func main() {
    b := bytes.Buffer{}
    // b := bytes.NewBuffer(nil)
    // b := bytes.NewBufferString("")

    b.Write([]byte("Hello,"))
    b.WriteByte(' ')
    b.Write([]byte("World!"))

    data := b.Next(13)
    fmt.Println(string(data)) // "Hello, World!"
}

With github.com/ShoshinNikita/go-disk-buffer.Buffer

package main

import (
    "fmt"

    buffer "github.com/ShoshinNikita/go-disk-buffer"
)

func main() {
    b := buffer.NewBufferWithMaxMemorySize(7) // store only 7 bytes in RAM
    // b := buffer.NewBuffer(nil)
    // b := buffer.NewBufferString("")

    b.Write([]byte("Hello,"))
    b.WriteByte(' ')
    b.Write([]byte("World!")) // will be stored on a disk in a temporary file

    data := b.Next(13)
    fmt.Println(string(data)) // "Hello, World!"
}

Benchmark

CPU: Intel Core i7-3630QM
RAM: 8 GB
Disk: HDD, 5400 rpm

Buffer_size_is_greater_than_data/bytes.Buffer-8     1000       1591091 ns/op      10043209 B/op     36 allocs/op
Buffer_size_is_greater_than_data/utils.Buffer-8     1000       1346077 ns/op       6901679 B/op     26 allocs/op

Buffer_size_is_equal_to_data/bytes.Buffer-8         1000       1760100 ns/op      10043195 B/op     36 allocs/op
Buffer_size_is_equal_to_data/utils.Buffer-8         2000       1357077 ns/op       7434159 B/op     27 allocs/op

Buffer_size_is_less_than_data/bytes.Buffer-8          50      36522090 ns/op     177848123 B/op     53 allocs/op
Buffer_size_is_less_than_data/utils.Buffer-8          10     110406320 ns/op     112327659 B/op     62 allocs/op

Available methods

Read
  • Read(p []byte) (n int, err error)
  • ReadByte() (byte, error)
  • Next(n int) []byte
  • WriteTo(w io.Writer) (n int64, err error)
Write
  • Write(p []byte) (n int, err error)
  • WriteByte(c byte) error
  • WriteRune(r rune) (n int, err error)
  • WriteString(s string) (n int, err error)
  • ReadFrom(r io.Reader) (n int64, err error)
Other
  • Len() int
  • Cap() int – equal to Len() method
  • Reset()

Unavailable methods

Can be added
  • ReadBytes(delim byte) (line []byte, err error)
  • ReadString(delim byte) (line string, err error)
  • ReadRune() (r rune, size int, err error)help wanted (check Buffer.readRune() method)
Won't be adeed
  • Bytes() []byte

    Reason: go-disk-buffer was created to store a huge amount of data. If your data can fit in RAM, you should use bytes.Buffer

  • String() string

    Reason: see the previous reason

  • Grow(n int)

    Reason: we can allocate the memory only in RAM. It doesn't make sense to allocate space on a disk

  • Truncate(n int)

  • UnreadByte() error

  • UnreadRune() error

Documentation

Index

Constants

View Source
const (
	// DefaultMaxMemorySize is used when Buffer is created with NewBuffer() or NewBufferString()
	DefaultMaxMemorySize = 2 << 20 // 2 MB
)

Variables

View Source
var (
	// ErrBufferFinished is used when Buffer.Write() method is called after Buffer.Read()
	ErrBufferFinished = errors.New("buffer is finished")
)

Functions

This section is empty.

Types

type Buffer

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

Buffer is a buffer which can store data on a disk. It isn't thread-safe!

func NewBuffer

func NewBuffer(buf []byte) *Buffer

NewBuffer creates a new Buffer with DefaultMaxMemorySize and calls Write(buf). If an error occurred, it panics

func NewBufferString

func NewBufferString(s string) *Buffer

NewBufferString calls NewBuffer([]byte(s))

func NewBufferWithMaxMemorySize

func NewBufferWithMaxMemorySize(maxInMemorySize int) *Buffer

NewBufferWithMaxMemorySize creates a new Buffer with passed maxInMemorySize

func (*Buffer) Cap

func (b *Buffer) Cap() int

Cap is equal to Buffer.Len()

func (*Buffer) ChangeTempDir added in v0.2.0

func (b *Buffer) ChangeTempDir(dir string) error

ChangeTempDir changes directory for temp files

func (*Buffer) EnableEncryption added in v0.3.0

func (b *Buffer) EnableEncryption() error

EnableEncryption enables encryption and generates an encryption key

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the number of bytes of the unread portion of the buffer

func (*Buffer) Next

func (b *Buffer) Next(n int) []byte

Next returns a slice containing the next n bytes from the buffer. If an error occurred, it panics

func (*Buffer) Read

func (b *Buffer) Read(data []byte) (n int, err error)

Read reads data from bytes.Buffer or from a file. A temp file is deleted when Read() encounter n == 0

func (*Buffer) ReadByte

func (b *Buffer) ReadByte() (byte, error)

ReadByte reads a single byte.

It uses Buffer.Read underhood

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom reads data from r until EOF and writes it into the Buffer.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets buffer and remove file if needed

func (*Buffer) Write

func (b *Buffer) Write(data []byte) (n int, err error)

Write writes data into bytes.Buffer while size of the Buffer is less than maxInMemorySize, when size of Buffer is equal to maxInMemorySize, Write creates a temporary file and writes remaining data into this one. Write returns ErrBufferFinished after the call of Buffer.Read(), Buffer.ReadByte() or Buffer.Next()

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(c byte) error

WriteByte writes a single byte.

It uses Buffer.Write underhood

func (*Buffer) WriteRune

func (b *Buffer) WriteRune(r rune) (n int, err error)

WriteRune writes a rune.

It uses bytes.Buffer and Buffer.Write underhood.

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (n int, err error)

WriteString writes a string

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo writes data to w until the buffer is drained or an error occurs.

Jump to

Keyboard shortcuts

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