@@ -22,6 +22,7 @@ import (
22
22
"path"
23
23
"strconv"
24
24
"strings"
25
+ "sync"
25
26
"time"
26
27
27
28
"github.com/pkg/errors"
@@ -42,7 +43,7 @@ type fileStoreFactory struct {
42
43
type fileStore struct {
43
44
sessionID quickfix.SessionID
44
45
cache quickfix.MessageStore
45
- offsets map [ int ] msgDef
46
+ offsets sync. Map
46
47
bodyFname string
47
48
headerFname string
48
49
sessionFname string
@@ -106,7 +107,7 @@ func newFileStore(sessionID quickfix.SessionID, dirname string, fileSync bool) (
106
107
store := & fileStore {
107
108
sessionID : sessionID ,
108
109
cache : memStore ,
109
- offsets : make ( map [ int ] msgDef ) ,
110
+ offsets : sync. Map {} ,
110
111
bodyFname : path .Join (dirname , fmt .Sprintf ("%s.%s" , sessionPrefix , "body" )),
111
112
headerFname : path .Join (dirname , fmt .Sprintf ("%s.%s" , sessionPrefix , "header" )),
112
113
sessionFname : path .Join (dirname , fmt .Sprintf ("%s.%s" , sessionPrefix , "session" )),
@@ -206,7 +207,7 @@ func (store *fileStore) populateCache() (creationTimePopulated bool, err error)
206
207
if cnt , err := fmt .Fscanf (tmpHeaderFile , "%d,%d,%d\n " , & seqNum , & offset , & size ); err != nil || cnt != 3 {
207
208
break
208
209
}
209
- store .offsets [ seqNum ] = msgDef {offset : offset , size : size }
210
+ store .offsets . Store ( seqNum , msgDef {offset : offset , size : size })
210
211
}
211
212
}
212
213
@@ -347,7 +348,7 @@ func (store *fileStore) SaveMessage(seqNum int, msg []byte) error {
347
348
}
348
349
}
349
350
350
- store .offsets [ seqNum ] = msgDef {offset : offset , size : len (msg )}
351
+ store .offsets . Store ( seqNum , msgDef {offset : offset , size : len (msg )})
351
352
return nil
352
353
}
353
354
@@ -360,10 +361,14 @@ func (store *fileStore) SaveMessageAndIncrNextSenderMsgSeqNum(seqNum int, msg []
360
361
}
361
362
362
363
func (store * fileStore ) getMessage (seqNum int ) (msg []byte , found bool , err error ) {
363
- msgInfo , found := store .offsets [ seqNum ]
364
+ msgInfoTemp , found := store .offsets . Load ( seqNum )
364
365
if ! found {
365
366
return
366
367
}
368
+ msgInfo , ok := msgInfoTemp .(msgDef )
369
+ if ! ok {
370
+ return nil , true , fmt .Errorf ("incorrect msgInfo type while reading file: %s" , store .bodyFname )
371
+ }
367
372
368
373
msg = make ([]byte , msgInfo .size )
369
374
if _ , err = store .bodyFile .ReadAt (msg , msgInfo .offset ); err != nil {
0 commit comments