From a941a0f31863ca99efcc443a4fe7cf5ca300620a Mon Sep 17 00:00:00 2001 From: Steve Yen Date: Tue, 25 Oct 2016 15:12:46 -0700 Subject: [PATCH 1/2] simplify DocumentFieldTerms append() usage --- index/upsidedown/index_reader.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/index/upsidedown/index_reader.go b/index/upsidedown/index_reader.go index 7714dc0a..288adaf2 100644 --- a/index/upsidedown/index_reader.go +++ b/index/upsidedown/index_reader.go @@ -119,12 +119,7 @@ func (i *IndexReader) DocumentFieldTerms(id index.IndexInternalID, fields []stri } for _, entry := range back.termEntries { if field, ok := fieldsMap[uint16(*entry.Field)]; ok { - terms, ok := rv[field] - if !ok { - terms = make([]string, 0) - } - terms = append(terms, *entry.Term) - rv[field] = terms + rv[field] = append(rv[field], *entry.Term) } } return rv, nil From 2a8237e8cc293295f26c022f12e0fadbafad7111 Mon Sep 17 00:00:00 2001 From: Steve Yen Date: Tue, 25 Oct 2016 15:20:49 -0700 Subject: [PATCH 2/2] optimize FacetsBuilder with cached fields & avoid some allocs --- index/index.go | 2 +- index/store/kvstore.go | 2 +- registry/store.go | 4 ++-- search/facets_builder.go | 12 +++++++----- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/index/index.go b/index/index.go index ee1bd161..ffcf6dcc 100644 --- a/index/index.go +++ b/index/index.go @@ -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) diff --git a/index/store/kvstore.go b/index/store/kvstore.go index f3a0109b..34698c7b 100644 --- a/index/store/kvstore.go +++ b/index/store/kvstore.go @@ -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 { diff --git a/registry/store.go b/registry/store.go index ad118ed0..83187763 100644 --- a/registry/store.go +++ b/registry/store.go @@ -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 diff --git a/search/facets_builder.go b/search/facets_builder.go index 89ad1bb8..702e2ec2 100644 --- a/search/facets_builder.go +++ b/search/facets_builder.go @@ -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 {