-
Notifications
You must be signed in to change notification settings - Fork 4
/
catalog.go
128 lines (100 loc) · 3.79 KB
/
catalog.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
package object
import (
"fmt"
"github.com/insolar/assured-ledger/ledger-core/conveyor/smachine"
"github.com/insolar/assured-ledger/ledger-core/instrumentation/inslogger"
"github.com/insolar/assured-ledger/ledger-core/instrumentation/trace"
"github.com/insolar/assured-ledger/ledger-core/log"
"github.com/insolar/assured-ledger/ledger-core/reference"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
//go:generate minimock -i github.com/insolar/assured-ledger/ledger-core/virtual/object.Catalog -o ./ -s _mock.go -g
type Catalog interface {
Get(ctx smachine.ExecutionContext, objectReference reference.Global) SharedStateAccessor
TryGet(ctx smachine.ExecutionContext, objectReference reference.Global) (SharedStateAccessor, bool)
Create(ctx smachine.ExecutionContext, objectReference reference.Global) SharedStateAccessor
GetOrCreate(ctx smachine.ExecutionContext, objectReference reference.Global) SharedStateAccessor
}
func NewLocalCatalog() *LocalCatalog {
return &LocalCatalog{}
}
type LocalCatalog struct{}
type errEntryMissing struct {
*log.Msg `txt:"entry is missing"`
ObjectReference reference.Global
}
type errEntryExists struct {
*log.Msg `txt:"entry already exists"`
ObjectReference reference.Global
}
func (p LocalCatalog) Get(ctx smachine.ExecutionContext, objectReference reference.Global) SharedStateAccessor {
if v, ok := p.TryGet(ctx, objectReference); ok {
return v
}
panic(throw.E("", errEntryMissing{ObjectReference: objectReference}))
}
func (p LocalCatalog) TryGet(ctx smachine.ExecutionContext, objectReference reference.Global) (SharedStateAccessor, bool) { // nolintcontractrequester/contractrequester.go:342
if v := ctx.GetPublishedLink(objectReference); v.IsAssignableTo((*SharedState)(nil)) {
return SharedStateAccessor{v}, true
}
return SharedStateAccessor{}, false
}
func (p LocalCatalog) initChildCtx(ctx smachine.ConstructionContext, ref reference.Global) {
traceID := fmt.Sprintf("object-%s", ref.String())
goCtx, err := inslogger.Clean(ctx.GetContext())
if err == nil {
goCtx, err = trace.SetID(goCtx, traceID)
}
if err != nil {
panic(err)
}
ctx.SetContext(goCtx)
ctx.SetTracerID(traceID)
}
func (p LocalCatalog) Create(ctx smachine.ExecutionContext, objectReference reference.Global) SharedStateAccessor {
if _, ok := p.TryGet(ctx, objectReference); ok {
panic(throw.E("", errEntryExists{ObjectReference: objectReference}))
}
ctx.InitChild(func(ctx smachine.ConstructionContext) smachine.StateMachine {
p.initChildCtx(ctx, objectReference)
return NewStateMachineObject(objectReference)
})
accessor, ok := p.TryGet(ctx, objectReference)
if !ok {
// we should get accessor always after InitChild in this step
panic(throw.IllegalState())
}
return accessor
}
func (p LocalCatalog) GetOrCreate(ctx smachine.ExecutionContext, objectReference reference.Global) SharedStateAccessor {
if v, ok := p.TryGet(ctx, objectReference); ok {
return v
}
ctx.InitChild(func(ctx smachine.ConstructionContext) smachine.StateMachine {
ctx.SetDependencyInheritanceMode(smachine.InheritResolvedDependencies)
p.initChildCtx(ctx, objectReference)
return NewStateMachineObject(objectReference)
})
accessor, ok := p.TryGet(ctx, objectReference)
if !ok {
// we should get accessor always after InitChild in this step
panic(throw.IllegalState())
}
return accessor
}
// //////////////////////////////////////
type SharedStateAccessor struct {
smachine.SharedDataLink
}
func (v SharedStateAccessor) Prepare(fn func(*SharedState)) smachine.SharedDataAccessor {
return v.PrepareAccess(func(data interface{}) bool {
fn(data.(*SharedState))
return false
})
}
func (v SharedStateAccessor) PrepareAndWakeUp(fn func(*SharedState)) smachine.SharedDataAccessor {
return v.PrepareAccess(func(data interface{}) bool {
fn(data.(*SharedState))
return true
})
}