forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
109 lines (99 loc) · 2.07 KB
/
store.go
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
99
100
101
102
103
104
105
106
107
108
109
package gno
type Store interface {
GetPackage(pkgPath string) *PackageValue
SetPackage(*PackageValue)
GetObject(oid ObjectID) Object
SetObject(Object)
GetType(tid TypeID) Type
SetType(Type)
}
// Used to keep track of in-mem objects during tx.
type CacheStore struct {
CachePkgs map[string]*PackageValue
CacheObjects map[ObjectID]Object
CacheTypes map[TypeID]Type
Store Store
}
func NewCacheStore(store Store) CacheStore {
return CacheStore{
CachePkgs: make(map[string]*PackageValue),
CacheObjects: make(map[ObjectID]Object),
CacheTypes: make(map[TypeID]Type),
Store: store,
}
}
func (cs CacheStore) GetPackage(pkgPath string) *PackageValue {
if pv, exists := cs.CachePkgs[pkgPath]; exists {
return pv
}
if cs.Store != nil {
pv := cs.Store.GetPackage(pkgPath)
cs.CachePkgs[pkgPath] = pv
return pv
} else {
return nil
}
}
func (cs CacheStore) SetPackage(pv *PackageValue) {
pkgPath := pv.PkgPath
if debug {
if pv2, ex := cs.CachePkgs[pkgPath]; ex {
if ex && pv != pv2 {
panic("duplicate package value")
}
}
}
cs.CachePkgs[pkgPath] = pv
}
func (cs CacheStore) GetObject(oid ObjectID) Object {
if oo, exists := cs.CacheObjects[oid]; exists {
return oo
}
if cs.Store != nil {
oo := cs.Store.GetObject(oid)
cs.CacheObjects[oid] = oo
return oo
} else {
return nil
}
}
func (cs CacheStore) SetObject(oo Object) {
oid := oo.GetObjectID()
if debug {
if oid.IsZero() {
panic("object id cannot be zero")
}
if oo2, ex := cs.CacheObjects[oid]; ex {
if ex && oo != oo2 {
panic("duplicate object")
}
}
}
cs.CacheObjects[oid] = oo
}
func (cs CacheStore) GetType(tid TypeID) Type {
if tt, exists := cs.CacheTypes[tid]; exists {
return tt
}
if cs.Store != nil {
tt := cs.Store.GetType(tid)
cs.CacheTypes[tid] = tt
return tt
} else {
return nil
}
}
func (cs CacheStore) SetType(tt Type) {
tid := tt.TypeID()
if debug {
if tt2, ex := cs.CacheTypes[tid]; ex {
if ex && tt != tt2 {
panic("duplicate type")
}
}
}
cs.CacheTypes[tid] = tt
}
func (cs CacheStore) Flush() {
// XXX
}