5
5
"io"
6
6
"sort"
7
7
8
+ encbin "encoding/binary"
9
+
8
10
"gopkg.in/src-d/go-git.v4/plumbing"
9
- "gopkg.in/src-d/go-git.v4/utils/binary"
10
11
)
11
12
12
13
const (
@@ -122,41 +123,32 @@ func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
122
123
return 0 , plumbing .ErrObjectNotFound
123
124
}
124
125
125
- offset , err := idx .getOffset (k , i )
126
+ offset := idx .getOffset (k , i )
126
127
127
128
if ! idx .offsetHashIsFull {
128
129
// Save the offset for reverse lookup
129
130
if idx .offsetHash == nil {
130
131
idx .offsetHash = make (map [int64 ]plumbing.Hash )
131
132
}
132
- idx .offsetHash [offset ] = h
133
+ idx .offsetHash [int64 ( offset ) ] = h
133
134
}
134
135
135
- return offset , err
136
+ return int64 ( offset ), nil
136
137
}
137
138
138
139
const isO64Mask = uint64 (1 ) << 31
139
140
140
- func (idx * MemoryIndex ) getOffset (firstLevel , secondLevel int ) ( int64 , error ) {
141
+ func (idx * MemoryIndex ) getOffset (firstLevel , secondLevel int ) uint64 {
141
142
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 ])
147
144
148
145
if (uint64 (ofs ) & isO64Mask ) != 0 {
149
146
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
157
149
}
158
150
159
- return int64 (ofs ), nil
151
+ return uint64 (ofs )
160
152
}
161
153
162
154
// FindCRC32 implements the Index interface.
@@ -167,13 +159,12 @@ func (idx *MemoryIndex) FindCRC32(h plumbing.Hash) (uint32, error) {
167
159
return 0 , plumbing .ErrObjectNotFound
168
160
}
169
161
170
- return idx .getCRC32 (k , i )
162
+ return idx .getCRC32 (k , i ), nil
171
163
}
172
164
173
- func (idx * MemoryIndex ) getCRC32 (firstLevel , secondLevel int ) ( uint32 , error ) {
165
+ func (idx * MemoryIndex ) getCRC32 (firstLevel , secondLevel int ) uint32 {
174
166
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 ])
177
168
}
178
169
179
170
// FindHash implements the Index interface.
@@ -213,22 +204,19 @@ func (idx *MemoryIndex) genOffsetHash() error {
213
204
idx .offsetHash = make (map [int64 ]plumbing.Hash , count )
214
205
idx .offsetHashIsFull = true
215
206
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 ++
228
216
}
229
-
230
- idx .offsetHash [int64 (entry .Offset )] = entry .Hash
231
217
}
218
+
219
+ return nil
232
220
}
233
221
234
222
// Count implements the Index interface.
@@ -297,22 +285,11 @@ func (i *idxfileEntryIter) Next() (*Entry, error) {
297
285
continue
298
286
}
299
287
288
+ mappedFirstLevel := i .idx .FanoutMapping [i .firstLevel ]
300
289
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 )
316
293
317
294
i .secondLevel ++
318
295
i .total ++
0 commit comments