-
Notifications
You must be signed in to change notification settings - Fork 55
/
memory.go
213 lines (181 loc) · 3.7 KB
/
memory.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
package zfile
import (
"io"
"os"
"sync"
"time"
)
type MemoryFile struct {
name string
lock sync.RWMutex
buffer byteBuffer
timing int64
stopTiming chan struct{}
stop bool
fbefore memoryFileFlushBefore
}
type MemoryFileOption func(*MemoryFile)
type memoryFileFlushBefore func(f *MemoryFile) error
func MemoryFileAutoFlush(second int64) func(*MemoryFile) {
return func(f *MemoryFile) {
f.timing = second
}
}
// MemoryFileFlushBefore is a function that will be called before flush to disk,take care to avoid writing to prevent deadlocks
func MemoryFileFlushBefore(fn memoryFileFlushBefore) func(*MemoryFile) {
return func(f *MemoryFile) {
f.fbefore = fn
}
}
func NewMemoryFile(name string, opt ...MemoryFileOption) *MemoryFile {
f := &MemoryFile{name: name, buffer: makeByteBuffer([]byte{})}
for _, o := range opt {
o(f)
}
f.stopTiming = make(chan struct{})
if f.timing > 0 {
go f.flushLoop()
}
return f
}
func (f *MemoryFile) flushLoop() {
ticker := time.NewTicker(time.Second * time.Duration(f.timing))
defer func() {
}()
for {
select {
case <-f.stopTiming:
return
case <-ticker.C:
_ = f.Sync()
}
}
}
func (f *MemoryFile) SetName(name string) {
f.name = name
}
func (f *MemoryFile) Bytes() []byte {
f.lock.RLock()
b := f.buffer.buffer
f.lock.RUnlock()
return b
}
func (f *MemoryFile) Stat() (os.FileInfo, error) {
return f, nil
}
func (f *MemoryFile) Read(buffer []byte) (int, error) {
f.lock.RLock()
n, err := f.buffer.Read(buffer)
f.lock.RUnlock()
return n, err
}
func (f *MemoryFile) Close() error {
f.lock.Lock()
if f.stop {
return nil
}
f.stop = true
f.lock.Unlock()
f.stopTiming <- struct{}{}
return f.Sync()
}
func (f *MemoryFile) Sync() error {
if f.Size() == 0 {
return nil
}
if f.fbefore != nil {
err := f.fbefore(f)
if err != nil {
return err
}
}
f.lock.Lock()
b := f.buffer.buffer
f.buffer.Reset()
f.lock.Unlock()
return WriteFile(f.name, b, true)
}
func (f *MemoryFile) Write(buffer []byte) (int, error) {
f.lock.Lock()
n, err := f.buffer.Write(buffer)
f.lock.Unlock()
return n, err
}
func (f *MemoryFile) Seek(offset int64, whence int) (int64, error) {
f.lock.RLock()
n, err := f.buffer.Seek(offset, whence)
f.lock.RUnlock()
return n, err
}
func (f *MemoryFile) Name() string {
return f.name
}
func (f *MemoryFile) Size() int64 {
f.lock.RLock()
l := int64(f.buffer.Len())
f.lock.RUnlock()
return l
}
func (f *MemoryFile) Mode() os.FileMode {
return 0666
}
func (f *MemoryFile) ModTime() time.Time {
return time.Time{}
}
func (f *MemoryFile) IsDir() bool {
return false
}
func (f *MemoryFile) Sys() interface{} {
return nil
}
type byteBuffer struct {
buffer []byte
index int
}
func makeByteBuffer(buffer []byte) byteBuffer {
return byteBuffer{
buffer: buffer,
index: 0,
}
}
func (bb *byteBuffer) Reset() {
bb.buffer = bb.buffer[:0]
bb.index = 0
}
func (bb *byteBuffer) Len() int {
return len(bb.buffer)
}
func (bb *byteBuffer) Position() int {
return bb.index
}
func (bb *byteBuffer) Bytes() []byte {
return bb.buffer
}
func (bb *byteBuffer) Read(buffer []byte) (int, error) {
if len(buffer) == 0 {
return 0, nil
}
if bb.index >= bb.Len() {
return 0, io.EOF
}
last := copy(buffer, bb.buffer[bb.index:])
bb.index += last
return last, nil
}
func (bb *byteBuffer) Write(buffer []byte) (int, error) {
bb.buffer = append(bb.buffer[:bb.index], buffer...)
bb.index += len(buffer)
return len(buffer), nil
}
func (bb *byteBuffer) Seek(offset int64, whence int) (int64, error) {
switch whence {
default:
case io.SeekStart:
bb.index = int(offset)
case io.SeekCurrent:
bb.index += int(offset)
case io.SeekEnd:
bb.index = bb.Len() - 1 - int(offset)
}
return int64(bb.index), nil
}