55 "io"
66 "sort"
77
8+ encbin "encoding/binary"
9+
810 "gopkg.in/src-d/go-git.v4/plumbing"
9- "gopkg.in/src-d/go-git.v4/utils/binary"
1011)
1112
1213const (
@@ -122,41 +123,32 @@ func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
122123 return 0 , plumbing .ErrObjectNotFound
123124 }
124125
125- offset , err := idx .getOffset (k , i )
126+ offset := idx .getOffset (k , i )
126127
127128 if ! idx .offsetHashIsFull {
128129 // Save the offset for reverse lookup
129130 if idx .offsetHash == nil {
130131 idx .offsetHash = make (map [int64 ]plumbing.Hash )
131132 }
132- idx .offsetHash [offset ] = h
133+ idx .offsetHash [int64 ( offset ) ] = h
133134 }
134135
135- return offset , err
136+ return int64 ( offset ), nil
136137}
137138
138139const isO64Mask = uint64 (1 ) << 31
139140
140- func (idx * MemoryIndex ) getOffset (firstLevel , secondLevel int ) ( int64 , error ) {
141+ func (idx * MemoryIndex ) getOffset (firstLevel , secondLevel int ) uint64 {
141142 offset := secondLevel << 2
142- buf := bytes .NewBuffer (idx .Offset32 [firstLevel ][offset : offset + 4 ])
143- ofs , err := binary .ReadUint32 (buf )
144- if err != nil {
145- return - 1 , err
146- }
143+ ofs := encbin .BigEndian .Uint32 (idx .Offset32 [firstLevel ][offset : offset + 4 ])
147144
148145 if (uint64 (ofs ) & isO64Mask ) != 0 {
149146 offset := 8 * (uint64 (ofs ) & ^ isO64Mask )
150- buf := bytes .NewBuffer (idx .Offset64 [offset : offset + 8 ])
151- n , err := binary .ReadUint64 (buf )
152- if err != nil {
153- return - 1 , err
154- }
155-
156- return int64 (n ), nil
147+ n := encbin .BigEndian .Uint64 (idx .Offset64 [offset : offset + 8 ])
148+ return n
157149 }
158150
159- return int64 (ofs ), nil
151+ return uint64 (ofs )
160152}
161153
162154// FindCRC32 implements the Index interface.
@@ -167,13 +159,12 @@ func (idx *MemoryIndex) FindCRC32(h plumbing.Hash) (uint32, error) {
167159 return 0 , plumbing .ErrObjectNotFound
168160 }
169161
170- return idx .getCRC32 (k , i )
162+ return idx .getCRC32 (k , i ), nil
171163}
172164
173- func (idx * MemoryIndex ) getCRC32 (firstLevel , secondLevel int ) ( uint32 , error ) {
165+ func (idx * MemoryIndex ) getCRC32 (firstLevel , secondLevel int ) uint32 {
174166 offset := secondLevel << 2
175- buf := bytes .NewBuffer (idx .CRC32 [firstLevel ][offset : offset + 4 ])
176- return binary .ReadUint32 (buf )
167+ return encbin .BigEndian .Uint32 (idx .CRC32 [firstLevel ][offset : offset + 4 ])
177168}
178169
179170// FindHash implements the Index interface.
@@ -213,22 +204,19 @@ func (idx *MemoryIndex) genOffsetHash() error {
213204 idx .offsetHash = make (map [int64 ]plumbing.Hash , count )
214205 idx .offsetHashIsFull = true
215206
216- iter , err := idx .Entries ()
217- if err != nil {
218- return err
219- }
220-
221- for {
222- entry , err := iter .Next ()
223- if err != nil {
224- if err == io .EOF {
225- return nil
226- }
227- return err
207+ var hash plumbing.Hash
208+ i := uint32 (0 )
209+ for firstLevel , fanoutValue := range idx .Fanout {
210+ mappedFirstLevel := idx .FanoutMapping [firstLevel ]
211+ for secondLevel := uint32 (0 ); i < fanoutValue ; i ++ {
212+ copy (hash [:], idx .Names [mappedFirstLevel ][secondLevel * objectIDLength :])
213+ offset := int64 (idx .getOffset (mappedFirstLevel , int (secondLevel )))
214+ idx .offsetHash [offset ] = hash
215+ secondLevel ++
228216 }
229-
230- idx .offsetHash [int64 (entry .Offset )] = entry .Hash
231217 }
218+
219+ return nil
232220}
233221
234222// Count implements the Index interface.
@@ -297,22 +285,11 @@ func (i *idxfileEntryIter) Next() (*Entry, error) {
297285 continue
298286 }
299287
288+ mappedFirstLevel := i .idx .FanoutMapping [i .firstLevel ]
300289 entry := new (Entry )
301- ofs := i .secondLevel * objectIDLength
302- copy (entry .Hash [:], i .idx .Names [i .idx .FanoutMapping [i .firstLevel ]][ofs :])
303-
304- pos := i .idx .FanoutMapping [entry .Hash [0 ]]
305-
306- offset , err := i .idx .getOffset (pos , i .secondLevel )
307- if err != nil {
308- return nil , err
309- }
310- entry .Offset = uint64 (offset )
311-
312- entry .CRC32 , err = i .idx .getCRC32 (pos , i .secondLevel )
313- if err != nil {
314- return nil , err
315- }
290+ copy (entry .Hash [:], i .idx .Names [mappedFirstLevel ][i .secondLevel * objectIDLength :])
291+ entry .Offset = i .idx .getOffset (mappedFirstLevel , i .secondLevel )
292+ entry .CRC32 = i .idx .getCRC32 (mappedFirstLevel , i .secondLevel )
316293
317294 i .secondLevel ++
318295 i .total ++
0 commit comments