@@ -859,10 +859,12 @@ func (w *layoutWriter) WriteDataBlock(b []byte) (block.Handle, error) {
859
859
return w .writeBlock (b , blockkind .SSTableData )
860
860
}
861
861
862
- // WritePrecompressedDataBlock writes a pre-compressed data block and its
863
- // pre-computed trailer to the writer, returning its block handle. It can mangle
864
- // the block data.
865
- func (w * layoutWriter ) WritePrecompressedDataBlock (blk block.PhysicalBlock ) (block.Handle , error ) {
862
+ // WritePrecompressedDataBlock writes a pre-compressed data block (including the
863
+ // trailer) and releases it, returning its block handle. Releases the block even
864
+ // in error cases.
865
+ func (w * layoutWriter ) WritePrecompressedDataBlock (
866
+ blk block.OwnedPhysicalBlock ,
867
+ ) (block.Handle , error ) {
866
868
return w .writePhysicalBlock (blk )
867
869
}
868
870
@@ -943,15 +945,15 @@ func (w *layoutWriter) writeNamedBlockUncompressed(
943
945
}
944
946
945
947
// WriteValueBlock writes a pre-finished value block (with the trailer) to the
946
- // writer. It can mangle the block data .
947
- func (w * layoutWriter ) WriteValueBlock (blk block.PhysicalBlock ) (block.Handle , error ) {
948
+ // writer and releases the buffer. The buffer is releaseed even in error cases .
949
+ func (w * layoutWriter ) WriteValueBlock (blk block.OwnedPhysicalBlock ) (block.Handle , error ) {
948
950
return w .writePhysicalBlock (blk )
949
951
}
950
952
951
- // WriteValueIndexBlock writes a value index block and adds it to the meta
952
- // index. It can mangle the block data .
953
+ // WriteValueIndexBlock writes a value index block, releases it, ands updates
954
+ // the meta index. The block is released even in error cases .
953
955
func (w * layoutWriter ) WriteValueIndexBlock (
954
- blk block.PhysicalBlock , vbih valblk.IndexHandle ,
956
+ blk block.OwnedPhysicalBlock , vbih valblk.IndexHandle ,
955
957
) (block.Handle , error ) {
956
958
h , err := w .writePhysicalBlock (blk )
957
959
if err != nil {
@@ -965,32 +967,31 @@ func (w *layoutWriter) WriteValueIndexBlock(
965
967
// writeBlock checksums, compresses, and writes out a block.
966
968
func (w * layoutWriter ) writeBlock (b []byte , kind block.Kind ) (block.Handle , error ) {
967
969
pb := w .physBlockMaker .Make (b , kind , block .NoFlags )
968
- defer pb .Release ()
969
- h , err := w .writePhysicalBlock (pb )
970
+ h , err := w .writePhysicalBlock (pb .Take ())
970
971
return h , err
971
972
}
972
973
973
974
// writeBlock checksums and writes out a block.
974
975
func (w * layoutWriter ) writeBlockUncompressed (b []byte , kind block.Kind ) (block.Handle , error ) {
975
976
pb := w .physBlockMaker .Make (b , kind , block .DontCompress )
976
- defer pb .Release ()
977
- h , err := w .writePhysicalBlock (pb )
977
+ h , err := w .writePhysicalBlock (pb .Take ())
978
978
return h , err
979
979
}
980
980
981
- // writePhysicalBlock writes a physical block to the writer, returning its block
982
- // handle. Does not release the physical block.
981
+ // writePhysicalBlock writes a physical block to the writer and releases it,
982
+ // returning its block handle. Releases the physical block even in error cases .
983
983
//
984
984
// writePhysicalBlock might mangle the block data.
985
- func (w * layoutWriter ) writePhysicalBlock (blk block.PhysicalBlock ) (block.Handle , error ) {
985
+ func (w * layoutWriter ) writePhysicalBlock (blk block.OwnedPhysicalBlock ) (block.Handle , error ) {
986
986
w .clearFromCache (w .offset )
987
- // Write the bytes to the file.
988
- n , err := blk .WriteTo (w .writable )
987
+ // Write the bytes to the file. Note that this call can mangle the block data,
988
+ // but it should not matter since we are releasing the TempBuffer right away.
989
+ length , err := block .WriteAndReleasePhysicalBlock (blk , w .writable )
989
990
if err != nil {
990
991
return block.Handle {}, err
991
992
}
992
- bh := block.Handle {Offset : w .offset , Length : uint64 (blk . LengthWithoutTrailer ())}
993
- w .offset += uint64 (n )
993
+ bh := block.Handle {Offset : w .offset , Length : uint64 (length . WithoutTrailer ())}
994
+ w .offset += uint64 (length . WithTrailer () )
994
995
return bh , nil
995
996
}
996
997
0 commit comments