-
Notifications
You must be signed in to change notification settings - Fork 9
/
diskstack.go
378 lines (311 loc) · 10.1 KB
/
diskstack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
Package diskstack implements a LIFO queue (known as a stack) on local disk.
The stack resizes a single file automatically according to the amount of
entries required.
Usage:
s, err := New("/path/to/file", int(0))
if err != nil {
// Do something.
}
for i := 0; i < 10; i++ {
if err := s.Push(i); err != nil {
// Do something.
}
}
var n int
ok, err := s.Pop(&n)
if err != nil {
// Do something
}
if !ok {
// Would mean the queue is empty, its not.
}
fmt.Println(n) // Prints 9
s.Pop(&n)
fmt.Println(n) // Prints 8
fmt.Println("stack length: ", s.Len()) // Prints "stack length: 8"
fmt.Println("stack size in bytes: ", s.Size())
*/
package diskstack
import (
"bytes"
"encoding/binary"
"encoding/gob"
"fmt"
"io"
"os"
"reflect"
"sync"
"sync/atomic"
"time"
"github.com/golang/glog"
)
const int64Size = 8
// internalVersion tracks the current version of the disk representation that
// we read/write. We should be able to read back versions that are <= our
// version number.
const internalVersion = 1
// VersionInfo is used to encode version information for the disk stack into
// our files. This struct can never have a field removed only added.
type VersionInfo struct {
// Version is the version of encoding used to by stack to encode this.
// This is not the same as the Semantic Version of the code.
Version uint64
// Created is when the file was actually created.
Created int64
}
// Encode encodes the VersionInfo to the start of a file. If an error returns
// the file will be truncated to 0.
func (v VersionInfo) encode(f *os.File) (n int, err error) {
defer func() {
if err != nil {
f.Truncate(0)
f.Seek(0, 0)
}
f.Sync()
}()
if _, err = f.Seek(0, 0); err != nil {
return 0, fmt.Errorf("could not seek the beginning of the file: %s", err)
}
buff := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buff)
// Write our data to the buffer.
if err = enc.Encode(v); err != nil {
return 0, fmt.Errorf("could not gob encode the data: %s", err)
}
// Write our version number to disk.
b := make([]byte, int64Size)
binary.LittleEndian.PutUint64(b, v.Version)
if _, err = f.Write(b); err != nil {
return 0, fmt.Errorf("unable to write the version number to disk")
}
f.Seek(-int64Size, 2)
if _, err := f.Read(b); err != nil {
panic(err)
}
// Write our size header to disk.
l := buff.Len()
binary.LittleEndian.PutUint64(b, uint64(l))
if _, err = f.Write(b); err != nil {
return 0, fmt.Errorf("unable to write data size to disk: %s", err)
}
f.Seek(-int64Size, 2)
if _, err := f.Read(b); err != nil {
panic(err)
}
// Write the buffer to disk.
if _, err = f.Write(buff.Bytes()); err != nil {
return 0, fmt.Errorf("unable to write our version info to disk: %s", err)
}
return int(int64Size + int64Size + l), nil
}
func (v *VersionInfo) decode(f *os.File) error {
if _, err := f.Seek(0, 0); err != nil {
return fmt.Errorf("could not seek the beginning of the file: %s", err)
}
// Read version number back.
b := make([]byte, int64Size)
if _, err := f.Read(b); err != nil {
return fmt.Errorf("cannot read the version number from the file: %s", err)
}
ver := binary.LittleEndian.Uint64(b)
if ver > internalVersion {
return fmt.Errorf("cannot read this file: current version number %d is greater than the libraries version number %d", ver, internalVersion)
}
// Read version block size.
if _, err := f.Read(b); err != nil {
return fmt.Errorf("cannot read the version block size back from the file: %s", err)
}
blockSize := binary.LittleEndian.Uint64(b)
b = make([]byte, blockSize)
if _, err := f.Read(b); err != nil {
return fmt.Errorf("cannot read the version block from the file: %s, err")
}
dec := gob.NewDecoder(bytes.NewBuffer(b))
if err := dec.Decode(v); err != nil {
return fmt.Errorf("cannot convert the version block on disk into a VersionInfo struct: %s", err)
}
return nil
}
// Stack implements a LIFO queue that can store a single object type that can
// be encoded by the gob encoder (encoding/gob) on local disk.
// The file is constantly resized based for each pop() and push() operation.
// All operations are thread-safe.
type Stack struct {
storedType string
f *os.File
rws io.ReadWriteSeeker
flush bool
mu sync.Mutex // Protects everything below.
length int64 // Only access with atomic operations.
size int64 // Dito.
}
// Option provides an optional argument to New().
type Option func(s *Stack)
// NoFlush indicates to not flush every write to disk. This increases speed but
// at the cost of possibly losing a Push() or Pop().
func NoFlush() Option {
return func(s *Stack) {
s.flush = false
}
}
// New creates a new instance of Stack. p is the location of where to write the
// stack. The file must not exist. dataType is the value that you will store
// in the stack. It must be gob encodable. Also see Pop() for more information
// on what can go here.
func New(p string, dataType interface{}, options ...Option) (*Stack, error) {
f, err := os.OpenFile(p, os.O_RDWR+os.O_CREATE+os.O_EXCL, 0666)
if err != nil {
return nil, err
}
vi := VersionInfo{Version: internalVersion, Created: time.Now().Unix()}
viLen, err := vi.encode(f)
if err != nil {
return nil, err
}
if _, err := f.Seek(int64(viLen), 0); err != nil {
return nil, fmt.Errorf("cannot seek past the VersionInfo header we wrote")
}
s := &Stack{flush: true, f: f, rws: f, length: 0, size: int64(viLen), storedType: reflect.TypeOf(dataType).Name()}
for _, o := range options {
o(s)
}
return s, nil
}
// Close closes the file backing the stack on disk. This does not erase the file.
func (d *Stack) Close() error {
return d.rws.(io.Closer).Close()
}
// Len returns the amount of items currently on the stack.
func (d *Stack) Len() int {
// Use atomic so that getting Len() is independent from our internal Mutex.
return int(atomic.LoadInt64(&d.length))
}
// Size returns the current size of the stack in bytes on disk.
func (d *Stack) Size() int {
// Use atomic so that getting Size() is independent from our internal Mutex.
return int(atomic.LoadInt64(&d.size))
}
// Pop returns the last stored value that was on the stack and puts it in data. data must be
// a pointer type (even to reference types) and must either be the same type as was
// passed via New() or a pointer to that type. If ok == false and err == nil,
// it indicates the stack was empty.
// Note: This should work with almost all fixed types and other basic types.
// But I'm sure some ***type thing will break this. Pointer to fixed value, struct, and
// reference types is all that is supported.
func (d *Stack) Pop(data interface{}) (ok bool, err error) {
if !d.isPtrToType(data) {
return false, fmt.Errorf("cannot pop stored data of type %s into type %T", d.storedType, data)
}
d.mu.Lock()
defer d.mu.Unlock()
if atomic.LoadInt64(&d.length) == 0 {
return false, nil
}
if err := d.readData(data); err != nil {
return false, err
}
atomic.AddInt64(&d.length, -1)
return true, nil
}
// readSize() reads the last 8 bytes of the file to pull a size header off.
// It then truncates the file 8 bytes and moves the offset -8 from the end.
// Returns -1 if it can't read from the end.
func (d *Stack) readSize() int64 {
// Read the last 8 bytes back as an encoded uint64.
d.rws.Seek(-8, 2)
b := make([]byte, 8)
if _, err := d.rws.Read(b); err != nil {
glog.Errorf("problem reading from disk stack: %s", err)
return -1
}
// Shrink the file by 8 bytes.
atomic.AddInt64(&d.size, -8)
d.f.Truncate(int64(d.Size()))
d.rws.Seek(-8, 2)
// Return the decoded size.
return int64(binary.LittleEndian.Uint64(b))
}
// readData calls n := readSize() and then seeks -n from the end of the file
// and asks the gob decoder to read the entry. It then truncates the file
// the size of the data we read and seeks to the new end position.
func (d *Stack) readData(data interface{}) error {
n := d.readSize()
if n < 0 {
return fmt.Errorf("could not read the size of the next entry on the stack")
}
if _, err := d.rws.Seek(-n, 2); err != nil {
return fmt.Errorf("could not seek back to start of data(-%d bytes): %s", n, err)
}
b := make([]byte, n)
rs, err := d.rws.Read(b)
if err != nil {
return fmt.Errorf("data size was %d, but encountered error trying to read that from disk: %s", err)
}
if int64(rs) != n {
d.f.Seek(int64(-rs), 1)
return fmt.Errorf("data size was %d, but only could read %d back", n, rs)
}
//read := byter{d.rws}
//read := bufio.NewReader(d.rws)
dec := gob.NewDecoder(bytes.NewBuffer(b))
if err := dec.Decode(data); err != nil {
return fmt.Errorf("could not decode the gob data: %s", err)
}
atomic.AddInt64(&d.size, -n)
d.f.Truncate(int64(d.Size()))
d.rws.Seek(-n, 2)
return nil
}
// Push pushes an new entry onto the stack. data must be of the same type
// passed in New().
func (d *Stack) Push(data interface{}) error {
if !d.isType(data) {
return fmt.Errorf("cannot push data of type %T into stack of type %s", data, d.storedType)
}
buff := bytes.NewBuffer([]byte{})
enc := gob.NewEncoder(buff)
// Write our data to the buffer.
if err := enc.Encode(data); err != nil {
return fmt.Errorf("could not gob encode the data: %s", err)
}
// Write our size header.
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(buff.Len()))
if _, err := buff.Write(b); err != nil {
return fmt.Errorf("unable to write data size to memory buffer: %s", err)
}
// Write the data to disk and update counters.
d.mu.Lock()
defer d.mu.Unlock()
d.rws.Seek(0, 2)
n, err := d.rws.Write(buff.Bytes())
if err != nil {
d.f.Truncate(int64(d.Size()))
d.rws.Seek(-int64(n), 2)
return fmt.Errorf("could not write to the file: %s", err)
}
atomic.AddInt64(&d.size, int64(n))
atomic.AddInt64(&d.length, 1)
if d.flush {
d.f.Sync()
}
return nil
}
func (d *Stack) isPtrToType(data interface{}) bool {
t := reflect.TypeOf(data)
if t.Kind() != reflect.Ptr {
return false
}
if t.Elem().Name() != d.storedType {
return false
}
return true
}
func (d *Stack) isType(data interface{}) bool {
t := reflect.TypeOf(data)
if t.Name() != d.storedType {
return false
}
return true
}