@@ -17,7 +17,6 @@ package scorch
1717import (
1818 "container/heap"
1919 "context"
20- "encoding/binary"
2120 "fmt"
2221 "os"
2322 "path/filepath"
@@ -42,9 +41,8 @@ type asynchSegmentResult struct {
4241 dict segment.TermDictionary
4342 dictItr segment.DictionaryIterator
4443
45- cardinality int
46- index int
47- docs * roaring.Bitmap
44+ index int
45+ docs * roaring.Bitmap
4846
4947 thesItr segment.ThesaurusIterator
5048
@@ -59,11 +57,11 @@ func init() {
5957 var err error
6058 lb1 , err = lev .NewLevenshteinAutomatonBuilder (1 , true )
6159 if err != nil {
62- panic (fmt .Errorf ("Levenshtein automaton ed1 builder err: %v" , err ))
60+ panic (fmt .Errorf ("levenshtein automaton ed1 builder err: %v" , err ))
6361 }
6462 lb2 , err = lev .NewLevenshteinAutomatonBuilder (2 , true )
6563 if err != nil {
66- panic (fmt .Errorf ("Levenshtein automaton ed2 builder err: %v" , err ))
64+ panic (fmt .Errorf ("levenshtein automaton ed2 builder err: %v" , err ))
6765 }
6866}
6967
@@ -474,7 +472,7 @@ func (is *IndexSnapshot) GetInternal(key []byte) ([]byte, error) {
474472func (is * IndexSnapshot ) DocCount () (uint64 , error ) {
475473 var rv uint64
476474 for _ , segment := range is .segment {
477- rv += segment .Count ()
475+ rv += segment .CountRoot ()
478476 }
479477 return rv , nil
480478}
@@ -501,7 +499,7 @@ func (is *IndexSnapshot) Document(id string) (rv index.Document, err error) {
501499 return nil , nil
502500 }
503501
504- docNum , err := docInternalToNumber ( next .ID )
502+ docNum , err := next .ID . Value ( )
505503 if err != nil {
506504 return nil , err
507505 }
@@ -571,7 +569,7 @@ func (is *IndexSnapshot) segmentIndexAndLocalDocNumFromGlobal(docNum uint64) (in
571569}
572570
573571func (is * IndexSnapshot ) ExternalID (id index.IndexInternalID ) (string , error ) {
574- docNum , err := docInternalToNumber ( id )
572+ docNum , err := id . Value ( )
575573 if err != nil {
576574 return "" , err
577575 }
@@ -589,7 +587,7 @@ func (is *IndexSnapshot) ExternalID(id index.IndexInternalID) (string, error) {
589587}
590588
591589func (is * IndexSnapshot ) segmentIndexAndLocalDocNum (id index.IndexInternalID ) (int , uint64 , error ) {
592- docNum , err := docInternalToNumber ( id )
590+ docNum , err := id . Value ( )
593591 if err != nil {
594592 return 0 , 0 , err
595593 }
@@ -778,25 +776,6 @@ func (is *IndexSnapshot) recycleTermFieldReader(tfr *IndexSnapshotTermFieldReade
778776 is .m2 .Unlock ()
779777}
780778
781- func docNumberToBytes (buf []byte , in uint64 ) []byte {
782- if len (buf ) != 8 {
783- if cap (buf ) >= 8 {
784- buf = buf [0 :8 ]
785- } else {
786- buf = make ([]byte , 8 )
787- }
788- }
789- binary .BigEndian .PutUint64 (buf , in )
790- return buf
791- }
792-
793- func docInternalToNumber (in index.IndexInternalID ) (uint64 , error ) {
794- if len (in ) != 8 {
795- return 0 , fmt .Errorf ("wrong len for IndexInternalID: %q" , in )
796- }
797- return binary .BigEndian .Uint64 (in ), nil
798- }
799-
800779func (is * IndexSnapshot ) documentVisitFieldTermsOnSegment (
801780 segmentIndex int , localDocNum uint64 , fields []string , cFields []string ,
802781 visitor index.DocValueVisitor , dvs segment.DocVisitState ) (
@@ -899,7 +878,7 @@ func (dvr *DocValueReader) BytesRead() uint64 {
899878func (dvr * DocValueReader ) VisitDocValues (id index.IndexInternalID ,
900879 visitor index.DocValueVisitor ,
901880) (err error ) {
902- docNum , err := docInternalToNumber ( id )
881+ docNum , err := id . Value ( )
903882 if err != nil {
904883 return err
905884 }
@@ -1299,3 +1278,23 @@ func (is *IndexSnapshot) TermFrequencies(field string, limit int, descending boo
12991278
13001279 return termFreqs [:limit ], nil
13011280}
1281+
1282+ // Ancestors returns the ancestor IDs for the given document ID. The prealloc
1283+ // slice can be provided to avoid allocations downstream, and MUST be empty.
1284+ func (i * IndexSnapshot ) Ancestors (ID index.IndexInternalID , prealloc []index.AncestorID ) ([]index.AncestorID , error ) {
1285+ // get segment and local doc num for the ID
1286+ seg , ldoc , err := i .segmentIndexAndLocalDocNum (ID )
1287+ if err != nil {
1288+ return nil , err
1289+ }
1290+ // get ancestors from the segment
1291+ prealloc = i .segment [seg ].Ancestors (ldoc , prealloc )
1292+ // get global offset for the segment (correcting factor for multi-segment indexes)
1293+ globalOffset := i .offsets [seg ]
1294+ // adjust ancestors to global doc numbers, not local to segment
1295+ for idx := range prealloc {
1296+ prealloc [idx ] = prealloc [idx ].Add (globalOffset )
1297+ }
1298+ // return adjusted ancestors
1299+ return prealloc , nil
1300+ }
0 commit comments