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

Code before:
		if _, isSet := os.LookupEnv(dconfig.EnvAssertNoInMemoryArchiveIndex); isSet {
			return archiveReader{}, fmt.Errorf("attempted to load archive index into memory but %s was set", dconfig.EnvAssertNoInMemoryArchiveIndex)
		}
		indexRdr, err = newInMemoryArchiveIndexReader(ctx, reader, footer, q, stats)
		if err != nil {
			return archiveReader{}, err
		}
	}

	return archiveReader{
		reader:      reader,
		indexReader: indexRdr,
		footer:      footer,
		dictCache:   dictCache,
	}, nil
}

// 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 {

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

Code after:
		if _, isSet := os.LookupEnv(dconfig.EnvAssertNoInMemoryArchiveIndex); isSet {
			return archiveReader{}, fmt.Errorf("attempted to load archive index into memory but %s was set", dconfig.EnvAssertNoInMemoryArchiveIndex)
		}
		indexRdr, err = newInMemoryArchiveIndexReader(ctx, reader, footer, q, stats)
		if err != nil {
			return archiveReader{}, err
		}
	}

	return archiveReader{
		reader:      reader,
		indexReader: indexRdr,
		footer:      footer,
		dictCache:   dictCache,
	}, nil
}

// 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 {