/
snapshot.go
executable file
·70 lines (60 loc) · 1.39 KB
/
snapshot.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
package rootmulti
import (
"errors"
"fmt"
"math"
"github.com/cosmos/cosmos-sdk/snapshots/types"
protoio "github.com/cosmos/gogoproto/io"
"github.com/swag-eag/swa/memiavl"
)
// Implements interface Snapshotter
func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) (returnErr error) {
if height > math.MaxUint32 {
return fmt.Errorf("height overflows uint32: %d", height)
}
version := uint32(height)
exporter, err := memiavl.NewMultiTreeExporter(rs.dir, version, rs.supportExportNonSnapshotVersion)
if err != nil {
return err
}
defer func() {
returnErr = errors.Join(returnErr, exporter.Close())
}()
for {
item, err := exporter.Next()
if err != nil {
if err == memiavl.ErrorExportDone {
break
}
return err
}
switch item := item.(type) {
case *memiavl.ExportNode:
if err := protoWriter.WriteMsg(&types.SnapshotItem{
Item: &types.SnapshotItem_IAVL{
IAVL: &types.SnapshotIAVLItem{
Key: item.Key,
Value: item.Value,
Height: int32(item.Height),
Version: item.Version,
},
},
}); err != nil {
return err
}
case string:
if err := protoWriter.WriteMsg(&types.SnapshotItem{
Item: &types.SnapshotItem_Store{
Store: &types.SnapshotStoreItem{
Name: item,
},
},
}); err != nil {
return err
}
default:
return fmt.Errorf("unknown item type %T", item)
}
}
return nil
}