Skip to content

Commit 49dff61

Browse files
authored
Merge pull request #1 from flatfeestack/feature/concurrency
change map to sync.map to support concurrency
2 parents 97741e7 + 40643b9 commit 49dff61

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

plumbing/format/idxfile/idxfile.go

+13-25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"io"
66
"sort"
7+
"sync"
78

89
encbin "encoding/binary"
910

@@ -56,7 +57,7 @@ type MemoryIndex struct {
5657
PackfileChecksum [20]byte
5758
IdxChecksum [20]byte
5859

59-
offsetHash map[int64]plumbing.Hash
60+
offsetHash sync.Map
6061
offsetHashIsFull bool
6162
}
6263

@@ -127,10 +128,7 @@ func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
127128

128129
if !idx.offsetHashIsFull {
129130
// 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)
134132
}
135133

136134
return int64(offset), nil
@@ -169,39 +167,29 @@ func (idx *MemoryIndex) getCRC32(firstLevel, secondLevel int) uint32 {
169167

170168
// FindHash implements the Index interface.
171169
func (idx *MemoryIndex) FindHash(o int64) (plumbing.Hash, error) {
172-
var hash plumbing.Hash
173-
var ok bool
174170

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
179173
}
180174

181175
// Lazily generate the reverse offset/hash map if required.
182-
if !idx.offsetHashIsFull || idx.offsetHash == nil {
176+
if !idx.offsetHashIsFull {
183177
if err := idx.genOffsetHash(); err != nil {
184178
return plumbing.ZeroHash, err
185179
}
186180

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+
}
192186
}
193187

194-
return hash, nil
188+
return plumbing.Hash{}, nil
195189
}
196190

197191
// genOffsetHash generates the offset/hash mapping for reverse search.
198192
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)
205193
idx.offsetHashIsFull = true
206194

207195
var hash plumbing.Hash
@@ -211,7 +199,7 @@ func (idx *MemoryIndex) genOffsetHash() error {
211199
for secondLevel := uint32(0); i < fanoutValue; i++ {
212200
copy(hash[:], idx.Names[mappedFirstLevel][secondLevel*objectIDLength:])
213201
offset := int64(idx.getOffset(mappedFirstLevel, int(secondLevel)))
214-
idx.offsetHash[offset] = hash
202+
idx.offsetHash.Store(offset, hash)
215203
secondLevel++
216204
}
217205
}

0 commit comments

Comments
 (0)