Passed
Pull Request — master (#2583)
by Tolga
03:10
created

cache.*SchemaReader.ListSchemas   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
package cache
2
3
import (
4
	"context"
5
	"errors"
6
	"fmt"
7
	"reflect"
8
9
	"github.com/Permify/permify/internal/storage"
10
	"github.com/Permify/permify/pkg/cache"
11
	"github.com/Permify/permify/pkg/database"
12
	base "github.com/Permify/permify/pkg/pb/base/v1"
13
)
14
15
// SchemaReader - Add cache behaviour to schema reader
16
type SchemaReader struct {
17
	delegate storage.SchemaReader
18
	cache    cache.Cache
19
}
20
21
// NewSchemaReader new instance of SchemaReader
22
func NewSchemaReader(delegate storage.SchemaReader, cache cache.Cache) *SchemaReader {
23
	return &SchemaReader{
24
		delegate: delegate,
25
		cache:    cache,
26
	}
27
}
28
29
// ReadSchema returns the schema definition for a specific tenant and version as a structured object.
30
func (r *SchemaReader) ReadSchema(ctx context.Context, tenantID, version string) (schema *base.SchemaDefinition, err error) {
31
	return r.delegate.ReadSchema(ctx, tenantID, version)
32
}
33
34
// ReadSchemaString returns the schema definition for a specific tenant and version as a string.
35
func (r *SchemaReader) ReadSchemaString(ctx context.Context, tenantID, version string) (definitions []string, err error) {
36
	return r.delegate.ReadSchemaString(ctx, tenantID, version)
37
}
38
39
// ReadEntityDefinition - Read entity definition from the repository
40
func (r *SchemaReader) ReadEntityDefinition(ctx context.Context, tenantID, entityName, version string) (definition *base.EntityDefinition, v string, err error) {
41
	var s interface{}
42
	found := false
43
	if version != "" {
44
		s, found = r.cache.Get(fmt.Sprintf("%s|%s|%s", tenantID, entityName, version))
45
	}
46
	if !found {
47
		definition, version, err = r.delegate.ReadEntityDefinition(ctx, tenantID, entityName, version)
48
		if err != nil {
49
			return nil, "", err
50
		}
51
		size := reflect.TypeOf(definition).Size()
52
		r.cache.Set(fmt.Sprintf("%s|%s|%s", tenantID, entityName, version), definition, int64(size))
53
		return definition, version, nil
54
	}
55
	def, ok := s.(*base.EntityDefinition)
56
	if !ok {
57
		return nil, "", errors.New(base.ErrorCode_ERROR_CODE_SCAN.String())
58
	}
59
	return def, "", err
60
}
61
62
// ReadRuleDefinition - Read rule definition from the repository
63
func (r *SchemaReader) ReadRuleDefinition(ctx context.Context, tenantID, ruleName, version string) (definition *base.RuleDefinition, v string, err error) {
64
	var s interface{}
65
	found := false
66
	if version != "" {
67
		s, found = r.cache.Get(fmt.Sprintf("%s|%s|%s", tenantID, ruleName, version))
68
	}
69
	if !found {
70
		definition, version, err = r.delegate.ReadRuleDefinition(ctx, tenantID, ruleName, version)
71
		if err != nil {
72
			return nil, "", err
73
		}
74
		size := reflect.TypeOf(definition).Size()
75
		r.cache.Set(fmt.Sprintf("%s|%s|%s", tenantID, ruleName, version), definition, int64(size))
76
		return definition, version, nil
77
	}
78
	def, ok := s.(*base.RuleDefinition)
79
	if !ok {
80
		return nil, "", errors.New(base.ErrorCode_ERROR_CODE_SCAN.String())
81
	}
82
	return def, "", err
83
}
84
85
// HeadVersion - Finds the latest version of the schema.
86
func (r *SchemaReader) HeadVersion(ctx context.Context, tenantID string) (version string, err error) {
87
	return r.delegate.HeadVersion(ctx, tenantID)
88
}
89
90
// ListSchemas - List all Schemas
91
func (r *SchemaReader) ListSchemas(ctx context.Context, tenantID string, pagination database.Pagination) (schemas []*base.SchemaList, ct database.EncodedContinuousToken, err error) {
92
	schemas, ct, err = r.delegate.ListSchemas(ctx, tenantID, pagination)
93
	if err != nil {
94
		return nil, nil, err
95
	}
96
	return schemas, ct, nil
97
}
98