4
4
"bytes"
5
5
"io"
6
6
"sort"
7
+ "sync"
7
8
8
9
encbin "encoding/binary"
9
10
@@ -56,7 +57,7 @@ type MemoryIndex struct {
56
57
PackfileChecksum [20 ]byte
57
58
IdxChecksum [20 ]byte
58
59
59
- offsetHash map [ int64 ]plumbing. Hash
60
+ offsetHash sync. Map
60
61
offsetHashIsFull bool
61
62
}
62
63
@@ -127,10 +128,7 @@ func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
127
128
128
129
if ! idx .offsetHashIsFull {
129
130
// Save the offset for reverse lookup
130
- if idx .offsetHash == nil {
131
- idx .offsetHash = make (map [int64 ]plumbing.Hash )
132
- }
133
- idx .offsetHash [int64 (offset )] = h
131
+ idx .offsetHash .Store (int64 (offset ), h )
134
132
}
135
133
136
134
return int64 (offset ), nil
@@ -169,39 +167,29 @@ func (idx *MemoryIndex) getCRC32(firstLevel, secondLevel int) uint32 {
169
167
170
168
// FindHash implements the Index interface.
171
169
func (idx * MemoryIndex ) FindHash (o int64 ) (plumbing.Hash , error ) {
172
- var hash plumbing.Hash
173
- var ok bool
174
170
175
- if idx .offsetHash != nil {
176
- if hash , ok = idx .offsetHash [o ]; ok {
177
- return hash , nil
178
- }
171
+ if hash , ok := idx .offsetHash .Load (o ); ok {
172
+ return hash .(plumbing.Hash ), nil
179
173
}
180
174
181
175
// Lazily generate the reverse offset/hash map if required.
182
- if ! idx .offsetHashIsFull || idx . offsetHash == nil {
176
+ if ! idx .offsetHashIsFull {
183
177
if err := idx .genOffsetHash (); err != nil {
184
178
return plumbing .ZeroHash , err
185
179
}
186
180
187
- hash , ok = idx .offsetHash [ o ]
188
- }
189
-
190
- if ! ok {
191
- return plumbing . ZeroHash , plumbing . ErrObjectNotFound
181
+ if hash , ok : = idx .offsetHash . Load ( o ); ! ok {
182
+ return plumbing . ZeroHash , plumbing . ErrObjectNotFound
183
+ } else {
184
+ return hash .(plumbing. Hash ), nil
185
+ }
192
186
}
193
187
194
- return hash , nil
188
+ return plumbing. Hash {} , nil
195
189
}
196
190
197
191
// genOffsetHash generates the offset/hash mapping for reverse search.
198
192
func (idx * MemoryIndex ) genOffsetHash () error {
199
- count , err := idx .Count ()
200
- if err != nil {
201
- return err
202
- }
203
-
204
- idx .offsetHash = make (map [int64 ]plumbing.Hash , count )
205
193
idx .offsetHashIsFull = true
206
194
207
195
var hash plumbing.Hash
@@ -211,7 +199,7 @@ func (idx *MemoryIndex) genOffsetHash() error {
211
199
for secondLevel := uint32 (0 ); i < fanoutValue ; i ++ {
212
200
copy (hash [:], idx .Names [mappedFirstLevel ][secondLevel * objectIDLength :])
213
201
offset := int64 (idx .getOffset (mappedFirstLevel , int (secondLevel )))
214
- idx .offsetHash [ offset ] = hash
202
+ idx .offsetHash . Store ( offset , hash )
215
203
secondLevel ++
216
204
}
217
205
}
0 commit comments