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