github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/common/file.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 common
    16  
    17  import "io"
    18  
    19  type FileType uint8
    20  
    21  const (
    22  	InvalidFile FileType = iota
    23  	MemFile
    24  	DiskFile
    25  )
    26  
    27  // FileInfo contains the basic info for a file.
    28  type FileInfo interface {
    29  	Name() string
    30  	Size() int64
    31  	OriginSize() int64
    32  	CompressAlgo() int
    33  }
    34  
    35  // IVFile is the general in-memory representation of resources like
    36  // segment, block, index, column part, etc. that managed by buffer
    37  // manager.
    38  type IVFile interface {
    39  	io.Reader
    40  	Ref()
    41  	Unref()
    42  	RefCount() int64
    43  	Stat() FileInfo
    44  	GetFileType() FileType
    45  }
    46  
    47  type IRWFile interface {
    48  	io.Writer
    49  	IVFile
    50  }
    51  
    52  func MockRWFile() *rwFile {
    53  	return &rwFile{
    54  		stat: baseFileInfo{},
    55  		data: make([]byte, 0),
    56  	}
    57  }
    58  
    59  type rwFile struct {
    60  	stat baseFileInfo
    61  	data []byte
    62  }
    63  
    64  func (f *rwFile) Write(p []byte) (n int, err error) {
    65  	n = len(p)
    66  	f.data = append(f.data, p...)
    67  	f.stat.size += int64(n)
    68  	return
    69  }
    70  
    71  func (f *rwFile) Read(p []byte) (n int, err error) {
    72  	n = len(p)
    73  	copy(p, f.data[:n])
    74  	return
    75  }
    76  
    77  func (f *rwFile) Ref()                  {}
    78  func (f *rwFile) Unref()                {}
    79  func (f *rwFile) RefCount() int64       { return 0 }
    80  func (f *rwFile) Stat() FileInfo        { return &f.stat }
    81  func (f *rwFile) GetFileType() FileType { return DiskFile }
    82  
    83  type baseFileInfo struct {
    84  	size int64
    85  }
    86  
    87  func (i *baseFileInfo) Name() string      { return "" }
    88  func (i *baseFileInfo) Size() int64       { return i.size }
    89  func (i *baseFileInfo) OriginSize() int64 { return i.size }
    90  func (i *baseFileInfo) CompressAlgo() int { return 0 }
    91  
    92  type compressedFileInfo struct {
    93  	size  int64
    94  	osize int64
    95  }
    96  
    97  func (i *compressedFileInfo) Name() string      { return "" }
    98  func (i *compressedFileInfo) Size() int64       { return i.size }
    99  func (i *compressedFileInfo) OriginSize() int64 { return i.osize }
   100  func (i *compressedFileInfo) CompressAlgo() int { return 1 }
   101  
   102  // baseMemFile is an abstraction of some pure in-memory resources.
   103  // It belongs to IVFile family.
   104  type baseMemFile struct {
   105  	stat baseFileInfo
   106  }
   107  
   108  func NewMemFile(size int64) IVFile {
   109  	return &baseMemFile{
   110  		stat: baseFileInfo{
   111  			size: size,
   112  		},
   113  	}
   114  }
   115  
   116  func (f *baseMemFile) Ref()                             {}
   117  func (f *baseMemFile) Unref()                           {}
   118  func (f *baseMemFile) RefCount() int64                  { return 0 }
   119  func (f *baseMemFile) Read(p []byte) (n int, err error) { return n, err }
   120  func (f *baseMemFile) Stat() FileInfo                   { return &f.stat }
   121  func (f *baseMemFile) GetFileType() FileType            { return MemFile }
   122  
   123  type mockCompressedFile struct {
   124  	stat compressedFileInfo
   125  }
   126  
   127  func (f *mockCompressedFile) Read(p []byte) (n int, err error) {
   128  	return n, err
   129  }
   130  
   131  func (f *mockCompressedFile) Ref() {
   132  }
   133  
   134  func (f *mockCompressedFile) Unref() {
   135  }
   136  
   137  func (f *mockCompressedFile) RefCount() int64 {
   138  	return 0
   139  }
   140  
   141  func (f *mockCompressedFile) Stat() FileInfo {
   142  	return &f.stat
   143  }
   144  
   145  func (f *mockCompressedFile) GetFileType() FileType {
   146  	return MemFile
   147  }
   148  
   149  func MockCompressedFile(size int64, osize int64) IVFile {
   150  	return &mockCompressedFile{stat: compressedFileInfo{
   151  		size:  size,
   152  		osize: osize,
   153  	}}
   154  }