github.com/matrixorigin/matrixone@v1.2.0/pkg/frontend/plan_cache.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 frontend
    16  
    17  import (
    18  	"container/list"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    21  	"github.com/matrixorigin/matrixone/pkg/sql/plan"
    22  )
    23  
    24  type cachedPlan struct {
    25  	sql   string
    26  	stmts []tree.Statement
    27  	plans []*plan.Plan
    28  }
    29  
    30  // planCache uses LRU to cache plan for the same sql
    31  type planCache struct {
    32  	capacity  int
    33  	lruList   *list.List
    34  	cachePool map[string]*list.Element
    35  }
    36  
    37  func newPlanCache(capacity int) *planCache {
    38  	return &planCache{
    39  		capacity: capacity,
    40  	}
    41  }
    42  
    43  func (pc *planCache) cache(sql string, stmts []tree.Statement, plans []*plan.Plan) {
    44  	if pc.cachePool == nil {
    45  		pc.cachePool = make(map[string]*list.Element)
    46  		pc.lruList = list.New()
    47  	}
    48  	for i := range stmts {
    49  		if plans[i] == nil {
    50  			// can not cache and clean all stmts
    51  			for _, stmt := range stmts {
    52  				if stmt != nil {
    53  					stmt.Free()
    54  				}
    55  			}
    56  			return
    57  		}
    58  	}
    59  	element := pc.lruList.PushFront(&cachedPlan{sql: sql, stmts: stmts, plans: plans})
    60  	pc.cachePool[sql] = element
    61  	if pc.lruList.Len() > pc.capacity {
    62  		toRemove := pc.lruList.Back()
    63  		toRemoveStmts := toRemove.Value.(*cachedPlan).stmts
    64  		for _, stmt := range toRemoveStmts {
    65  			stmt.Free()
    66  		}
    67  
    68  		pc.lruList.Remove(toRemove)
    69  		delete(pc.cachePool, toRemove.Value.(*cachedPlan).sql)
    70  	}
    71  }
    72  
    73  // get gets a cached plan by its sql
    74  func (pc *planCache) get(sql string) *cachedPlan {
    75  	if pc.cachePool == nil {
    76  		return nil
    77  	}
    78  	if element, ok := pc.cachePool[sql]; ok {
    79  		pc.lruList.MoveToFront(element)
    80  		cp := element.Value.(*cachedPlan)
    81  		return cp
    82  	}
    83  	return nil
    84  }
    85  
    86  func (pc *planCache) isCached(sql string) bool {
    87  	if pc.cachePool == nil {
    88  		return false
    89  	}
    90  	_, isCached := pc.cachePool[sql]
    91  	return isCached
    92  }
    93  
    94  func (pc *planCache) clean() {
    95  	if pc.lruList != nil {
    96  		for i := 0; i < pc.lruList.Len(); i++ {
    97  			toRemove := pc.lruList.Front()
    98  			toRemoveStmts := toRemove.Value.(*cachedPlan).stmts
    99  			for _, stmt := range toRemoveStmts {
   100  				stmt.Free()
   101  			}
   102  		}
   103  	}
   104  	pc.lruList = nil
   105  	pc.cachePool = nil
   106  }