Move the Related doc counter to prevent a race

Closes #10768
This commit is contained in:
Bjørn Erik Pedersen 2023-03-01 14:59:08 +01:00
parent a669467d98
commit 5c317c55e7

View file

@ -143,13 +143,6 @@ type IndexConfig struct {
// Will lower case all string values in and queries tothis index. // Will lower case all string values in and queries tothis index.
// May get better accurate results, but at a slight performance cost. // May get better accurate results, but at a slight performance cost.
ToLower bool ToLower bool
// Counts the number of documents in the index.
numDocs int
}
func (cfg *IndexConfig) incrNumDocs() {
cfg.numDocs++
} }
// Document is the interface an indexable document in Hugo must fulfill. // Document is the interface an indexable document in Hugo must fulfill.
@ -178,6 +171,8 @@ type FragmentProvider interface {
type InvertedIndex struct { type InvertedIndex struct {
cfg Config cfg Config
index map[string]map[Keyword][]Document index map[string]map[Keyword][]Document
// Counts the number of documents added to each index.
indexDocCount map[string]int
minWeight int minWeight int
maxWeight int maxWeight int
@ -199,7 +194,7 @@ func (idx *InvertedIndex) getIndexCfg(name string) (IndexConfig, bool) {
// NewInvertedIndex creates a new InvertedIndex. // NewInvertedIndex creates a new InvertedIndex.
// Documents to index must be added in Add. // Documents to index must be added in Add.
func NewInvertedIndex(cfg Config) *InvertedIndex { func NewInvertedIndex(cfg Config) *InvertedIndex {
idx := &InvertedIndex{index: make(map[string]map[Keyword][]Document), cfg: cfg} idx := &InvertedIndex{index: make(map[string]map[Keyword][]Document), indexDocCount: make(map[string]int), cfg: cfg}
for _, conf := range cfg.Indices { for _, conf := range cfg.Indices {
idx.index[conf.Name] = make(map[Keyword][]Document) idx.index[conf.Name] = make(map[Keyword][]Document)
if conf.Weight < idx.minWeight { if conf.Weight < idx.minWeight {
@ -221,7 +216,7 @@ func (idx *InvertedIndex) Add(ctx context.Context, docs ...Document) error {
panic("index is finalized") panic("index is finalized")
} }
var err error var err error
for i, config := range idx.cfg.Indices { for _, config := range idx.cfg.Indices {
if config.Weight == 0 { if config.Weight == 0 {
// Disabled // Disabled
continue continue
@ -251,8 +246,7 @@ func (idx *InvertedIndex) Add(ctx context.Context, docs ...Document) error {
} }
if added { if added {
c := &idx.cfg.Indices[i] idx.indexDocCount[config.Name]++
(*c).incrNumDocs()
} }
} }
} }
@ -270,12 +264,12 @@ func (idx *InvertedIndex) Finalize(ctx context.Context) error {
continue continue
} }
setm := idx.index[config.Name] setm := idx.index[config.Name]
numDocs := config.numDocs if idx.indexDocCount[config.Name] == 0 {
if numDocs == 0 {
continue continue
} }
// Remove high cardinality terms. // Remove high cardinality terms.
numDocs := idx.indexDocCount[config.Name]
for k, v := range setm { for k, v := range setm {
percentageWithKeyword := int(math.Ceil(float64(len(v)) / float64(numDocs) * 100)) percentageWithKeyword := int(math.Ceil(float64(len(v)) / float64(numDocs) * 100))
if percentageWithKeyword > config.CardinalityThreshold { if percentageWithKeyword > config.CardinalityThreshold {