github.com/matrixorigin/matrixone@v1.2.0/pkg/util/export/etl/buf.go (about)

     1  // Copyright 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package etl
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"io"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    23  )
    24  
    25  // var _ table.RowWriter = (*BufWriter)(nil)
    26  var _ io.WriteCloser = (*BufWriter)(nil)
    27  
    28  type BufWriter struct {
    29  	ctx    context.Context
    30  	writer io.Writer
    31  	buf    *bytes.Buffer
    32  }
    33  
    34  func NewBufWriter(ctx context.Context, writer io.Writer) *BufWriter {
    35  	w := &BufWriter{
    36  		ctx:    ctx,
    37  		writer: writer,
    38  		buf:    bytes.NewBuffer(make([]byte, 0, mpool.MB)),
    39  	}
    40  	return w
    41  }
    42  
    43  func (w *BufWriter) Write(p []byte) (n int, err error) {
    44  	return w.buf.Write(p)
    45  }
    46  
    47  func (w *BufWriter) Close() error {
    48  	_, err := w.writer.Write(w.buf.Next(w.buf.Len()))
    49  	w.buf = nil
    50  	return err
    51  }