github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/lsmkv/cursor_bucket_roaring_set.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package lsmkv
    13  
    14  import (
    15  	"fmt"
    16  
    17  	"github.com/weaviate/sroar"
    18  	"github.com/weaviate/weaviate/adapters/repos/db/roaringset"
    19  )
    20  
    21  type CursorRoaringSet interface {
    22  	First() ([]byte, *sroar.Bitmap)
    23  	Next() ([]byte, *sroar.Bitmap)
    24  	Seek([]byte) ([]byte, *sroar.Bitmap)
    25  	Close()
    26  }
    27  
    28  type cursorRoaringSet struct {
    29  	combinedCursor *roaringset.CombinedCursor
    30  	unlock         func()
    31  }
    32  
    33  func (c *cursorRoaringSet) First() ([]byte, *sroar.Bitmap) {
    34  	return c.combinedCursor.First()
    35  }
    36  
    37  func (c *cursorRoaringSet) Next() ([]byte, *sroar.Bitmap) {
    38  	return c.combinedCursor.Next()
    39  }
    40  
    41  func (c *cursorRoaringSet) Seek(key []byte) ([]byte, *sroar.Bitmap) {
    42  	return c.combinedCursor.Seek(key)
    43  }
    44  
    45  func (c *cursorRoaringSet) Close() {
    46  	c.unlock()
    47  }
    48  
    49  func (b *Bucket) CursorRoaringSet() CursorRoaringSet {
    50  	return b.cursorRoaringSet(false)
    51  }
    52  
    53  func (b *Bucket) CursorRoaringSetKeyOnly() CursorRoaringSet {
    54  	return b.cursorRoaringSet(true)
    55  }
    56  
    57  func (b *Bucket) cursorRoaringSet(keyOnly bool) CursorRoaringSet {
    58  	b.flushLock.RLock()
    59  
    60  	// TODO move to helper func
    61  	if err := checkStrategyRoaringSet(b.strategy); err != nil {
    62  		panic(fmt.Sprintf("CursorRoaringSet() called on strategy other than '%s'", StrategyRoaringSet))
    63  	}
    64  
    65  	innerCursors, unlockSegmentGroup := b.disk.newRoaringSetCursors()
    66  
    67  	// we have a flush-RLock, so we have the guarantee that the flushing state
    68  	// will not change for the lifetime of the cursor, thus there can only be two
    69  	// states: either a flushing memtable currently exists - or it doesn't
    70  	if b.flushing != nil {
    71  		innerCursors = append(innerCursors, b.flushing.newRoaringSetCursor())
    72  	}
    73  	innerCursors = append(innerCursors, b.active.newRoaringSetCursor())
    74  
    75  	// cursors are in order from oldest to newest, with the memtable cursor
    76  	// being at the very top
    77  	return &cursorRoaringSet{
    78  		combinedCursor: roaringset.NewCombinedCursor(innerCursors, keyOnly),
    79  		unlock: func() {
    80  			unlockSegmentGroup()
    81  			b.flushLock.RUnlock()
    82  		},
    83  	}
    84  }