forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
778 lines (719 loc) · 20.3 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
package gno
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/gnolang/gno/pkgs/amino"
"github.com/gnolang/gno/pkgs/std"
"github.com/gnolang/gno/pkgs/store"
)
const iavlCacheSize = 1024 * 1024 // TODO increase and parameterize.
// return nil if package doesn't exist.
type PackageGetter func(pkgPath string) (*PackageNode, *PackageValue)
// inject natives into a new or loaded package (value and node)
type PackageInjector func(store Store, pn *PackageNode)
type Store interface {
// STABLE
SetPackageGetter(PackageGetter)
GetPackage(pkgPath string, isImport bool) *PackageValue
SetCachePackage(*PackageValue)
GetPackageRealm(pkgPath string) *Realm
SetPackageRealm(*Realm)
GetObject(oid ObjectID) Object
GetObjectSafe(oid ObjectID) Object
SetObject(Object)
DelObject(Object)
GetType(tid TypeID) Type
GetTypeSafe(tid TypeID) Type
SetCacheType(Type)
SetType(Type)
GetBlockNode(Location) BlockNode
GetBlockNodeSafe(Location) BlockNode
SetBlockNode(BlockNode)
// UNSTABLE
SetStrictGo2GnoMapping(bool)
AddGo2GnoMapping(rt reflect.Type, pkgPath string, name string)
Go2GnoType(rt reflect.Type) Type
GetAllocator() *Allocator
NumMemPackages() int64
// Upon restart, all packages will be re-preprocessed; This
// loads BlockNodes and Types onto the store for persistence
// version 1.
AddMemPackage(memPkg *std.MemPackage)
GetMemPackage(path string) *std.MemPackage
GetMemFile(path string, name string) *std.MemFile
IterMemPackage() <-chan *std.MemPackage
ClearObjectCache() // for each delivertx.
Fork() Store // for checktx, simulate, and queries.
SwapStores(baseStore, iavlStore store.Store) // for gas wrappers.
SetPackageInjector(PackageInjector) // for natives
SetLogStoreOps(enabled bool)
SprintStoreOps() string
LogSwitchRealm(rlmpath string) // to mark change of realm boundaries
ClearCache()
Print()
}
// Used to keep track of in-mem objects during tx.
type defaultStore struct {
alloc *Allocator // for accounting for cached items
pkgGetter PackageGetter // non-realm packages
cacheObjects map[ObjectID]Object
cacheTypes map[TypeID]Type
cacheNodes map[Location]BlockNode
cacheNativeTypes map[reflect.Type]Type // go spec: reflect.Type are comparable
baseStore store.Store // for objects, types, nodes
iavlStore store.Store // for escaped object hashes
pkgInjector PackageInjector // for injecting natives
go2gnoMap map[string]string // go pkgpath.name -> gno pkgpath.name
go2gnoStrict bool // if true, native->gno type conversion must be registered.
// transient
opslog []StoreOp // for debugging and testing.
current map[string]struct{} // for detecting import cycles.
}
func NewStore(alloc *Allocator, baseStore, iavlStore store.Store) *defaultStore {
ds := &defaultStore{
alloc: alloc,
pkgGetter: nil,
cacheObjects: make(map[ObjectID]Object),
cacheTypes: make(map[TypeID]Type),
cacheNodes: make(map[Location]BlockNode),
cacheNativeTypes: make(map[reflect.Type]Type),
baseStore: baseStore,
iavlStore: iavlStore,
go2gnoMap: make(map[string]string),
go2gnoStrict: true,
current: make(map[string]struct{}),
}
InitStoreCaches(ds)
return ds
}
func (ds *defaultStore) GetAllocator() *Allocator {
return ds.alloc
}
func (ds *defaultStore) SetPackageGetter(pg PackageGetter) {
ds.pkgGetter = pg
}
// Gets package from cache, or loads it from baseStore, or gets it from package getter.
func (ds *defaultStore) GetPackage(pkgPath string, isImport bool) *PackageValue {
// detect circular imports
if isImport {
if _, exists := ds.current[pkgPath]; exists {
panic(fmt.Sprintf("import cycle detected: %q", pkgPath))
}
ds.current[pkgPath] = struct{}{}
defer delete(ds.current, pkgPath)
}
// first, check cache.
oid := ObjectIDFromPkgPath(pkgPath)
if oo, exists := ds.cacheObjects[oid]; exists {
pv := oo.(*PackageValue)
return pv
}
// else, load package.
if ds.baseStore != nil {
if oo := ds.loadObjectSafe(oid); oo != nil {
pv := oo.(*PackageValue)
_ = pv.GetBlock(ds) // preload
// get package associated realm if nil.
if pv.IsRealm() && pv.Realm == nil {
rlm := ds.GetPackageRealm(pkgPath)
pv.Realm = rlm
}
// get package node.
pl := PackageNodeLocation(pkgPath)
pn, ok := ds.GetBlockNodeSafe(pl).(*PackageNode)
if !ok {
// Do not inject packages from packageGetter
// that don't have corresponding *PackageNodes.
} else {
// Inject natives after load.
if ds.pkgInjector != nil {
if pn.HasAttribute(ATTR_INJECTED) {
// e.g. in checktx or simulate or query.
pn.PrepareNewValues(pv)
} else {
// pv.GetBlock(ds) // preload pv.Block
ds.pkgInjector(ds, pn)
pn.SetAttribute(ATTR_INJECTED, true)
pn.PrepareNewValues(pv)
}
}
}
// Rederive pv.fBlocksMap.
pv.deriveFBlocksMap(ds)
return pv
}
}
// otherwise, fetch from pkgGetter.
if ds.pkgGetter != nil {
if pn, pv := ds.pkgGetter(pkgPath); pv != nil {
// e.g. tests/imports_tests loads example/gno.land/r/... realms.
// if pv.IsRealm() {
// panic("realm packages cannot be gotten from pkgGetter")
// }
ds.SetBlockNode(pn)
// NOTE: not SetObject() here,
// we don't want to overwrite
// the value from pkgGetter.
// Realm values obtained this way
// will get written elsewhere
// later.
ds.cacheObjects[oid] = pv
// inject natives after init.
if ds.pkgInjector != nil {
if pn.HasAttribute(ATTR_INJECTED) {
// not sure why this would happen.
panic("should not happen")
// pn.PrepareNewValues(pv)
} else {
ds.pkgInjector(ds, pn)
pn.SetAttribute(ATTR_INJECTED, true)
pn.PrepareNewValues(pv)
}
}
// cache all types. usually preprocess() sets types,
// but packages gotten from the pkgGetter may skip this step,
// so fill in store.CacheTypes here.
for _, tv := range pv.GetBlock(nil).Values {
if tv.T.Kind() == TypeKind {
t := tv.GetType()
ds.SetCacheType(t)
}
}
return pv
}
}
// otherwise, package does not exist.
return nil
}
// Used to set throwaway packages.
func (ds *defaultStore) SetCachePackage(pv *PackageValue) {
oid := ObjectIDFromPkgPath(pv.PkgPath)
if _, exists := ds.cacheObjects[oid]; exists {
panic(fmt.Sprintf("package %s already exists in cache", pv.PkgPath))
}
ds.cacheObjects[oid] = pv
}
// Some atomic operation.
func (ds *defaultStore) GetPackageRealm(pkgPath string) (rlm *Realm) {
oid := ObjectIDFromPkgPath(pkgPath)
key := backendRealmKey(oid)
bz := ds.baseStore.Get([]byte(key))
if bz == nil {
return nil
}
amino.MustUnmarshal(bz, &rlm)
if debug {
if rlm.ID != oid.PkgID {
panic(fmt.Sprintf("unexpected realm id: expected %v but got %v",
oid.PkgID, rlm.ID))
}
}
return rlm
}
// An atomic operation to set the package realm info (id counter etc).
func (ds *defaultStore) SetPackageRealm(rlm *Realm) {
oid := ObjectIDFromPkgPath(rlm.Path)
key := backendRealmKey(oid)
bz := amino.MustMarshal(rlm)
ds.baseStore.Set([]byte(key), bz)
}
// NOTE: does not consult the packageGetter, so instead
// call GetPackage() for packages.
// NOTE: current implementation behavior requires
// all []TypedValue types and TypeValue{} types to be
// loaded (non-ref) types.
func (ds *defaultStore) GetObject(oid ObjectID) Object {
oo := ds.GetObjectSafe(oid)
if oo == nil {
panic(fmt.Sprintf("unexpected object with id %s", oid.String()))
}
return oo
}
func (ds *defaultStore) GetObjectSafe(oid ObjectID) Object {
// check cache.
if oo, exists := ds.cacheObjects[oid]; exists {
return oo
}
// check baseStore.
if ds.baseStore != nil {
if oo := ds.loadObjectSafe(oid); oo != nil {
if debug {
if _, ok := oo.(*PackageValue); ok {
panic("packages must be fetched with GetPackage()")
}
}
return oo
}
}
return nil
}
// loads and caches an object.
// CONTRACT: object isn't already in the cache.
func (ds *defaultStore) loadObjectSafe(oid ObjectID) Object {
key := backendObjectKey(oid)
hashbz := ds.baseStore.Get([]byte(key))
if hashbz != nil {
hash := hashbz[:HashSize]
bz := hashbz[HashSize:]
var oo Object
ds.alloc.AllocateAmino(int64(len(bz)))
amino.MustUnmarshal(bz, &oo)
if debug {
if oo.GetObjectID() != oid {
panic(fmt.Sprintf("unexpected object id: expected %v but got %v",
oid, oo.GetObjectID()))
}
}
oo.SetHash(ValueHash{NewHashlet(hash)})
ds.cacheObjects[oid] = oo
_ = fillTypesOfValue(ds, oo)
return oo
}
return nil
}
// NOTE: unlike GetObject(), SetObject() is also used to persist updated
// package values.
func (ds *defaultStore) SetObject(oo Object) {
oid := oo.GetObjectID()
// replace children/fields with Ref.
o2 := copyValueWithRefs(nil, oo)
// marshal to binary.
bz := amino.MustMarshalAny(o2)
// set hash.
hash := HashBytes(bz) // XXX objectHash(bz)???
if len(hash) != HashSize {
panic("should not happen")
}
oo.SetHash(ValueHash{hash})
// save bytes to backend.
if ds.baseStore != nil {
key := backendObjectKey(oid)
hashbz := make([]byte, len(hash)+len(bz))
copy(hashbz, hash.Bytes())
copy(hashbz[HashSize:], bz)
ds.baseStore.Set([]byte(key), hashbz)
}
// save object to cache.
if debug {
if oid.IsZero() {
panic("object id cannot be zero")
}
if oo2, exists := ds.cacheObjects[oid]; exists {
if oo != oo2 {
panic(fmt.Sprintf(
"duplicate object: set %s (oid: %s) but %s (oid %s) already exists",
oo.String(), oid.String(), oo2.String(), oo2.GetObjectID().String()))
}
}
}
ds.cacheObjects[oid] = oo
// make store op log entry
if ds.opslog != nil {
var op StoreOpType
if oo.GetIsNewReal() {
op = StoreOpNew
} else {
op = StoreOpMod
}
ds.opslog = append(ds.opslog,
StoreOp{Type: op, Object: o2.(Object)})
}
// if escaped, add hash to iavl.
if oo.GetIsEscaped() && ds.iavlStore != nil {
var key, value []byte
key = []byte(oid.String())
value = hash.Bytes()
ds.iavlStore.Set(key, value)
}
}
func (ds *defaultStore) DelObject(oo Object) {
oid := oo.GetObjectID()
// delete from cache.
delete(ds.cacheObjects, oid)
// delete from backend.
if ds.baseStore != nil {
key := backendObjectKey(oid)
ds.baseStore.Delete([]byte(key))
}
// make realm op log entry
if ds.opslog != nil {
ds.opslog = append(ds.opslog,
StoreOp{Type: StoreOpDel, Object: oo})
}
}
// NOTE: not used quite yet.
// NOTE: The implementation matches that of GetObject() in anticipation of what
// the persistent type system might work like.
func (ds *defaultStore) GetType(tid TypeID) Type {
tt := ds.GetTypeSafe(tid)
if tt == nil {
ds.Print()
panic(fmt.Sprintf("unexpected type with id %s", tid.String()))
}
return tt
}
func (ds *defaultStore) GetTypeSafe(tid TypeID) Type {
// check cache.
if tt, exists := ds.cacheTypes[tid]; exists {
return tt
}
// check backend.
if ds.baseStore != nil {
key := backendTypeKey(tid)
bz := ds.baseStore.Get([]byte(key))
if bz != nil {
var tt Type
amino.MustUnmarshal(bz, &tt)
if debug {
if tt.TypeID() != tid {
panic(fmt.Sprintf("unexpected type id: expected %v but got %v",
tid, tt.TypeID()))
}
}
// set in cache.
ds.cacheTypes[tid] = tt
// after setting in cache, fill tt.
fillType(ds, tt)
return tt
}
}
return nil
}
func (ds *defaultStore) SetCacheType(tt Type) {
tid := tt.TypeID()
if tt2, exists := ds.cacheTypes[tid]; exists {
if tt != tt2 {
// NOTE: not sure why this would happen.
panic("should not happen")
} else {
// already set.
}
} else {
ds.cacheTypes[tid] = tt
}
}
func (ds *defaultStore) SetType(tt Type) {
tid := tt.TypeID()
// return if tid already known.
if tt2, exists := ds.cacheTypes[tid]; exists {
if tt != tt2 {
// this can happen for a variety of reasons.
// TODO classify them and optimize.
return
}
}
// save type to backend.
if ds.baseStore != nil {
key := backendTypeKey(tid)
tcopy := copyTypeWithRefs(tt)
bz := amino.MustMarshalAny(tcopy)
ds.baseStore.Set([]byte(key), bz)
}
// save type to cache.
ds.cacheTypes[tid] = tt
}
func (ds *defaultStore) GetBlockNode(loc Location) BlockNode {
bn := ds.GetBlockNodeSafe(loc)
if bn == nil {
panic(fmt.Sprintf("unexpected node with location %s", loc.String()))
}
return bn
}
func (ds *defaultStore) GetBlockNodeSafe(loc Location) BlockNode {
// check cache.
if bn, exists := ds.cacheNodes[loc]; exists {
return bn
}
// check backend.
if ds.baseStore != nil {
key := backendNodeKey(loc)
bz := ds.baseStore.Get([]byte(key))
if bz != nil {
var bn BlockNode
amino.MustUnmarshal(bz, &bn)
if debug {
if bn.GetLocation() != loc {
panic(fmt.Sprintf("unexpected node location: expected %v but got %v",
loc, bn.GetLocation()))
}
}
ds.cacheNodes[loc] = bn
return bn
}
}
return nil
}
func (ds *defaultStore) SetBlockNode(bn BlockNode) {
loc := bn.GetLocation()
if loc.IsZero() {
panic("unexpected zero location in blocknode")
}
// save node to backend.
if ds.baseStore != nil {
// TODO: implement copyValueWithRefs() for Nodes.
// key := backendNodeKey(loc)
// ds.backend.Set([]byte(key), bz)
}
// save node to cache.
ds.cacheNodes[loc] = bn
// XXX duplicate?
// XXX
}
func (ds *defaultStore) NumMemPackages() int64 {
ctrkey := []byte(backendPackageIndexCtrKey())
ctrbz := ds.baseStore.Get(ctrkey)
if ctrbz == nil {
return 0
} else {
ctr, err := strconv.Atoi(string(ctrbz))
if err != nil {
panic(err)
}
return int64(ctr)
}
}
func (ds *defaultStore) incGetPackageIndexCounter() uint64 {
ctrkey := []byte(backendPackageIndexCtrKey())
ctrbz := ds.baseStore.Get(ctrkey)
if ctrbz == nil {
nextbz := strconv.Itoa(1)
ds.baseStore.Set(ctrkey, []byte(nextbz))
return 1
} else {
ctr, err := strconv.Atoi(string(ctrbz))
if err != nil {
panic(err)
}
nextbz := strconv.Itoa(ctr + 1)
ds.baseStore.Set(ctrkey, []byte(nextbz))
return uint64(ctr) + 1
}
}
func (ds *defaultStore) AddMemPackage(memPkg *std.MemPackage) {
memPkg.Validate() // NOTE: duplicate validation.
ctr := ds.incGetPackageIndexCounter()
idxkey := []byte(backendPackageIndexKey(ctr))
bz := amino.MustMarshal(memPkg)
ds.baseStore.Set(idxkey, []byte(memPkg.Path))
pathkey := []byte(backendPackagePathKey(memPkg.Path))
ds.iavlStore.Set(pathkey, bz)
}
func (ds *defaultStore) GetMemPackage(path string) *std.MemPackage {
pathkey := []byte(backendPackagePathKey(path))
bz := ds.iavlStore.Get(pathkey)
if bz == nil {
panic(fmt.Sprintf(
"missing package at path %s", string(pathkey)))
}
var memPkg *std.MemPackage
amino.MustUnmarshal(bz, &memPkg)
return memPkg
}
func (ds *defaultStore) GetMemFile(path string, name string) *std.MemFile {
memPkg := ds.GetMemPackage(path)
memFile := memPkg.GetFile(name)
return memFile
}
func (ds *defaultStore) IterMemPackage() <-chan *std.MemPackage {
ctrkey := []byte(backendPackageIndexCtrKey())
ctrbz := ds.baseStore.Get(ctrkey)
if ctrbz == nil {
return nil
} else {
ctr, err := strconv.Atoi(string(ctrbz))
if err != nil {
panic(err)
}
ch := make(chan *std.MemPackage, 0)
go func() {
for i := uint64(1); i <= uint64(ctr); i++ {
idxkey := []byte(backendPackageIndexKey(i))
path := ds.baseStore.Get(idxkey)
if path == nil {
panic(fmt.Sprintf(
"missing package index %d", i))
}
memPkg := ds.GetMemPackage(string(path))
ch <- memPkg
}
close(ch)
}()
return ch
}
}
// Unstable.
// This function is used to clear the object cache every transaction.
// It also sets a new allocator.
func (ds *defaultStore) ClearObjectCache() {
ds.alloc.Reset()
ds.cacheObjects = make(map[ObjectID]Object) // new cache.
ds.opslog = nil // new ops log.
if len(ds.current) > 0 {
ds.current = make(map[string]struct{})
}
ds.SetCachePackage(Uverse())
}
// Unstable.
// This function is used to handle queries and checktx transactions.
func (ds *defaultStore) Fork() Store {
ds2 := &defaultStore{
alloc: ds.alloc.Fork().Reset(),
pkgGetter: ds.pkgGetter,
cacheObjects: make(map[ObjectID]Object), // new cache.
cacheTypes: ds.cacheTypes,
cacheNodes: ds.cacheNodes,
cacheNativeTypes: ds.cacheNativeTypes,
baseStore: ds.baseStore,
iavlStore: ds.iavlStore,
pkgInjector: ds.pkgInjector,
go2gnoMap: ds.go2gnoMap,
go2gnoStrict: ds.go2gnoStrict,
opslog: nil, // new ops log.
current: make(map[string]struct{}),
}
ds2.SetCachePackage(Uverse())
return ds2
}
// TODO: consider a better/faster/simpler way of achieving the overall same goal?
func (ds *defaultStore) SwapStores(baseStore, iavlStore store.Store) {
ds.baseStore = baseStore
ds.iavlStore = iavlStore
}
func (ds *defaultStore) SetPackageInjector(inj PackageInjector) {
ds.pkgInjector = inj
}
func (ds *defaultStore) Flush() {
// XXX
}
//----------------------------------------
// StoreOp
type StoreOpType uint8
const (
StoreOpNew StoreOpType = iota
StoreOpMod
StoreOpDel
StoreOpSwitchRealm
)
type StoreOp struct {
Type StoreOpType
Object Object // ref'd objects
RlmPath string // for StoreOpSwitchRealm
}
// used by the tests/file_test system to check
// veracity of realm operations.
func (sop StoreOp) String() string {
switch sop.Type {
case StoreOpNew:
return fmt.Sprintf("c[%v]=%s",
sop.Object.GetObjectID(),
prettyJSON(amino.MustMarshalJSON(sop.Object)))
case StoreOpMod:
return fmt.Sprintf("u[%v]=%s",
sop.Object.GetObjectID(),
prettyJSON(amino.MustMarshalJSON(sop.Object)))
case StoreOpDel:
return fmt.Sprintf("d[%v]",
sop.Object.GetObjectID())
case StoreOpSwitchRealm:
return fmt.Sprintf("switchrealm[%q]",
sop.RlmPath)
default:
panic("should not happen")
}
}
func (ds *defaultStore) SetLogStoreOps(enabled bool) {
if enabled {
ds.ResetStoreOps()
} else {
ds.opslog = nil
}
}
// resets .realmops.
func (ds *defaultStore) ResetStoreOps() {
ds.opslog = make([]StoreOp, 0, 1024)
}
// for test/file_test.go, to test realm changes.
func (ds *defaultStore) SprintStoreOps() string {
ss := make([]string, 0, len(ds.opslog))
for _, sop := range ds.opslog {
ss = append(ss, sop.String())
}
return strings.Join(ss, "\n")
}
func (ds *defaultStore) LogSwitchRealm(rlmpath string) {
ds.opslog = append(ds.opslog,
StoreOp{Type: StoreOpSwitchRealm, RlmPath: rlmpath})
}
func (ds *defaultStore) ClearCache() {
ds.cacheObjects = make(map[ObjectID]Object)
ds.cacheTypes = make(map[TypeID]Type)
ds.cacheNodes = make(map[Location]BlockNode)
ds.cacheNativeTypes = make(map[reflect.Type]Type)
// restore builtin types to cache.
InitStoreCaches(ds)
}
// for debugging
func (ds *defaultStore) Print() {
fmt.Println("//----------------------------------------")
fmt.Println("defaultStore:baseStore...")
store.Print(ds.baseStore)
fmt.Println("//----------------------------------------")
fmt.Println("defaultStore:iavlStore...")
store.Print(ds.iavlStore)
fmt.Println("//----------------------------------------")
fmt.Println("defaultStore:cacheTypes...")
for tid, typ := range ds.cacheTypes {
fmt.Printf("- %v: %v\n", tid, typ)
}
fmt.Println("//----------------------------------------")
fmt.Println("defaultStore:cacheNodes...")
for loc, bn := range ds.cacheNodes {
fmt.Printf("- %v: %v\n", loc, bn)
}
}
//----------------------------------------
// backend keys
func backendObjectKey(oid ObjectID) string {
return "oid:" + oid.String()
}
// oid: associated package value object id.
func backendRealmKey(oid ObjectID) string {
return "oid:" + oid.String() + "#realm"
}
func backendTypeKey(tid TypeID) string {
return "tid:" + tid.String()
}
func backendNodeKey(loc Location) string {
return "node:" + loc.String()
}
func backendPackageIndexCtrKey() string {
return fmt.Sprintf("pkgidx:counter")
}
func backendPackageIndexKey(index uint64) string {
return fmt.Sprintf("pkgidx:%020d", index)
}
func backendPackagePathKey(path string) string {
return fmt.Sprintf("pkg:" + path)
}
//----------------------------------------
// builtin types and packages
func InitStoreCaches(store Store) {
types := []Type{
BoolType, UntypedBoolType,
StringType, UntypedStringType,
IntType, Int8Type, Int16Type, Int32Type, Int64Type, UntypedRuneType,
UintType, Uint8Type, Uint16Type, Uint32Type, Uint64Type,
BigintType, UntypedBigintType,
gTypeType,
gPackageType,
blockType{},
Float32Type, Float64Type,
gErrorType, // from uverse.go
}
for _, tt := range types {
store.SetCacheType(tt)
}
store.SetCachePackage(Uverse())
}