github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/model/transferdels.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 model
    16  
    17  import (
    18  	"bytes"
    19  	"strconv"
    20  	"sync"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  )
    25  
    26  type TransDels struct {
    27  	// any txn starts after this ts won't check this table
    28  	noUseAfter types.TS
    29  	bornTime   time.Time
    30  	// row -> commit ts
    31  	Mapping map[int]types.TS
    32  }
    33  
    34  func NewTransDels(endts types.TS) *TransDels {
    35  	return &TransDels{
    36  		noUseAfter: endts,
    37  		bornTime:   time.Now(),
    38  		Mapping:    make(map[int]types.TS),
    39  	}
    40  }
    41  
    42  func (t *TransDels) PPString() string {
    43  	var w bytes.Buffer
    44  	for row, ts := range t.Mapping {
    45  		_, _ = w.WriteString(strconv.Itoa(row))
    46  		_, _ = w.WriteString(":")
    47  		_, _ = w.WriteString(ts.ToString())
    48  		_, _ = w.WriteString(", ")
    49  	}
    50  	return w.String()
    51  }
    52  
    53  type TransDelsForBlks struct {
    54  	sync.RWMutex
    55  	dels map[types.Blockid]*TransDels
    56  }
    57  
    58  func NewTransDelsForBlks() *TransDelsForBlks {
    59  	return &TransDelsForBlks{
    60  		dels: make(map[types.Blockid]*TransDels),
    61  	}
    62  }
    63  
    64  func (t *TransDelsForBlks) GetDelsForBlk(blkid types.Blockid) *TransDels {
    65  	t.RLock()
    66  	defer t.RUnlock()
    67  	return t.dels[blkid]
    68  }
    69  
    70  func (t *TransDelsForBlks) SetDelsForBlk(blkid types.Blockid, dels *TransDels) {
    71  	t.Lock()
    72  	defer t.Unlock()
    73  	t.dels[blkid] = dels
    74  }
    75  
    76  func (t *TransDelsForBlks) Prune(gap time.Duration) {
    77  	t.Lock()
    78  	defer t.Unlock()
    79  	for blkid, del := range t.dels {
    80  		if time.Since(del.bornTime) > gap {
    81  			delete(t.dels, blkid)
    82  		}
    83  	}
    84  }