| 1 |
// Copyright 2025 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 sitesmatrix |
| 15 |
|
| 16 |
import ( |
| 17 |
"cmp" |
| 18 |
"fmt" |
| 19 |
"iter" |
| 20 |
"maps" |
| 21 |
"slices" |
| 22 |
"sort" |
| 23 |
"sync" |
| 24 |
|
| 25 |
"github.com/gohugoio/hashstructure" |
| 26 |
"github.com/gohugoio/hugo/common/hashing" |
| 27 |
"github.com/gohugoio/hugo/common/hmaps" |
| 28 |
"github.com/gohugoio/hugo/common/predicate" |
| 29 |
"github.com/gohugoio/hugo/common/types" |
| 30 |
"github.com/gohugoio/hugo/hugofs/hglob" |
| 31 |
) |
| 32 |
|
| 33 |
var ( |
| 34 |
_ VectorStore = &IntSets{} |
| 35 |
_ VectorStore = &vectorStoreMap{} |
| 36 |
_ hashstructure.Hashable = &IntSets{} |
| 37 |
_ hashstructure.Hashable = &vectorStoreMap{} |
| 38 |
) |
| 39 |
|
| 40 |
func newVectorStoreMap(cap int) *vectorStoreMap { |
| 41 |
return &vectorStoreMap{ |
| 42 |
sets: make(Vectors, cap), |
| 43 |
h: &hashOnce{}, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func newVectorStoreMapFromVectors(v Vectors) *vectorStoreMap { |
| 48 |
return &vectorStoreMap{ |
| 49 |
sets: v, |
| 50 |
h: &hashOnce{}, |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// A vector store backed by a map. |
| 55 |
type vectorStoreMap struct { |
| 56 |
sets Vectors |
| 57 |
h *hashOnce |
| 58 |
} |
| 59 |
|
| 60 |
func (s *vectorStoreMap) initHash() { |
| 61 |
s.h.once.Do(func() { |
| 62 |
var err error |
| 63 |
s.h.hash, err = hashing.Hash(s.sets) |
| 64 |
if err != nil { |
| 65 |
panic(fmt.Errorf("failed to calculate hash for MapVectorStore: %w", err)) |
| 66 |
} |
| 67 |
}) |
| 68 |
} |
| 69 |
|
| 70 |
func (s *vectorStoreMap) setVector(vec Vector) { |
| 71 |
s.sets[vec] = struct{}{} |
| 72 |
} |
| 73 |
|
| 74 |
func (s *vectorStoreMap) KeysSorted() ([]int, []int, []int) { |
| 75 |
var k0, k1, k2 []int |
| 76 |
for v := range s.sets { |
| 77 |
k0 = append(k0, v.Language()) |
| 78 |
k1 = append(k1, v.Version()) |
| 79 |
k2 = append(k2, v.Role()) |
| 80 |
} |
| 81 |
sort.Ints(k0) |
| 82 |
sort.Ints(k1) |
| 83 |
sort.Ints(k2) |
| 84 |
k0 = slices.Compact(k0) |
| 85 |
k1 = slices.Compact(k1) |
| 86 |
k2 = slices.Compact(k2) |
| 87 |
|
| 88 |
return k0, k1, k2 |
| 89 |
} |
| 90 |
|
| 91 |
func (s *vectorStoreMap) Hash() (uint64, error) { |
| 92 |
s.initHash() |
| 93 |
return s.h.hash, nil |
| 94 |
} |
| 95 |
|
| 96 |
func (s *vectorStoreMap) MustHash() uint64 { |
| 97 |
i, _ := s.Hash() |
| 98 |
return i |
| 99 |
} |
| 100 |
|
| 101 |
func (s *vectorStoreMap) HasVector(v Vector) bool { |
| 102 |
if _, ok := s.sets[v]; ok { |
| 103 |
return true |
| 104 |
} |
| 105 |
return false |
| 106 |
} |
| 107 |
|
| 108 |
func (s *vectorStoreMap) HasAnyVector(v VectorProvider) bool { |
| 109 |
if v == nil || s.LenVectors() == 0 || v.LenVectors() == 0 { |
| 110 |
return false |
| 111 |
} |
| 112 |
|
| 113 |
return !v.ForEachVector(func(vec Vector) bool { |
| 114 |
if s.HasVector(vec) { |
| 115 |
return false // stop iteration |
| 116 |
} |
| 117 |
return true // continue iteration |
| 118 |
}) |
| 119 |
} |
| 120 |
|
| 121 |
func (s *vectorStoreMap) LenVectors() int { |
| 122 |
return len(s.sets) |
| 123 |
} |
| 124 |
|
| 125 |
// Complement returns a new VectorStore that contains all vectors in s that are not in any of ss. |
| 126 |
func (s *vectorStoreMap) Complement(ss ...VectorProvider) VectorStore { |
| 127 |
if len(ss) == 0 || (len(ss) == 1 && ss[0] == s) { |
| 128 |
return nil |
| 129 |
} |
| 130 |
|
| 131 |
for _, v := range ss { |
| 132 |
if v == s { |
| 133 |
var s *vectorStoreMap |
| 134 |
return s |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
result := newVectorStoreMap(36) |
| 139 |
|
| 140 |
s.ForEachVector(func(vec Vector) bool { |
| 141 |
var found bool |
| 142 |
for _, v := range ss { |
| 143 |
if v.HasVector(vec) { |
| 144 |
found = true |
| 145 |
break |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
if !found { |
| 150 |
result.setVector(vec) |
| 151 |
} |
| 152 |
|
| 153 |
return true |
| 154 |
}) |
| 155 |
|
| 156 |
return result |
| 157 |
} |
| 158 |
|
| 159 |
func (s *vectorStoreMap) EqualsVector(other VectorProvider) bool { |
| 160 |
if other == nil { |
| 161 |
return false |
| 162 |
} |
| 163 |
if s == other { |
| 164 |
return true |
| 165 |
} |
| 166 |
if s.LenVectors() != other.LenVectors() { |
| 167 |
return false |
| 168 |
} |
| 169 |
return other.ForEachVector(func(v Vector) bool { |
| 170 |
_, ok := s.sets[v] |
| 171 |
return ok |
| 172 |
}) |
| 173 |
} |
| 174 |
|
| 175 |
func (s *vectorStoreMap) VectorSample() Vector { |
| 176 |
if len(s.sets) == 0 { |
| 177 |
panic("no vectors available") |
| 178 |
} |
| 179 |
for v := range s.sets { |
| 180 |
return v |
| 181 |
} |
| 182 |
panic("unreachable") |
| 183 |
} |
| 184 |
|
| 185 |
func (s *vectorStoreMap) ForEachVector(yield func(v Vector) bool) bool { |
| 186 |
if len(s.sets) == 0 { |
| 187 |
return true |
| 188 |
} |
| 189 |
for v := range s.sets { |
| 190 |
if !yield(v) { |
| 191 |
return false |
| 192 |
} |
| 193 |
} |
| 194 |
return true |
| 195 |
} |
| 196 |
|
| 197 |
func (s *vectorStoreMap) Vectors() []Vector { |
| 198 |
if s == nil || len(s.sets) == 0 { |
| 199 |
return nil |
| 200 |
} |
| 201 |
var vectors []Vector |
| 202 |
for v := range s.sets { |
| 203 |
vectors = append(vectors, v) |
| 204 |
} |
| 205 |
sort.Slice(vectors, func(i, j int) bool { |
| 206 |
v1, v2 := vectors[i], vectors[j] |
| 207 |
return v1.Compare(v2) < 0 |
| 208 |
}) |
| 209 |
return vectors |
| 210 |
} |
| 211 |
|
| 212 |
func (s *vectorStoreMap) WithLanguageIndices(i ...int) VectorStore { |
| 213 |
c := s.clone() |
| 214 |
|
| 215 |
for v := range c.sets { |
| 216 |
for _, i := range i { |
| 217 |
nv := v |
| 218 |
nv[Language] = i |
| 219 |
c.sets[nv] = struct{}{} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return c |
| 224 |
} |
| 225 |
|
| 226 |
func (s *vectorStoreMap) WithVersionIndices(indices ...int) VectorStore { |
| 227 |
old := s.clone() |
| 228 |
c := newVectorStoreMap(len(old.sets) * len(indices)) |
| 229 |
for v := range old.sets { |
| 230 |
for _, i := range indices { |
| 231 |
nv := v |
| 232 |
nv[Version] = i |
| 233 |
c.sets[nv] = struct{}{} |
| 234 |
} |
| 235 |
} |
| 236 |
return c |
| 237 |
} |
| 238 |
|
| 239 |
func (s *vectorStoreMap) WithRoleIndices(indices ...int) VectorStore { |
| 240 |
old := s.clone() |
| 241 |
c := newVectorStoreMap(len(old.sets) * len(indices)) |
| 242 |
for v := range old.sets { |
| 243 |
for _, i := range indices { |
| 244 |
nv := v |
| 245 |
nv[Role] = i |
| 246 |
c.sets[nv] = struct{}{} |
| 247 |
} |
| 248 |
} |
| 249 |
return c |
| 250 |
} |
| 251 |
|
| 252 |
func (s *vectorStoreMap) HasLanguage(i int) bool { |
| 253 |
for v := range s.sets { |
| 254 |
if v.Language() == i { |
| 255 |
return true |
| 256 |
} |
| 257 |
} |
| 258 |
return false |
| 259 |
} |
| 260 |
|
| 261 |
func (s *vectorStoreMap) HasVersion(i int) bool { |
| 262 |
for v := range s.sets { |
| 263 |
if v.Version() == i { |
| 264 |
return true |
| 265 |
} |
| 266 |
} |
| 267 |
return false |
| 268 |
} |
| 269 |
|
| 270 |
func (s *vectorStoreMap) HasRole(i int) bool { |
| 271 |
for v := range s.sets { |
| 272 |
if v.Role() == i { |
| 273 |
return true |
| 274 |
} |
| 275 |
} |
| 276 |
return false |
| 277 |
} |
| 278 |
|
| 279 |
func (s *vectorStoreMap) clone() *vectorStoreMap { |
| 280 |
c := *s |
| 281 |
c.h = &hashOnce{} |
| 282 |
c.sets = maps.Clone(s.sets) |
| 283 |
return &c |
| 284 |
} |
| 285 |
|
| 286 |
func NewIntSetsBuilder(cfg *ConfiguredDimensions) *IntSetsBuilder { |
| 287 |
if cfg == nil { |
| 288 |
panic("cfg is required") |
| 289 |
} |
| 290 |
return &IntSetsBuilder{cfg: cfg, s: &IntSets{h: &hashOnce{}}} |
| 291 |
} |
| 292 |
|
| 293 |
type ConfiguredDimension interface { |
| 294 |
predicate.IndexMatcher |
| 295 |
IndexDefault() int |
| 296 |
ResolveIndex(string) int |
| 297 |
ResolveName(int) string |
| 298 |
ForEachIndex() iter.Seq[int] |
| 299 |
Len() int |
| 300 |
} |
| 301 |
|
| 302 |
// ConfiguredDimensions holds the configured dimensions for the site matrix. |
| 303 |
type ConfiguredDimensions struct { |
| 304 |
ConfiguredLanguages ConfiguredDimension |
| 305 |
ConfiguredVersions ConfiguredDimension |
| 306 |
ConfiguredRoles ConfiguredDimension |
| 307 |
CommonSitesMatrix CommonSitestMatrix |
| 308 |
|
| 309 |
singleVectorStoreCache *hmaps.Cache[Vector, *IntSets] |
| 310 |
} |
| 311 |
|
| 312 |
func (c *ConfiguredDimensions) IsSingleVector() bool { |
| 313 |
return c.ConfiguredLanguages.Len() == 1 && c.ConfiguredRoles.Len() == 1 && c.ConfiguredVersions.Len() == 1 |
| 314 |
} |
| 315 |
|
| 316 |
// GetOrCreateSingleVectorStore returns a VectorStore for the given vector. |
| 317 |
func (c *ConfiguredDimensions) GetOrCreateSingleVectorStore(vec Vector) *IntSets { |
| 318 |
store, _ := c.singleVectorStoreCache.GetOrCreate(vec, func() (*IntSets, error) { |
| 319 |
is := &IntSets{} |
| 320 |
is.setValuesInNilSets(vec, true, true, true) |
| 321 |
return is, nil |
| 322 |
}) |
| 323 |
return store |
| 324 |
} |
| 325 |
|
| 326 |
func (c *ConfiguredDimensions) Init() error { |
| 327 |
c.singleVectorStoreCache = hmaps.NewCache[Vector, *IntSets]() |
| 328 |
b := NewIntSetsBuilder(c).WithDefaultsIfNotSet().Build() |
| 329 |
defaultVec := b.VectorSample() |
| 330 |
c.singleVectorStoreCache.Set(defaultVec, b) |
| 331 |
c.CommonSitesMatrix.DefaultSite = b |
| 332 |
|
| 333 |
return nil |
| 334 |
} |
| 335 |
|
| 336 |
type CommonSitestMatrix struct { |
| 337 |
DefaultSite VectorStore |
| 338 |
} |
| 339 |
|
| 340 |
func (c *ConfiguredDimensions) ResolveNames(v Vector) types.Strings3 { |
| 341 |
return types.Strings3{ |
| 342 |
c.ConfiguredLanguages.ResolveName(v.Language()), |
| 343 |
c.ConfiguredVersions.ResolveName(v.Version()), |
| 344 |
c.ConfiguredRoles.ResolveName(v.Role()), |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
func (c *ConfiguredDimensions) ResolveVector(names types.Strings3) Vector { |
| 349 |
var vec Vector |
| 350 |
if s := names[0]; s != "" { |
| 351 |
vec[0] = c.ConfiguredLanguages.ResolveIndex(s) |
| 352 |
} else { |
| 353 |
vec[0] = c.ConfiguredLanguages.IndexDefault() |
| 354 |
} |
| 355 |
if s := names[1]; s != "" { |
| 356 |
vec[1] = c.ConfiguredVersions.ResolveIndex(s) |
| 357 |
} else { |
| 358 |
vec[1] = c.ConfiguredVersions.IndexDefault() |
| 359 |
} |
| 360 |
if s := names[2]; s != "" { |
| 361 |
vec[2] = c.ConfiguredRoles.ResolveIndex(s) |
| 362 |
} else { |
| 363 |
vec[2] = c.ConfiguredRoles.IndexDefault() |
| 364 |
} |
| 365 |
return vec |
| 366 |
} |
| 367 |
|
| 368 |
// IntSets holds the ordered sets of integers for the dimensions, |
| 369 |
// which is used for fast membership testing of files, resources and pages. |
| 370 |
type IntSets struct { |
| 371 |
languages *hmaps.OrderedIntSet `mapstructure:"-" json:"-"` |
| 372 |
versions *hmaps.OrderedIntSet `mapstructure:"-" json:"-"` |
| 373 |
roles *hmaps.OrderedIntSet `mapstructure:"-" json:"-"` |
| 374 |
|
| 375 |
h *hashOnce |
| 376 |
} |
| 377 |
|
| 378 |
var NilStore *IntSets = nil |
| 379 |
|
| 380 |
type hashOnce struct { |
| 381 |
once sync.Once |
| 382 |
hash uint64 |
| 383 |
} |
| 384 |
|
| 385 |
func (s *IntSets) ToVectorStoreMap() *vectorStoreMap { |
| 386 |
if s == nil { |
| 387 |
return nil |
| 388 |
} |
| 389 |
|
| 390 |
result := newVectorStoreMap(36) |
| 391 |
|
| 392 |
s.ForEachVector(func(vec Vector) bool { |
| 393 |
result.setVector(vec) |
| 394 |
return true |
| 395 |
}) |
| 396 |
|
| 397 |
return result |
| 398 |
} |
| 399 |
|
| 400 |
func (s *IntSets) IsSuperSet(other *IntSets) bool { |
| 401 |
return s.languages.IsSuperSet(other.languages) && |
| 402 |
s.versions.IsSuperSet(other.versions) && |
| 403 |
s.roles.IsSuperSet(other.roles) |
| 404 |
} |
| 405 |
|
| 406 |
func (s *IntSets) DifferenceCardinality(other *IntSets) Vector { |
| 407 |
return Vector{ |
| 408 |
int(s.languages.Values().DifferenceCardinality(other.languages.Values())), |
| 409 |
int(s.versions.Values().DifferenceCardinality(other.versions.Values())), |
| 410 |
int(s.roles.Values().DifferenceCardinality(other.roles.Values())), |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
func (s *IntSets) Intersects(other *IntSets) bool { |
| 415 |
if s == nil || other == nil { |
| 416 |
return false |
| 417 |
} |
| 418 |
return s.languages.Values().IntersectionCardinality(other.languages.Values()) > 0 && |
| 419 |
s.versions.Values().IntersectionCardinality(other.versions.Values()) > 0 && |
| 420 |
s.roles.Values().IntersectionCardinality(other.roles.Values()) > 0 |
| 421 |
} |
| 422 |
|
| 423 |
// Complement returns a new VectorStore that contains all vectors in s that are not in any of ss. |
| 424 |
func (s *IntSets) Complement(ss ...VectorProvider) VectorStore { |
| 425 |
if len(ss) == 0 || (len(ss) == 1 && ss[0] == s) { |
| 426 |
return NilStore |
| 427 |
} |
| 428 |
|
| 429 |
for _, v := range ss { |
| 430 |
vv, ok := v.(*IntSets) |
| 431 |
if !ok { |
| 432 |
continue |
| 433 |
} |
| 434 |
if vv.IsSuperSet(s) { |
| 435 |
return NilStore |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
result := newVectorStoreMap(36) |
| 440 |
|
| 441 |
s.ForEachVector(func(vec Vector) bool { |
| 442 |
var found bool |
| 443 |
for _, v := range ss { |
| 444 |
if v.HasVector(vec) { |
| 445 |
found = true |
| 446 |
break |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if !found { |
| 451 |
result.setVector(vec) |
| 452 |
} |
| 453 |
|
| 454 |
return true |
| 455 |
}) |
| 456 |
|
| 457 |
return result |
| 458 |
} |
| 459 |
|
| 460 |
func (s *IntSets) EqualsVector(other VectorProvider) bool { |
| 461 |
if s == nil && other == nil { |
| 462 |
return true |
| 463 |
} |
| 464 |
if s == nil || other == nil { |
| 465 |
return false |
| 466 |
} |
| 467 |
if s == other { |
| 468 |
return true |
| 469 |
} |
| 470 |
if s.LenVectors() != other.LenVectors() { |
| 471 |
return false |
| 472 |
} |
| 473 |
|
| 474 |
return other.ForEachVector(func(v Vector) bool { |
| 475 |
return s.HasVector(v) |
| 476 |
}) |
| 477 |
} |
| 478 |
|
| 479 |
func (s *IntSets) VectorSample() Vector { |
| 480 |
if s.LenVectors() == 0 { |
| 481 |
panic("no vectors available") |
| 482 |
} |
| 483 |
|
| 484 |
return Vector{ |
| 485 |
s.languages.Next(0), |
| 486 |
s.versions.Next(0), |
| 487 |
s.roles.Next(0), |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
// The reason we don't use iter.Seq is https://github.com/golang/go/issues/69015 |
| 492 |
// This is 60% faster and allocation free. |
| 493 |
// The yield function should return false to stop iteration. |
| 494 |
func (s *IntSets) ForEachVector(yield func(v Vector) bool) bool { |
| 495 |
if s.LenVectors() == 0 { |
| 496 |
return true |
| 497 |
} |
| 498 |
|
| 499 |
b := s.languages.ForEachKey(func(lang int) bool { |
| 500 |
return s.versions.ForEachKey(func(ver int) bool { |
| 501 |
return s.roles.ForEachKey(func(role int) bool { |
| 502 |
return yield(Vector{lang, ver, role}) |
| 503 |
}) |
| 504 |
}) |
| 505 |
}) |
| 506 |
|
| 507 |
return b |
| 508 |
} |
| 509 |
|
| 510 |
func (s *IntSets) Vectors() []Vector { |
| 511 |
if s.LenVectors() == 0 { |
| 512 |
return nil |
| 513 |
} |
| 514 |
|
| 515 |
var vectors []Vector |
| 516 |
s.ForEachVector(func(v Vector) bool { |
| 517 |
vectors = append(vectors, v) |
| 518 |
return true |
| 519 |
}) |
| 520 |
|
| 521 |
sort.Slice(vectors, func(i, j int) bool { |
| 522 |
v1, v2 := vectors[i], vectors[j] |
| 523 |
return v1.Compare(v2) < 0 |
| 524 |
}) |
| 525 |
|
| 526 |
return vectors |
| 527 |
} |
| 528 |
|
| 529 |
func (s *IntSets) KeysSorted() ([]int, []int, []int) { |
| 530 |
if s == nil { |
| 531 |
return nil, nil, nil |
| 532 |
} |
| 533 |
languages := s.languages.KeysSorted() |
| 534 |
versions := s.versions.KeysSorted() |
| 535 |
roles := s.roles.KeysSorted() |
| 536 |
return languages, versions, roles |
| 537 |
} |
| 538 |
|
| 539 |
func (s *IntSets) HasLanguage(lang int) bool { |
| 540 |
if s == nil { |
| 541 |
return false |
| 542 |
} |
| 543 |
return s.languages.Has(lang) |
| 544 |
} |
| 545 |
|
| 546 |
// LenVectors returns the total number of vectors represented by the IntSets. |
| 547 |
// This is the Cartesian product of the lengths of the individual sets. |
| 548 |
// This will be 0 if s is nil or any of the sets is empty. |
| 549 |
func (s *IntSets) LenVectors() int { |
| 550 |
if s == nil { |
| 551 |
return 0 |
| 552 |
} |
| 553 |
return s.languages.Len() * s.versions.Len() * s.roles.Len() |
| 554 |
} |
| 555 |
|
| 556 |
func (s *IntSets) HasRole(role int) bool { |
| 557 |
if s == nil { |
| 558 |
return false |
| 559 |
} |
| 560 |
return s.roles.Has(role) |
| 561 |
} |
| 562 |
|
| 563 |
func (s *IntSets) String() string { |
| 564 |
return fmt.Sprintf("Languages: %v, Versions: %v, Roles: %v", s.languages, s.versions, s.roles) |
| 565 |
} |
| 566 |
|
| 567 |
// HasVector checks if the given vector is contained in the sets. |
| 568 |
func (s *IntSets) HasVector(v Vector) bool { |
| 569 |
if s == nil { |
| 570 |
return false |
| 571 |
} |
| 572 |
if !s.languages.Has(v.Language()) { |
| 573 |
return false |
| 574 |
} |
| 575 |
if !s.versions.Has(v.Version()) { |
| 576 |
return false |
| 577 |
} |
| 578 |
if !s.roles.Has(v.Role()) { |
| 579 |
return false |
| 580 |
} |
| 581 |
return true |
| 582 |
} |
| 583 |
|
| 584 |
func (s *IntSets) HasAnyVector(v VectorProvider) bool { |
| 585 |
if s == nil || v == nil { |
| 586 |
return false |
| 587 |
} |
| 588 |
if s.LenVectors() == 0 || v.LenVectors() == 0 { |
| 589 |
return false |
| 590 |
} |
| 591 |
if v.LenVectors() == 1 { |
| 592 |
// Fast path. |
| 593 |
return s.HasVector(v.VectorSample()) |
| 594 |
} |
| 595 |
|
| 596 |
if vs, ok := v.(*IntSets); ok { |
| 597 |
// Fast path. |
| 598 |
return s.Intersects(vs) |
| 599 |
} |
| 600 |
|
| 601 |
return !v.ForEachVector(func(vec Vector) bool { |
| 602 |
if s.HasVector(vec) { |
| 603 |
return false // stop iteration |
| 604 |
} |
| 605 |
return true // continue iteration |
| 606 |
}) |
| 607 |
} |
| 608 |
|
| 609 |
func (s *IntSets) HasVersion(ver int) bool { |
| 610 |
if s == nil { |
| 611 |
return false |
| 612 |
} |
| 613 |
return s.versions.Has(ver) |
| 614 |
} |
| 615 |
|
| 616 |
func (s IntSets) shallowClone() *IntSets { |
| 617 |
s.h = &hashOnce{} |
| 618 |
return &s |
| 619 |
} |
| 620 |
|
| 621 |
// WithLanguageIndices replaces the current language set with the given language indices. |
| 622 |
func (s *IntSets) WithLanguageIndices(indices ...int) VectorStore { |
| 623 |
c := s.shallowClone() |
| 624 |
c.languages = hmaps.NewOrderedIntSet(indices...) |
| 625 |
return c.init() |
| 626 |
} |
| 627 |
|
| 628 |
// WithVersionIndices replaces the current version set with the given version indices. |
| 629 |
func (s *IntSets) WithVersionIndices(indices ...int) VectorStore { |
| 630 |
c := s.shallowClone() |
| 631 |
c.versions = hmaps.NewOrderedIntSet(indices...) |
| 632 |
return c.init() |
| 633 |
} |
| 634 |
|
| 635 |
// WithRoleIndices replaces the current role set with the given role indices. |
| 636 |
func (s *IntSets) WithRoleIndices(indices ...int) VectorStore { |
| 637 |
c := s.shallowClone() |
| 638 |
c.roles = hmaps.NewOrderedIntSet(indices...) |
| 639 |
return c.init() |
| 640 |
} |
| 641 |
|
| 642 |
func (s *IntSets) Hash() (uint64, error) { |
| 643 |
s.initHash() |
| 644 |
return s.h.hash, nil |
| 645 |
} |
| 646 |
|
| 647 |
func (s *IntSets) MustHash() uint64 { |
| 648 |
if s == nil { |
| 649 |
return 0 |
| 650 |
} |
| 651 |
hash, err := s.Hash() |
| 652 |
if err != nil { |
| 653 |
panic(fmt.Errorf("failed to calculate hash for IntSets: %w", err)) |
| 654 |
} |
| 655 |
return hash |
| 656 |
} |
| 657 |
|
| 658 |
// setDefaultsIfNotSet applies default values to the IntSets if they are not already set. |
| 659 |
func (s *IntSets) setDefaultsIfNotSet(cfg *ConfiguredDimensions) { |
| 660 |
if s.languages == nil { |
| 661 |
s.languages = hmaps.NewOrderedIntSet() |
| 662 |
s.languages.Set(cfg.ConfiguredLanguages.IndexDefault()) |
| 663 |
} |
| 664 |
if s.versions == nil { |
| 665 |
s.versions = hmaps.NewOrderedIntSet() |
| 666 |
s.versions.Set(cfg.ConfiguredVersions.IndexDefault()) |
| 667 |
} |
| 668 |
if s.roles == nil { |
| 669 |
s.roles = hmaps.NewOrderedIntSet() |
| 670 |
s.roles.Set(cfg.ConfiguredRoles.IndexDefault()) |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
func (s *IntSets) setDefaultsAndAllLAnguagesIfNotSet(cfg *ConfiguredDimensions) { |
| 675 |
if s.languages == nil { |
| 676 |
s.languages = hmaps.NewOrderedIntSet() |
| 677 |
for i := range cfg.ConfiguredLanguages.ForEachIndex() { |
| 678 |
s.languages.Set(i) |
| 679 |
} |
| 680 |
} |
| 681 |
s.setDefaultsIfNotSet(cfg) |
| 682 |
} |
| 683 |
|
| 684 |
func (s *IntSets) setAllIfNotSet(cfg *ConfiguredDimensions) { |
| 685 |
if s.languages == nil { |
| 686 |
s.languages = hmaps.NewOrderedIntSet() |
| 687 |
for i := range cfg.ConfiguredLanguages.ForEachIndex() { |
| 688 |
s.languages.Set(i) |
| 689 |
} |
| 690 |
} |
| 691 |
if s.versions == nil { |
| 692 |
s.versions = hmaps.NewOrderedIntSet() |
| 693 |
for i := range cfg.ConfiguredVersions.ForEachIndex() { |
| 694 |
s.versions.Set(i) |
| 695 |
} |
| 696 |
} |
| 697 |
if s.roles == nil { |
| 698 |
s.roles = hmaps.NewOrderedIntSet() |
| 699 |
for i := range cfg.ConfiguredRoles.ForEachIndex() { |
| 700 |
s.roles.Set(i) |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
func (s *IntSets) initHash() { |
| 706 |
s.h.once.Do(func() { |
| 707 |
var err error |
| 708 |
s.h.hash, err = hashing.Hash(s.languages.Words(), s.versions.Words(), s.roles.Words()) |
| 709 |
if err != nil { |
| 710 |
panic(fmt.Errorf("failed to calculate hash for IntSets: %w", err)) |
| 711 |
} |
| 712 |
}) |
| 713 |
} |
| 714 |
|
| 715 |
func (s *IntSets) init() *IntSets { |
| 716 |
return s |
| 717 |
} |
| 718 |
|
| 719 |
func (s *IntSets) setDimensionsFromOtherIfNotSet(other VectorIterator) { |
| 720 |
if other == nil { |
| 721 |
return |
| 722 |
} |
| 723 |
setLang := s.languages == nil |
| 724 |
setVer := s.versions == nil |
| 725 |
setRole := s.roles == nil |
| 726 |
|
| 727 |
if !(setLang || setVer || setRole) { |
| 728 |
return |
| 729 |
} |
| 730 |
|
| 731 |
other.ForEachVector(func(v Vector) bool { |
| 732 |
if !s.HasVector(v) { |
| 733 |
s.setValuesInNilSets(v, setLang, setVer, setRole) |
| 734 |
} |
| 735 |
return true |
| 736 |
}) |
| 737 |
} |
| 738 |
|
| 739 |
func (s *IntSets) setValuesInNilSets(vec Vector, setLang, setVer, setRole bool) { |
| 740 |
if setLang { |
| 741 |
if s.languages == nil { |
| 742 |
s.languages = hmaps.NewOrderedIntSet() |
| 743 |
} |
| 744 |
s.languages.Set(vec.Language()) |
| 745 |
} |
| 746 |
if setVer { |
| 747 |
if s.versions == nil { |
| 748 |
s.versions = hmaps.NewOrderedIntSet() |
| 749 |
} |
| 750 |
s.versions.Set(vec.Version()) |
| 751 |
} |
| 752 |
|
| 753 |
if setRole { |
| 754 |
if s.roles == nil { |
| 755 |
s.roles = hmaps.NewOrderedIntSet() |
| 756 |
} |
| 757 |
s.roles.Set(vec.Role()) |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
type IntSetsBuilder struct { |
| 762 |
cfg *ConfiguredDimensions |
| 763 |
s *IntSets |
| 764 |
|
| 765 |
// Set when a Glob (e.g. "en") filter is provided but no matches are found. |
| 766 |
GlobFilterMisses Bools |
| 767 |
} |
| 768 |
|
| 769 |
func (b *IntSetsBuilder) Build() *IntSets { |
| 770 |
b.s.init() |
| 771 |
|
| 772 |
if b.s.LenVectors() == 1 { |
| 773 |
// Cache it or use the existing cached version, which will allow b.s to be GCed. |
| 774 |
bb, _ := b.cfg.singleVectorStoreCache.GetOrCreate(b.s.VectorSample(), func() (*IntSets, error) { |
| 775 |
return b.s, nil |
| 776 |
}) |
| 777 |
return bb |
| 778 |
} |
| 779 |
return b.s |
| 780 |
} |
| 781 |
|
| 782 |
func (b *IntSetsBuilder) WithConfig(cfg IntSetsConfig) *IntSetsBuilder { |
| 783 |
applyFilter := func(what string, values []string, matcher ConfiguredDimension) (*hmaps.OrderedIntSet, error) { |
| 784 |
var result *hmaps.OrderedIntSet |
| 785 |
if len(values) == 0 { |
| 786 |
|
| 787 |
if cfg.ApplyDefaults > 0 { |
| 788 |
result = hmaps.NewOrderedIntSet() |
| 789 |
} |
| 790 |
switch cfg.ApplyDefaults { |
| 791 |
case IntSetsConfigApplyDefaultsIfNotSet: |
| 792 |
result.Set(matcher.IndexDefault()) |
| 793 |
case IntSetsConfigApplyDefaultsAndAllLanguagesIfNotSet: |
| 794 |
if what == "languages" { |
| 795 |
for i := range matcher.ForEachIndex() { |
| 796 |
result.Set(i) |
| 797 |
} |
| 798 |
} else { |
| 799 |
result.Set(matcher.IndexDefault()) |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
return result, nil |
| 804 |
} |
| 805 |
|
| 806 |
filter, err := predicate.NewIndexStringPredicateFromGlobsAndRanges(values, matcher.ResolveIndex, hglob.GetGlobDot) |
| 807 |
if err != nil { |
| 808 |
return nil, fmt.Errorf("failed to create filter for %s: %w", what, err) |
| 809 |
} |
| 810 |
iter, err := matcher.IndexMatch(filter) |
| 811 |
if err != nil { |
| 812 |
return nil, fmt.Errorf("failed to match %s %q: %w", what, values, err) |
| 813 |
} |
| 814 |
for i := range iter { |
| 815 |
if result == nil { |
| 816 |
result = hmaps.NewOrderedIntSet() |
| 817 |
} |
| 818 |
result.Set(i) |
| 819 |
} |
| 820 |
|
| 821 |
return result, nil |
| 822 |
} |
| 823 |
|
| 824 |
l, err1 := applyFilter("languages", cfg.Globs.Languages, b.cfg.ConfiguredLanguages) |
| 825 |
v, err2 := applyFilter("versions", cfg.Globs.Versions, b.cfg.ConfiguredVersions) |
| 826 |
r, err3 := applyFilter("roles", cfg.Globs.Roles, b.cfg.ConfiguredRoles) |
| 827 |
|
| 828 |
if err := cmp.Or(err1, err2, err3); err != nil { |
| 829 |
panic(fmt.Errorf("failed to apply filters: %w", err)) |
| 830 |
} |
| 831 |
|
| 832 |
b.GlobFilterMisses = Bools{ |
| 833 |
len(cfg.Globs.Languages) > 0 && l == nil, |
| 834 |
len(cfg.Globs.Versions) > 0 && v == nil, |
| 835 |
len(cfg.Globs.Roles) > 0 && r == nil, |
| 836 |
} |
| 837 |
|
| 838 |
b.s.languages = l |
| 839 |
b.s.versions = v |
| 840 |
b.s.roles = r |
| 841 |
|
| 842 |
return b |
| 843 |
} |
| 844 |
|
| 845 |
func (s *IntSetsBuilder) WithLanguageIndices(idxs ...int) *IntSetsBuilder { |
| 846 |
if len(idxs) == 0 { |
| 847 |
return s |
| 848 |
} |
| 849 |
if s.s.languages == nil { |
| 850 |
s.s.languages = hmaps.NewOrderedIntSet() |
| 851 |
} |
| 852 |
for _, i := range idxs { |
| 853 |
s.s.languages.Set(i) |
| 854 |
} |
| 855 |
return s |
| 856 |
} |
| 857 |
|
| 858 |
func (s *IntSetsBuilder) WithVersionIndices(idxs ...int) *IntSetsBuilder { |
| 859 |
if len(idxs) == 0 { |
| 860 |
return s |
| 861 |
} |
| 862 |
if s.s.versions == nil { |
| 863 |
s.s.versions = hmaps.NewOrderedIntSet() |
| 864 |
} |
| 865 |
for _, i := range idxs { |
| 866 |
s.s.versions.Set(i) |
| 867 |
} |
| 868 |
return s |
| 869 |
} |
| 870 |
|
| 871 |
func (s *IntSetsBuilder) WithRoleIndices(idxs ...int) *IntSetsBuilder { |
| 872 |
if len(idxs) == 0 { |
| 873 |
return s |
| 874 |
} |
| 875 |
if s.s.roles == nil { |
| 876 |
s.s.roles = hmaps.NewOrderedIntSet() |
| 877 |
} |
| 878 |
for _, i := range idxs { |
| 879 |
s.s.roles.Set(i) |
| 880 |
} |
| 881 |
return s |
| 882 |
} |
| 883 |
|
| 884 |
func (s *IntSetsBuilder) WithDefaultsAndAllLanguagesIfNotSet() *IntSetsBuilder { |
| 885 |
s.s.setDefaultsAndAllLAnguagesIfNotSet(s.cfg) |
| 886 |
return s |
| 887 |
} |
| 888 |
|
| 889 |
func (s *IntSetsBuilder) WithAllIfNotSet() *IntSetsBuilder { |
| 890 |
s.s.setAllIfNotSet(s.cfg) |
| 891 |
return s |
| 892 |
} |
| 893 |
|
| 894 |
func (s *IntSetsBuilder) WithDefaultsIfNotSet() *IntSetsBuilder { |
| 895 |
s.s.setDefaultsIfNotSet(s.cfg) |
| 896 |
return s |
| 897 |
} |
| 898 |
|
| 899 |
func (s *IntSetsBuilder) WithDimensionsFromOtherIfNotSet(other VectorIterator) *IntSetsBuilder { |
| 900 |
s.s.setDimensionsFromOtherIfNotSet(other) |
| 901 |
return s |
| 902 |
} |
| 903 |
|
| 904 |
func (b *IntSetsBuilder) WithSets(languages, versions, roles *hmaps.OrderedIntSet) *IntSetsBuilder { |
| 905 |
b.s.languages = languages |
| 906 |
b.s.versions = versions |
| 907 |
b.s.roles = roles |
| 908 |
return b |
| 909 |
} |
| 910 |
|
| 911 |
type IntSetsConfigApplyDefaults int |
| 912 |
|
| 913 |
const ( |
| 914 |
IntSetsConfigApplyDefaultsNone IntSetsConfigApplyDefaults = iota |
| 915 |
IntSetsConfigApplyDefaultsIfNotSet |
| 916 |
IntSetsConfigApplyDefaultsAndAllLanguagesIfNotSet |
| 917 |
) |
| 918 |
|
| 919 |
type IntSetsConfig struct { |
| 920 |
ApplyDefaults IntSetsConfigApplyDefaults |
| 921 |
Globs StringSlices |
| 922 |
} |
| 923 |
|
| 924 |
// Sites holds configuration about which sites a file/content/page/resource belongs to. |
| 925 |
type Sites struct { |
| 926 |
// Matrix defines what sites to build this content for. |
| 927 |
Matrix StringSlices `mapstructure:"matrix" json:"matrix"` |
| 928 |
// Complements defines what sites to complement with this content. |
| 929 |
Complements StringSlices `mapstructure:"complements" json:"complements"` |
| 930 |
} |
| 931 |
|
| 932 |
func (s *Sites) Equal(other Sites) bool { |
| 933 |
return s.Matrix.Equal(other.Matrix) && s.Complements.Equal(other.Complements) |
| 934 |
} |
| 935 |
|
| 936 |
func (s *Sites) SetFromParamsIfNotSet(params hmaps.Params) { |
| 937 |
const ( |
| 938 |
matrixKey = "matrix" |
| 939 |
complementsKey = "complements" |
| 940 |
) |
| 941 |
|
| 942 |
if m, ok := params[matrixKey]; ok { |
| 943 |
s.Matrix.SetFromParamsIfNotSet(m.(hmaps.Params)) |
| 944 |
} |
| 945 |
if f, ok := params[complementsKey]; ok { |
| 946 |
s.Complements.SetFromParamsIfNotSet(f.(hmaps.Params)) |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
// IsZero returns true if all slices are empty. |
| 951 |
func (s Sites) IsZero() bool { |
| 952 |
return s.Matrix.IsZero() && s.Complements.IsZero() |
| 953 |
} |
| 954 |
|
| 955 |
// StringSlices holds slices of Glob patterns for languages, versions, and roles. |
| 956 |
type StringSlices struct { |
| 957 |
Languages []string `mapstructure:"languages" json:"languages"` |
| 958 |
Versions []string `mapstructure:"versions" json:"versions"` |
| 959 |
Roles []string `mapstructure:"roles" json:"roles"` |
| 960 |
} |
| 961 |
|
| 962 |
func (d StringSlices) Equal(other StringSlices) bool { |
| 963 |
return slices.Equal(d.Languages, other.Languages) && |
| 964 |
slices.Equal(d.Versions, other.Versions) && |
| 965 |
slices.Equal(d.Roles, other.Roles) |
| 966 |
} |
| 967 |
|
| 968 |
func (d *StringSlices) SetFromParamsIfNotSet(params hmaps.Params) { |
| 969 |
const ( |
| 970 |
languagesKey = "languages" |
| 971 |
versionsKey = "versions" |
| 972 |
rolesKey = "roles" |
| 973 |
) |
| 974 |
|
| 975 |
if len(d.Languages) == 0 { |
| 976 |
if v, ok := params[languagesKey]; ok { |
| 977 |
d.Languages = types.ToStringSlicePreserveString(v) |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
if len(d.Versions) == 0 { |
| 982 |
if v, ok := params[versionsKey]; ok { |
| 983 |
d.Versions = types.ToStringSlicePreserveString(v) |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
if len(d.Roles) == 0 { |
| 988 |
if v, ok := params[rolesKey]; ok { |
| 989 |
d.Roles = types.ToStringSlicePreserveString(v) |
| 990 |
} |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
func (d StringSlices) IsZero() bool { |
| 995 |
return len(d.Languages) == 0 && len(d.Versions) == 0 && len(d.Roles) == 0 |
| 996 |
} |
| 997 |
|
| 998 |
// Used in tests. |
| 999 |
type testDimension struct { |
| 1000 |
names []string |
| 1001 |
} |
| 1002 |
|
| 1003 |
func (m testDimension) Len() int { |
| 1004 |
return len(m.names) |
| 1005 |
} |
| 1006 |
|
| 1007 |
func (m testDimension) IndexDefault() int { |
| 1008 |
return 0 |
| 1009 |
} |
| 1010 |
|
| 1011 |
func (m *testDimension) ResolveIndex(name string) int { |
| 1012 |
for i, n := range m.names { |
| 1013 |
if n == name { |
| 1014 |
return i |
| 1015 |
} |
| 1016 |
} |
| 1017 |
return -1 |
| 1018 |
} |
| 1019 |
|
| 1020 |
func (m *testDimension) ResolveName(i int) string { |
| 1021 |
if i < 0 || i >= len(m.names) { |
| 1022 |
return "" |
| 1023 |
} |
| 1024 |
return m.names[i] |
| 1025 |
} |
| 1026 |
|
| 1027 |
func (m *testDimension) ForEachIndex() iter.Seq[int] { |
| 1028 |
return func(yield func(i int) bool) { |
| 1029 |
for i := range m.names { |
| 1030 |
if !yield(i) { |
| 1031 |
return |
| 1032 |
} |
| 1033 |
} |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
func (m *testDimension) IndexMatch(match predicate.P[predicate.IndexString]) (iter.Seq[int], error) { |
| 1038 |
return func(yield func(i int) bool) { |
| 1039 |
for i, n := range m.names { |
| 1040 |
if match(predicate.IndexString{Index: i, String: n}) { |
| 1041 |
if !yield(i) { |
| 1042 |
return |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} |
| 1046 |
}, nil |
| 1047 |
} |
| 1048 |
|
| 1049 |
// NewTestingDimensions creates a new ConfiguredDimensions for testing. |
| 1050 |
func NewTestingDimensions(languages, versions, roles []string) *ConfiguredDimensions { |
| 1051 |
c := &ConfiguredDimensions{ |
| 1052 |
ConfiguredLanguages: &testDimension{names: languages}, |
| 1053 |
ConfiguredVersions: &testDimension{names: versions}, |
| 1054 |
ConfiguredRoles: &testDimension{names: roles}, |
| 1055 |
} |
| 1056 |
if err := c.Init(); err != nil { |
| 1057 |
panic(err) |
| 1058 |
} |
| 1059 |
return c |
| 1060 |
} |