This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
snapshot.go
267 lines (228 loc) · 5.88 KB
/
snapshot.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
package rex
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"time"
"github.com/cheekybits/genny/generic"
"github.com/remerge/gobi"
)
type _T_ generic.Type
// type gobi generic.Type
type SnapshoterFor_T_ struct {
basePath string
ext string
timeLayout string
}
var _T_EncodeGauge = ""
var _T_DecodeGauge = ""
var _T_Snapshoter *SnapshoterFor_T_ = NewSnapshoterFor_T_("_basePath", "_ext", "_timeLayout")
func NewSnapshoterFor_T_(basePath, ext, timeLayout string) *SnapshoterFor_T_ {
return &SnapshoterFor_T_{basePath, ext, timeLayout}
}
func (self *SnapshoterFor_T_) BasePath() string {
path, err := filepath.Abs(self.basePath)
_MayPanic(err)
return path
}
func (self *SnapshoterFor_T_) Path(name string) string {
return filepath.Join(self.BasePath(), name)
}
func (self *SnapshoterFor_T_) Glob() ([]string, error) {
if len(self.ext) > 0 {
return filepath.Glob(self.Path("*." + self.ext))
} else {
return filepath.Glob(self.Path("*"))
}
}
func (self *SnapshoterFor_T_) Newest() (string, error) {
files, err := self.sorted()
if err != nil {
return "", err
}
if len(files) == 0 {
return "", errors.New("No snapshot found in " + self.BasePath())
}
return files[len(files)-1], nil
}
// explicit setter
func (self *SnapshoterFor_T_) SetExt(n string) {
self.ext = n
}
// func (self *SnapshoterFor_T_) Offset(file string) int64 {
// pattern := regexp.MustCompile(`\-(\d+)(\-\d+)?\.` + self.Ext + `$`)
// r := pattern.FindStringSubmatch(file)
// if len(r) < 2 {
// _MayPanic(errors.New("invalid filename passed to SnapshotOffseter:" + file))
// }
// i, err := strconv.Atoi(r[1])
// _MayPanic(err)
// return int64(i)
// }
// func (self *SnapshoterFor_T_) NewestOffsetSafe() int64 {
// defer func() {
// if r := recover(); r != nil {
// }
// }()
// file, err := self.Newest()
// if err != nil {
// return -1
// }
// return self.Offset(file)
// }
func (self *SnapshoterFor_T_) sorted() ([]string, error) {
files, err := self.Glob()
if err != nil {
return nil, err
}
sort.Strings(files)
return files, nil
}
func removeFiles(files []string) (deleted []string, errs []error) {
if len(files) == 0 {
return []string{}, nil
}
deleted = make([]string, 0)
errs = make([]error, 0)
for _, file := range files {
err := os.Remove(file)
if err == nil {
deleted = append(deleted, file)
} else {
errs = append(errs, errors.New(fmt.Sprintf("not deleted %s : %v", file, err)))
}
}
return deleted, errs
}
// clean all but the newest
func (self *SnapshoterFor_T_) Clean() ([]string, []error) {
files, err := self.sorted()
_MayPanic(err)
if len(files) <= 1 {
return []string{}, nil
}
return removeFiles(files[0 : len(files)-1])
}
func (self *SnapshoterFor_T_) CleanAll() ([]string, []error) {
files, err := self.Glob()
_MayPanic(err)
return removeFiles(files[0 : len(files)-1])
}
func (self *SnapshoterFor_T_) Load(b []byte) (*_G_, error) {
buf := bytes.NewBuffer(b)
return self.LoadFromReader(buf)
}
func (self *SnapshoterFor_T_) LoadFromReader(r io.Reader) (result *_G_, err error) {
dec := gobi.NewDecoder(r)
dec.SetupSizeMetrics(_T_DecodeGauge)
result = new(_G_)
err = dec.Decode(result)
if err != nil {
return nil, err
}
return result, nil
}
func (self *SnapshoterFor_T_) LoadFromReaderAsInterface(r io.Reader) (result interface{}, err error) {
return self.LoadFromReader(r)
}
func (self *SnapshoterFor_T_) LoadFromFile(filename string) (result *_G_, err error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer func() {
_MayPanic(file.Close())
}()
return self.LoadFromReader(file)
}
func (self *SnapshoterFor_T_) LoadNewestFromFile() (result *_G_, err error) {
file, err := self.Newest()
if err != nil {
return nil, err
}
return self.LoadFromFile(file)
}
func (self *SnapshoterFor_T_) LoadNewestFromFileAsInterface() (result interface{}, err error) {
return self.LoadNewestFromFile()
}
func (self *SnapshoterFor_T_) BytesToFileNamedByTime(b []byte) (string, error) {
filename := _T_Snapshoter.CurrentTimeBasedFilename()
err := self.BytesToFile(b, _T_Snapshoter.CurrentTimeBasedFilename())
return filename, err
}
func (self *SnapshoterFor_T_) BytesToFile(b []byte, filename string) error {
dir := filepath.Dir(filename)
err := os.MkdirAll(dir, 0777)
if err != nil {
return err
}
fo, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
_MayPanic(fo.Close())
}()
w := bufio.NewWriter(fo)
if _, err = w.Write(b); err != nil {
return err
}
if err = w.Flush(); err != nil {
return err
}
return nil
}
func (self *SnapshoterFor_T_) CurrentTimeBasedFilename() string {
rpath := fmt.Sprintf("%s.%s", time.Now().Format(self.timeLayout), self.ext)
return self.Path(rpath)
}
func (self *SnapshoterFor_T_) Filename(date time.Time) string {
return fmt.Sprintf("%s.%s", date.Format(self.timeLayout), self.ext)
}
func (self *SnapshoterFor_T_) AbsFilename(date time.Time) string {
return self.Path(self.Filename(date))
}
func (self *_G_) SnapshotToFileNamedBy(time time.Time) (err error) {
return self.SnapshotToFile(_T_Snapshoter.AbsFilename(time))
}
func (self *_G_) SnapshotToFileNamedByTime() (filename string, err error) {
filename = _T_Snapshoter.CurrentTimeBasedFilename()
err = self.SnapshotToFile(filename)
return filename, err
}
func (self *_G_) SnapshotToWriter(w io.Writer) {
enc := gobi.NewEncoder(w)
enc.SetupSizeMetrics(_T_EncodeGauge)
err := enc.Encode(self)
_MayPanic(err)
}
func (self *_G_) SnapshotToFile(filename string) error {
dir := filepath.Dir(filename)
err := os.MkdirAll(dir, 0777)
if err != nil {
return err
}
fo, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
_MayPanic(fo.Close())
}()
w := bufio.NewWriter(fo)
self.SnapshotToWriter(w)
if err = w.Flush(); err != nil {
return err
}
return nil
}
func (self *_G_) Snapshot() []byte {
var result bytes.Buffer
self.SnapshotToWriter(&result)
return result.Bytes()
}