-
Notifications
You must be signed in to change notification settings - Fork 686
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work in progress
- Loading branch information
Showing
9 changed files
with
595 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) 2021 Couchbase, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package document | ||
|
||
import ( | ||
"encoding/binary" | ||
"math" | ||
|
||
"github.com/blevesearch/bleve/v2/size" | ||
index "github.com/blevesearch/bleve_index_api" | ||
) | ||
|
||
type NumericRangeField struct { | ||
name string | ||
arrayPositions []uint64 | ||
options index.FieldIndexingOptions | ||
value []byte | ||
} | ||
|
||
func (n *NumericRangeField) Size() int { | ||
return reflectStaticSizeNumericField + size.SizeOfPtr + | ||
len(n.name) + | ||
len(n.arrayPositions)*size.SizeOfPtr | ||
} | ||
|
||
func (n *NumericRangeField) Name() string { | ||
return n.name | ||
} | ||
|
||
func (n *NumericRangeField) ArrayPositions() []uint64 { | ||
return n.arrayPositions | ||
} | ||
|
||
func (n *NumericRangeField) Options() index.FieldIndexingOptions { | ||
return n.options | ||
} | ||
|
||
func (n *NumericRangeField) EncodedFieldType() byte { | ||
return 'r' | ||
} | ||
|
||
func (n *NumericRangeField) AnalyzedLength() int { | ||
return 0 | ||
} | ||
|
||
func (n *NumericRangeField) AnalyzedTokenFrequencies() index.TokenFrequencies { | ||
return nil | ||
} | ||
|
||
func (n *NumericRangeField) Analyze() { | ||
|
||
} | ||
|
||
func (n *NumericRangeField) Value() []byte { | ||
return n.value | ||
} | ||
|
||
func (n *NumericRangeField) NumPlainTextBytes() uint64 { | ||
return 0 | ||
} | ||
|
||
func (n *NumericRangeField) Number() (float64, error) { | ||
return math.Float64frombits(binary.BigEndian.Uint64(n.value)), nil | ||
} | ||
|
||
func NewNumericRangeField(name string, arrayPositions []uint64, number float64) *NumericRangeField { | ||
value := make([]byte, 8) | ||
binary.BigEndian.PutUint64(value, math.Float64bits(number)) | ||
return &NumericRangeField{ | ||
name: name, | ||
arrayPositions: arrayPositions, | ||
value: value, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright (c) 2021 Couchbase, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package scorch | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"sync/atomic" | ||
|
||
index "github.com/blevesearch/bleve_index_api" | ||
segment "github.com/blevesearch/scorch_segment_api/v2" | ||
) | ||
|
||
type IndexSnapshotNumericRangeReader struct { | ||
min, max float64 | ||
inclusiveMin, inclusiveMax bool | ||
|
||
|
||
term []byte | ||
field string | ||
snapshot *IndexSnapshot | ||
dicts []segment.TermDictionary | ||
postings []segment.PostingsList | ||
iterators []segment.PostingsIterator | ||
segmentOffset int | ||
|
||
currPosting segment.Posting | ||
currID index.IndexInternalID | ||
recycle bool | ||
} | ||
|
||
func (i *IndexSnapshotNumericRangeReader) Size() int { | ||
return 0 | ||
} | ||
|
||
func (i *IndexSnapshotNumericRangeReader) Next(preAlloced *index.TermFieldDoc) (*index.TermFieldDoc, error) { | ||
rv := preAlloced | ||
if rv == nil { | ||
rv = &index.TermFieldDoc{} | ||
} | ||
// find the next hit | ||
for i.segmentOffset < len(i.iterators) { | ||
next, err := i.iterators[i.segmentOffset].Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if next != nil { | ||
// make segment number into global number by adding offset | ||
globalOffset := i.snapshot.offsets[i.segmentOffset] | ||
nnum := next.Number() | ||
rv.ID = docNumberToBytes(rv.ID, nnum+globalOffset) | ||
|
||
i.currID = rv.ID | ||
i.currPosting = next | ||
return rv, nil | ||
} | ||
i.segmentOffset++ | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (i *IndexSnapshotNumericRangeReader) Advance(ID index.IndexInternalID, preAlloced *index.TermFieldDoc) (*index.TermFieldDoc, error) { | ||
// FIXME do something better | ||
// for now, if we need to seek backwards, then restart from the beginning | ||
if i.currPosting != nil && bytes.Compare(i.currID, ID) >= 0 { | ||
i2, err := i.snapshot.NumericRangeReader(i.field, i.min, i.max, i.inclusiveMin, i.inclusiveMax) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// close the current term field reader before replacing it with a new one | ||
_ = i.Close() | ||
*i = *(i2.(*IndexSnapshotNumericRangeReader)) | ||
} | ||
num, err := docInternalToNumber(ID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error converting to doc number % x - %v", ID, err) | ||
} | ||
segIndex, ldocNum := i.snapshot.segmentIndexAndLocalDocNumFromGlobal(num) | ||
if segIndex >= len(i.snapshot.segment) { | ||
return nil, fmt.Errorf("computed segment index %d out of bounds %d", | ||
segIndex, len(i.snapshot.segment)) | ||
} | ||
// skip directly to the target segment | ||
i.segmentOffset = segIndex | ||
next, err := i.iterators[i.segmentOffset].Advance(ldocNum) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if next == nil { | ||
// we jumped directly to the segment that should have contained it | ||
// but it wasn't there, so reuse Next() which should correctly | ||
// get the next hit after it (we moved i.segmentOffset) | ||
return i.Next(preAlloced) | ||
} | ||
|
||
if preAlloced == nil { | ||
preAlloced = &index.TermFieldDoc{} | ||
} | ||
preAlloced.ID = docNumberToBytes(preAlloced.ID, next.Number()+ | ||
i.snapshot.offsets[segIndex]) | ||
i.currID = preAlloced.ID | ||
i.currPosting = next | ||
return preAlloced, nil | ||
} | ||
|
||
func (i *IndexSnapshotNumericRangeReader) Count() uint64 { | ||
var rv uint64 | ||
for _, posting := range i.postings { | ||
rv += posting.Count() | ||
} | ||
return rv | ||
} | ||
|
||
func (i *IndexSnapshotNumericRangeReader) Close() error { | ||
if i.snapshot != nil { | ||
atomic.AddUint64(&i.snapshot.parent.stats.TotTermSearchersFinished, uint64(1)) | ||
|
||
// FIXME disabling any recycling for POC | ||
//i.snapshot.recycleTermFieldReader(i) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.