github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tables/txnentries/compactblk.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 txnentries
    16  
    17  import (
    18  	"sync"
    19  	"time"
    20  
    21  	"github.com/RoaringBitmap/roaring"
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/compute"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    29  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/model"
    30  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/wal"
    32  )
    33  
    34  type compactBlockEntry struct {
    35  	sync.RWMutex
    36  	txn       txnif.AsyncTxn
    37  	from      handle.Block
    38  	to        handle.Block
    39  	scheduler tasks.TaskScheduler
    40  	deletes   *roaring.Bitmap
    41  }
    42  
    43  func NewCompactBlockEntry(
    44  	txn txnif.AsyncTxn,
    45  	from, to handle.Block,
    46  	scheduler tasks.TaskScheduler,
    47  	sortIdx []uint32,
    48  	deletes *roaring.Bitmap) *compactBlockEntry {
    49  
    50  	page := model.NewTransferHashPage(from.Fingerprint(), time.Now())
    51  	if to != nil {
    52  		toId := to.Fingerprint()
    53  		prefix := model.EncodeBlockKeyPrefix(toId.SegmentID, toId.BlockID)
    54  		offsetMapping := compute.GetOffsetMapBeforeApplyDeletes(deletes)
    55  		if deletes != nil && !deletes.IsEmpty() {
    56  			delCnt := deletes.GetCardinality()
    57  			for i, idx := range sortIdx {
    58  				if int(idx) < len(offsetMapping) {
    59  					sortIdx[i] = offsetMapping[idx]
    60  				} else {
    61  					sortIdx[i] = idx + uint32(delCnt)
    62  				}
    63  			}
    64  		}
    65  		for i, idx := range sortIdx {
    66  			rowid := model.EncodePhyAddrKeyWithPrefix(prefix, uint32(i))
    67  			page.Train(idx, rowid)
    68  		}
    69  		_ = scheduler.AddTransferPage(page)
    70  	}
    71  	return &compactBlockEntry{
    72  		txn:       txn,
    73  		from:      from,
    74  		to:        to,
    75  		scheduler: scheduler,
    76  		deletes:   deletes,
    77  	}
    78  }
    79  
    80  func (entry *compactBlockEntry) IsAborted() bool { return false }
    81  func (entry *compactBlockEntry) PrepareRollback() (err error) {
    82  	// TODO: remove block file? (should be scheduled and executed async)
    83  	_ = entry.scheduler.DeleteTransferPage(entry.from.Fingerprint())
    84  	return
    85  }
    86  func (entry *compactBlockEntry) ApplyRollback(index *wal.Index) (err error) {
    87  	//TODO:?
    88  	return
    89  }
    90  func (entry *compactBlockEntry) ApplyCommit(index *wal.Index) (err error) {
    91  	_ = entry.from.GetMeta().(*catalog.BlockEntry).GetBlockData().TryUpgrade()
    92  	return
    93  }
    94  
    95  func (entry *compactBlockEntry) MakeCommand(csn uint32) (cmd txnif.TxnCmd, err error) {
    96  	to := &common.ID{}
    97  	if entry.to != nil {
    98  		to = entry.to.Fingerprint()
    99  	}
   100  	cmd = newCompactBlockCmd((*common.ID)(entry.from.Fingerprint()), to, entry.txn, csn)
   101  	return
   102  }
   103  
   104  func (entry *compactBlockEntry) Set1PC()     {}
   105  func (entry *compactBlockEntry) Is1PC() bool { return false }
   106  func (entry *compactBlockEntry) PrepareCommit() (err error) {
   107  	dataBlock := entry.from.GetMeta().(*catalog.BlockEntry).GetBlockData()
   108  	if dataBlock.HasDeleteIntentsPreparedIn(entry.txn.GetStartTS(), types.MaxTs()) {
   109  		err = moerr.NewTxnWWConflictNoCtx()
   110  	}
   111  	return
   112  }