github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/blockio/funcs.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 blockio
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    23  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    24  	"github.com/matrixorigin/matrixone/pkg/container/types"
    25  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    26  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    27  	"github.com/matrixorigin/matrixone/pkg/objectio"
    28  )
    29  
    30  func LoadColumnsData(
    31  	ctx context.Context,
    32  	metaType objectio.DataMetaType,
    33  	cols []uint16,
    34  	typs []types.Type,
    35  	fs fileservice.FileService,
    36  	location objectio.Location,
    37  	m *mpool.MPool,
    38  	policy fileservice.Policy,
    39  ) (bat *batch.Batch, release func(), err error) {
    40  	name := location.Name()
    41  	var meta objectio.ObjectMeta
    42  	var ioVectors *fileservice.IOVector
    43  	if meta, err = objectio.FastLoadObjectMeta(ctx, &location, false, fs); err != nil {
    44  		return
    45  	}
    46  	dataMeta := meta.MustGetMeta(metaType)
    47  	if ioVectors, err = objectio.ReadOneBlock(ctx, &dataMeta, name.String(), location.ID(), cols, typs, m, fs, policy); err != nil {
    48  		return
    49  	}
    50  	release = func() {
    51  		objectio.ReleaseIOVector(ioVectors)
    52  	}
    53  	bat = batch.NewWithSize(len(cols))
    54  	var obj any
    55  	for i := range cols {
    56  		obj, err = objectio.Decode(ioVectors.Entries[i].CachedData.Bytes())
    57  		if err != nil {
    58  			return
    59  		}
    60  		bat.Vecs[i] = obj.(*vector.Vector)
    61  		bat.SetRowCount(bat.Vecs[i].Length())
    62  	}
    63  	//TODO call CachedData.Release
    64  	return
    65  }
    66  
    67  func LoadColumnsData2(
    68  	ctx context.Context,
    69  	metaType objectio.DataMetaType,
    70  	cols []uint16,
    71  	typs []types.Type,
    72  	fs fileservice.FileService,
    73  	location objectio.Location,
    74  	policy fileservice.Policy,
    75  	needCopy bool,
    76  	vPool *containers.VectorPool,
    77  ) (vectors []containers.Vector, release func(), err error) {
    78  	name := location.Name()
    79  	var meta objectio.ObjectMeta
    80  	var ioVectors *fileservice.IOVector
    81  	if meta, err = objectio.FastLoadObjectMeta(ctx, &location, false, fs); err != nil {
    82  		return
    83  	}
    84  	dataMeta := meta.MustGetMeta(metaType)
    85  	if ioVectors, err = objectio.ReadOneBlock(ctx, &dataMeta, name.String(), location.ID(), cols, typs, nil, fs, policy); err != nil {
    86  		return
    87  	}
    88  	defer func() {
    89  		if needCopy {
    90  			objectio.ReleaseIOVector(ioVectors)
    91  			return
    92  		}
    93  		release = func() {
    94  			objectio.ReleaseIOVector(ioVectors)
    95  		}
    96  	}()
    97  	var obj any
    98  	vectors = make([]containers.Vector, len(cols))
    99  	for i := range cols {
   100  		obj, err = objectio.Decode(ioVectors.Entries[i].CachedData.Bytes())
   101  		if err != nil {
   102  			return
   103  		}
   104  
   105  		var vec containers.Vector
   106  		if needCopy {
   107  			if vec, err = containers.CloneVector(
   108  				obj.(*vector.Vector),
   109  				vPool.GetMPool(),
   110  				vPool,
   111  			); err != nil {
   112  				return
   113  			}
   114  		} else {
   115  			vec = containers.ToTNVector(obj.(*vector.Vector), nil)
   116  		}
   117  		vectors[i] = vec
   118  	}
   119  	if err != nil {
   120  		for _, col := range vectors {
   121  			if col != nil {
   122  				col.Close()
   123  			}
   124  		}
   125  		return nil, release, err
   126  	}
   127  	return
   128  }
   129  
   130  func LoadColumns(
   131  	ctx context.Context,
   132  	cols []uint16,
   133  	typs []types.Type,
   134  	fs fileservice.FileService,
   135  	location objectio.Location,
   136  	m *mpool.MPool,
   137  	policy fileservice.Policy,
   138  ) (bat *batch.Batch, release func(), err error) {
   139  	return LoadColumnsData(ctx, objectio.SchemaData, cols, typs, fs, location, m, policy)
   140  }
   141  
   142  func LoadTombstoneColumns(
   143  	ctx context.Context,
   144  	cols []uint16,
   145  	typs []types.Type,
   146  	fs fileservice.FileService,
   147  	location objectio.Location,
   148  	m *mpool.MPool,
   149  ) (bat *batch.Batch, release func(), err error) {
   150  	return LoadColumnsData(ctx, objectio.SchemaTombstone, cols, typs, fs, location, m, fileservice.Policy(0))
   151  }
   152  
   153  // LoadColumns2 load columns data from file service for TN
   154  // need to copy data from vPool to avoid releasing cache
   155  func LoadColumns2(
   156  	ctx context.Context,
   157  	cols []uint16,
   158  	typs []types.Type,
   159  	fs fileservice.FileService,
   160  	location objectio.Location,
   161  	policy fileservice.Policy,
   162  	needCopy bool,
   163  	vPool *containers.VectorPool,
   164  ) (vectors []containers.Vector, release func(), err error) {
   165  	return LoadColumnsData2(ctx, objectio.SchemaData, cols, typs, fs, location, policy, needCopy, vPool)
   166  }
   167  
   168  // LoadTombstoneColumns2 load tombstone data from file service for TN
   169  // need to copy data from vPool to avoid releasing cache
   170  func LoadTombstoneColumns2(
   171  	ctx context.Context,
   172  	cols []uint16,
   173  	typs []types.Type,
   174  	fs fileservice.FileService,
   175  	location objectio.Location,
   176  	needCopy bool,
   177  	vPool *containers.VectorPool,
   178  ) (vectors []containers.Vector, release func(), err error) {
   179  	return LoadColumnsData2(ctx, objectio.SchemaTombstone, cols, typs, fs, location, fileservice.Policy(0), needCopy, vPool)
   180  }
   181  
   182  func LoadOneBlock(
   183  	ctx context.Context,
   184  	fs fileservice.FileService,
   185  	key objectio.Location,
   186  	metaType objectio.DataMetaType,
   187  ) (*batch.Batch, error) {
   188  	meta, err := objectio.FastLoadObjectMeta(ctx, &key, false, fs)
   189  	if err != nil {
   190  		return nil, err
   191  	}
   192  	data := meta.MustGetMeta(metaType)
   193  
   194  	idxes := make([]uint16, data.BlockHeader().ColumnCount())
   195  	for i := range idxes {
   196  		idxes[i] = uint16(i)
   197  	}
   198  	bat, err := objectio.ReadOneBlockAllColumns(ctx, &data, key.Name().String(),
   199  		uint32(key.ID()), idxes, fileservice.SkipAllCache, fs)
   200  	return bat, err
   201  }