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

Code before:

func (f *inMemoryArchiveIndexReader) getSuffix(idx uint32) suffix {
	if idx >= f.getNumChunks() {
		return suffix{}
	}
	start := uint64(idx) * hash.SuffixLen
	return suffix(f.suffixes[start : start+hash.SuffixLen])
}

func (f *inMemoryArchiveIndexReader) clone() (archiveIndexReader, error) {
	if !dynassert.Assert(f.refCnt.Add(1) > 1, "attempt to clone a closed inMemoryArchiveIndexReader") {
		// Restore previous refcnt, despite being in a weird state...
		f.refCnt.Add(-1)
		// Just return ourselves in this weird state I guess...
		return f, nil
	}
	return f, nil
}

func (f *inMemoryArchiveIndexReader) Close() error {
	cnt := f.refCnt.Add(-1)
	dynassert.Assert(cnt >= 0, "invalid cnt on inMemoryArchiveIndexReader. closed more times than cloned?")
	// No need to restore count which was over closed. We already incorrectly
	// released the bytes and can't necessarily do anything about it.
	if cnt == 0 {
		numBytes := len(f.chunkRefs)*4 + len(f.prefixes)*8 + len(f.spanIndex)*8 + len(f.suffixes)
		f.q.ReleaseQuotaBytes(numBytes)
	}
	return nil
}

// clone returns a new archiveReader with a new (provided) reader. All other fields are immutable or thread safe,
// so they are copied.
func (ar archiveReader) clone() (archiveReader, error) {
	reader, err := ar.reader.clone()
	if err != nil {
		return archiveReader{}, err
	}
	indexReader, err := ar.indexReader.clone()
	if err != nil {
		return archiveReader{}, err
	}
	return archiveReader{
		reader:      reader,
		indexReader: indexReader,
		footer:      ar.footer,
		dictCache:   ar.dictCache, // cache is thread safe.
	}, nil
}

type readerAtWithStatsBridge struct {

Review comment (suggestion):
```suggestion
		numBytes := len(f.chunkRefs)*uint32Size + len(f.prefixes)*uint64Size + len(f.spanIndex)*uint64Size + len(f.suffixes)
```

Code after:

func (f *inMemoryArchiveIndexReader) getSuffix(idx uint32) suffix {
	if idx >= f.getNumChunks() {
		return suffix{}
	}
	start := uint64(idx) * hash.SuffixLen
	return suffix(f.suffixes[start : start+hash.SuffixLen])
}

func (f *inMemoryArchiveIndexReader) clone() (archiveIndexReader, error) {
	if !dynassert.Assert(f.refCnt.Add(1) > 1, "attempt to clone a closed inMemoryArchiveIndexReader") {
		// Restore previous refcnt, despite being in a weird state...
		f.refCnt.Add(-1)
		// Just return ourselves in this weird state I guess...
		return f, nil
	}
	return f, nil
}

func (f *inMemoryArchiveIndexReader) Close() error {
	cnt := f.refCnt.Add(-1)
	dynassert.Assert(cnt >= 0, "invalid cnt on inMemoryArchiveIndexReader. closed more times than cloned?")
	// No need to restore count which was over closed. We already incorrectly
	// released the bytes and can't necessarily do anything about it.
	if cnt == 0 {
		numBytes := len(f.chunkRefs)*uint32Size + len(f.prefixes)*uint64Size + len(f.spanIndex)*uint64Size + len(f.suffixes)
		f.q.ReleaseQuotaBytes(numBytes)
	}
	return nil
}

// clone returns a new archiveReader with a new (provided) reader. All other fields are immutable or thread safe,
// so they are copied.
func (ar archiveReader) clone() (archiveReader, error) {
	reader, err := ar.reader.clone()
	if err != nil {
		return archiveReader{}, err
	}
	indexReader, err := ar.indexReader.clone()
	if err != nil {
		return archiveReader{}, err
	}
	return archiveReader{
		reader:      reader,
		indexReader: indexReader,
		footer:      ar.footer,
		dictCache:   ar.dictCache, // cache is thread safe.
	}, nil
}

type readerAtWithStatsBridge struct {