Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 8d75d23

Browse files
committed
plumbing: idxfile, Crc32 to CRC32 and return ok from findHashIndex
Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
1 parent b944bc4 commit 8d75d23

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

plumbing/format/idxfile/decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func readObjectNames(idx *MemoryIndex, r io.Reader) error {
124124

125125
idx.Names = append(idx.Names, bin)
126126
idx.Offset32 = append(idx.Offset32, make([]byte, buckets*4))
127-
idx.Crc32 = append(idx.Crc32, make([]byte, buckets*4))
127+
idx.CRC32 = append(idx.CRC32, make([]byte, buckets*4))
128128
}
129129

130130
return nil
@@ -133,7 +133,7 @@ func readObjectNames(idx *MemoryIndex, r io.Reader) error {
133133
func readCRC32(idx *MemoryIndex, r io.Reader) error {
134134
for k := 0; k < fanout; k++ {
135135
if pos := idx.FanoutMapping[k]; pos != noMapping {
136-
if _, err := io.ReadFull(r, idx.Crc32[pos]); err != nil {
136+
if _, err := io.ReadFull(r, idx.CRC32[pos]); err != nil {
137137
return err
138138
}
139139
}

plumbing/format/idxfile/encoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (e *Encoder) encodeCRC32(idx *MemoryIndex) (int, error) {
8989
continue
9090
}
9191

92-
n, err := e.Write(idx.Crc32[pos])
92+
n, err := e.Write(idx.CRC32[pos])
9393
if err != nil {
9494
return size, err
9595
}

plumbing/format/idxfile/idxfile.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ type MemoryIndex struct {
4141
Version uint32
4242
Fanout [256]uint32
4343
// FanoutMapping maps the position in the fanout table to the position
44-
// in the Names, Offset32 and Crc32 slices. This improves the memory
44+
// in the Names, Offset32 and CRC32 slices. This improves the memory
4545
// usage by not needing an array with unnecessary empty slots.
4646
FanoutMapping [256]int
4747
Names [][]byte
4848
Offset32 [][]byte
49-
Crc32 [][]byte
49+
CRC32 [][]byte
5050
Offset64 []byte
5151
PackfileChecksum [20]byte
5252
IdxChecksum [20]byte
@@ -61,20 +61,20 @@ func NewMemoryIndex() *MemoryIndex {
6161
return &MemoryIndex{}
6262
}
6363

64-
func (idx *MemoryIndex) findHashIndex(h plumbing.Hash) int {
64+
func (idx *MemoryIndex) findHashIndex(h plumbing.Hash) (int, bool) {
6565
k := idx.FanoutMapping[h[0]]
6666
if k == noMapping {
67-
return -1
67+
return 0, false
6868
}
6969

7070
if len(idx.Names) <= k {
71-
return -1
71+
return 0, false
7272
}
7373

7474
data := idx.Names[k]
7575
high := uint64(len(idx.Offset32[k])) >> 2
7676
if high == 0 {
77-
return -1
77+
return 0, false
7878
}
7979

8080
low := uint64(0)
@@ -86,7 +86,7 @@ func (idx *MemoryIndex) findHashIndex(h plumbing.Hash) int {
8686
if cmp < 0 {
8787
high = mid
8888
} else if cmp == 0 {
89-
return int(mid)
89+
return int(mid), true
9090
} else {
9191
low = mid + 1
9292
}
@@ -96,13 +96,13 @@ func (idx *MemoryIndex) findHashIndex(h plumbing.Hash) int {
9696
}
9797
}
9898

99-
return -1
99+
return 0, false
100100
}
101101

102102
// Contains implements the Index interface.
103103
func (idx *MemoryIndex) Contains(h plumbing.Hash) (bool, error) {
104-
i := idx.findHashIndex(h)
105-
return i >= 0, nil
104+
_, ok := idx.findHashIndex(h)
105+
return ok, nil
106106
}
107107

108108
// FindOffset implements the Index interface.
@@ -112,8 +112,8 @@ func (idx *MemoryIndex) FindOffset(h plumbing.Hash) (int64, error) {
112112
}
113113

114114
k := idx.FanoutMapping[h[0]]
115-
i := idx.findHashIndex(h)
116-
if i < 0 {
115+
i, ok := idx.findHashIndex(h)
116+
if !ok {
117117
return 0, plumbing.ErrObjectNotFound
118118
}
119119

@@ -147,17 +147,17 @@ func (idx *MemoryIndex) getOffset(firstLevel, secondLevel int) (int64, error) {
147147
// FindCRC32 implements the Index interface.
148148
func (idx *MemoryIndex) FindCRC32(h plumbing.Hash) (uint32, error) {
149149
k := idx.FanoutMapping[h[0]]
150-
i := idx.findHashIndex(h)
151-
if i < 0 {
150+
i, ok := idx.findHashIndex(h)
151+
if !ok {
152152
return 0, plumbing.ErrObjectNotFound
153153
}
154154

155-
return idx.getCrc32(k, i)
155+
return idx.getCRC32(k, i)
156156
}
157157

158-
func (idx *MemoryIndex) getCrc32(firstLevel, secondLevel int) (uint32, error) {
158+
func (idx *MemoryIndex) getCRC32(firstLevel, secondLevel int) (uint32, error) {
159159
offset := secondLevel << 2
160-
buf := bytes.NewBuffer(idx.Crc32[firstLevel][offset : offset+4])
160+
buf := bytes.NewBuffer(idx.CRC32[firstLevel][offset : offset+4])
161161
return binary.ReadUint32(buf)
162162
}
163163

@@ -253,7 +253,7 @@ func (i *idxfileEntryIter) Next() (*Entry, error) {
253253
}
254254
entry.Offset = uint64(offset)
255255

256-
entry.CRC32, err = i.idx.getCrc32(pos, i.secondLevel)
256+
entry.CRC32, err = i.idx.getCRC32(pos, i.secondLevel)
257257
if err != nil {
258258
return nil, err
259259
}

plumbing/format/idxfile/writer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (w *Writer) createIndex() (*MemoryIndex, error) {
132132

133133
idx.Names = append(idx.Names, make([]byte, 0))
134134
idx.Offset32 = append(idx.Offset32, make([]byte, 0))
135-
idx.Crc32 = append(idx.Crc32, make([]byte, 0))
135+
idx.CRC32 = append(idx.CRC32, make([]byte, 0))
136136
}
137137

138138
idx.Names[bucket] = append(idx.Names[bucket], o.Hash[:]...)
@@ -148,7 +148,7 @@ func (w *Writer) createIndex() (*MemoryIndex, error) {
148148

149149
buf.Truncate(0)
150150
binary.WriteUint32(buf, uint32(o.CRC32))
151-
idx.Crc32[bucket] = append(idx.Crc32[bucket], buf.Bytes()...)
151+
idx.CRC32[bucket] = append(idx.CRC32[bucket], buf.Bytes()...)
152152
}
153153

154154
for j := last + 1; j < 256; j++ {

0 commit comments

Comments
 (0)