| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package page |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"fmt" |
| 19 |
"sync" |
| 20 |
|
| 21 |
"github.com/gohugoio/hugo/common/para" |
| 22 |
"github.com/gohugoio/hugo/common/types" |
| 23 |
"github.com/gohugoio/hugo/config" |
| 24 |
"github.com/gohugoio/hugo/related" |
| 25 |
"github.com/mitchellh/mapstructure" |
| 26 |
"github.com/spf13/cast" |
| 27 |
) |
| 28 |
|
| 29 |
var ( |
| 30 |
// Assert that Pages and PageGroup implements the PageGenealogist interface. |
| 31 |
_ PageGenealogist = (Pages)(nil) |
| 32 |
_ PageGenealogist = PageGroup{} |
| 33 |
) |
| 34 |
|
| 35 |
// A PageGenealogist finds related pages in a page collection. This interface is implemented |
| 36 |
// by Pages and PageGroup, which makes it available as `{{ .RegularRelated . }}` etc. |
| 37 |
type PageGenealogist interface { |
| 38 |
// Template example: |
| 39 |
// {{ $related := .RegularPages.Related . }} |
| 40 |
Related(ctx context.Context, opts any) (Pages, error) |
| 41 |
|
| 42 |
// Template example: |
| 43 |
// {{ $related := .RegularPages.RelatedIndices . "tags" "date" }} |
| 44 |
// Deprecated: Use Related instead. |
| 45 |
RelatedIndices(ctx context.Context, doc related.Document, indices ...any) (Pages, error) |
| 46 |
|
| 47 |
// Template example: |
| 48 |
// {{ $related := .RegularPages.RelatedTo ( keyVals "tags" "hugo", "rocks") ( keyVals "date" .Date ) }} |
| 49 |
// Deprecated: Use Related instead. |
| 50 |
RelatedTo(ctx context.Context, args ...types.KeyValues) (Pages, error) |
| 51 |
} |
| 52 |
|
| 53 |
// Related searches all the configured indices with the search keywords from the |
| 54 |
// supplied document. |
| 55 |
func (p Pages) Related(ctx context.Context, optsv any) (Pages, error) { |
| 56 |
if len(p) == 0 { |
| 57 |
return nil, nil |
| 58 |
} |
| 59 |
|
| 60 |
var opts related.SearchOpts |
| 61 |
switch v := optsv.(type) { |
| 62 |
case related.Document: |
| 63 |
opts.Document = v |
| 64 |
case map[string]any: |
| 65 |
if err := mapstructure.WeakDecode(v, &opts); err != nil { |
| 66 |
return nil, err |
| 67 |
} |
| 68 |
default: |
| 69 |
return nil, fmt.Errorf("invalid argument type %T", optsv) |
| 70 |
} |
| 71 |
|
| 72 |
result, err := p.search(ctx, opts) |
| 73 |
if err != nil { |
| 74 |
return nil, err |
| 75 |
} |
| 76 |
|
| 77 |
return result, nil |
| 78 |
} |
| 79 |
|
| 80 |
// RelatedIndices searches the given indices with the search keywords from the |
| 81 |
// supplied document. |
| 82 |
// Deprecated: Use Related instead. |
| 83 |
func (p Pages) RelatedIndices(ctx context.Context, doc related.Document, indices ...any) (Pages, error) { |
| 84 |
indicesStr, err := cast.ToStringSliceE(indices) |
| 85 |
if err != nil { |
| 86 |
return nil, err |
| 87 |
} |
| 88 |
|
| 89 |
opts := related.SearchOpts{ |
| 90 |
Document: doc, |
| 91 |
Indices: indicesStr, |
| 92 |
} |
| 93 |
|
| 94 |
result, err := p.search(ctx, opts) |
| 95 |
if err != nil { |
| 96 |
return nil, err |
| 97 |
} |
| 98 |
|
| 99 |
return result, nil |
| 100 |
} |
| 101 |
|
| 102 |
// RelatedTo searches the given indices with the corresponding values. |
| 103 |
// Deprecated: Use Related instead. |
| 104 |
func (p Pages) RelatedTo(ctx context.Context, args ...types.KeyValues) (Pages, error) { |
| 105 |
if len(p) == 0 { |
| 106 |
return nil, nil |
| 107 |
} |
| 108 |
|
| 109 |
opts := related.SearchOpts{ |
| 110 |
NamedSlices: args, |
| 111 |
} |
| 112 |
|
| 113 |
return p.search(ctx, opts) |
| 114 |
} |
| 115 |
|
| 116 |
func (p Pages) search(ctx context.Context, opts related.SearchOpts) (Pages, error) { |
| 117 |
return p.withInvertedIndex(ctx, func(idx *related.InvertedIndex) ([]related.Document, error) { |
| 118 |
return idx.Search(ctx, opts) |
| 119 |
}) |
| 120 |
} |
| 121 |
|
| 122 |
func (p Pages) withInvertedIndex(ctx context.Context, search func(idx *related.InvertedIndex) ([]related.Document, error)) (Pages, error) { |
| 123 |
if len(p) == 0 { |
| 124 |
return nil, nil |
| 125 |
} |
| 126 |
|
| 127 |
d, ok := p[0].(RelatedDocsHandlerProvider) |
| 128 |
if !ok { |
| 129 |
return nil, fmt.Errorf("invalid type %T in related search", p[0]) |
| 130 |
} |
| 131 |
|
| 132 |
cache := d.GetInternalRelatedDocsHandler() |
| 133 |
|
| 134 |
searchIndex, err := cache.getOrCreateIndex(ctx, p) |
| 135 |
if err != nil { |
| 136 |
return nil, err |
| 137 |
} |
| 138 |
|
| 139 |
result, err := search(searchIndex) |
| 140 |
if err != nil { |
| 141 |
return nil, err |
| 142 |
} |
| 143 |
|
| 144 |
if len(result) > 0 { |
| 145 |
mp := make(Pages, len(result)) |
| 146 |
for i, match := range result { |
| 147 |
mp[i] = match.(Page) |
| 148 |
} |
| 149 |
return mp, nil |
| 150 |
} |
| 151 |
|
| 152 |
return nil, nil |
| 153 |
} |
| 154 |
|
| 155 |
type cachedPostingList struct { |
| 156 |
p Pages |
| 157 |
|
| 158 |
postingList *related.InvertedIndex |
| 159 |
} |
| 160 |
|
| 161 |
type RelatedDocsHandler struct { |
| 162 |
cfg related.Config |
| 163 |
|
| 164 |
postingLists []*cachedPostingList |
| 165 |
mu sync.RWMutex |
| 166 |
|
| 167 |
workers *para.Workers |
| 168 |
} |
| 169 |
|
| 170 |
func NewRelatedDocsHandler(cfg related.Config) *RelatedDocsHandler { |
| 171 |
return &RelatedDocsHandler{cfg: cfg, workers: para.New(config.GetNumWorkerMultiplier())} |
| 172 |
} |
| 173 |
|
| 174 |
func (s *RelatedDocsHandler) Clone() *RelatedDocsHandler { |
| 175 |
return NewRelatedDocsHandler(s.cfg) |
| 176 |
} |
| 177 |
|
| 178 |
// This assumes that a lock has been acquired. |
| 179 |
func (s *RelatedDocsHandler) getIndex(p Pages) *related.InvertedIndex { |
| 180 |
for _, ci := range s.postingLists { |
| 181 |
if pagesEqual(p, ci.p) { |
| 182 |
return ci.postingList |
| 183 |
} |
| 184 |
} |
| 185 |
return nil |
| 186 |
} |
| 187 |
|
| 188 |
func (s *RelatedDocsHandler) getOrCreateIndex(ctx context.Context, p Pages) (*related.InvertedIndex, error) { |
| 189 |
s.mu.RLock() |
| 190 |
cachedIndex := s.getIndex(p) |
| 191 |
if cachedIndex != nil { |
| 192 |
s.mu.RUnlock() |
| 193 |
return cachedIndex, nil |
| 194 |
} |
| 195 |
s.mu.RUnlock() |
| 196 |
|
| 197 |
s.mu.Lock() |
| 198 |
defer s.mu.Unlock() |
| 199 |
|
| 200 |
// Double check. |
| 201 |
if cachedIndex := s.getIndex(p); cachedIndex != nil { |
| 202 |
return cachedIndex, nil |
| 203 |
} |
| 204 |
|
| 205 |
for _, c := range s.cfg.Indices { |
| 206 |
if c.Type == related.TypeFragments { |
| 207 |
// This will trigger building the Pages' fragment map. |
| 208 |
g, _ := s.workers.Start(ctx) |
| 209 |
for _, page := range p { |
| 210 |
fp, ok := page.(related.FragmentProvider) |
| 211 |
if !ok { |
| 212 |
continue |
| 213 |
} |
| 214 |
g.Run(func() error { |
| 215 |
fp.Fragments(ctx) |
| 216 |
return nil |
| 217 |
}) |
| 218 |
} |
| 219 |
|
| 220 |
if err := g.Wait(); err != nil { |
| 221 |
return nil, err |
| 222 |
} |
| 223 |
|
| 224 |
break |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
searchIndex := related.NewInvertedIndex(s.cfg) |
| 229 |
|
| 230 |
for _, page := range p { |
| 231 |
if err := searchIndex.Add(ctx, page); err != nil { |
| 232 |
return nil, err |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
s.postingLists = append(s.postingLists, &cachedPostingList{p: p, postingList: searchIndex}) |
| 237 |
|
| 238 |
if err := searchIndex.Finalize(ctx); err != nil { |
| 239 |
return nil, err |
| 240 |
} |
| 241 |
|
| 242 |
return searchIndex, nil |
| 243 |
} |