forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
filestore.go
331 lines (287 loc) · 9.66 KB
/
filestore.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package quickfix
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"time"
"github.com/alpacahq/quickfix/config"
)
type msgDef struct {
offset int64
size int
}
type fileStoreFactory struct {
settings *Settings
}
type fileStore struct {
sessionID SessionID
cache *memoryStore
offsets map[int]msgDef
bodyFname string
headerFname string
sessionFname string
senderSeqNumsFname string
targetSeqNumsFname string
bodyFile *os.File
headerFile *os.File
sessionFile *os.File
senderSeqNumsFile *os.File
targetSeqNumsFile *os.File
}
// NewFileStoreFactory returns a file-based implementation of MessageStoreFactory
func NewFileStoreFactory(settings *Settings) MessageStoreFactory {
return fileStoreFactory{settings: settings}
}
// Create creates a new FileStore implementation of the MessageStore interface
func (f fileStoreFactory) Create(sessionID SessionID) (msgStore MessageStore, err error) {
sessionSettings, ok := f.settings.SessionSettings()[sessionID]
if !ok {
return nil, fmt.Errorf("unknown session: %v", sessionID)
}
dirname, err := sessionSettings.Setting(config.FileStorePath)
if err != nil {
return nil, err
}
return newFileStore(sessionID, dirname)
}
func newFileStore(sessionID SessionID, dirname string) (*fileStore, error) {
if err := os.MkdirAll(dirname, os.ModePerm); err != nil {
return nil, err
}
sessionPrefix := sessionIDFilenamePrefix(sessionID)
store := &fileStore{
sessionID: sessionID,
cache: &memoryStore{},
offsets: make(map[int]msgDef),
bodyFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "body")),
headerFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "header")),
sessionFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "session")),
senderSeqNumsFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "senderseqnums")),
targetSeqNumsFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "targetseqnums")),
}
if err := store.Refresh(); err != nil {
return nil, err
}
return store, nil
}
// Reset deletes the store files and sets the seqnums back to 1
func (store *fileStore) Reset() error {
store.cache.Reset()
if err := store.Close(); err != nil {
return err
}
if err := removeFile(store.bodyFname); err != nil {
return err
}
if err := removeFile(store.headerFname); err != nil {
return err
}
if err := removeFile(store.sessionFname); err != nil {
return err
}
if err := removeFile(store.senderSeqNumsFname); err != nil {
return err
}
if err := removeFile(store.targetSeqNumsFname); err != nil {
return err
}
return store.Refresh()
}
// Refresh closes the store files and then reloads from them
func (store *fileStore) Refresh() (err error) {
store.cache.Reset()
if err = store.Close(); err != nil {
return err
}
creationTimePopulated, err := store.populateCache()
if err != nil {
return err
}
if store.bodyFile, err = openOrCreateFile(store.bodyFname, 0660); err != nil {
return err
}
if store.headerFile, err = openOrCreateFile(store.headerFname, 0660); err != nil {
return err
}
if store.sessionFile, err = openOrCreateFile(store.sessionFname, 0660); err != nil {
return err
}
if store.senderSeqNumsFile, err = openOrCreateFile(store.senderSeqNumsFname, 0660); err != nil {
return err
}
if store.targetSeqNumsFile, err = openOrCreateFile(store.targetSeqNumsFname, 0660); err != nil {
return err
}
if !creationTimePopulated {
if err := store.setSession(); err != nil {
return err
}
}
store.SetNextSenderMsgSeqNum(store.NextSenderMsgSeqNum())
store.SetNextTargetMsgSeqNum(store.NextTargetMsgSeqNum())
return nil
}
func (store *fileStore) populateCache() (creationTimePopulated bool, err error) {
if tmpHeaderFile, err := os.Open(store.headerFname); err == nil {
defer tmpHeaderFile.Close()
for {
var seqNum, size int
var offset int64
if cnt, err := fmt.Fscanf(tmpHeaderFile, "%d,%d,%d\n", &seqNum, &offset, &size); err != nil || cnt != 3 {
break
}
store.offsets[seqNum] = msgDef{offset: offset, size: size}
}
}
if timeBytes, err := ioutil.ReadFile(store.sessionFname); err == nil {
var ctime time.Time
if err := ctime.UnmarshalText(timeBytes); err == nil {
store.cache.creationTime = ctime
creationTimePopulated = true
}
}
if senderSeqNumBytes, err := ioutil.ReadFile(store.senderSeqNumsFname); err == nil {
if senderSeqNum, err := strconv.Atoi(string(senderSeqNumBytes)); err == nil {
store.cache.SetNextSenderMsgSeqNum(senderSeqNum)
}
}
if targetSeqNumBytes, err := ioutil.ReadFile(store.targetSeqNumsFname); err == nil {
if targetSeqNum, err := strconv.Atoi(string(targetSeqNumBytes)); err == nil {
store.cache.SetNextTargetMsgSeqNum(targetSeqNum)
}
}
return creationTimePopulated, nil
}
func (store *fileStore) setSession() error {
if _, err := store.sessionFile.Seek(0, os.SEEK_SET); err != nil {
return fmt.Errorf("unable to rewind file: %s: %s", store.sessionFname, err.Error())
}
data, err := store.cache.CreationTime().MarshalText()
if err != nil {
return fmt.Errorf("unable to marshal session time to file: %s: %s", store.sessionFname, err.Error())
}
if _, err := store.sessionFile.Write(data); err != nil {
return fmt.Errorf("unable to write to file: %s: %s", store.sessionFname, err.Error())
}
if err := store.sessionFile.Sync(); err != nil {
return fmt.Errorf("unable to flush file: %s: %s", store.sessionFname, err.Error())
}
return nil
}
func (store *fileStore) setSeqNum(f *os.File, seqNum int) error {
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
return fmt.Errorf("unable to rewind file: %s: %s", f.Name(), err.Error())
}
if _, err := fmt.Fprintf(f, "%019d", seqNum); err != nil {
return fmt.Errorf("unable to write to file: %s: %s", f.Name(), err.Error())
}
if err := f.Sync(); err != nil {
return fmt.Errorf("unable to flush file: %s: %s", f.Name(), err.Error())
}
return nil
}
// NextSenderMsgSeqNum returns the next MsgSeqNum that will be sent
func (store *fileStore) NextSenderMsgSeqNum() int {
return store.cache.NextSenderMsgSeqNum()
}
// NextTargetMsgSeqNum returns the next MsgSeqNum that should be received
func (store *fileStore) NextTargetMsgSeqNum() int {
return store.cache.NextTargetMsgSeqNum()
}
// SetNextSenderMsgSeqNum sets the next MsgSeqNum that will be sent
func (store *fileStore) SetNextSenderMsgSeqNum(next int) error {
store.cache.SetNextSenderMsgSeqNum(next)
return store.setSeqNum(store.senderSeqNumsFile, next)
}
// SetNextTargetMsgSeqNum sets the next MsgSeqNum that should be received
func (store *fileStore) SetNextTargetMsgSeqNum(next int) error {
store.cache.SetNextTargetMsgSeqNum(next)
return store.setSeqNum(store.targetSeqNumsFile, next)
}
// IncrNextSenderMsgSeqNum increments the next MsgSeqNum that will be sent
func (store *fileStore) IncrNextSenderMsgSeqNum() error {
store.cache.IncrNextSenderMsgSeqNum()
return store.setSeqNum(store.senderSeqNumsFile, store.cache.NextSenderMsgSeqNum())
}
// IncrNextTargetMsgSeqNum increments the next MsgSeqNum that should be received
func (store *fileStore) IncrNextTargetMsgSeqNum() error {
store.cache.IncrNextTargetMsgSeqNum()
return store.setSeqNum(store.targetSeqNumsFile, store.cache.NextTargetMsgSeqNum())
}
// CreationTime returns the creation time of the store
func (store *fileStore) CreationTime() time.Time {
return store.cache.CreationTime()
}
func (store *fileStore) SaveMessage(seqNum int, msg []byte) error {
offset, err := store.bodyFile.Seek(0, os.SEEK_END)
if err != nil {
return fmt.Errorf("unable to seek to end of file: %s: %s", store.bodyFname, err.Error())
}
if _, err := store.headerFile.Seek(0, os.SEEK_END); err != nil {
return fmt.Errorf("unable to seek to end of file: %s: %s", store.headerFname, err.Error())
}
if _, err := fmt.Fprintf(store.headerFile, "%d,%d,%d\n", seqNum, offset, len(msg)); err != nil {
return fmt.Errorf("unable to write to file: %s: %s", store.headerFname, err.Error())
}
store.offsets[seqNum] = msgDef{offset: offset, size: len(msg)}
if _, err := store.bodyFile.Write(msg); err != nil {
return fmt.Errorf("unable to write to file: %s: %s", store.bodyFname, err.Error())
}
if err := store.bodyFile.Sync(); err != nil {
return fmt.Errorf("unable to flush file: %s: %s", store.bodyFname, err.Error())
}
if err := store.headerFile.Sync(); err != nil {
return fmt.Errorf("unable to flush file: %s: %s", store.headerFname, err.Error())
}
return nil
}
func (store *fileStore) getMessage(seqNum int) (msg []byte, found bool, err error) {
msgInfo, found := store.offsets[seqNum]
if !found {
return
}
msg = make([]byte, msgInfo.size)
if _, err = store.bodyFile.ReadAt(msg, msgInfo.offset); err != nil {
return nil, true, fmt.Errorf("unable to read from file: %s: %s", store.bodyFname, err.Error())
}
return msg, true, nil
}
func (store *fileStore) GetMessages(beginSeqNum, endSeqNum int) ([][]byte, error) {
var msgs [][]byte
for seqNum := beginSeqNum; seqNum <= endSeqNum; seqNum++ {
m, found, err := store.getMessage(seqNum)
if err != nil {
return nil, err
}
if found {
msgs = append(msgs, m)
}
}
return msgs, nil
}
// Close closes the store's files
func (store *fileStore) Close() error {
if err := closeFile(store.bodyFile); err != nil {
return err
}
if err := closeFile(store.headerFile); err != nil {
return err
}
if err := closeFile(store.sessionFile); err != nil {
return err
}
if err := closeFile(store.senderSeqNumsFile); err != nil {
return err
}
if err := closeFile(store.targetSeqNumsFile); err != nil {
return err
}
store.bodyFile = nil
store.headerFile = nil
store.sessionFile = nil
store.senderSeqNumsFile = nil
store.targetSeqNumsFile = nil
return nil
}