-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_cache.go
More file actions
76 lines (67 loc) · 2.19 KB
/
Copy pathschema_cache.go
File metadata and controls
76 lines (67 loc) · 2.19 KB
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
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package dgdao
import (
"reflect"
"sync"
)
// schemaCheckCache remembers which node types have already passed the
// per-write schema step in process(), so heavy write workloads don't pay a
// schema round-trip on every mutation:
//
// - with AutoSchema enabled, the Go types already pushed via UpdateSchema
// - with AutoSchema disabled, the Dgraph type names confirmed to exist in
// the database schema
//
// Like consumeMu, it is held by pointer so every value copy of a client (and
// every NewClient call that dedupes to the same underlying client) shares one
// cache. DropAll invalidates it; schema Alters are additive in Dgraph, so
// UpdateSchema and AlterSchema do not. The deliberate trade-off is that
// schema changes made by *other* processes are not observed for types already
// cached — WithSchemaCache(false) restores the uncached per-write behavior.
type schemaCheckCache struct {
mu sync.Mutex
synced map[reflect.Type]struct{}
verified map[string]struct{}
}
func newSchemaCheckCache() *schemaCheckCache {
return &schemaCheckCache{
synced: make(map[reflect.Type]struct{}),
verified: make(map[string]struct{}),
}
}
// isSynced reports whether UpdateSchema has already succeeded for t.
func (s *schemaCheckCache) isSynced(t reflect.Type) bool {
s.mu.Lock()
defer s.mu.Unlock()
_, ok := s.synced[t]
return ok
}
func (s *schemaCheckCache) markSynced(t reflect.Type) {
s.mu.Lock()
defer s.mu.Unlock()
s.synced[t] = struct{}{}
}
// isVerified reports whether typeName has already been confirmed present in
// the database schema.
func (s *schemaCheckCache) isVerified(typeName string) bool {
s.mu.Lock()
defer s.mu.Unlock()
_, ok := s.verified[typeName]
return ok
}
func (s *schemaCheckCache) markVerified(typeName string) {
s.mu.Lock()
defer s.mu.Unlock()
s.verified[typeName] = struct{}{}
}
// invalidate forgets everything, forcing the next write of each type to
// redo its schema sync or verification.
func (s *schemaCheckCache) invalidate() {
s.mu.Lock()
defer s.mu.Unlock()
s.synced = make(map[reflect.Type]struct{})
s.verified = make(map[string]struct{})
}