github.com/matrixorigin/matrixone@v1.2.0/pkg/objectio/constructors.go (about)

     1  // Copyright 2021 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 objectio
    16  
    17  import (
    18  	"io"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/compress"
    21  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    22  	"github.com/matrixorigin/matrixone/pkg/fileservice/memorycache"
    23  )
    24  
    25  type CacheConstructor = func(r io.Reader, buf []byte, allocator fileservice.CacheDataAllocator) (memorycache.CacheData, error)
    26  type CacheConstructorFactory = func(size int64, algo uint8) CacheConstructor
    27  
    28  // use this to replace all other constructors
    29  func constructorFactory(size int64, algo uint8) CacheConstructor {
    30  	return func(reader io.Reader, data []byte, allocator fileservice.CacheDataAllocator) (cacheData memorycache.CacheData, err error) {
    31  		if len(data) == 0 {
    32  			data, err = io.ReadAll(reader)
    33  			if err != nil {
    34  				return
    35  			}
    36  		}
    37  
    38  		// no compress
    39  		if algo == compress.None {
    40  			cacheData = allocator.Alloc(len(data))
    41  			copy(cacheData.Bytes(), data)
    42  			return cacheData, nil
    43  		}
    44  
    45  		// lz4 compress
    46  		decompressed := allocator.Alloc(int(size))
    47  		bs, err := compress.Decompress(data, decompressed.Bytes(), compress.Lz4)
    48  		if err != nil {
    49  			return
    50  		}
    51  		decompressed = decompressed.Slice(len(bs))
    52  		return decompressed, nil
    53  	}
    54  }
    55  
    56  func Decode(buf []byte) (any, error) {
    57  	header := DecodeIOEntryHeader(buf)
    58  	codec := GetIOEntryCodec(*header)
    59  	if codec.NoUnmarshal() {
    60  		return buf[IOEntryHeaderSize:], nil
    61  	}
    62  	v, err := codec.Decode(buf[IOEntryHeaderSize:])
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	return v, nil
    67  }