-
Notifications
You must be signed in to change notification settings - Fork 4
/
writer.go
71 lines (60 loc) · 1.9 KB
/
writer.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
package ctlsection
import (
"github.com/insolar/assured-ledger/ledger-core/ledger"
"github.com/insolar/assured-ledger/ledger-core/ledger/server/buildersvc/bundle"
"github.com/insolar/assured-ledger/ledger-core/reference"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
func newCtlEntryWriter(ref reference.Global, entry bundle.MarshalerTo) bundle.Writeable {
if entry == nil {
panic(throw.IllegalValue())
}
return &ctlEntryWriter{ ref: ref, entry: entry}
}
//nolint //TODO revisit
func newCtlEntryWriterExt(ref reference.Global, entryFn func (bundle.PayloadSection) (bundle.MarshalerTo, error)) bundle.Writeable {
if entryFn == nil {
panic(throw.IllegalValue())
}
return &ctlEntryWriter{ ref: ref, prepareFn: entryFn}
}
type ctlEntryWriter struct {
ref reference.Global
prepareFn func (bundle.PayloadSection) (bundle.MarshalerTo, error)
entry bundle.MarshalerTo
dirIndex ledger.DirectoryIndex
receptacle bundle.PayloadReceptacle
}
func (p *ctlEntryWriter) PrepareWrite(snapshot bundle.Snapshot) error {
switch {
case p.prepareFn != nil:
cp, err := snapshot.GetPayloadSection(ledger.ControlSection)
if err != nil {
return err
}
p.entry, err = p.prepareFn(cp)
if err != nil {
return err
}
case p.entry == nil:
return throw.IllegalState()
}
cd, err := snapshot.GetDirectorySection(ledger.ControlSection)
if err != nil {
return err
}
p.dirIndex = cd.GetNextDirectoryIndex()
de := bundle.DirectoryEntry{ Key: p.ref }
p.receptacle, de.Loc, err = cd.AllocateEntryStorage(p.entry.ProtoSize())
if err != nil {
return err
}
return cd.AppendDirectoryEntry(p.dirIndex, de)
}
func (p *ctlEntryWriter) ApplyWrite() ([]ledger.DirectoryIndex, error) {
if err := p.receptacle.ApplyMarshalTo(p.entry); err != nil {
return nil, err
}
return []ledger.DirectoryIndex{p.dirIndex}, nil
}
func (p *ctlEntryWriter) ApplyRollback() {}