github.com/weaviate/weaviate@v1.24.6/adapters/repos/db/lsmkv/cursor_segment_collection_reusable.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  	"github.com/weaviate/weaviate/entities/lsmkv"
    16  )
    17  
    18  type segmentCursorCollectionReusable struct {
    19  	segment    *segment
    20  	nextOffset uint64
    21  	nodeBuf    segmentCollectionNode
    22  }
    23  
    24  func (s *segment) newCollectionCursorReusable() *segmentCursorCollectionReusable {
    25  	return &segmentCursorCollectionReusable{
    26  		segment: s,
    27  	}
    28  }
    29  
    30  func (s *segmentCursorCollectionReusable) seek(key []byte) ([]byte, []value, error) {
    31  	node, err := s.segment.index.Seek(key)
    32  	if err != nil {
    33  		return nil, nil, err
    34  	}
    35  
    36  	err = s.parseCollectionNodeInto(nodeOffset{node.Start, node.End})
    37  	if err != nil {
    38  		return s.nodeBuf.primaryKey, nil, err
    39  	}
    40  
    41  	s.nextOffset = node.End
    42  
    43  	return s.nodeBuf.primaryKey, s.nodeBuf.values, nil
    44  }
    45  
    46  func (s *segmentCursorCollectionReusable) next() ([]byte, []value, error) {
    47  	if s.nextOffset >= s.segment.dataEndPos {
    48  		return nil, nil, lsmkv.NotFound
    49  	}
    50  
    51  	err := s.parseCollectionNodeInto(nodeOffset{start: s.nextOffset})
    52  	// make sure to set the next offset before checking the error. The error
    53  	// could be 'entities.Deleted' which would require that the offset is still advanced
    54  	// for the next cycle
    55  	s.nextOffset = s.nextOffset + uint64(s.nodeBuf.offset)
    56  	if err != nil {
    57  		return s.nodeBuf.primaryKey, nil, err
    58  	}
    59  
    60  	return s.nodeBuf.primaryKey, s.nodeBuf.values, nil
    61  }
    62  
    63  func (s *segmentCursorCollectionReusable) first() ([]byte, []value, error) {
    64  	s.nextOffset = s.segment.dataStartPos
    65  
    66  	err := s.parseCollectionNodeInto(nodeOffset{start: s.nextOffset})
    67  	if err != nil {
    68  		return s.nodeBuf.primaryKey, nil, err
    69  	}
    70  
    71  	s.nextOffset = s.nextOffset + uint64(s.nodeBuf.offset)
    72  
    73  	return s.nodeBuf.primaryKey, s.nodeBuf.values, nil
    74  }
    75  
    76  func (s *segmentCursorCollectionReusable) parseCollectionNodeInto(offset nodeOffset) error {
    77  	r, err := s.segment.newNodeReader(offset)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	return ParseCollectionNodeInto(r, &s.nodeBuf)
    83  }