-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
299 lines (269 loc) · 7.05 KB
/
cache.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
/*
Package textbuffer provides (as a first rough prototype) handling of cached
byte buffers.
Clients create a *TextBuffer, which caches portions of bytes of an io.ReaderAt.
The text buffer implements io.ReaderAt itself, thus may be used in the same
fashion as the underlying reader.
reader := strings.NewReader("The quick brown fox jumps over the lazy dog.")
cache := textbuffer.NewCache(reader, 3)
bytebuf := make([]byte, 10)
n, err := cache.ReadAt(bytebuf, 4) // reads "quick..."
*/
package textbuffer
import (
"container/list"
"fmt"
"io"
"time"
)
// Note: this is just a quick hack / first draft
// TODO polish this up
// TODO use a priority queue instead of a slice of buffers
const BufferSize = 10
const DefaultCacheBufferCount = 3
//const RecycleTimeout = time.Minute
const RecycleTimeout = 2 * time.Second
var makes int
var frees int
var get, trash chan *TextBuffer
type TextBuffer struct {
lastAccess time.Time
offset int64
buffer []byte
}
// Create a text buffer. Will use DefaultBufferSize, if buffersize is 0.
func NewBuffer() *TextBuffer {
buf := make([]byte, BufferSize)
buf[0] = 0
textbuf := &TextBuffer{lastAccess: time.Now(), offset: -1, buffer: buf}
return textbuf
}
func (tbuf *TextBuffer) String() string {
since := time.Since(tbuf.lastAccess) / time.Millisecond
if len(tbuf.buffer) == 0 || tbuf.buffer[0] == 0 {
return fmt.Sprintf("<%dms %d|empty>", since, tbuf.offset)
} else {
return fmt.Sprintf("<%dms %d|\"%s\">", since, tbuf.offset, debugPrefix(string(tbuf.buffer)))
}
}
type queued struct {
when time.Time
textbuf *TextBuffer
}
func startRecycler() {
get = make(chan *TextBuffer)
trash = make(chan *TextBuffer)
go func() {
q := new(list.List)
running := true
for running {
if q.Len() == 0 {
q.PushFront(queued{when: time.Now(), textbuf: NewBuffer()})
fmt.Println("creating new text buffer")
}
e := q.Front()
timeout := time.NewTimer(RecycleTimeout)
fmt.Println("--------- setting timeout ------------")
select {
case b := <-trash:
if b == nil {
fmt.Println("=== STOP RECYCLER =========")
running = false
break
}
fmt.Println("adding buffer back to free list")
timeout.Stop()
q.PushFront(queued{when: time.Now(), textbuf: b})
case get <- e.Value.(queued).textbuf:
timeout.Stop()
q.Remove(e)
case <-timeout.C:
fmt.Printf("timeout expired, Q-len = %d\n", q.Len())
e := q.Front().Next() // skip 1st one (which is always present)
for e != nil {
n := e.Next()
if time.Since(e.Value.(queued).when) > RecycleTimeout {
q.Remove(e)
tbuf := e.Value.(queued).textbuf
fmt.Printf("releaseing buffer = %v for GC\n", tbuf)
e.Value = nil
}
e = n
}
fmt.Printf(" now Q-len = %d\n", q.Len())
}
//time.Sleep(3 * time.Second) // for debugging
}
}()
return
}
func stopRecycler() {
trash <- nil
}
func offsetBoundary(offset int64) int64 {
return (offset / BufferSize) * BufferSize
}
// === TextCache =============================================================
type TextCache struct {
textlen int64
buffers []*TextBuffer
reader io.ReaderAt
}
func NewCache(reader io.ReaderAt, buffercount int) *TextCache {
if buffercount == 0 {
buffercount = DefaultCacheBufferCount
}
cache := &TextCache{}
cache.buffers = make([]*TextBuffer, buffercount)
cache.reader = reader
return cache
}
func (cache *TextCache) Dump() {
for i, buf := range cache.buffers {
if buf != nil {
fmt.Printf("[%2d] \"%v\"\n", i, string(buf.buffer))
} else {
fmt.Printf("[%2d] \"\"\n", i)
}
}
}
func (cache *TextCache) ReadAt(buffer []byte, offset int64) (N int, err error) {
L := len(buffer) // target count, will be decreased incrementally
buf, inx, n := cache.loadText(offset)
start := int(offset - offsetBoundary(offset))
fmt.Printf("reading pos %d in %d|\"%s\"\n", start, inx, string(buf.buffer))
avail := n - start
fmt.Printf("L = %d, start = %d, avail = %d\n", L, start, avail)
if avail >= L { // can fill in one go, should be frequent case
slice := buf.buffer[start : start+L]
copy(buffer, slice)
} else { // fill buffer incrementally
slice := buf.buffer[start:]
copy(buffer, slice)
fmt.Printf("(1) now buffer = '%s'\n", buffer)
L -= avail
N += avail
for L > 0 {
buf, inx, n = cache.loadText(offset + int64(N))
avail = n
fmt.Printf("L = %d, N = %d, avail = %d\n", L, N, avail)
if avail >= L { // can fill in one go
slice = buf.buffer[:L]
fmt.Printf("copy '%s' -> '%s'\n", buffer[N:], slice)
copy(buffer[N:], slice)
fmt.Printf("(3) now buffer = '%s'\n", buffer)
N += L
L = 0 // done
} else {
fmt.Printf("copy '%s' -> '%s'\n", buffer[N:], buf.buffer)
copy(buffer[N:], buf.buffer)
fmt.Printf("(2) now buffer = '%s'\n", buffer)
L -= avail
N += avail
}
}
}
return
}
func (cache *TextCache) loadText(offset int64) (buf *TextBuffer, inx int, n int) {
buf, inx = cache.findBufferForOffset(offset)
if inx == -1 {
boundary := offsetBoundary(offset)
buf, inx = cache.getFreeBuffer()
var err error
n, err = cache.reader.ReadAt(buf.buffer, boundary)
fmt.Printf("read from reader: %d bytes|\"%s\"\n", n, debugPrefix(string(buf.buffer)))
if err != nil {
panic(fmt.Sprintf("error loading text, %d bytes read: %v", n, err.Error()))
}
}
buf.lastAccess = time.Now()
return
}
func (cache *TextCache) getFreeBuffer() (*TextBuffer, int) {
for inx, buf := range cache.buffers {
if buf == nil {
buf = <-get
buf.offset = -1
cache.buffers[inx] = buf
fmt.Println("get buffer from recycler")
return buf, inx
}
}
buf, inx := cache.findBufferLRU()
buf.offset = -1
return buf, inx
}
func (cache *TextCache) findBufferLRU() (*TextBuffer, int) {
var index int = -1
recent := time.Now()
for inx, b := range cache.buffers {
if b != nil {
if b.lastAccess.Before(recent) {
recent = b.lastAccess
index = inx
}
}
}
if index >= 0 {
fmt.Printf("LRU buffer = #%d\n", index)
return cache.buffers[index], index
}
return nil, -1
}
func (cache *TextCache) releaseBuffer(inx int) {
buf := cache.buffers[inx]
if buf != nil {
fmt.Printf("release buffer #%d|%s\n", inx, buf.String())
cache.buffers[inx] = nil
trash <- buf
}
}
func (cache *TextCache) findBufferForOffset(offset int64) (*TextBuffer, int) {
boundary := offsetBoundary(offset)
for inx, buf := range cache.buffers {
if buf != nil && buf.offset == boundary {
return buf, inx
}
}
return nil, -1
}
/*
func main() {
pool := make([][]byte, 20)
get, trash := makeRecycler()
var m runtime.MemStats
for {
b := <-get
i := rand.Intn(len(pool))
if pool[i] != nil {
trash <- pool[i]
frees++
}
pool[i] = b
time.Sleep(time.Second)
bytes := 0
for i := 0; i < len(pool); i++ {
if pool[i] != nil {
bytes += len(pool[i])
}
}
runtime.ReadMemStats(&m)
fmt.Printf("%d,%d,%d,%d,%d,%d,%d\n", m.HeapSys, bytes, m.HeapAlloc,
m.HeapIdle, m.HeapReleased, makes, frees)
}
}
*/
func debugPrefix(s string) string {
if len(s) > 8 {
return fmt.Sprintf("%.5s...", s)
} else {
return s
}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}