github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/tables/pnode.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/RoaringBitmap/roaring"
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	"github.com/matrixorigin/matrixone/pkg/objectio"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/compute"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    30  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/index"
    32  )
    33  
    34  var _ NodeT = (*persistedNode)(nil)
    35  
    36  type persistedNode struct {
    37  	common.RefHelper
    38  	object *baseObject
    39  }
    40  
    41  func newPersistedNode(object *baseObject) *persistedNode {
    42  	node := &persistedNode{
    43  		object: object,
    44  	}
    45  	node.OnZeroCB = node.close
    46  	return node
    47  }
    48  
    49  func (node *persistedNode) close() {}
    50  
    51  func (node *persistedNode) Rows() (uint32, error) {
    52  	stats, err := node.object.meta.MustGetObjectStats()
    53  	if err != nil {
    54  		return 0, err
    55  	}
    56  	return stats.Rows(), nil
    57  }
    58  
    59  func (node *persistedNode) BatchDedup(
    60  	ctx context.Context,
    61  	txn txnif.TxnReader,
    62  	precommit bool,
    63  	keys containers.Vector,
    64  	keysZM index.ZM,
    65  	rowmask *roaring.Bitmap,
    66  	bf objectio.BloomFilter,
    67  ) (err error) {
    68  	panic("should not be called")
    69  }
    70  
    71  func (node *persistedNode) ContainsKey(ctx context.Context, key any, blkID uint32) (ok bool, err error) {
    72  	pkIndex, err := MakeImmuIndex(ctx, node.object.meta, nil, node.object.rt)
    73  	if err != nil {
    74  		return
    75  	}
    76  	if err = pkIndex.Dedup(ctx, key, node.object.rt, blkID); err == nil {
    77  		return
    78  	}
    79  	if !moerr.IsMoErrCode(err, moerr.OkExpectedPossibleDup) {
    80  		return
    81  	}
    82  	ok = true
    83  	err = nil
    84  	return
    85  }
    86  
    87  func (node *persistedNode) Foreach(
    88  	ctx context.Context,
    89  	readSchema *catalog.Schema,
    90  	blkID uint16,
    91  	colIdx int,
    92  	op func(v any, isNull bool, row int) error,
    93  	sel []uint32,
    94  	mp *mpool.MPool,
    95  ) (err error) {
    96  	var data containers.Vector
    97  	if data, err = node.object.LoadPersistedColumnData(
    98  		ctx,
    99  		readSchema,
   100  		colIdx,
   101  		mp,
   102  		blkID,
   103  	); err != nil {
   104  		return
   105  	}
   106  	defer data.Close()
   107  	for _, row := range sel {
   108  		val := data.Get(int(row))
   109  		isNull := data.IsNull(int(row))
   110  		err := op(val, isNull, int(row))
   111  		if err != nil {
   112  			return err
   113  		}
   114  	}
   115  	return nil
   116  }
   117  
   118  func (node *persistedNode) GetDataWindow(
   119  	readSchema *catalog.Schema, colIdxes []int, from, to uint32, mp *mpool.MPool,
   120  ) (bat *containers.Batch, err error) {
   121  	panic("to be implemented")
   122  }
   123  
   124  func (node *persistedNode) IsPersisted() bool { return true }
   125  
   126  func (node *persistedNode) PrepareAppend(rows uint32) (n uint32, err error) {
   127  	panic(moerr.NewInternalErrorNoCtx("not supported"))
   128  }
   129  
   130  func (node *persistedNode) ApplyAppend(
   131  	_ *containers.Batch,
   132  	_ txnif.AsyncTxn,
   133  ) (from int, err error) {
   134  	panic(moerr.NewInternalErrorNoCtx("not supported"))
   135  }
   136  
   137  func (node *persistedNode) GetValueByRow(_ *catalog.Schema, _, _ int) (v any, isNull bool) {
   138  	panic(moerr.NewInternalErrorNoCtx("todo"))
   139  }
   140  
   141  func (node *persistedNode) GetRowsByKey(key any) ([]uint32, error) {
   142  	panic(moerr.NewInternalErrorNoCtx("todo"))
   143  }
   144  
   145  // only used by tae only
   146  // not to optimize it
   147  func (node *persistedNode) GetRowByFilter(
   148  	ctx context.Context,
   149  	txn txnif.TxnReader,
   150  	filter *handle.Filter,
   151  	mp *mpool.MPool,
   152  ) (blkID uint16, row uint32, err error) {
   153  	for blkID = uint16(0); blkID < uint16(node.object.meta.BlockCnt()); blkID++ {
   154  		var ok bool
   155  		ok, err = node.ContainsKey(ctx, filter.Val, uint32(blkID))
   156  		if err != nil {
   157  			return
   158  		}
   159  		if !ok {
   160  			continue
   161  		}
   162  		// Note: sort key do not change
   163  		schema := node.object.meta.GetSchema()
   164  		var sortKey containers.Vector
   165  		sortKey, err = node.object.LoadPersistedColumnData(ctx, schema, schema.GetSingleSortKeyIdx(), mp, blkID)
   166  		if err != nil {
   167  			return
   168  		}
   169  		defer sortKey.Close()
   170  		rows := make([]uint32, 0)
   171  		err = sortKey.Foreach(func(v any, _ bool, offset int) error {
   172  			if compute.CompareGeneric(v, filter.Val, sortKey.GetType().Oid) == 0 {
   173  				row := uint32(offset)
   174  				rows = append(rows, row)
   175  				return nil
   176  			}
   177  			return nil
   178  		}, nil)
   179  		if err != nil && !moerr.IsMoErrCode(err, moerr.OkExpectedDup) {
   180  			return
   181  		}
   182  		if len(rows) == 0 {
   183  			continue
   184  		}
   185  
   186  		// Load persisted commit ts
   187  		var commitTSVec containers.Vector
   188  		commitTSVec, err = node.object.LoadPersistedCommitTS(blkID)
   189  		if err != nil {
   190  			return
   191  		}
   192  		defer commitTSVec.Close()
   193  
   194  		// Load persisted deletes
   195  		view := containers.NewColumnView(0)
   196  		if err = node.object.FillPersistedDeletes(ctx, blkID, txn, view.BaseView, mp); err != nil {
   197  			return
   198  		}
   199  
   200  		exist := false
   201  		var deleted bool
   202  		for _, offset := range rows {
   203  			commitTS := commitTSVec.Get(int(offset)).(types.TS)
   204  			startTS := txn.GetStartTS()
   205  			if commitTS.Greater(&startTS) {
   206  				break
   207  			}
   208  			deleted = view.IsDeleted(int(offset))
   209  			if !deleted {
   210  				exist = true
   211  				row = offset
   212  				break
   213  			}
   214  		}
   215  		if exist {
   216  			return
   217  		}
   218  	}
   219  	err = moerr.NewNotFoundNoCtx()
   220  	return
   221  }
   222  
   223  func (node *persistedNode) CollectAppendInRange(
   224  	start, end types.TS, withAborted bool, mp *mpool.MPool,
   225  ) (bat *containers.BatchWithVersion, err error) {
   226  	// logtail should have sent metaloc
   227  	return nil, nil
   228  }