-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
63 lines (51 loc) · 1007 Bytes
/
fs.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
package snail
import (
"errors"
"fmt"
)
type FS interface {
Write(src []byte) error
}
type LocalFS struct {
cfg *Config
table Table
}
func NewLocalFS(cfg *Config) LocalFS {
return LocalFS{
cfg: cfg,
table: newTable(cfg.TableSize),
}
}
func (f *LocalFS) Write(src *[]byte) (int, error) {
p := encodePayload(src)
l := len(p)
if l > f.cfg.TableSize {
return 0, fmt.Errorf("oversized payload")
}
wl, err := f.table.write(f.cfg, &p)
if err != nil {
if errors.Is(err, errorTableFull) {
// Handle case that table is full
if err := f.table.flush(f.cfg.FilePath); err != nil {
return 0, err
}
if err := f.rotateTable(); err != nil {
return 0, err
}
return f.table.write(f.cfg, &p)
} else {
return 0, err
}
} else {
return wl, nil
}
}
func (f *LocalFS) rotateTable() error {
err := f.table.flush(f.cfg.FilePath)
if err != nil && !errors.Is(err, errorTableFlushed) {
return err
}
t := newTable(f.cfg.TableSize)
f.table = t
return nil
}