github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/model/pages.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 "fmt" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/logutil" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 25 ) 26 27 type HashPageTable = TransferTable[*TransferHashPage] 28 29 type TransferHashPage struct { 30 common.RefHelper 31 bornTS time.Time 32 id *common.ID // not include blk offset 33 hashmap map[uint32]types.Rowid 34 isTransient bool 35 } 36 37 func NewTransferHashPage(id *common.ID, ts time.Time, isTransient bool) *TransferHashPage { 38 page := &TransferHashPage{ 39 bornTS: ts, 40 id: id, 41 hashmap: make(map[uint32]types.Rowid), 42 isTransient: isTransient, 43 } 44 page.OnZeroCB = page.Close 45 return page 46 } 47 48 func (page *TransferHashPage) ID() *common.ID { return page.id } 49 func (page *TransferHashPage) BornTS() time.Time { return page.bornTS } 50 51 func (page *TransferHashPage) TTL(now time.Time, ttl time.Duration) bool { 52 if page.isTransient { 53 ttl /= 2 54 } 55 return now.After(page.bornTS.Add(ttl)) 56 } 57 58 func (page *TransferHashPage) Close() { 59 logutil.Debugf("Closing %s", page.String()) 60 page.hashmap = make(map[uint32]types.Rowid) 61 } 62 63 func (page *TransferHashPage) Length() int { 64 return len(page.hashmap) 65 } 66 67 func (page *TransferHashPage) String() string { 68 var w bytes.Buffer 69 _, _ = w.WriteString(fmt.Sprintf("hashpage[%s][%s][Len=%d]", 70 page.id.BlockString(), 71 page.bornTS.String(), 72 len(page.hashmap))) 73 return w.String() 74 } 75 76 func (page *TransferHashPage) Pin() *common.PinnedItem[*TransferHashPage] { 77 page.Ref() 78 return &common.PinnedItem[*TransferHashPage]{ 79 Val: page, 80 } 81 } 82 83 func (page *TransferHashPage) Train(from uint32, to types.Rowid) { 84 page.hashmap[from] = to 85 } 86 87 func (page *TransferHashPage) Transfer(from uint32) (dest types.Rowid, ok bool) { 88 dest, ok = page.hashmap[from] 89 return 90 }