0
0
Fork 0

optimize FacetsBuilder with cached fields & avoid some allocs

This commit is contained in:
Steve Yen 2016-10-25 15:20:49 -07:00
parent a941a0f318
commit 2a8237e8cc
4 changed files with 11 additions and 9 deletions

View File

@ -87,7 +87,7 @@ type FieldTerms map[string][]string
// FieldsNotYetCached returns a list of fields not yet cached out of a larger list of fields
func (f FieldTerms) FieldsNotYetCached(fields []string) []string {
var rv []string
rv := make([]string, 0, len(fields))
for _, field := range fields {
if _, ok := f[field]; !ok {
rv = append(rv, field)

View File

@ -16,7 +16,7 @@ package store
import "encoding/json"
// KVStore is an abstraction for working with KV stores. Note that
// KVStore is an abstraction for working with KV stores. Note that
// in order to be used with the bleve.registry, it must also implement
// a constructor function of the registry.KVStoreConstructor type.
type KVStore interface {

View File

@ -28,8 +28,8 @@ func RegisterKVStore(name string, constructor KVStoreConstructor) {
stores[name] = constructor
}
// KVStoreConstructor is used to build a KVStore of a specific type when
// specificied by the index configuration. In addition to meeting the
// KVStoreConstructor is used to build a KVStore of a specific type when
// specificied by the index configuration. In addition to meeting the
// store.KVStore interface, KVStores must also support this constructor.
// Note that currently the values of config must
// be able to be marshaled and unmarshaled using the encoding/json library (used

View File

@ -29,6 +29,7 @@ type FacetBuilder interface {
type FacetsBuilder struct {
indexReader index.IndexReader
facets map[string]FacetBuilder
fields []string
}
func NewFacetsBuilder(indexReader index.IndexReader) *FacetsBuilder {
@ -43,14 +44,15 @@ func (fb *FacetsBuilder) Add(name string, facetBuilder FacetBuilder) {
}
func (fb *FacetsBuilder) Update(docMatch *DocumentMatch) error {
var fields []string
for _, facetBuilder := range fb.facets {
fields = append(fields, facetBuilder.Field())
if fb.fields == nil {
for _, facetBuilder := range fb.facets {
fb.fields = append(fb.fields, facetBuilder.Field())
}
}
if len(fields) > 0 {
if len(fb.fields) > 0 {
// find out which fields haven't been loaded yet
fieldsToLoad := docMatch.CachedFieldTerms.FieldsNotYetCached(fields)
fieldsToLoad := docMatch.CachedFieldTerms.FieldsNotYetCached(fb.fields)
// look them up
fieldTerms, err := fb.indexReader.DocumentFieldTerms(docMatch.IndexInternalID, fieldsToLoad)
if err != nil {