Documentation ¶
Index ¶
- type Buffer
- func (bfr *Buffer) Abort() (err error)
- func (bfr *Buffer) Cleanup() (err error)
- func (bfr *Buffer) Close() (err error)
- func (bfr *Buffer) Read(p []byte) (n int, err error)
- func (bfr *Buffer) Stats() stat.Stats
- func (bfr *Buffer) Write(p []byte) (n int, err error)
- func (bfr *Buffer) WriteLine(ln []byte) (err error)
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer implements both StatsReadCloser and StatsWriteCloser interfaces.
Buffer is meant to abstract away the details of writing and reading to either a file buffer or in-memory buffer.
Buffer will: - compress writes if Options.Compress is true. - keep track of buffer statistics - calculate MD5 checksum on calling Close() - calculate the buffer size - provide the tmp file path in the file stats. - clean up tmp file if Abort() or Cleanup() are called.
func NewBuffer ¶
Example ¶
bfr, err := NewBuffer(nil) if bfr == nil { return } fmt.Println(err) // output: <nil> fmt.Println(bfr.sts.Created != "") // output: true fmt.Println(bfr.w != nil) // output true fmt.Println(bfr.wGzip) // output <nil> fmt.Println(bfr.wSize != nil) // output true fmt.Println(bfr.bBuf != nil) // output: true fmt.Println(bfr.fBuf) // output <nil> fmt.Println(bfr.r != nil) // output true fmt.Println(bfr.hshr != nil) // output true
Output: <nil> true true <nil> true true <nil> true true
func (*Buffer) Abort ¶
Abort will clear the buffer (remove tmp file if exists) and prevent further buffer writes.
Example ¶
bfr, _ := NewBuffer(nil) if bfr == nil { return } // write first bfr.WriteLine([]byte("test line")) bfr.WriteLine([]byte("test line")) err := bfr.Abort() fmt.Println(err) // output: <nil>
Output: <nil>
func (*Buffer) Cleanup ¶
Cleanup will remove the tmp file (if exists) or reset the in-memory buffer (if used).
Cleanup should not be used until the user is done with the contents of the buffer.
Cleanup is called automatically as part of the abort process but since the user may wish to read from the buffer after closing, Cleanup will need to be called after Close, especially if using compression since Close flushes the compression buffer and finalizes writing.
Example ¶
bfr, _ := NewBuffer(nil) if bfr == nil { return } // write first bfr.WriteLine([]byte("test line")) bfr.WriteLine([]byte("test line")) err := bfr.Cleanup() // bytes should be re-set // so reading from bBuf should return EOF b := make([]byte, 1) n, bErr := bfr.bBuf.Read(b) fmt.Println(err) // output: <nil> fmt.Println(n) // output: 0 fmt.Println(bErr) // output: EOF
Output: <nil> 0 EOF
func (*Buffer) Close ¶
Close prevents further writing and flushes writes to the underlying buffer.
Example ¶
bfr, _ := NewBuffer(nil) if bfr == nil { return } // write first bfr.WriteLine([]byte("test line")) bfr.WriteLine([]byte("test line")) fmt.Println(bfr.sts.Checksum == "") // output: true fmt.Println(bfr.sts.Size) // output: 0 err := bfr.Close() fmt.Println(err) // output: <nil> fmt.Println(bfr.sts.Checksum != "") // output: true fmt.Println(bfr.sts.Size) // output: 20 // closing again has no effect err = bfr.Close() fmt.Println(err) // output: <nil> fmt.Println(bfr.sts.Checksum != "") // output: true fmt.Println(bfr.sts.Size) // output: 20
Output: true 0 <nil> true 20 <nil> true 20
func (*Buffer) Read ¶
Read will read the raw underlying buffer bytes. If the buffer is writing with compression it will not decompress on reads. Read is made for reading the final written bytes and copying them to the final location.
Close should be called before Read as Close will sync the underlying buffer. This is especially important when using compression and/or a tmp file.
Example ¶
bfr, _ := NewBuffer(nil) if bfr == nil { return } // write first bfr.Write([]byte("test line\n")) bfr.Write([]byte("test line\n")) // read b := make([]byte, 20) bfr.Read(b) fmt.Print(string(b)) // output: test line, test line
Output: test line test line
func (*Buffer) Stats ¶
Example ¶
bfr, _ := NewBuffer(nil) if bfr == nil { return } // write first bfr.WriteLine([]byte("test line")) bfr.WriteLine([]byte("test line")) sts := bfr.Stats() fmt.Println(sts.ByteCnt) // output: 20 fmt.Println(sts.LineCnt) // output: 2
Output: 20 2
func (*Buffer) Write ¶
Write will write to the underlying buffer. The underlying bytes writing will be compressed if compression was specified on buffer initialization.
Write is thread safe.
Example ¶
bfr, _ := NewBuffer(nil) if bfr == nil { return } // write first n1, err1 := bfr.Write([]byte("test line\n")) n2, err2 := bfr.Write([]byte("test line\n")) // read b := make([]byte, 20) bfr.Read(b) fmt.Println(n1) // output: 10 fmt.Println(err1) // output: <nil> fmt.Println(n2) // output: 10 fmt.Println(err2) // output: <nil> fmt.Print(string(b)) // output: test line, test line fmt.Println(bfr.sts.ByteCnt) // output: 20 fmt.Println(bfr.sts.LineCnt) // output: 0 fmt.Println(bfr.wSize.Size()) // output: 20
Output: 10 <nil> 10 <nil> test line test line 20 0 20
func (*Buffer) WriteLine ¶
Write will write to the underlying buffer. The underlying bytes writing will be compressed if compression was specified on buffer initialization.
WriteLine is thread safe.
Example ¶
bfr, _ := NewBuffer(nil) if bfr == nil { return } // write first err1 := bfr.WriteLine([]byte("test line")) err2 := bfr.WriteLine([]byte("test line")) // read b := make([]byte, 20) bfr.Read(b) fmt.Println(err1) // output: <nil> fmt.Println(err2) // output: <nil> fmt.Print(string(b)) // output: test line, test line fmt.Println(bfr.sts.ByteCnt) // output: 20 fmt.Println(bfr.sts.LineCnt) // output: 2 fmt.Println(bfr.wSize.Size()) // output: 20
Output: <nil> <nil> test line test line 20 2 20
type Options ¶
type Options struct { // UseFileBuf specifies to use a tmp file for the delayed writing. // Can optionally also specify the tmp directory and tmp name // prefix. UseFileBuf bool // FileBufDir optionally specifies the temp directory. If not specified then // the os default temp dir is used. FileBufDir string // FileBufPrefix optionally specifies the temp file prefix. // The full tmp file name is randomly generated and guaranteed // not to conflict with existing files. A prefix can help one find // the tmp file. FileBufPrefix string // Compress set to true will turn on gzip compression. // Writes will be compressed but reads will read raw // compressed bytes. Compress bool }
func NewOptions ¶
func NewOptions() *Options
Example ¶
opt := NewOptions() if opt == nil { return } fmt.Println(opt.FileBufDir) // output: fmt.Println(opt.UseFileBuf) // output: false fmt.Println(opt.FileBufPrefix) // output: fmt.Println(opt.Compress) // output: false
Output: false false