github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/buffer/evict.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 buffer
    16  
    17  import (
    18  	"fmt"
    19  	"sync"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/buffer/base"
    22  	sq "github.com/yireyun/go-queue"
    23  )
    24  
    25  type EvictNode struct {
    26  	Handle base.IEvictHandle
    27  	Iter   uint64
    28  }
    29  
    30  type IEvictHolder interface {
    31  	sync.Locker
    32  	Enqueue(n *EvictNode)
    33  	Dequeue() *EvictNode
    34  }
    35  
    36  type SimpleEvictHolder struct {
    37  	Queue *sq.EsQueue
    38  	sync.Mutex
    39  }
    40  
    41  const (
    42  	EVICT_HOLDER_CAPACITY uint64 = 100000
    43  )
    44  
    45  type SimpleEvictHolderCtx struct {
    46  	QCapacity uint64
    47  }
    48  
    49  func NewSimpleEvictHolder(ctx ...any) IEvictHolder {
    50  	c := EVICT_HOLDER_CAPACITY
    51  	if len(ctx) > 0 {
    52  		context := ctx[0].(*SimpleEvictHolderCtx)
    53  		if context != nil {
    54  			c = context.QCapacity
    55  		}
    56  	}
    57  	holder := &SimpleEvictHolder{
    58  		Queue: sq.NewQueue(uint32(c)),
    59  	}
    60  	return holder
    61  }
    62  
    63  func (holder *SimpleEvictHolder) Enqueue(node *EvictNode) {
    64  	holder.Queue.Put(node)
    65  }
    66  
    67  func (holder *SimpleEvictHolder) Dequeue() *EvictNode {
    68  	r, ok, _ := holder.Queue.Get()
    69  	if !ok {
    70  		return nil
    71  	}
    72  	return r.(*EvictNode)
    73  }
    74  
    75  func (node *EvictNode) String() string {
    76  	return fmt.Sprintf("EvictNode(%v, %d)", node.Handle, node.Iter)
    77  }
    78  
    79  func (node *EvictNode) Unloadable(h base.IEvictHandle) bool {
    80  	if node.Handle != h {
    81  		panic("Logic error")
    82  	}
    83  	return h.Iteration() == node.Iter
    84  }