@@ -71,10 +71,6 @@ func (s *vectorStoreMap) setVector(vec Vector) {
7171 s .sets [vec ] = struct {}{}
7272}
7373
74- func (s * vectorStoreMap ) Ordinal () int {
75- return 0
76- }
77-
7874func (s * vectorStoreMap ) KeysSorted () ([]int , []int , []int ) {
7975 var k0 , k1 , k2 []int
8076 for v := range s .sets {
@@ -271,12 +267,45 @@ type ConfiguredDimension interface {
271267 ResolveIndex (string ) int
272268 ResolveName (int ) string
273269 ForEachIndex () iter.Seq [int ]
270+ Len () int
274271}
275272
273+ // ConfiguredDimensions holds the configured dimensions for the site matrix.
276274type ConfiguredDimensions struct {
277275 ConfiguredLanguages ConfiguredDimension
278276 ConfiguredVersions ConfiguredDimension
279277 ConfiguredRoles ConfiguredDimension
278+ CommonSitesMatrix CommonSitestMatrix
279+
280+ singleVectorStoreCache * maps.Cache [Vector , * IntSets ]
281+ }
282+
283+ func (c * ConfiguredDimensions ) IsSingleVector () bool {
284+ return c .ConfiguredLanguages .Len () == 1 && c .ConfiguredRoles .Len () == 1 && c .ConfiguredVersions .Len () == 1
285+ }
286+
287+ // GetOrCreateSingleVectorStore returns a VectorStore for the given vector.
288+ func (c * ConfiguredDimensions ) GetOrCreateSingleVectorStore (vec Vector ) * IntSets {
289+ store , _ := c .singleVectorStoreCache .GetOrCreate (vec , func () (* IntSets , error ) {
290+ is := & IntSets {}
291+ is .setValuesInNilSets (vec , true , true , true )
292+ return is , nil
293+ })
294+ return store
295+ }
296+
297+ func (c * ConfiguredDimensions ) Init () error {
298+ c .singleVectorStoreCache = maps .NewCache [Vector , * IntSets ]()
299+ b := NewIntSetsBuilder (c ).WithDefaultsIfNotSet ().Build ()
300+ defaultVec := b .VectorSample ()
301+ c .singleVectorStoreCache .Set (defaultVec , b )
302+ c .CommonSitesMatrix .DefaultSite = b
303+
304+ return nil
305+ }
306+
307+ type CommonSitestMatrix struct {
308+ DefaultSite VectorStore
280309}
281310
282311func (c * ConfiguredDimensions ) ResolveNames (v Vector ) types.Strings3 {
@@ -317,6 +346,8 @@ type IntSets struct {
317346 h * hashOnce
318347}
319348
349+ var NilStore * IntSets = nil
350+
320351type hashOnce struct {
321352 once sync.Once
322353 hash uint64
@@ -363,7 +394,7 @@ func (s *IntSets) Intersects(other *IntSets) bool {
363394// Complement returns a new VectorStore that contains all vectors in s that are not in any of ss.
364395func (s * IntSets ) Complement (ss ... VectorProvider ) VectorStore {
365396 if len (ss ) == 0 || (len (ss ) == 1 && ss [0 ] == s ) {
366- return nil
397+ return NilStore
367398 }
368399
369400 for _ , v := range ss {
@@ -372,8 +403,7 @@ func (s *IntSets) Complement(ss ...VectorProvider) VectorStore {
372403 continue
373404 }
374405 if vv .IsSuperSet (s ) {
375- var s * IntSets
376- return s
406+ return NilStore
377407 }
378408 }
379409
@@ -484,6 +514,9 @@ func (s *IntSets) HasLanguage(lang int) bool {
484514 return s .languages .Has (lang )
485515}
486516
517+ // LenVectors returns the total number of vectors represented by the IntSets.
518+ // This is the Cartesian product of the lengths of the individual sets.
519+ // This will be 0 if s is nil or any of the sets is empty.
487520func (s * IntSets ) LenVectors () int {
488521 if s == nil {
489522 return 0
@@ -526,6 +559,10 @@ func (s *IntSets) HasAnyVector(v VectorProvider) bool {
526559 if s .LenVectors () == 0 || v .LenVectors () == 0 {
527560 return false
528561 }
562+ if v .LenVectors () == 1 {
563+ // Fast path.
564+ return s .HasVector (v .VectorSample ())
565+ }
529566
530567 if vs , ok := v .(* IntSets ); ok {
531568 // Fast path.
@@ -688,6 +725,14 @@ type IntSetsBuilder struct {
688725
689726func (b * IntSetsBuilder ) Build () * IntSets {
690727 b .s .init ()
728+
729+ if b .s .LenVectors () == 1 {
730+ // Cache it or use the existing cached version, which will allow b.s to be GCed.
731+ bb , _ := b .cfg .singleVectorStoreCache .GetOrCreate (b .s .VectorSample (), func () (* IntSets , error ) {
732+ return b .s , nil
733+ })
734+ return bb
735+ }
691736 return b .s
692737}
693738
@@ -889,6 +934,10 @@ type testDimension struct {
889934 names []string
890935}
891936
937+ func (m testDimension ) Len () int {
938+ return len (m .names )
939+ }
940+
892941func (m testDimension ) IndexDefault () int {
893942 return 0
894943}
@@ -933,9 +982,13 @@ func (m *testDimension) IndexMatch(match predicate.P[string]) (iter.Seq[int], er
933982
934983// NewTestingDimensions creates a new ConfiguredDimensions for testing.
935984func NewTestingDimensions (languages , versions , roles []string ) * ConfiguredDimensions {
936- return & ConfiguredDimensions {
985+ c := & ConfiguredDimensions {
937986 ConfiguredLanguages : & testDimension {names : languages },
938987 ConfiguredVersions : & testDimension {names : versions },
939988 ConfiguredRoles : & testDimension {names : roles },
940989 }
990+ if err := c .Init (); err != nil {
991+ panic (err )
992+ }
993+ return c
941994}
0 commit comments