-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasync_buffer.go
More file actions
116 lines (97 loc) · 2.54 KB
/
async_buffer.go
File metadata and controls
116 lines (97 loc) · 2.54 KB
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
package main
import (
"context"
"io"
"sync"
"github.com/unxed/f4/vfs"
"github.com/unxed/vtui"
"github.com/unxed/f4/piecetable"
)
// AsyncBuffer provides non-blocking access to a file, returning ErrLoading
// and triggering background fetches when data is missing.
type AsyncBuffer struct {
file vfs.ReadAtCloser
size int
ctx context.Context
cancelCtx context.CancelFunc
mu sync.Mutex
loaded map[int][]byte // Chunk index -> Data
fetching map[int]bool // Chunk index -> is currently being fetched
chunkSize int
}
func NewAsyncBuffer(ctx context.Context, f vfs.ReadAtCloser) *AsyncBuffer {
bCtx, bCancel := context.WithCancel(ctx)
return &AsyncBuffer{
file: f,
size: int(f.Size()),
ctx: bCtx,
cancelCtx: bCancel,
loaded: make(map[int][]byte),
fetching: make(map[int]bool),
chunkSize: 256 * 1024, // 256 KB chunks
}
}
func (b *AsyncBuffer) Close() {
b.cancelCtx()
}
func (b *AsyncBuffer) Size() int {
return b.size
}
func (b *AsyncBuffer) Read(offset, length int) ([]byte, error) {
if offset < 0 || offset >= b.size || length <= 0 {
return nil, nil
}
if offset+length > b.size {
length = b.size - offset
}
startChunk := offset / b.chunkSize
endChunk := (offset + length - 1) / b.chunkSize
res := make([]byte, 0, length)
missingData := false
b.mu.Lock()
for i := startChunk; i <= endChunk; i++ {
if data, ok := b.loaded[i]; ok {
// Chunk is loaded. Extract needed bytes.
cStart := i * b.chunkSize
takeStart := offset - cStart
if takeStart < 0 { takeStart = 0 }
takeEnd := (offset + length) - cStart
if takeEnd > len(data) { takeEnd = len(data) }
if takeEnd > takeStart {
res = append(res, data[takeStart:takeEnd]...)
}
} else {
missingData = true
if !b.fetching[i] {
b.fetching[i] = true
go b.fetchChunk(i)
}
}
}
b.mu.Unlock()
if missingData {
return nil, piecetable.ErrLoading
}
return res, nil
}
func (b *AsyncBuffer) fetchChunk(idx int) {
off := int64(idx * b.chunkSize)
sz := b.chunkSize
if off+int64(sz) > int64(b.size) {
sz = int(int64(b.size) - off)
}
buf := make([]byte, sz)
n, err := b.file.ReadAt(b.ctx, buf, off)
vtui.FrameManager.PostTask(func() {
b.mu.Lock()
defer b.mu.Unlock()
delete(b.fetching, idx)
if b.ctx.Err() == nil && (err == nil || err == io.EOF) {
b.loaded[idx] = buf[:n]
vtui.FrameManager.Redraw()
} else if err != nil && err != context.Canceled {
// Report error but allow retry on next UI scroll
vtui.DebugLog("AsyncBuffer: failed to fetch chunk %d: %v", idx, err)
}
})
}