-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_record_store.go
153 lines (121 loc) · 3.17 KB
/
fs_record_store.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
package gokvstore
import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/google/uuid"
"github.com/gustapinto/go-kv-store/gen"
"google.golang.org/protobuf/proto"
)
// FsRecordStore A filesystem (local disk) based record store. Implements [RecordStore]
type FsRecordStore struct {
dataDir string
}
var _ RecordStore = (*FsRecordStore)(nil)
// NewFsRecordStore Create a new [FsRecordStore]
func NewFsRecordStore(dataDir string) *FsRecordStore {
absPath, err := filepath.Abs(dataDir)
if err != nil {
return nil
}
if err := os.MkdirAll(absPath, fs.FileMode(0755)); err != nil {
return nil
}
return &FsRecordStore{
dataDir: absPath,
}
}
func (f *FsRecordStore) List() (recordPaths []string, err error) {
builder := strings.Builder{}
builder.WriteString(f.dataDir)
builder.WriteString("/*.binpb")
recordsGlobPattern := filepath.Clean(builder.String())
paths, err := filepath.Glob(recordsGlobPattern)
if err != nil {
return nil, err
}
return paths, err
}
func (f *FsRecordStore) Read(recordPath string) (*gen.Record, error) {
file, err := os.Open(recordPath)
if err != nil {
return nil, err
}
defer file.Close()
buffer, err := io.ReadAll(file)
if err != nil {
return nil, err
}
var record gen.Record
if err := proto.Unmarshal(buffer, &record); err != nil {
return nil, err
}
return &record, nil
}
func (f *FsRecordStore) Remove(recordPath string) error {
if err := os.Remove(recordPath); err != nil {
return err
}
return nil
}
func (f *FsRecordStore) createRecordFile(recordPath string, buffer []byte) error {
file, err := os.OpenFile(recordPath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(buffer)
return err
}
func (f *FsRecordStore) updateRecordFile(recordPath string, buffer []byte) error {
tempDir, err := os.MkdirTemp(f.dataDir, "tmp*")
if err != nil {
return err
}
defer os.RemoveAll(tempDir)
builder := strings.Builder{}
builder.WriteString(uuid.NewString())
builder.WriteString(".binpb")
tempFilePath := filepath.Join(tempDir, builder.String())
tempFile, err := os.OpenFile(tempFilePath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return err
}
defer tempFile.Close()
if _, err := tempFile.Write(buffer); err != nil {
return err
}
return os.Rename(tempFile.Name(), recordPath)
}
func (f *FsRecordStore) Write(recordPath string, record *gen.Record) error {
buffer, err := proto.Marshal(record)
if err != nil {
return err
}
if _, err := os.Stat(recordPath); errors.Is(err, os.ErrNotExist) {
return f.createRecordFile(recordPath, buffer)
}
return f.updateRecordFile(recordPath, buffer)
}
func (f *FsRecordStore) MakeRecordPath(fileId string) string {
builder := strings.Builder{}
builder.WriteString(fileId)
builder.WriteString(".binpb")
return filepath.Join(f.dataDir, builder.String())
}
func (f *FsRecordStore) MakeStoreForCollection(dir string) (RecordStore, error) {
collectionPath, err := filepath.Abs(filepath.Join(f.dataDir, dir))
if err != nil {
return nil, err
}
return NewFsRecordStore(collectionPath), nil
}
func (f *FsRecordStore) Truncate() error {
if err := os.RemoveAll(f.dataDir); err != nil {
return err
}
return nil
}