-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Skip other sections when reading metadata #826
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import ( | |
"hash/crc64" | ||
"log" | ||
"os" | ||
"slices" | ||
"sort" | ||
|
||
"github.com/rs/xid" | ||
|
@@ -94,20 +95,15 @@ func (r *reader) Str() (string, error) { | |
} | ||
|
||
func (r *reader) readTOC(toc *indexTOC) error { | ||
sz, err := r.r.Size() | ||
if err != nil { | ||
return err | ||
} | ||
r.off = sz - 8 | ||
|
||
var tocSection simpleSection | ||
if err := tocSection.read(r); err != nil { | ||
return err | ||
} | ||
|
||
r.seek(tocSection.off) | ||
return r.readTOCSections(toc, []string{}) | ||
} | ||
|
||
sectionCount, err := r.U32() | ||
// readTOCSections reads the table of contents of the index file. | ||
// | ||
// If the tags parameter is non-empty, it reads only those tagged sections for efficiency | ||
// and does not populate the other sections. | ||
func (r *reader) readTOCSections(toc *indexTOC, tags []string) error { | ||
tocSection, sectionCount, err := r.readHeader() | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -126,11 +122,14 @@ func (r *reader) readTOC(toc *indexTOC) error { | |
return err | ||
} | ||
|
||
skipSection := len(tags) > 0 && !slices.Contains(tags, tag) | ||
sec := secs[tag] | ||
if sec == nil || sec.kind() != sectionKind(kind) { | ||
// If we don't recognize the section, we may be reading a newer index than the current version. Use | ||
// a "dummy section" struct to skip over it. | ||
log.Printf("encountered unrecognized index section (%s), skipping over it", tag) | ||
skipSection = true | ||
log.Printf("encountered malformed index section (%s), skipping over it", tag) | ||
|
||
switch sectionKind(kind) { | ||
case sectionKindSimple: | ||
sec = &simpleSection{} | ||
|
@@ -143,8 +142,14 @@ func (r *reader) readTOC(toc *indexTOC) error { | |
} | ||
} | ||
|
||
if err := sec.read(r); err != nil { | ||
return err | ||
if skipSection { | ||
if err := sec.skip(r); err != nil { | ||
return err | ||
} | ||
} else { | ||
if err := sec.read(r); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} else { | ||
|
@@ -169,6 +174,27 @@ func (r *reader) readTOC(toc *indexTOC) error { | |
return nil | ||
} | ||
|
||
func (r *reader) readHeader() (simpleSection, uint32, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I factored out the first part of |
||
sz, err := r.r.Size() | ||
if err != nil { | ||
return simpleSection{}, 0, err | ||
} | ||
r.off = sz - 8 | ||
|
||
var tocSection simpleSection | ||
if err := tocSection.read(r); err != nil { | ||
return simpleSection{}, 0, err | ||
} | ||
|
||
r.seek(tocSection.off) | ||
|
||
sectionCount, err := r.U32() | ||
if err != nil { | ||
return simpleSection{}, 0, err | ||
} | ||
return tocSection, sectionCount, nil | ||
} | ||
|
||
func (r *indexData) readSectionBlob(sec simpleSection) ([]byte, error) { | ||
return r.file.Read(sec.off, sec.sz) | ||
} | ||
|
@@ -205,7 +231,7 @@ func readSectionU64(f IndexFile, sec simpleSection) ([]uint64, error) { | |
return arr, nil | ||
} | ||
|
||
func (r *reader) readJSON(data interface{}, sec *simpleSection) error { | ||
func (r *reader) readJSON(data interface{}, sec simpleSection) error { | ||
blob, err := r.r.Read(sec.off, sec.sz) | ||
if err != nil { | ||
return err | ||
|
@@ -228,7 +254,7 @@ func (r *reader) readIndexData(toc *indexTOC) (*indexData, error) { | |
branchNames: []map[uint]string{}, | ||
} | ||
|
||
repos, md, err := r.readMetadata(toc) | ||
repos, md, err := r.parseMetadata(toc.metaData, toc.repoMetaData) | ||
if md != nil && !canReadVersion(md) { | ||
return nil, fmt.Errorf("file is v%d, want v%d", md.IndexFormatVersion, IndexFormatVersion) | ||
} else if err != nil { | ||
|
@@ -395,9 +421,9 @@ func (r *reader) readIndexData(toc *indexTOC) (*indexData, error) { | |
return &d, nil | ||
} | ||
|
||
func (r *reader) readMetadata(toc *indexTOC) ([]*Repository, *IndexMetadata, error) { | ||
func (r *reader) parseMetadata(metaData simpleSection, repoMetaData simpleSection) ([]*Repository, *IndexMetadata, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also simplified this method, as it's not a big deal to be copying |
||
var md IndexMetadata | ||
if err := r.readJSON(&md, &toc.metaData); err != nil { | ||
if err := r.readJSON(&md, metaData); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
|
@@ -410,7 +436,7 @@ func (r *reader) readMetadata(toc *indexTOC) ([]*Repository, *IndexMetadata, err | |
} | ||
|
||
if len(blob) == 0 { | ||
blob, err = r.r.Read(toc.repoMetaData.off, toc.repoMetaData.sz) | ||
blob, err = r.r.Read(repoMetaData.off, repoMetaData.sz) | ||
if err != nil { | ||
return nil, &md, err | ||
} | ||
|
@@ -573,11 +599,11 @@ func NewSearcher(r IndexFile) (Searcher, error) { | |
func ReadMetadata(inf IndexFile) ([]*Repository, *IndexMetadata, error) { | ||
rd := &reader{r: inf} | ||
var toc indexTOC | ||
if err := rd.readTOC(&toc); err != nil { | ||
err := rd.readTOCSections(&toc, []string{"metaData", "repoMetaData"}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return rd.readMetadata(&toc) | ||
return rd.parseMetadata(toc.metaData, toc.repoMetaData) | ||
} | ||
|
||
// ReadMetadataPathAlive is like ReadMetadataPath except that it only returns | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of introducing the "skip" concept, I could have taken advantage of the fact that the metadata sections are always first in the TOC. However, our index reading code is structured around flexible "section tags", and I got the feeling that section ordering wasn't an invariant we wanted to rely on.