-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiskstorage.go
More file actions
106 lines (84 loc) · 2.54 KB
/
Copy pathdiskstorage.go
File metadata and controls
106 lines (84 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
package sebtopic
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/micvbang/go-helpy/filepathy"
"github.com/micvbang/simple-event-broker/internal/infrastructure/logger"
"github.com/micvbang/simple-event-broker/seberr"
)
type DiskStorage struct {
log logger.Logger
rootDir string
}
// NewDiskStorage returns a *DiskStorage that stores its data in rootDir on
// local disk.
func NewDiskStorage(log logger.Logger, rootDir string) *DiskStorage {
return &DiskStorage{
log: log,
rootDir: rootDir,
}
}
func (ds *DiskStorage) Writer(_ context.Context, key string) (io.WriteCloser, error) {
batchPath := ds.rootDirPath(key)
log := ds.log.WithField("key", key).WithField("path", batchPath)
log.Debugf("creating dirs")
err := os.MkdirAll(filepath.Dir(batchPath), os.ModePerm)
if err != nil {
return nil, fmt.Errorf("creating topic dir: %w", err)
}
log.Debugf("creating file")
f, err := os.Create(batchPath)
if err != nil {
return nil, fmt.Errorf("opening file '%s': %w", batchPath, err)
}
return f, nil
}
func (ds *DiskStorage) Reader(_ context.Context, key string) (io.ReadCloser, error) {
batchPath := ds.rootDirPath(key)
log := ds.log.WithField("key", key).WithField("path", batchPath)
log.Debugf("opening file")
f, err := os.Open(batchPath)
if err != nil {
if os.IsNotExist(err) {
err = errors.Join(err, seberr.ErrNotInStorage)
}
return nil, fmt.Errorf("opening record batch '%s': %w", batchPath, err)
}
return f, nil
}
func (ds *DiskStorage) ListFiles(_ context.Context, topicName string, extension string, startAfter *string) ([]File, error) {
log := ds.log.
WithField("topicName", topicName).
WithField("extension", extension)
log.Debugf("listing files")
t0 := time.Now()
rootDirPath := ds.rootDirPath("")
topicPath := ds.rootDirPath(topicName)
files := make([]File, 0, 128)
walkConfig := filepathy.WalkConfig{Files: true, Extensions: []string{extension}}
err := filepathy.Walk(topicPath, walkConfig, func(path string, info os.FileInfo, _ error) error {
// NOTE: Base could return "." or "/"
if startAfter != nil && filepath.Base(path) <= *startAfter {
return nil
}
relPath, err := filepath.Rel(rootDirPath, path)
if err != nil {
return fmt.Errorf("getting relative path for '%s': %w", path, err)
}
files = append(files, File{
Size: info.Size(),
Path: relPath,
})
return nil
})
log.Debugf("found %d files (%s)", len(files), time.Since(t0))
return files, err
}
func (ds *DiskStorage) rootDirPath(key string) string {
return filepath.Join(ds.rootDir, key)
}