Skip to content
This repository has been archived by the owner on Oct 16, 2018. It is now read-only.

Re-work segment.Fields/Terms to use iterators #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .excludemetalint
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ vendor/
generated/
_mock.go
.pb.go
index/segment/mem/ids_map_gen.go
_gen.go
20 changes: 19 additions & 1 deletion generated-source-files.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ install-m3x-repo: install-glide install-generics-bin

# Generation rule for all generated types
.PHONY: genny-all
genny-all: genny-map-all
genny-all: genny-map-all genny-arraypool-all

# Tests that all currently generated types match their contents if they were regenerated
.PHONY: test-genny-all
Expand Down Expand Up @@ -115,3 +115,21 @@ genny-map-segment-fs-fst-terms-offset: install-m3x-repo
rename_type_prefix=fstTermsOffsets
# Rename generated map file
mv -f $(m3ninx_package_path)/index/segment/fs/map_gen.go $(m3ninx_package_path)/index/segment/fs/fst_terms_offsets_map_gen.go


# generation rule for all generated arraypools
.PHONY: genny-arraypool-all
genny-arraypool-all: \
genny-arraypool-bytes-slice-array-pool

# arraypool generation rule for ./x/bytes.SliceArrayPool
.PHONY: genny-arraypool-bytes-slice-array-pool
genny-arraypool-bytes-slice-array-pool: install-m3x-repo
cd $(m3x_package_path) && make genny-arraypool \
pkg=bytes \
elem_type=[]byte \
target_package=$(m3ninx_package)/x/bytes \
out_file=slice_arraypool_gen.go \
rename_type_prefix=Slice \
rename_type_middle=Slice \
rename_constructor=NewSliceArrayPool
25 changes: 19 additions & 6 deletions index/segment/fs/fs_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 120 additions & 0 deletions index/segment/fs/fst_terms_iterator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package fs

import (
sgmt "github.com/m3db/m3ninx/index/segment"
"github.com/m3db/m3x/context"
xerrors "github.com/m3db/m3x/errors"

"github.com/couchbase/vellum"
)

type newFstTermsIterOpts struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it seems strange to me to call these options since they're actually required parameters and we need to call close on them, maybe it would be worth just passing them directly in the constructor?

ctx context.Context
opts Options
fst *vellum.FST
finalizeFST bool
}

func (o *newFstTermsIterOpts) Close() error {
o.ctx.Close()
if o.finalizeFST {
return o.fst.Close()
}
return nil
}

func newFSTTermsIter(opts newFstTermsIterOpts) *fstTermsIter {
return &fstTermsIter{iterOpts: opts}
}

// nolint: maligned
type fstTermsIter struct {
iterOpts newFstTermsIterOpts

initialized bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if it's worth it but I think you can resolve the maligned lint by moving initialized above done:

iter        *vellum.FSTIterator
err         error
initialized bool
done        bool

iter *vellum.FSTIterator
err error
done bool

current []byte
currentValue uint64
}

var _ sgmt.OrderedBytesSliceIterator = &fstTermsIter{}

func (f *fstTermsIter) Next() bool {
if f.done || f.err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to check if the context has been closed here?

return false
}

var err error

if !f.initialized {
f.initialized = true
f.iter = &vellum.FSTIterator{}
err = f.iter.Reset(f.iterOpts.fst, minByteKey, maxByteKey, nil)
} else {
err = f.iter.Next()
}

if err != nil {
f.done = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe only set f.done to true if the error was a vellum.ErrIteratorDone in case we ever want to distinguish the two case in the future?

if err != vellum.ErrIteratorDone {
f.err = err
}
return false
}

nextBytes, nextValue := f.iter.Current()
// taking a copy of the bytes to avoid referring to mmap'd memory
// TODO(prateek): back this by a bytes pool.
f.current = append([]byte(nil), nextBytes...)
f.currentValue = nextValue
return true
}

func (f *fstTermsIter) CurrentExtended() ([]byte, uint64) {
return f.current, f.currentValue
}

func (f *fstTermsIter) Current() []byte {
return f.current
}

func (f *fstTermsIter) Err() error {
return f.err
}

func (f *fstTermsIter) Close() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to set f.done to true here in case someone calls close before reaching the end of the iterator?

f.current = nil

multiErr := xerrors.MultiError{}

if f.iter != nil {
multiErr = multiErr.Add(f.iter.Close())
}
f.iter = nil

multiErr = multiErr.Add(f.iterOpts.Close())
return multiErr.FinalError()
}
106 changes: 106 additions & 0 deletions index/segment/fs/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package fs

import (
"github.com/m3db/m3ninx/postings"
"github.com/m3db/m3ninx/postings/roaring"
"github.com/m3db/m3ninx/x/bytes"

"github.com/m3db/m3x/instrument"
"github.com/m3db/m3x/pool"
)

const (
defaultBytesArrayPoolCapacity = 1024
)

// Options is a collection of knobs for a fs segment.
type Options interface {
// SetInstrumentOptions sets the instrument options.
SetInstrumentOptions(value instrument.Options) Options

// InstrumentOptions returns the instrument options.
InstrumentOptions() instrument.Options

// SetBytesSliceArrayPool sets the bytes slice array pool.
SetBytesSliceArrayPool(value bytes.SliceArrayPool) Options

// BytesSliceArrayPool returns the bytes slice array pool.
BytesSliceArrayPool() bytes.SliceArrayPool

// SetPostingsListPool sets the postings list pool.
SetPostingsListPool(value postings.Pool) Options

// PostingsListPool returns the postings list pool.
PostingsListPool() postings.Pool
}

type opts struct {
iopts instrument.Options
bytesSliceArrPool bytes.SliceArrayPool
postingsPool postings.Pool
}

// NewOptions returns new options.
func NewOptions() Options {
arrPool := bytes.NewSliceArrayPool(bytes.SliceArrayPoolOpts{
Capacity: defaultBytesArrayPoolCapacity,
Options: pool.NewObjectPoolOptions(),
})
arrPool.Init()

return &opts{
iopts: instrument.NewOptions(),
bytesSliceArrPool: arrPool,
postingsPool: postings.NewPool(nil, roaring.NewPostingsList),
}
}

func (o *opts) SetInstrumentOptions(v instrument.Options) Options {
opts := *o
opts.iopts = v
return &opts
}

func (o *opts) InstrumentOptions() instrument.Options {
return o.iopts
}

func (o *opts) SetBytesSliceArrayPool(value bytes.SliceArrayPool) Options {
opts := *o
opts.bytesSliceArrPool = value
return &opts
}

func (o *opts) BytesSliceArrayPool() bytes.SliceArrayPool {
return o.bytesSliceArrPool
}

func (o *opts) SetPostingsListPool(v postings.Pool) Options {
opts := *o
opts.postingsPool = v
return &opts
}

func (o *opts) PostingsListPool() postings.Pool {
return o.postingsPool
}
Loading