-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.go
106 lines (95 loc) · 2.02 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
package adb
// Account database
// Snapshot
// Copyright © 2018 Eduard Sesigin. All rights reserved. Contacts: <[email protected]>
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"sync/atomic"
)
type Snapshoter struct {
path string
state int64
lastSnapshot int64
accounts *Accounts
}
func newSnapshoter(symbol *Symbol, path string) *Snapshoter {
return &Snapshoter{
path: path,
state: stateClosed,
accounts: newAccounts(symbol),
}
}
func (s *Snapshoter) start() error {
files, err := ioutil.ReadDir(s.path)
if err != nil {
return err
}
snaps := make([]string, 0)
logs := make([]string, 0)
for _, fileName := range files {
fns := fileName.Name()
if strings.HasSuffix(fns, snapExt) {
snaps = append(snaps, fns)
}
if strings.HasSuffix(fns, logExt) {
logs = append(logs, fns)
}
}
// При старте по идее не должно быть снапов и логов!
if len(snaps) > 0 || len(logs) > 0 {
sort.Strings(snaps)
sort.Strings(logs)
return fmt.Errorf("Logs: %v \nSnaps:%v", logs, snaps)
}
return nil
}
func (s *Snapshoter) clean() error {
files, err := ioutil.ReadDir(s.path)
if err != nil {
return err
}
for _, fileName := range files {
fns := fileName.Name()
if strings.HasSuffix(fns, snapExt) || strings.HasSuffix(fns, logExt) {
if err := os.Remove(s.path + fns); err != nil {
return nil
}
}
}
return nil
}
func (s *Snapshoter) currentSnap() (string, error) {
files, err := ioutil.ReadDir(s.path)
if err != nil {
return "", err
}
snaps := make([]string, 0)
for _, fileName := range files {
fns := fileName.Name()
if strings.HasSuffix(fns, snapExt) {
snaps = append(snaps, fns)
}
}
if len(snaps) > 0 {
sort.Strings(snaps)
if ln := len(snaps); ln > 1 {
return snaps[ln-2], nil
} else {
return snaps[ln-1], nil
}
}
return "", fmt.Errorf("Snapshot does not exists")
}
func (s *Snapshoter) stop() {
}
func (s *Snapshoter) worker() {
for {
if atomic.LoadInt64(&s.state) == stateClosed {
return
}
}
}