-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileSyncer.go
190 lines (177 loc) · 4.2 KB
/
FileSyncer.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
package m2obj
import (
"io/ioutil"
"sync"
"time"
)
type noBoundObjErr struct{}
func (e noBoundObjErr) Error() string {
return "no bound object to be synced"
}
// FileSyncer
//
// Data serialization management and synchronization between files and memory using Formatter
//
// !!! Only Bind to GROUP Object please !!!
type FileSyncer struct {
// HardLoad
//
// Uses SetVal(), instead of GroupMerge(). This means that each loading will clear all previous data
//
// DEFAULT: false
HardLoad bool
// AutoSaveTiming
//
// <0: Don't auto save
// =0: DEFAULT. Auto save when obj changed
// >0(ms): Auto save when timer triggered
AutoSaveTiming int64
// AutoLoadTiming
//
// <=0: DEFAULT. Don't auto load
// >0(ms): Auto load when timer triggered
// while AutoLoadTiming > 0, the AutoSaveTiming is disabled
AutoLoadTiming int64
// path to the file
filePath string
// an instance of a kind of data formatters, which must implements the interface Formatter
formatter Formatter
// bound object
obj *Object
// file mutex
fileMutex sync.Mutex
// object mutex
objMutex sync.Mutex
}
func (fs *FileSyncer) GetFilePath() (filePath string) {
fs.fileMutex.Lock()
defer fs.fileMutex.Unlock()
return fs.filePath
}
func (fs *FileSyncer) SetFilePath(filePath string) {
fs.fileMutex.Lock()
defer fs.fileMutex.Unlock()
fs.filePath = filePath
return
}
func (fs *FileSyncer) GetBoundObject() (obj *Object) {
fs.objMutex.Lock()
defer fs.objMutex.Unlock()
return fs.obj
}
func (fs *FileSyncer) SetFormatter(formatter Formatter) {
fs.fileMutex.Lock()
defer fs.fileMutex.Unlock()
fs.objMutex.Lock()
defer fs.objMutex.Unlock()
fs.formatter = formatter
return
}
func (fs *FileSyncer) Save() (err error) {
fs.objMutex.Lock()
if fs.obj == nil {
fs.objMutex.Unlock()
return noBoundObjErr{}
} else {
fs.objMutex.Unlock()
}
var buf []byte
fs.objMutex.Lock()
buf, err = fs.formatter.Marshal(fs.obj)
fs.objMutex.Unlock()
if err == nil {
fs.fileMutex.Lock()
err = ioutil.WriteFile(fs.filePath, buf, 0644)
fs.fileMutex.Unlock()
}
return
}
func (fs *FileSyncer) Load() (err error) {
fs.objMutex.Lock()
if fs.obj == nil {
fs.objMutex.Unlock()
return noBoundObjErr{}
} else {
fs.objMutex.Unlock()
}
var buf []byte
fs.fileMutex.Lock()
buf, err = ioutil.ReadFile(fs.filePath)
fs.fileMutex.Unlock()
if err == nil {
var obj *Object
obj, err = fs.formatter.Unmarshal(buf)
if err == nil {
if fs.HardLoad {
fs.objMutex.Lock()
fs.obj.setVal(obj, false)
fs.objMutex.Unlock()
} else {
fs.objMutex.Lock()
err = fs.obj.groupMerge(obj, true, false)
fs.objMutex.Unlock()
}
}
}
return
}
// BindObject
//
// !!! Only Bind to GROUP Object !!!
func (fs *FileSyncer) BindObject(obj *Object) {
if obj == nil || !obj.IsGroup() {
panic(invalidTypeErr(""))
}
fs.objMutex.Lock()
defer fs.objMutex.Unlock()
if fs.obj != nil {
fs.obj.onChange = nil
}
fs.obj = obj
fs.obj.onChange = func() {
if fs.AutoLoadTiming <= 0 && fs.AutoSaveTiming == 0 {
_ = fs.Save()
}
}
}
// NewFileSyncer
//
// Creates a new FileSyncer with filePath and formatter.
//
// To enable the FileSyncer, Follow the steps below :
//
// 1. Call NewFileSyncer (set filePath and formatter).
// 2. Set some options such as AutoSaveTiming, AutoLoadTiming, HardLoad, etc...
// The filePath and formatter can be set as well but using `Set` method.
// 3. Call FileSyncer.BindObject to bind the object that to be synced.
// 4. Then you can see the FileSyncer starts working automatically.
// 5. You can also call FileSyncer.Save or FileSyncer.Load to sync manually.
func NewFileSyncer(filePath string, formatter Formatter) *FileSyncer {
fs := &FileSyncer{
filePath: filePath,
formatter: formatter,
HardLoad: false,
obj: nil,
}
go func() {
for {
func() {
defer func() {
_ = recover()
}()
autoLoadTiming := fs.AutoLoadTiming
autoSaveTiming := fs.AutoSaveTiming
if autoLoadTiming > 0 {
time.Sleep(time.Duration(autoLoadTiming * int64(time.Millisecond)))
_ = fs.Load()
} else if autoSaveTiming > 0 {
time.Sleep(time.Duration(autoSaveTiming * int64(time.Millisecond)))
_ = fs.Save()
} else {
time.Sleep(time.Second)
}
}()
}
}()
return fs
}