File size: 2,672 Bytes
95d599c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
//  CONTACT: hello@weaviate.io
//

package classcache

import (
	"context"
	"fmt"
	"slices"

	"github.com/weaviate/weaviate/entities/versioned"
)

const classCacheKey = "classCache"

var errorNoClassCache = fmt.Errorf("context does not contain classCache")

func ContextWithClassCache(ctx context.Context) context.Context {
	if ctx.Value(classCacheKey) != nil {
		return ctx
	}
	return context.WithValue(ctx, classCacheKey, &classCache{})
}

func RemoveClassFromContext(ctxWithClassCache context.Context, name string) error {
	cache, err := extractCache(ctxWithClassCache)
	if err != nil {
		return err
	}

	cache.Delete(name)
	return nil
}

func ClassesFromContext(ctxWithClassCache context.Context, getter func(names ...string) (map[string]versioned.Class, error), names ...string) (map[string]versioned.Class, error) {
	cache, err := extractCache(ctxWithClassCache)
	if err != nil {
		return nil, err
	}

	versionedClasses := map[string]versioned.Class{}
	notFoundInCtx := []string{}
	for _, name := range names {
		// collect what is not in context
		if entry, ok := cache.Load(name); ok {
			versionedClasses[entry.class.Class] = versioned.Class{Class: entry.class, Version: entry.version}
			continue
		}
		notFoundInCtx = append(notFoundInCtx, name)
	}

	// remove dedup, empty and a void calls if there is non
	slices.Sort(notFoundInCtx)
	notFoundInCtx = slices.Compact(notFoundInCtx)
	if len(notFoundInCtx) == 0 {
		return versionedClasses, nil
	}

	if len(notFoundInCtx) > 1 && notFoundInCtx[0] == "" {
		notFoundInCtx = notFoundInCtx[1:]
	}

	// TODO prevent concurrent getter calls for the same class if it was not loaded,
	// get once and share results
	vclasses, err := getter(notFoundInCtx...)
	if err != nil {
		return versionedClasses, err
	}

	for _, vclass := range vclasses {
		// do not replace entry if it was loaded in the meantime by concurrent access
		entry, _ := cache.LoadOrStore(vclass.Class.Class, &classCacheEntry{class: vclass.Class, version: vclass.Version})
		versionedClasses[entry.class.Class] = versioned.Class{Class: entry.class, Version: entry.version}
	}

	return versionedClasses, nil
}

func extractCache(ctx context.Context) (*classCache, error) {
	value := ctx.Value(classCacheKey)
	if value == nil {
		return nil, errorNoClassCache
	}
	cache, ok := value.(*classCache)
	if !ok {
		return nil, errorNoClassCache
	}
	return cache, nil
}