github.com/maypok86/otter@v1.2.1/internal/generated/node/be.go (about)

     1  // Code generated by NodeGenerator. DO NOT EDIT.
     2  
     3  // Package node is a generated generator package.
     4  package node
     5  
     6  import (
     7  	"sync/atomic"
     8  	"unsafe"
     9  
    10  	"github.com/maypok86/otter/internal/unixtime"
    11  )
    12  
    13  // BE is a cache entry that provide the following features:
    14  //
    15  // 1. Base
    16  //
    17  // 2. Expiration
    18  type BE[K comparable, V any] struct {
    19  	key        K
    20  	value      V
    21  	prev       *BE[K, V]
    22  	next       *BE[K, V]
    23  	prevExp    *BE[K, V]
    24  	nextExp    *BE[K, V]
    25  	expiration uint32
    26  	state      uint32
    27  	frequency  uint8
    28  	queueType  uint8
    29  }
    30  
    31  // NewBE creates a new BE.
    32  func NewBE[K comparable, V any](key K, value V, expiration, cost uint32) Node[K, V] {
    33  	return &BE[K, V]{
    34  		key:        key,
    35  		value:      value,
    36  		expiration: expiration,
    37  		state:      aliveState,
    38  	}
    39  }
    40  
    41  // CastPointerToBE casts a pointer to BE.
    42  func CastPointerToBE[K comparable, V any](ptr unsafe.Pointer) Node[K, V] {
    43  	return (*BE[K, V])(ptr)
    44  }
    45  
    46  func (n *BE[K, V]) Key() K {
    47  	return n.key
    48  }
    49  
    50  func (n *BE[K, V]) Value() V {
    51  	return n.value
    52  }
    53  
    54  func (n *BE[K, V]) AsPointer() unsafe.Pointer {
    55  	return unsafe.Pointer(n)
    56  }
    57  
    58  func (n *BE[K, V]) Prev() Node[K, V] {
    59  	return n.prev
    60  }
    61  
    62  func (n *BE[K, V]) SetPrev(v Node[K, V]) {
    63  	if v == nil {
    64  		n.prev = nil
    65  		return
    66  	}
    67  	n.prev = (*BE[K, V])(v.AsPointer())
    68  }
    69  
    70  func (n *BE[K, V]) Next() Node[K, V] {
    71  	return n.next
    72  }
    73  
    74  func (n *BE[K, V]) SetNext(v Node[K, V]) {
    75  	if v == nil {
    76  		n.next = nil
    77  		return
    78  	}
    79  	n.next = (*BE[K, V])(v.AsPointer())
    80  }
    81  
    82  func (n *BE[K, V]) PrevExp() Node[K, V] {
    83  	return n.prevExp
    84  }
    85  
    86  func (n *BE[K, V]) SetPrevExp(v Node[K, V]) {
    87  	if v == nil {
    88  		n.prevExp = nil
    89  		return
    90  	}
    91  	n.prevExp = (*BE[K, V])(v.AsPointer())
    92  }
    93  
    94  func (n *BE[K, V]) NextExp() Node[K, V] {
    95  	return n.nextExp
    96  }
    97  
    98  func (n *BE[K, V]) SetNextExp(v Node[K, V]) {
    99  	if v == nil {
   100  		n.nextExp = nil
   101  		return
   102  	}
   103  	n.nextExp = (*BE[K, V])(v.AsPointer())
   104  }
   105  
   106  func (n *BE[K, V]) HasExpired() bool {
   107  	return n.expiration <= unixtime.Now()
   108  }
   109  
   110  func (n *BE[K, V]) Expiration() uint32 {
   111  	return n.expiration
   112  }
   113  
   114  func (n *BE[K, V]) Cost() uint32 {
   115  	return 1
   116  }
   117  
   118  func (n *BE[K, V]) IsAlive() bool {
   119  	return atomic.LoadUint32(&n.state) == aliveState
   120  }
   121  
   122  func (n *BE[K, V]) Die() {
   123  	atomic.StoreUint32(&n.state, deadState)
   124  }
   125  
   126  func (n *BE[K, V]) Frequency() uint8 {
   127  	return n.frequency
   128  }
   129  
   130  func (n *BE[K, V]) IncrementFrequency() {
   131  	n.frequency = minUint8(n.frequency+1, maxFrequency)
   132  }
   133  
   134  func (n *BE[K, V]) DecrementFrequency() {
   135  	n.frequency--
   136  }
   137  
   138  func (n *BE[K, V]) ResetFrequency() {
   139  	n.frequency = 0
   140  }
   141  
   142  func (n *BE[K, V]) MarkSmall() {
   143  	n.queueType = smallQueueType
   144  }
   145  
   146  func (n *BE[K, V]) IsSmall() bool {
   147  	return n.queueType == smallQueueType
   148  }
   149  
   150  func (n *BE[K, V]) MarkMain() {
   151  	n.queueType = mainQueueType
   152  }
   153  
   154  func (n *BE[K, V]) IsMain() bool {
   155  	return n.queueType == mainQueueType
   156  }
   157  
   158  func (n *BE[K, V]) Unmark() {
   159  	n.queueType = unknownQueueType
   160  }