github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/io_entry.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  	"bytes"
    19  	"io"
    20  	"os"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/fileservice/memorycache"
    23  )
    24  
    25  func (i *IOEntry) setCachedData() error {
    26  	if i.ToCacheData == nil {
    27  		return nil
    28  	}
    29  	if len(i.Data) == 0 {
    30  		return nil
    31  	}
    32  	if i.allocator == nil {
    33  		i.allocator = DefaultCacheDataAllocator
    34  	}
    35  	bs, err := i.ToCacheData(bytes.NewReader(i.Data), i.Data, i.allocator)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	i.CachedData = bs
    40  	return nil
    41  }
    42  
    43  func (i *IOEntry) ReadFromOSFile(file *os.File) error {
    44  	r := io.LimitReader(file, i.Size)
    45  
    46  	if cap(i.Data) < int(i.Size) {
    47  		i.Data = make([]byte, i.Size)
    48  	} else {
    49  		i.Data = i.Data[:i.Size]
    50  	}
    51  
    52  	n, err := io.ReadFull(r, i.Data)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	if n != int(i.Size) {
    57  		return io.ErrUnexpectedEOF
    58  	}
    59  
    60  	if i.WriterForRead != nil {
    61  		if _, err := i.WriterForRead.Write(i.Data); err != nil {
    62  			return err
    63  		}
    64  	}
    65  	if i.ReadCloserForRead != nil {
    66  		*i.ReadCloserForRead = io.NopCloser(bytes.NewReader(i.Data))
    67  	}
    68  	if err := i.setCachedData(); err != nil {
    69  		return err
    70  	}
    71  
    72  	i.done = true
    73  
    74  	return nil
    75  }
    76  
    77  func CacheOriginalData(r io.Reader, data []byte, allocator CacheDataAllocator) (cacheData memorycache.CacheData, err error) {
    78  	if len(data) == 0 {
    79  		data, err = io.ReadAll(r)
    80  		if err != nil {
    81  			return
    82  		}
    83  	}
    84  	cacheData = allocator.Alloc(len(data))
    85  	copy(cacheData.Bytes(), data)
    86  	return
    87  }