git/odb/pack: rename, unexport index offset consts
This commit is contained in:
parent
bdc9ff0d87
commit
edd354eaa1
@ -8,47 +8,49 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// MagicWidth is the width of the magic header of packfiles version 2
|
// indexMagicWidth is the width of the magic header of packfiles version
|
||||||
// and newer.
|
// 2 and newer.
|
||||||
MagicWidth = 4
|
indexMagicWidth = 4
|
||||||
// VersionWidth is the width of the version following the magic header.
|
// indexVersionWidth is the width of the version following the magic
|
||||||
VersionWidth = 4
|
|
||||||
// V2Width is the total width of the header in V2.
|
|
||||||
V2Width = MagicWidth + VersionWidth
|
|
||||||
// V1Width is the total width of the header in V1.
|
|
||||||
V1Width = 0
|
|
||||||
|
|
||||||
// FanoutEntries is the number of entries in the fanout table.
|
|
||||||
FanoutEntries = 256
|
|
||||||
// FanoutEntryWidth is the width of each entry in the fanout table.
|
|
||||||
FanoutEntryWidth = 4
|
|
||||||
// FanoutWidth is the width of the entire fanout table.
|
|
||||||
FanoutWidth = FanoutEntries * FanoutEntryWidth
|
|
||||||
|
|
||||||
// OffsetV1Start is the location of the first object outside of the V1
|
|
||||||
// header.
|
// header.
|
||||||
OffsetV1Start = V1Width + FanoutWidth
|
indexVersionWidth = 4
|
||||||
// OffsetV2Start is the location of the first object outside of the V2
|
// indexV2Width is the total width of the header in V2.
|
||||||
// header.
|
indexV2Width = indexMagicWidth + indexVersionWidth
|
||||||
OffsetV2Start = V2Width + FanoutWidth
|
// indexV1Width is the total width of the header in V1.
|
||||||
|
indexV1Width = 0
|
||||||
|
|
||||||
// ObjectNameWidth is the width of a SHA1 object name.
|
// indexFanoutEntries is the number of entries in the fanout table.
|
||||||
ObjectNameWidth = 20
|
indexFanoutEntries = 256
|
||||||
// ObjectCRCWidth is the width of the CRC accompanying each object in
|
// indexFanoutEntryWidth is the width of each entry in the fanout table.
|
||||||
// V2.
|
indexFanoutEntryWidth = 4
|
||||||
ObjectCRCWidth = 4
|
// indexFanoutWidth is the width of the entire fanout table.
|
||||||
// ObjectSmallOffsetWidth is the width of the small offset encoded into
|
indexFanoutWidth = indexFanoutEntries * indexFanoutEntryWidth
|
||||||
// each object.
|
|
||||||
ObjectSmallOffsetWidth = 4
|
// indexOffsetV1Start is the location of the first object outside of the
|
||||||
// ObjectLargeOffsetWidth is the width of the optional large offset
|
// V1 header.
|
||||||
|
indexOffsetV1Start = indexV1Width + indexFanoutWidth
|
||||||
|
// indexOffsetV2Start is the location of the first object outside of the
|
||||||
|
// V2 header.
|
||||||
|
indexOffsetV2Start = indexV2Width + indexFanoutWidth
|
||||||
|
|
||||||
|
// indexObjectNameWidth is the width of a SHA1 object name.
|
||||||
|
indexObjectNameWidth = 20
|
||||||
|
// indexObjectCRCWidth is the width of the CRC accompanying each object
|
||||||
|
// in V2.
|
||||||
|
indexObjectCRCWidth = 4
|
||||||
|
// indexObjectSmallOffsetWidth is the width of the small offset encoded
|
||||||
|
// into each object.
|
||||||
|
indexObjectSmallOffsetWidth = 4
|
||||||
|
// indexObjectLargeOffsetWidth is the width of the optional large offset
|
||||||
// encoded into the small offset.
|
// encoded into the small offset.
|
||||||
ObjectLargeOffsetWidth = 8
|
indexObjectLargeOffsetWidth = 8
|
||||||
|
|
||||||
// ObjectEntryV1Width is the width of one contiguous object entry in V1.
|
// indexObjectEntryV1Width is the width of one contiguous object entry
|
||||||
ObjectEntryV1Width = ObjectNameWidth + ObjectSmallOffsetWidth
|
// in V1.
|
||||||
// ObjectEntryV2Width is the width of one non-contiguous object entry in
|
indexObjectEntryV1Width = indexObjectNameWidth + indexObjectSmallOffsetWidth
|
||||||
// V2.
|
// indexObjectEntryV2Width is the width of one non-contiguous object
|
||||||
ObjectEntryV2Width = ObjectNameWidth + ObjectCRCWidth + ObjectSmallOffsetWidth
|
// entry in V2.
|
||||||
|
indexObjectEntryV2Width = indexObjectNameWidth + indexObjectCRCWidth + indexObjectSmallOffsetWidth
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -10,17 +10,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestDecodeIndexV1InvalidFanout(t *testing.T) {
|
func TestDecodeIndexV1InvalidFanout(t *testing.T) {
|
||||||
idx, err := DecodeIndex(bytes.NewReader(make([]byte, FanoutWidth-1)))
|
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth-1)))
|
||||||
|
|
||||||
assert.Equal(t, ErrShortFanout, err)
|
assert.Equal(t, ErrShortFanout, err)
|
||||||
assert.Nil(t, idx)
|
assert.Nil(t, idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeIndexV2(t *testing.T) {
|
func TestDecodeIndexV2(t *testing.T) {
|
||||||
buf := make([]byte, 0, V2Width+FanoutWidth)
|
buf := make([]byte, 0, indexV2Width+indexFanoutWidth)
|
||||||
buf = append(buf, 0xff, 0x74, 0x4f, 0x63)
|
buf = append(buf, 0xff, 0x74, 0x4f, 0x63)
|
||||||
buf = append(buf, 0x0, 0x0, 0x0, 0x2)
|
buf = append(buf, 0x0, 0x0, 0x0, 0x2)
|
||||||
for i := 0; i < FanoutEntries; i++ {
|
for i := 0; i < indexFanoutEntries; i++ {
|
||||||
x := make([]byte, 4)
|
x := make([]byte, 4)
|
||||||
|
|
||||||
binary.BigEndian.PutUint32(x, uint32(3))
|
binary.BigEndian.PutUint32(x, uint32(3))
|
||||||
@ -36,10 +36,10 @@ func TestDecodeIndexV2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeIndexV2InvalidFanout(t *testing.T) {
|
func TestDecodeIndexV2InvalidFanout(t *testing.T) {
|
||||||
buf := make([]byte, 0, V2Width+FanoutWidth-FanoutEntryWidth)
|
buf := make([]byte, 0, indexV2Width+indexFanoutWidth-indexFanoutEntryWidth)
|
||||||
buf = append(buf, 0xff, 0x74, 0x4f, 0x63)
|
buf = append(buf, 0xff, 0x74, 0x4f, 0x63)
|
||||||
buf = append(buf, 0x0, 0x0, 0x0, 0x2)
|
buf = append(buf, 0x0, 0x0, 0x0, 0x2)
|
||||||
buf = append(buf, make([]byte, FanoutWidth-1)...)
|
buf = append(buf, make([]byte, indexFanoutWidth-1)...)
|
||||||
|
|
||||||
idx, err := DecodeIndex(bytes.NewReader(buf))
|
idx, err := DecodeIndex(bytes.NewReader(buf))
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ func TestDecodeIndexV2InvalidFanout(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeIndexV1(t *testing.T) {
|
func TestDecodeIndexV1(t *testing.T) {
|
||||||
idx, err := DecodeIndex(bytes.NewReader(make([]byte, FanoutWidth)))
|
idx, err := DecodeIndex(bytes.NewReader(make([]byte, indexFanoutWidth)))
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, V1, idx.version)
|
assert.Equal(t, V1, idx.version)
|
||||||
|
@ -90,7 +90,7 @@ func init() {
|
|||||||
// Since we have an even distribution of SHA1s in the generated index,
|
// Since we have an even distribution of SHA1s in the generated index,
|
||||||
// each entry will increase by the number of entries per slot (see: eps
|
// each entry will increase by the number of entries per slot (see: eps
|
||||||
// above).
|
// above).
|
||||||
fanout := make([]uint32, FanoutEntries)
|
fanout := make([]uint32, indexFanoutEntries)
|
||||||
for i := 0; i < len(fanout); i++ {
|
for i := 0; i < len(fanout); i++ {
|
||||||
// Begin the index at (i+1), since the fanout table mandates
|
// Begin the index at (i+1), since the fanout table mandates
|
||||||
// objects less than the value at index "i".
|
// objects less than the value at index "i".
|
||||||
|
@ -39,14 +39,14 @@ func v1ShaOffset(at int64) int64 {
|
|||||||
return v1EntryOffset(at) +
|
return v1EntryOffset(at) +
|
||||||
// Skip past the 4-byte object offset in the desired entry to
|
// Skip past the 4-byte object offset in the desired entry to
|
||||||
// the SHA1.
|
// the SHA1.
|
||||||
ObjectSmallOffsetWidth
|
indexObjectSmallOffsetWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
// v1EntryOffset returns the location of the packfile offset for the object
|
// v1EntryOffset returns the location of the packfile offset for the object
|
||||||
// given at "at".
|
// given at "at".
|
||||||
func v1EntryOffset(at int64) int64 {
|
func v1EntryOffset(at int64) int64 {
|
||||||
// Skip the L1 fanout table
|
// Skip the L1 fanout table
|
||||||
return OffsetV1Start +
|
return indexOffsetV1Start +
|
||||||
// Skip the object entries before the one located at "at"
|
// Skip the object entries before the one located at "at"
|
||||||
(ObjectEntryV1Width * at)
|
(indexObjectEntryV1Width * at)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
V1IndexFanout = make([]uint32, FanoutEntries)
|
V1IndexFanout = make([]uint32, indexFanoutEntries)
|
||||||
|
|
||||||
V1IndexSmallEntry = []byte{
|
V1IndexSmallEntry = []byte{
|
||||||
0x0, 0x0, 0x0, 0x1,
|
0x0, 0x0, 0x0, 0x1,
|
||||||
@ -82,12 +82,12 @@ func init() {
|
|||||||
V1IndexFanout[i] = 3
|
V1IndexFanout[i] = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
fanout := make([]byte, FanoutWidth)
|
fanout := make([]byte, indexFanoutWidth)
|
||||||
for i, n := range V1IndexFanout {
|
for i, n := range V1IndexFanout {
|
||||||
binary.BigEndian.PutUint32(fanout[i*FanoutEntryWidth:], n)
|
binary.BigEndian.PutUint32(fanout[i*indexFanoutEntryWidth:], n)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 0, OffsetV1Start+(3*ObjectEntryV1Width))
|
buf := make([]byte, 0, indexOffsetV1Start+(3*indexObjectEntryV1Width))
|
||||||
|
|
||||||
buf = append(buf, fanout...)
|
buf = append(buf, fanout...)
|
||||||
buf = append(buf, V1IndexSmallEntry...)
|
buf = append(buf, V1IndexSmallEntry...)
|
||||||
|
@ -48,20 +48,20 @@ func v2Search(idx *Index, name []byte, at int64) (*IndexEntry, int, error) {
|
|||||||
// v2ShaOffset returns the offset of a SHA1 given at "at" in the V2 index file.
|
// v2ShaOffset returns the offset of a SHA1 given at "at" in the V2 index file.
|
||||||
func v2ShaOffset(at int64) int64 {
|
func v2ShaOffset(at int64) int64 {
|
||||||
// Skip the packfile index header and the L1 fanout table.
|
// Skip the packfile index header and the L1 fanout table.
|
||||||
return OffsetV2Start +
|
return indexOffsetV2Start +
|
||||||
// Skip until the desired name in the sorted names table.
|
// Skip until the desired name in the sorted names table.
|
||||||
(ObjectNameWidth * at)
|
(indexObjectNameWidth * at)
|
||||||
}
|
}
|
||||||
|
|
||||||
// v2SmallOffsetOffset returns the offset of an object's small (4-byte) offset
|
// v2SmallOffsetOffset returns the offset of an object's small (4-byte) offset
|
||||||
// given by "at".
|
// given by "at".
|
||||||
func v2SmallOffsetOffset(at, total int64) int64 {
|
func v2SmallOffsetOffset(at, total int64) int64 {
|
||||||
// Skip the packfile index header and the L1 fanout table.
|
// Skip the packfile index header and the L1 fanout table.
|
||||||
return OffsetV2Start +
|
return indexOffsetV2Start +
|
||||||
// Skip the name table.
|
// Skip the name table.
|
||||||
(ObjectNameWidth * total) +
|
(indexObjectNameWidth * total) +
|
||||||
// Skip the CRC table.
|
// Skip the CRC table.
|
||||||
(ObjectCRCWidth * total) +
|
(indexObjectCRCWidth * total) +
|
||||||
// Skip until the desired index in the small offsets table.
|
// Skip until the desired index in the small offsets table.
|
||||||
(ObjectSmallOffsetWidth * at)
|
(indexObjectSmallOffsetWidth * at)
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ var (
|
|||||||
0xff, 0x74, 0x4f, 0x63,
|
0xff, 0x74, 0x4f, 0x63,
|
||||||
0x00, 0x00, 0x00, 0x02,
|
0x00, 0x00, 0x00, 0x02,
|
||||||
}
|
}
|
||||||
V2IndexFanout = make([]uint32, FanoutEntries)
|
V2IndexFanout = make([]uint32, indexFanoutEntries)
|
||||||
|
|
||||||
V2IndexNames = []byte{
|
V2IndexNames = []byte{
|
||||||
0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
|
0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
|
||||||
@ -90,12 +90,12 @@ func init() {
|
|||||||
V2IndexFanout[i] = 3
|
V2IndexFanout[i] = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
fanout := make([]byte, FanoutWidth)
|
fanout := make([]byte, indexFanoutWidth)
|
||||||
for i, n := range V2IndexFanout {
|
for i, n := range V2IndexFanout {
|
||||||
binary.BigEndian.PutUint32(fanout[i*FanoutEntryWidth:], n)
|
binary.BigEndian.PutUint32(fanout[i*indexFanoutEntryWidth:], n)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 0, OffsetV2Start+3*(ObjectEntryV2Width)+ObjectLargeOffsetWidth)
|
buf := make([]byte, 0, indexOffsetV2Start+3*(indexObjectEntryV2Width)+indexObjectLargeOffsetWidth)
|
||||||
buf = append(buf, V2IndexHeader...)
|
buf = append(buf, V2IndexHeader...)
|
||||||
buf = append(buf, fanout...)
|
buf = append(buf, fanout...)
|
||||||
buf = append(buf, V2IndexNames...)
|
buf = append(buf, V2IndexNames...)
|
||||||
|
@ -19,9 +19,9 @@ const (
|
|||||||
func (v IndexVersion) Width() int64 {
|
func (v IndexVersion) Width() int64 {
|
||||||
switch v {
|
switch v {
|
||||||
case V2:
|
case V2:
|
||||||
return V2Width
|
return indexV2Width
|
||||||
case V1:
|
case V1:
|
||||||
return V1Width
|
return indexV1Width
|
||||||
}
|
}
|
||||||
panic(fmt.Sprintf("git/odb/pack: width unknown for pack version %d", v))
|
panic(fmt.Sprintf("git/odb/pack: width unknown for pack version %d", v))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user