Debian Codegraph @

README.md

raw symbols SCIP
1 Buffer
2 ==========
3
4 [![GoDoc](https://godoc.org/github.com/djherbis/buffer?status.svg)](https://godoc.org/github.com/djherbis/buffer)
5 [![Release](https://img.shields.io/github/release/djherbis/buffer.svg)](https://github.com/djherbis/buffer/releases/latest)
6 [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.txt)
7 [![Build Status](https://travis-ci.org/djherbis/buffer.svg?branch=master)](https://travis-ci.org/djherbis/buffer)
8 [![Coverage Status](https://coveralls.io/repos/djherbis/buffer/badge.svg?branch=master)](https://coveralls.io/r/djherbis/buffer?branch=master)
9 [![Go Report Card](https://goreportcard.com/badge/github.com/djherbis/buffer)](https://goreportcard.com/report/github.com/djherbis/buffer)
10
11 Usage
12 ------------
13
14 The following buffers provide simple unique behaviours which when composed can create complex buffering strategies. For use with github.com/djherbis/nio for Buffered io.Pipe and io.Copy implementations.
15
16 For example:
17
18 ```go
19 import (
20 "github.com/djherbis/buffer"
21 "github.com/djherbis/nio"
22
23 "io/ioutil"
24 )
25
26 // Buffer 32KB to Memory, after that buffer to 100MB chunked files
27 buf := buffer.NewUnboundedBuffer(32*1024, 100*1024*1024)
28 nio.Copy(w, r, buf) // Reads from r, writes to buf, reads from buf writes to w (concurrently).
29
30 // Buffer 32KB to Memory, discard overflow
31 buf = buffer.NewSpill(32*1024, ioutil.Discard)
32 nio.Copy(w, r, buf)
33 ```
34
35 Supported Buffers
36 ------------
37
38 #### Bounded Buffers ####
39
40 Memory: Wrapper for bytes.Buffer
41
42 File: File-based buffering. The file never exceeds Cap() in length, no matter how many times its written/read from. It accomplishes this by "wrapping" around the fixed max-length file when the data gets too long but there is available freed space at the beginning of the file. The caller is responsible for closing and deleting the file when done.
43
44 ```go
45 import (
46 "ioutil"
47 "os"
48
49 "github.com/djherbis/buffer"
50 )
51
52 // Create a File-based Buffer with max size 100MB
53 file, err := ioutil.TempFile("", "buffer")
54 if err != nil {
55 return err
56 }
57 defer os.Remove(file.Name())
58 defer file.Close()
59
60 buf := buffer.NewFile(100*1024*1024, file)
61
62 // A simpler way:
63 pool := buffer.NewFilePool(100*1024*1024, "") // "" -- use temp dir
64 buf, err := pool.Get() // allocate the buffer
65 if err != nil {
66 return err
67 }
68 defer pool.Put(buf) // close and remove the allocated file for the buffer
69
70 ```
71
72 Multi: A fixed length linked-list of buffers. Each buffer reads from the next buffer so that all the buffered data is shifted upwards in the list when reading. Writes are always written to the first buffer in the list whose Len() < Cap().
73
74 ```go
75 import (
76 "github.com/djherbis/buffer"
77 )
78
79 mem := buffer.New(32*1024)
80 file := buffer.NewFile(100*1024*1024, someFileObj)) // you'll need to manage Open(), Close() and Delete someFileObj
81
82 // Buffer composed of 32KB of memory, and 100MB of file.
83 buf := buffer.NewMulti(mem, file)
84 ```
85
86 #### Unbounded Buffers ####
87
88 Partition: A queue of buffers. Writes always go to the last buffer in the queue. If all buffers are full, a new buffer is "pushed" to the end of the queue (generated by a user-given function). Reads come from the first buffer, when the first buffer is emptied it is "popped" off the queue.
89
90 ```go
91 import (
92 "github.com/djherbis/buffer"
93 )
94
95 // Create 32 KB sized-chunks of memory as needed to expand/contract the buffer size.
96 buf := buffer.NewPartition(buffer.NewMemPool(32*1024))
97
98 // Create 100 MB sized-chunks of files as needed to expand/contract the buffer size.
99 buf = buffer.NewPartition(buffer.NewFilePool(100*1024*1024, ""))
100 ```
101
102 Ring: A single buffer which begins overwriting the oldest buffered data when it reaches its capacity.
103
104 ```go
105 import (
106 "github.com/djherbis/buffer"
107 )
108
109 // Create a File-based Buffer with max size 100MB
110 file := buffer.NewFile(100*1024*1024, someFileObj) // you'll need to Open(), Close() and Delete someFileObj.
111
112 // If buffered data exceeds 100MB, overwrite oldest data as new data comes in
113 buf := buffer.NewRing(file) // requires BufferAt interface.
114 ```
115
116 Spill: A single buffer which when full, writes the overflow to a given io.Writer.
117 -> Note that it will actually "spill" whenever there is an error while writing, this should only be a "full" error.
118
119 ```go
120 import (
121 "github.com/djherbis/buffer"
122 "github.com/djherbis/nio"
123
124 "io/ioutil"
125 )
126
127 // Buffer 32KB to Memory, discard overflow
128 buf := buffer.NewSpill(32*1024, ioutil.Discard)
129 nio.Copy(w, r, buf)
130 ```
131
132 #### Empty Buffer ####
133
134 Discard: Reads always return EOF, writes goto ioutil.Discard.
135
136 ```go
137 import (
138 "github.com/djherbis/buffer"
139 )
140
141 // Reads will return io.EOF, writes will return success (nil error, full write) but no data was written.
142 buf := buffer.Discard
143 ```
144
145 Custom Buffers
146 ------------
147
148 Feel free to implement your own buffer, just meet the required interface (Buffer/BufferAt) and compose away!
149
150 ```go
151
152 // Buffer Interface used by Multi and Partition
153 type Buffer interface {
154 Len() int64
155 Cap() int64
156 io.Reader
157 io.Writer
158 Reset()
159 }
160
161 // BufferAt interface used by Ring
162 type BufferAt interface {
163 Buffer
164 io.ReaderAt
165 io.WriterAt
166 }
167
168 ```
169
170 Installation
171 ------------
172 ```sh
173 go get github.com/djherbis/buffer
174 ```