github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/cache/lrucache/entry.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 lrucache
    16  
    17  type entryType int8
    18  
    19  const (
    20  	etTest entryType = iota
    21  	etCold
    22  	etHot
    23  )
    24  
    25  func (p entryType) String() string {
    26  	switch p {
    27  	case etTest:
    28  		return "test"
    29  	case etCold:
    30  		return "cold"
    31  	case etHot:
    32  		return "hot"
    33  	}
    34  	return "unknown"
    35  }
    36  
    37  type entry struct {
    38  	key       key
    39  	val       *Value
    40  	blockLink struct {
    41  		next *entry
    42  		prev *entry
    43  	}
    44  	size       int64
    45  	ptype      entryType
    46  	referenced int32
    47  	shard      *shard
    48  	ref        refcnt
    49  }
    50  
    51  func newEntry(s *shard, key key, size int64) *entry {
    52  	e := entryAllocNew()
    53  	*e = entry{
    54  		key:   key,
    55  		size:  size,
    56  		ptype: etCold,
    57  		shard: s,
    58  	}
    59  	e.blockLink.next = e
    60  	e.blockLink.prev = e
    61  	e.ref.init(1)
    62  	return e
    63  }
    64  
    65  func (e *entry) free() {
    66  	e.setValue(nil)
    67  	*e = entry{}
    68  	entryAllocFree(e)
    69  }
    70  
    71  func (e *entry) next() *entry {
    72  	if e == nil {
    73  		return nil
    74  	}
    75  	return e.blockLink.next
    76  }
    77  
    78  func (e *entry) prev() *entry {
    79  	if e == nil {
    80  		return nil
    81  	}
    82  	return e.blockLink.prev
    83  }
    84  
    85  func (e *entry) link(s *entry) {
    86  	s.blockLink.prev = e.blockLink.prev
    87  	s.blockLink.prev.blockLink.next = s
    88  	s.blockLink.next = e
    89  	s.blockLink.next.blockLink.prev = s
    90  }
    91  
    92  func (e *entry) unlink() *entry {
    93  	next := e.blockLink.next
    94  	e.blockLink.prev.blockLink.next = e.blockLink.next
    95  	e.blockLink.next.blockLink.prev = e.blockLink.prev
    96  	e.blockLink.prev = e
    97  	e.blockLink.next = e
    98  	return next
    99  }
   100  
   101  func (e *entry) setValue(v *Value) {
   102  	if v != nil {
   103  		v.acquire()
   104  	}
   105  	old := e.val
   106  	e.val = v
   107  	if old != nil {
   108  		old.release()
   109  	}
   110  }
   111  
   112  func (e *entry) peekValue() *Value {
   113  	return e.val
   114  }
   115  
   116  func (e *entry) acquireValue() *Value {
   117  	v := e.val
   118  	if v != nil {
   119  		v.acquire()
   120  	}
   121  	return v
   122  }