github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/cache/lfucache/flushable.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 lfucache
    16  
    17  import (
    18  	"fmt"
    19  	"sync/atomic"
    20  )
    21  
    22  type flushable interface {
    23  	get([]byte) ([]byte, bool, internalKeyKind)
    24  	newIter(*iterOptions) internalIterator
    25  	newFlushIter(*iterOptions, *uint64) internalIterator
    26  	inuseBytes() uint64
    27  	totalBytes() uint64
    28  	readyForFlush() bool
    29  	getID() int64
    30  	count() int
    31  }
    32  
    33  type flushableEntry struct {
    34  	flushable
    35  	flushed              chan struct{}
    36  	readerRefs           int32
    37  	releaseMemAccounting func()
    38  }
    39  
    40  func (e *flushableEntry) readerRef() {
    41  	switch v := atomic.AddInt32(&e.readerRefs, 1); {
    42  	case v <= 1:
    43  		panic(fmt.Sprintf("mcache: inconsistent reference count: %d", v))
    44  	}
    45  }
    46  
    47  func (e *flushableEntry) readerUnref() {
    48  	switch v := atomic.AddInt32(&e.readerRefs, -1); {
    49  	case v < 0:
    50  		panic(fmt.Sprintf("mcache: inconsistent reference count: %d", v))
    51  	case v == 0:
    52  		if e.releaseMemAccounting == nil {
    53  			panic("mcache: memtable reservation already released")
    54  		}
    55  		e.releaseMemAccounting()
    56  		e.releaseMemAccounting = nil
    57  	}
    58  }
    59  
    60  type flushableList []*flushableEntry