Repository: dolthub/dolt
File: go/store/nbs/archive_reader.go (Go)

Code before:
}

// newInMemoryArchiveIndexReader implements the original index loading logic for non-file readers
func newInMemoryArchiveIndexReader(ctx context.Context, reader tableReaderAt, footer archiveFooter, q MemoryQuotaProvider, stats *Stats) (archiveIndexReader, error) {
	byteOffSpan := footer.indexByteOffsetSpan()
	secRdr := newSectionReader(ctx, reader, int64(byteOffSpan.offset), int64(byteOffSpan.length), stats)
	byteSpans, err := q.AcquireQuotaUint64s(ctx, int(footer.byteSpanCount)+1)
	if err != nil {
		return nil, fmt.Errorf("Failed to allocate byteSpans uint64 slice: %w", err)
	}
	bytesSoFar := len(byteSpans) * 8
	byteSpans[0] = 0 // Null byteSpan to simplify logic.
	err = binary.Read(secRdr, binary.BigEndian, byteSpans[1:])
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to read byte spans: %w", err)
	}

	prefixSpan := footer.indexPrefixSpan()
	prefixRdr := newSectionReader(ctx, reader, int64(prefixSpan.offset), int64(prefixSpan.length), stats)
	prefixes, err := q.AcquireQuotaUint64s(ctx, int(footer.chunkCount))
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to allocate prefixes uint32 slice: %w", err)
	}
	bytesSoFar += len(prefixes) * 8
	err = binary.Read(prefixRdr, binary.BigEndian, prefixes[:])
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to read prefixes: %w", err)
	}

	chunkRefSpan := footer.indexChunkRefSpan()
	chunkRdr := newSectionReader(ctx, reader, int64(chunkRefSpan.offset), int64(chunkRefSpan.length), stats)
	chnks, err := q.AcquireQuotaUint32s(ctx, int(footer.chunkCount)*2)
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to allocate chunks uint32 slice: %w", err)
	}
	bytesSoFar += len(chnks) * 4
	err = binary.Read(chunkRdr, binary.BigEndian, chnks[:])
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to read chunk references: %w", err)
	}

	suffixSpan := footer.indexSuffixSpan()
	sufRdr := newSectionReader(ctx, reader, int64(suffixSpan.offset), int64(suffixSpan.length), stats)
	suffixes, err := q.AcquireQuotaBytes(ctx, int(suffixSpan.length))
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)

Review comment (suggestion):
```suggestion
	bytesSoFar += len(prefixes) * uint64Size
```

Code after:
}

// newInMemoryArchiveIndexReader implements the original index loading logic for non-file readers
func newInMemoryArchiveIndexReader(ctx context.Context, reader tableReaderAt, footer archiveFooter, q MemoryQuotaProvider, stats *Stats) (archiveIndexReader, error) {
	byteOffSpan := footer.indexByteOffsetSpan()
	secRdr := newSectionReader(ctx, reader, int64(byteOffSpan.offset), int64(byteOffSpan.length), stats)
	byteSpans, err := q.AcquireQuotaUint64s(ctx, int(footer.byteSpanCount)+1)
	if err != nil {
		return nil, fmt.Errorf("Failed to allocate byteSpans uint64 slice: %w", err)
	}
	bytesSoFar := len(byteSpans) * uint64Size
	byteSpans[0] = 0 // Null byteSpan to simplify logic.
	err = binary.Read(secRdr, binary.BigEndian, byteSpans[1:])
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to read byte spans: %w", err)
	}

	prefixSpan := footer.indexPrefixSpan()
	prefixRdr := newSectionReader(ctx, reader, int64(prefixSpan.offset), int64(prefixSpan.length), stats)
	prefixes, err := q.AcquireQuotaUint64s(ctx, int(footer.chunkCount))
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to allocate prefixes uint32 slice: %w", err)
	}
	bytesSoFar += len(prefixes) * uint64Size
	err = binary.Read(prefixRdr, binary.BigEndian, prefixes[:])
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to read prefixes: %w", err)
	}

	chunkRefSpan := footer.indexChunkRefSpan()
	chunkRdr := newSectionReader(ctx, reader, int64(chunkRefSpan.offset), int64(chunkRefSpan.length), stats)
	chnks, err := q.AcquireQuotaUint32s(ctx, int(footer.chunkCount)*2)
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to allocate chunks uint32 slice: %w", err)
	}
	bytesSoFar += len(chnks) * uint32Size
	err = binary.Read(chunkRdr, binary.BigEndian, chnks[:])
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)
		return nil, fmt.Errorf("Failed to read chunk references: %w", err)
	}

	suffixSpan := footer.indexSuffixSpan()
	sufRdr := newSectionReader(ctx, reader, int64(suffixSpan.offset), int64(suffixSpan.length), stats)
	suffixes, err := q.AcquireQuotaBytes(ctx, int(suffixSpan.length))
	if err != nil {
		q.ReleaseQuotaBytes(bytesSoFar)