-
Notifications
You must be signed in to change notification settings - Fork 4
/
smdrop_catalog.go
102 lines (84 loc) · 2.46 KB
/
smdrop_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
package datawriter
import (
"github.com/insolar/assured-ledger/ledger-core/conveyor/smachine"
"github.com/insolar/assured-ledger/ledger-core/ledger/jet"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
type JetDropKey jet.DropID
type DropCataloger interface {
Create(ctx smachine.ExecutionContext, dropCfg DropInfo, terminateFn smachine.TerminationHandlerFunc) DropDataLink
Get(ctx smachine.SharedStateContext, dropID jet.DropID) DropDataLink
}
type JetTreeOp uint8
const (
JetGenesis JetTreeOp = iota
JetStraight
JetGenesisSplit
JetSplit
JetMerge
)
var _ DropCataloger = DropCatalog{}
type DropCatalog struct {}
func (DropCatalog) Create(ctx smachine.ExecutionContext, dropCfg DropInfo, terminateFn smachine.TerminationHandlerFunc) DropDataLink {
if ctx.GetPublished(JetDropKey(dropCfg.ID)) != nil {
panic(throw.IllegalState())
}
ctx.InitChildExt(JetDropCreate(dropCfg), smachine.CreateDefaultValues{ TerminationHandler: terminateFn }, nil)
switch sdl := ctx.GetPublishedLink(JetDropKey(dropCfg.ID)); {
case sdl.IsZero():
panic(throw.IllegalState())
case sdl.IsAssignableTo(&DropSharedData{}):
return DropDataLink{sdl}
default:
panic(throw.IllegalState())
}
}
func (DropCatalog) Get(ctx smachine.SharedStateContext, dropID jet.DropID) DropDataLink {
switch sdl := ctx.GetPublishedLink(JetDropKey(dropID)); {
case sdl.IsZero():
case sdl.IsAssignableTo(&DropSharedData{}):
return DropDataLink{sdl}
}
return DropDataLink{}
}
func JetDropCreate(dropCfg DropInfo) smachine.CreateFunc {
return func(smachine.ConstructionContext) smachine.StateMachine {
sm := &SMDropBuilder{}
sm.sd.info = dropCfg
return sm
}
}
func RegisterJetDrop(ctx smachine.SharedStateContext, sd *DropSharedData) bool {
switch {
case !sd.info.ID.IsValid():
panic(throw.IllegalState())
case sd.ready.IsZero():
panic(throw.IllegalState())
case sd.finalize.IsZero():
panic(throw.IllegalState())
}
sdl := ctx.Share(sd, smachine.ShareDataDirect)
if !ctx.Publish(JetDropKey(sd.info.ID), sdl) {
ctx.Unshare(sdl)
return false
}
return true
}
type DropDataLink struct {
smachine.SharedDataLink
}
func (v DropDataLink) TryAccess(fn func(sd *DropSharedData)) smachine.BoolDecision {
if fn == nil {
panic(throw.IllegalValue())
}
if i := v.SharedDataLink.TryDirectAccess(); i != nil {
fn(i.(*DropSharedData))
return true
}
return false
}
func (v DropDataLink) MustAccess(fn func(sd *DropSharedData)) {
if !v.TryAccess(fn) {
panic(throw.IllegalState())
}
}