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

Code before:
		return compactionPlan{}, fmt.Errorf("failed to finalize archive: %w", err)
	}

	name, err := aw.getName()
	if err != nil {
		return compactionPlan{}, fmt.Errorf("failed to get name of conjoined archive: %w", err)
	}

	idxSize := int(writer.pos)

	bufBytes, err := q.AcquireQuotaByteSlice(ctx, idxSize)
	if err != nil {
		return compactionPlan{}, err
	}
	acquiredBytes += idxSize
	bufBytes = bufBytes[:0]
	buf := bytes.NewBuffer(bufBytes)
	if err := writer.Flush(buf); err != nil {
		return compactionPlan{}, fmt.Errorf("failed to build index buffer while conjoining archives: %w", err)
	}

	stats.BytesPerConjoin.Sample(dataBlocksLen + uint64(len(buf.Bytes())))

	writer = nil
	// Our temporary slices used for serializing the index are no longer needed.
	q.ReleaseQuotaBytes(acquiredBytes - idxSize)

	return compactionPlan{
		sources:             orderedSrcs,
		name:                name,
		suffix:              ArchiveFileSuffix,
		mergedIndex:         buf.Bytes(),
		chunkCount:          chunkCounter,
		totalCompressedData: dataBlocksLen,
		closer: func() {
			q.ReleaseQuotaBytes(idxSize)
		},
	}, nil
}


Review comment (suggestion):
Above we have:

```go
defer func() {
		if err != nil {
			q.ReleaseQuotaBytes(acquiredBytes)
```

seems like we should remove the err != nil condition, and adjust `acquiredBytes -= idxSize` here.

acuiredBytes may be better named as `stackFrameOnlyBytes` or something like that. I dunno. This all seems like it's begging for future me to mess it up.

Code after:
	dataBlocksLen := aw.bytesWritten

	// The conjoin process is a little different from the normal archive writing process. We manually stick everything
	// into the writer, and then finalize the index and footer at the end. The datablocks will be written in separately
	// after we have created the index and footer.
	//
	// So we set the workflow stage to stageIndex because we skipped the byte span insertion stage.
	aw.workflowStage = stageIndex

	err = aw.indexFinalize(hash.Hash{})
	if err != nil {
		return compactionPlan{}, fmt.Errorf("failed to finalize archive: %w", err)
	}

	name, err := aw.getName()
	if err != nil {
		return compactionPlan{}, fmt.Errorf("failed to get name of conjoined archive: %w", err)
	}

	idxSize := int(writer.pos)

	bufBytes, err := q.AcquireQuotaByteSlice(ctx, idxSize)
	if err != nil {
		return compactionPlan{}, err
	}
	acquiredBytesOnStack += idxSize
	bufBytes = bufBytes[:0]
	buf := bytes.NewBuffer(bufBytes)
	if err := writer.Flush(buf); err != nil {
		return compactionPlan{}, fmt.Errorf("failed to build index buffer while conjoining archives: %w", err)
	}

	stats.BytesPerConjoin.Sample(dataBlocksLen + uint64(len(buf.Bytes())))

	writer = nil

	// On return, we are going to release quota bytes we are still responsible for.
	// We are transfering responsibility for idxSize bytes to our caller.
	acquiredBytesOnStack -= idxSize

	return compactionPlan{
		sources:             orderedSrcs,
		name:                name,
		suffix:              ArchiveFileSuffix,
		mergedIndex:         buf.Bytes(),
		chunkCount:          chunkCounter,
		totalCompressedData: dataBlocksLen,
		closer: func() {
			q.ReleaseQuotaBytes(idxSize)
		},
	}, nil