github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/io.go (about)

     1  // Copyright 2022 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 fileservice
    16  
    17  import (
    18  	"io"
    19  	"sync/atomic"
    20  )
    21  
    22  type readCloser struct {
    23  	r         io.Reader
    24  	closeFunc func() error
    25  }
    26  
    27  var _ io.ReadCloser = new(readCloser)
    28  
    29  func (r *readCloser) Read(data []byte) (int, error) {
    30  	return r.r.Read(data)
    31  }
    32  
    33  func (r *readCloser) Close() error {
    34  	return r.closeFunc()
    35  }
    36  
    37  type countingReader struct {
    38  	R io.Reader
    39  	C *atomic.Int64
    40  }
    41  
    42  var _ io.Reader = new(countingReader)
    43  
    44  func (c *countingReader) Read(data []byte) (int, error) {
    45  	n, err := c.R.Read(data)
    46  	c.C.Add(int64(n))
    47  	return n, err
    48  }
    49  
    50  type writeCloser struct {
    51  	w         io.Writer
    52  	closeFunc func() error
    53  }
    54  
    55  var _ io.WriteCloser = new(writeCloser)
    56  
    57  func (r *writeCloser) Write(data []byte) (int, error) {
    58  	return r.w.Write(data)
    59  }
    60  
    61  func (r *writeCloser) Close() error {
    62  	return r.closeFunc()
    63  }
    64  
    65  var ioBufferPool = NewPool(
    66  	256,
    67  	func() []byte {
    68  		return make([]byte, 32*1024)
    69  	},
    70  	nil,
    71  	nil,
    72  )