github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tables/utils.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 tables
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/fileservice"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db/dbutils"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index/indexwrapper"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/model"
    26  
    27  	"github.com/matrixorigin/matrixone/pkg/objectio"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/blockio"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    30  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    32  )
    33  
    34  func LoadPersistedColumnData(
    35  	ctx context.Context,
    36  	rt *dbutils.Runtime,
    37  	id *common.ID,
    38  	def *catalog.ColDef,
    39  	location objectio.Location,
    40  	mp *mpool.MPool,
    41  ) (vec containers.Vector, err error) {
    42  	if def.IsPhyAddr() {
    43  		return model.PreparePhyAddrData(&id.BlockID, 0, location.Rows(), rt.VectorPool.Transient)
    44  	}
    45  	//Extend lifetime of vectors is without the function.
    46  	//need to copy. closeFunc will be nil.
    47  	vectors, _, err := blockio.LoadColumns2(
    48  		ctx, []uint16{uint16(def.SeqNum)},
    49  		[]types.Type{def.Type},
    50  		rt.Fs.Service,
    51  		location,
    52  		fileservice.Policy(0),
    53  		true,
    54  		rt.VectorPool.Transient)
    55  	if err != nil {
    56  		return
    57  	}
    58  	return vectors[0], nil
    59  }
    60  
    61  func LoadPersistedColumnDatas(
    62  	ctx context.Context,
    63  	schema *catalog.Schema,
    64  	rt *dbutils.Runtime,
    65  	id *common.ID,
    66  	colIdxs []int,
    67  	location objectio.Location,
    68  	mp *mpool.MPool,
    69  ) ([]containers.Vector, error) {
    70  	cols := make([]uint16, 0)
    71  	typs := make([]types.Type, 0)
    72  	vectors := make([]containers.Vector, len(colIdxs))
    73  	phyAddIdx := -1
    74  	for i, colIdx := range colIdxs {
    75  		def := schema.ColDefs[colIdx]
    76  		if def.IsPhyAddr() {
    77  			vec, err := model.PreparePhyAddrData(&id.BlockID, 0, location.Rows(), rt.VectorPool.Transient)
    78  			if err != nil {
    79  				return nil, err
    80  			}
    81  			phyAddIdx = i
    82  			vectors[phyAddIdx] = vec
    83  			continue
    84  		}
    85  		cols = append(cols, def.SeqNum)
    86  		typs = append(typs, def.Type)
    87  	}
    88  	if len(cols) == 0 {
    89  		return vectors, nil
    90  	}
    91  	//Extend lifetime of vectors is without the function.
    92  	//need to copy. closeFunc will be nil.
    93  	vecs, _, err := blockio.LoadColumns2(
    94  		ctx, cols,
    95  		typs,
    96  		rt.Fs.Service,
    97  		location,
    98  		fileservice.Policy(0),
    99  		true,
   100  		rt.VectorPool.Transient)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  	for i, vec := range vecs {
   105  		idx := i
   106  		if idx >= phyAddIdx && phyAddIdx > -1 {
   107  			idx++
   108  		}
   109  		vectors[idx] = vec
   110  	}
   111  	return vectors, nil
   112  }
   113  
   114  func ReadPersistedBlockRow(location objectio.Location) int {
   115  	return int(location.Rows())
   116  }
   117  
   118  func LoadPersistedDeletes(
   119  	ctx context.Context,
   120  	pkName string,
   121  	fs *objectio.ObjectFS,
   122  	location objectio.Location,
   123  	mp *mpool.MPool,
   124  ) (bat *containers.Batch, isPersistedByCN bool, release func(), err error) {
   125  	if isPersistedByCN, err = blockio.IsPersistedByCN(ctx, location, fs.Service); err != nil {
   126  		return
   127  	}
   128  	bat, release, err = LoadPersistedDeletesBySchema(ctx, pkName, fs, location, isPersistedByCN, mp)
   129  	return
   130  }
   131  
   132  func LoadPersistedDeletesBySchema(
   133  	ctx context.Context,
   134  	pkName string,
   135  	fs *objectio.ObjectFS,
   136  	location objectio.Location,
   137  	isPersistedByCN bool,
   138  	mp *mpool.MPool,
   139  ) (bat *containers.Batch, release func(), err error) {
   140  	movbat, release, err := blockio.ReadBlockDeleteBySchema(ctx, location, fs.Service, isPersistedByCN)
   141  	if err != nil {
   142  		return
   143  	}
   144  	bat = containers.NewBatch()
   145  	if isPersistedByCN {
   146  		colNames := []string{catalog.PhyAddrColumnName, catalog.AttrPKVal}
   147  		for i := 0; i < 2; i++ {
   148  			bat.AddVector(colNames[i], containers.ToTNVector(movbat.Vecs[i], mp))
   149  		}
   150  	} else {
   151  		colNames := []string{catalog.PhyAddrColumnName, catalog.AttrCommitTs, catalog.AttrPKVal, catalog.AttrAborted}
   152  		for i := 0; i < 4; i++ {
   153  			bat.AddVector(colNames[i], containers.ToTNVector(movbat.Vecs[i], mp))
   154  		}
   155  	}
   156  	return
   157  }
   158  
   159  // func MakeBFLoader(
   160  // 	meta *catalog.BlockEntry,
   161  // 	bf objectio.BloomFilter,
   162  // 	cache model.LRUCache,
   163  // 	fs fileservice.FileService,
   164  // ) indexwrapper.Loader {
   165  // 	return func(ctx context.Context) ([]byte, error) {
   166  // 		location := meta.GetMetaLoc()
   167  // 		var err error
   168  // 		if len(bf) == 0 {
   169  // 			if bf, err = LoadBF(ctx, location, cache, fs, false); err != nil {
   170  // 				return nil, err
   171  // 			}
   172  // 		}
   173  // 		return bf.GetBloomFilter(uint32(location.ID())), nil
   174  // 	}
   175  // }
   176  
   177  func MakeImmuIndex(
   178  	ctx context.Context,
   179  	meta *catalog.ObjectEntry,
   180  	bf objectio.BloomFilter,
   181  	rt *dbutils.Runtime,
   182  ) (idx indexwrapper.ImmutIndex, err error) {
   183  	stats, err := meta.MustGetObjectStats()
   184  	if err != nil {
   185  		return
   186  	}
   187  	idx = indexwrapper.NewImmutIndex(
   188  		stats.SortKeyZoneMap(), bf, stats.ObjectLocation(),
   189  	)
   190  	return
   191  }