github.com/matrixorigin/matrixone@v0.7.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  	"bytes"
    19  
    20  	"github.com/RoaringBitmap/roaring"
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tables/indexwrapper"
    26  )
    27  
    28  var _ NodeT = (*persistedNode)(nil)
    29  
    30  type persistedNode struct {
    31  	common.RefHelper
    32  	block   *baseBlock
    33  	pkIndex indexwrapper.Index
    34  	indexes map[int]indexwrapper.Index
    35  }
    36  
    37  func newPersistedNode(block *baseBlock) *persistedNode {
    38  	node := &persistedNode{
    39  		block: block,
    40  	}
    41  	node.OnZeroCB = node.close
    42  	if block.meta.HasPersistedData() {
    43  		node.init()
    44  	}
    45  	return node
    46  }
    47  
    48  func (node *persistedNode) close() {
    49  	for i, index := range node.indexes {
    50  		index.Close()
    51  		node.indexes[i] = nil
    52  	}
    53  	node.indexes = nil
    54  }
    55  
    56  func (node *persistedNode) init() {
    57  	node.indexes = make(map[int]indexwrapper.Index)
    58  	schema := node.block.meta.GetSchema()
    59  	pkIdx := -1
    60  	if schema.HasPK() {
    61  		pkIdx = schema.GetSingleSortKeyIdx()
    62  	}
    63  	for i := range schema.ColDefs {
    64  		index := indexwrapper.NewImmutableIndex()
    65  		if err := index.ReadFrom(
    66  			node.block.bufMgr,
    67  			node.block.fs,
    68  			node.block.meta.AsCommonID(),
    69  			node.block.meta.GetMetaLoc(),
    70  			schema.ColDefs[i]); err != nil {
    71  			panic(err)
    72  		}
    73  		node.indexes[i] = index
    74  		if i == pkIdx {
    75  			node.pkIndex = index
    76  		}
    77  	}
    78  }
    79  
    80  func (node *persistedNode) Rows() uint32 {
    81  	location := node.block.meta.GetMetaLoc()
    82  	return uint32(ReadPersistedBlockRow(location))
    83  }
    84  
    85  func (node *persistedNode) BatchDedup(
    86  	keys containers.Vector,
    87  	skipFn func(row uint32) error) (sels *roaring.Bitmap, err error) {
    88  	return node.pkIndex.BatchDedup(keys, skipFn)
    89  }
    90  
    91  func (node *persistedNode) ContainsKey(key any) (ok bool, err error) {
    92  	if err = node.pkIndex.Dedup(key, nil); err == nil {
    93  		return
    94  	}
    95  	if !moerr.IsMoErrCode(err, moerr.OkExpectedPossibleDup) {
    96  		return
    97  	}
    98  	ok = true
    99  	err = nil
   100  	return
   101  }
   102  
   103  func (node *persistedNode) GetColumnDataWindow(
   104  	from uint32,
   105  	to uint32,
   106  	colIdx int,
   107  	buffer *bytes.Buffer,
   108  ) (vec containers.Vector, err error) {
   109  	var data containers.Vector
   110  	if data, err = node.block.LoadPersistedColumnData(
   111  		colIdx,
   112  		buffer); err != nil {
   113  		return
   114  	}
   115  	if to-from == uint32(data.Length()) {
   116  		vec = data
   117  	} else {
   118  		vec = data.CloneWindow(int(from), int(to-from), common.DefaultAllocator)
   119  		data.Close()
   120  	}
   121  	return
   122  }
   123  
   124  func (node *persistedNode) GetDataWindow(
   125  	from, to uint32) (bat *containers.Batch, err error) {
   126  	data, err := node.block.LoadPersistedData()
   127  	if err != nil {
   128  		return
   129  	}
   130  
   131  	if to-from == uint32(data.Length()) {
   132  		bat = data
   133  	} else {
   134  		bat = data.CloneWindow(int(from), int(to-from), common.DefaultAllocator)
   135  		data.Close()
   136  	}
   137  	return
   138  }
   139  
   140  func (node *persistedNode) IsPersisted() bool { return true }
   141  
   142  func (node *persistedNode) PrepareAppend(rows uint32) (n uint32, err error) {
   143  	panic(moerr.NewInternalErrorNoCtx("not supported"))
   144  }
   145  
   146  func (node *persistedNode) ApplyAppend(
   147  	_ *containers.Batch,
   148  	_ txnif.AsyncTxn,
   149  ) (from int, err error) {
   150  	panic(moerr.NewInternalErrorNoCtx("not supported"))
   151  }
   152  
   153  func (node *persistedNode) GetValueByRow(row, col int) (v any) {
   154  	panic(moerr.NewInternalErrorNoCtx("todo"))
   155  }
   156  
   157  func (node *persistedNode) GetRowsByKey(key any) ([]uint32, error) {
   158  	panic(moerr.NewInternalErrorNoCtx("todo"))
   159  }