Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions v2/index_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ func LoadIndex(idx index.Index, r io.Reader, opts ...Option) error {
o := ApplyOptions(opts...)

reader := internalio.ToByteReadSeeker(r)
pragma, err := carv1.ReadHeader(r, o.MaxAllowedHeaderSize)
v1h, err := carv1.ReadHeader(r, o.MaxAllowedHeaderSize) // pragma for a v2
if err != nil {
return fmt.Errorf("error reading car header: %w", err)
}

var dataSize, dataOffset int64
switch pragma.Version {
switch v1h.Version {
case 1:
break
case 2:
Expand Down Expand Up @@ -89,7 +89,19 @@ func LoadIndex(idx index.Index, r io.Reader, opts ...Option) error {
return fmt.Errorf("expected data payload header version of 1; got %d", v1h.Version)
}
default:
return fmt.Errorf("expected either version 1 or 2; got %d", pragma.Version)
return fmt.Errorf("expected either version 1 or 2; got %d", v1h.Version)
}

identityRoots := make(map[cid.Cid]bool)
if o.StoreIdentityCIDs {
for _, r := range v1h.Roots {
if r.Prefix().MhType == multihash.IDENTITY {
if uint64(r.ByteLen()) > o.MaxIndexCidSize {
return &ErrCidTooLarge{MaxSize: o.MaxIndexCidSize, CurrentSize: uint64(r.ByteLen())}
}
identityRoots[r] = true
}
}
}

// Record the start of each section, with first section starring from current position in the
Expand Down Expand Up @@ -137,6 +149,11 @@ func LoadIndex(idx index.Index, r io.Reader, opts ...Option) error {
if uint64(cidLen) > o.MaxIndexCidSize {
return &ErrCidTooLarge{MaxSize: o.MaxIndexCidSize, CurrentSize: uint64(cidLen)}
}
for r := range identityRoots {
if r.Equals(c) {
identityRoots[r] = false // flag as unnecessary to store as an index special-case
}
}
records = append(records, index.Record{Cid: c, Offset: uint64(sectionOffset)})
}

Expand All @@ -156,6 +173,12 @@ func LoadIndex(idx index.Index, r io.Reader, opts ...Option) error {
}
}

for r, store := range identityRoots {
if store {
records = append(records, index.Record{Cid: r, Offset: uint64(1)})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather this be 0 but it can't be because of https://github.com/filecoin-project/index-provider/blob/b918da7508b05263988561ec3f28ca31e0fcacb9/multihash_iterator.go#L48-L52

But also, is it really something we should report to the indexer, perhaps it should get filtered out before it gets there. 🤷

}
}

if err := idx.Load(records); err != nil {
return err
}
Expand Down