Skip to content

Commit

Permalink
remove Buffer method totally
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Ye <[email protected]>
  • Loading branch information
yeya24 committed Jul 6, 2023
1 parent 6486b6c commit a6f69de
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions pkg/block/indexheader/binary_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,19 @@ func WriteBinary(ctx context.Context, bkt objstore.BucketReader, id ulid.ULID, f
if err != nil {
return nil, errors.Wrap(err, "new index reader")
}
var memWriterBuf []byte
tmpFilename := ""
if filename != "" {
tmpFilename = filename + ".tmp"
} else {
// Initialize the buffer for memWriter only.
memWriterBuf = make([]byte, 32*1024)
}

// Buffer for copying and encbuffers.
// This also will control the size of file writer buffer.
buf := make([]byte, 32*1024)
bw, err := newBinaryWriter(id, tmpFilename, buf)
bw, err := newBinaryWriter(id, tmpFilename, buf, memWriterBuf)
if err != nil {
return nil, errors.Wrap(err, "new binary index header writer")
}
Expand Down Expand Up @@ -127,7 +131,7 @@ func WriteBinary(ctx context.Context, bkt objstore.BucketReader, id ulid.ULID, f
return nil, os.Rename(tmpFilename, filename)
}

return bw.Buffer(), nil
return memWriterBuf, nil
}

type chunkedIndexReader struct {
Expand Down Expand Up @@ -249,7 +253,7 @@ type binaryWriter struct {
crc32 hash.Hash
}

func newBinaryWriter(id ulid.ULID, cacheFilename string, buf []byte) (w *binaryWriter, err error) {
func newBinaryWriter(id ulid.ULID, cacheFilename string, buf []byte, memWriterBuf []byte) (w *binaryWriter, err error) {
var binWriter PosWriter
if cacheFilename != "" {
dir := filepath.Dir(cacheFilename)
Expand Down Expand Up @@ -282,7 +286,7 @@ func newBinaryWriter(id ulid.ULID, cacheFilename string, buf []byte) (w *binaryW
}
binWriter = fileWriter
} else {
binWriter = NewMemoryWriter(id, len(buf))
binWriter = NewMemoryWriter(id, memWriterBuf)
}

w = &binaryWriter{
Expand All @@ -303,7 +307,6 @@ func newBinaryWriter(id ulid.ULID, cacheFilename string, buf []byte) (w *binaryW
type PosWriter interface {
Pos() uint64
Write(bufs ...[]byte) error
Buffer() []byte
Flush() error
Sync() error
Close() error
Expand All @@ -315,10 +318,10 @@ type MemoryWriter struct {
pos uint64
}

func NewMemoryWriter(id ulid.ULID, size int) *MemoryWriter {
func NewMemoryWriter(id ulid.ULID, buf []byte) *MemoryWriter {
return &MemoryWriter{
id: id,
buf: bytes.NewBuffer(make([]byte, size)),
buf: bytes.NewBuffer(buf),
pos: 0,
}
}
Expand All @@ -345,10 +348,6 @@ func (mw *MemoryWriter) Write(bufs ...[]byte) error {
return nil
}

func (mw *MemoryWriter) Buffer() []byte {
return mw.buf.Bytes()
}

func (mw *MemoryWriter) Flush() error {
return nil
}
Expand Down Expand Up @@ -404,11 +403,6 @@ func (fw *FileWriter) Write(bufs ...[]byte) error {
return nil
}

// Buffer is not used at all for FileWriter.
func (fw *FileWriter) Buffer() []byte {
return nil
}

func (fw *FileWriter) Flush() error {
return fw.fileWriter.Flush()
}
Expand Down Expand Up @@ -465,10 +459,6 @@ func (w *binaryWriter) Write(p []byte) (int, error) {
return int(w.writer.Pos() - n), err
}

func (w *binaryWriter) Buffer() []byte {
return w.writer.Buffer()
}

func (w *binaryWriter) Close() error {
return w.writer.Close()
}
Expand Down

0 comments on commit a6f69de

Please sign in to comment.