Skip to content

Commit

Permalink
Drop repeated package name from CarStats
Browse files Browse the repository at this point in the history
Cosmetic refactor to rename `car.CarStats` to `car.Stats`, which looks
more fluent when using the API.
  • Loading branch information
masih committed Jul 6, 2022
1 parent 3a0f2a4 commit 1fabdd7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
40 changes: 20 additions & 20 deletions v2/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func (r *Reader) IndexReader() io.ReaderAt {
return internalio.NewOffsetReadSeeker(r.r, int64(r.Header.IndexOffset))
}

// CarStats is returned by an Inspect() call
type CarStats struct {
// Stats is returned by an Inspect() call
type Stats struct {
Version uint64
Header Header
Roots []cid.Cid
Expand All @@ -142,7 +142,7 @@ type CarStats struct {
}

// Inspect does a quick scan of a CAR, performing basic validation of the format
// and returning a CarStats object that provides a high-level description of the
// and returning a Stats object that provides a high-level description of the
// contents of the CAR.
// Inspect works for CARv1 and CARv2 contents. A CARv1 will return an
// uninitialized Header value.
Expand All @@ -169,26 +169,26 @@ type CarStats struct {
// them and have any reason to not trust the source.
//
// * Blocks use codecs that your system doesn't have access to—which may mean
// you can't traverse a DAG or use the contained data. CarStats#CodecCounts
// you can't traverse a DAG or use the contained data. Stats.CodecCounts
// contains a list of codecs found in the CAR so this can be checked.
//
// * CIDs use multihashes that your system doesn't have access to—which will
// mean you can't validate block hashes are correct (using validateBlockHash
// in this case will result in a failure). CarStats#MhTypeCounts contains a
// list of multihashes found in the CAR so this can bechecked.
// in this case will result in a failure). Stats.MhTypeCounts contains a
// list of multihashes found in the CAR so this can be checked.
//
// * The presence of IDENTITY CIDs, which may not be supported (or desired) by
// the consumer of the CAR. CarStats#CodecCounts can determine the presence
// the consumer of the CAR. Stats.CodecCounts can determine the presence
// of IDENTITY CIDs.
//
// * Roots: the number of roots, duplicates, and whether they are related to the
// blocks contained within the CAR. CarStats contains a list of Roots and a
// blocks contained within the CAR. Stats contains a list of Roots and a
// RootsPresent bool so further checks can be performed.
//
// * DAG completeness is not checked. Any properties relating to the DAG, or
// DAGs contained within a CAR are the responsibility of the user to check.
func (r *Reader) Inspect(validateBlockHash bool) (CarStats, error) {
stats := CarStats{
func (r *Reader) Inspect(validateBlockHash bool) (Stats, error) {
stats := Stats{
Version: r.Version,
Header: r.Header,
CodecCounts: make(map[multicodec.Code]uint64),
Expand All @@ -206,7 +206,7 @@ func (r *Reader) Inspect(validateBlockHash bool) (CarStats, error) {
// read roots, not using Roots(), because we need the offset setup in the data trader
header, err := carv1.ReadHeader(dr, r.opts.MaxAllowedHeaderSize)
if err != nil {
return CarStats{}, err
return Stats{}, err
}
stats.Roots = header.Roots
var rootsPresentCount int
Expand All @@ -219,7 +219,7 @@ func (r *Reader) Inspect(validateBlockHash bool) (CarStats, error) {
if err == io.EOF {
// if the length of bytes read is non-zero when the error is EOF then signal an unclean EOF.
if sectionLength > 0 {
return CarStats{}, io.ErrUnexpectedEOF
return Stats{}, io.ErrUnexpectedEOF
}
// otherwise, this is a normal ending
break
Expand All @@ -230,20 +230,20 @@ func (r *Reader) Inspect(validateBlockHash bool) (CarStats, error) {
break
}
if sectionLength > r.opts.MaxAllowedSectionSize {
return CarStats{}, util.ErrSectionTooLarge
return Stats{}, util.ErrSectionTooLarge
}

// decode just the CID bytes
cidLen, c, err := cid.CidFromReader(dr)
if err != nil {
return CarStats{}, err
return Stats{}, err
}

if sectionLength < uint64(cidLen) {
// this case is handled different in the normal ReadNode() path since it
// slurps in the whole section bytes and decodes CID from there - so an
// error should come from a failing io.ReadFull
return CarStats{}, fmt.Errorf("section length shorter than CID length")
return Stats{}, fmt.Errorf("section length shorter than CID length")
}

// is this a root block? (also account for duplicate root CIDs)
Expand Down Expand Up @@ -278,7 +278,7 @@ func (r *Reader) Inspect(validateBlockHash bool) (CarStats, error) {
}
mh, err := multihash.SumStream(blockReader, cp.MhType, mhl)
if err != nil {
return CarStats{}, err
return Stats{}, err
}
var gotCid cid.Cid
switch cp.Version {
Expand All @@ -287,15 +287,15 @@ func (r *Reader) Inspect(validateBlockHash bool) (CarStats, error) {
case 1:
gotCid = cid.NewCidV1(cp.Codec, mh)
default:
return CarStats{}, fmt.Errorf("invalid cid version: %d", cp.Version)
return Stats{}, fmt.Errorf("invalid cid version: %d", cp.Version)
}
if !gotCid.Equals(c) {
return CarStats{}, fmt.Errorf("mismatch in content integrity, expected: %s, got: %s", c, gotCid)
return Stats{}, fmt.Errorf("mismatch in content integrity, expected: %s, got: %s", c, gotCid)
}
} else {
// otherwise, skip over it
if _, err := dr.Seek(int64(blockLength), io.SeekCurrent); err != nil {
return CarStats{}, err
return Stats{}, err
}
}

Expand Down Expand Up @@ -329,7 +329,7 @@ func (r *Reader) Inspect(validateBlockHash bool) (CarStats, error) {
// is intended to be a fast initial scan
ind, size, err := index.ReadFromWithSize(r.IndexReader())
if err != nil {
return CarStats{}, err
return Stats{}, err
}
stats.IndexCodec = ind.Codec()
stats.IndexSize = uint64(size)
Expand Down
12 changes: 6 additions & 6 deletions v2/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ func TestInspect(t *testing.T) {
path string
carHex string
zerLenAsEOF bool
expectedStats carv2.CarStats
expectedStats carv2.Stats
}{
{
name: "IndexlessCarV2",
path: "testdata/sample-v2-indexless.car",
expectedStats: carv2.CarStats{
expectedStats: carv2.Stats{
Version: 2,
Header: carv2.Header{
Characteristics: carv2.Characteristics{0, 0},
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestInspect(t *testing.T) {
// same payload as IndexlessCarV2, so only difference is the Version & Header
name: "CarV1",
path: "testdata/sample-v1.car",
expectedStats: carv2.CarStats{
expectedStats: carv2.Stats{
Version: 1,
Header: carv2.Header{},
Roots: []cid.Cid{mustCidDecode("bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy")},
Expand All @@ -342,7 +342,7 @@ func TestInspect(t *testing.T) {
// same payload as IndexlessCarV2, so only difference is the Header
name: "CarV2ProducedByBlockstore",
path: "testdata/sample-rw-bs-v2.car",
expectedStats: carv2.CarStats{
expectedStats: carv2.Stats{
Version: 2,
Header: carv2.Header{
DataOffset: 1464,
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestInspect(t *testing.T) {
name: "CarV1VersionWithZeroLenSectionIsOne",
path: "testdata/sample-v1-with-zero-len-section.car",
zerLenAsEOF: true,
expectedStats: carv2.CarStats{
expectedStats: carv2.Stats{
Version: 1,
Header: carv2.Header{},
Roots: []cid.Cid{mustCidDecode("bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy")},
Expand Down Expand Up @@ -403,7 +403,7 @@ func TestInspect(t *testing.T) {
name: "IdentityCID",
// 47 {version:1,roots:[identity cid]} 25 identity cid (dag-json {"identity":"block"})
carHex: "2f a265726f6f747381d82a581a0001a90200147b226964656e74697479223a22626c6f636b227d6776657273696f6e01 19 01a90200147b226964656e74697479223a22626c6f636b227d",
expectedStats: carv2.CarStats{
expectedStats: carv2.Stats{
Version: 1,
Roots: []cid.Cid{mustCidDecode("baguqeaaupmrgszdfnz2gs5dzei5ceytmn5rwwit5")},
RootsPresent: true,
Expand Down

0 comments on commit 1fabdd7

Please sign in to comment.