Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-develope committed Nov 13, 2023
1 parent c0dcf8f commit 0f462be
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
21 changes: 12 additions & 9 deletions store/commitment/iavl/tree.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package iavl

import (
"errors"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -93,12 +94,12 @@ func (t *IavlTree) Prune(version uint64) error {
return t.tree.DeleteVersionsTo(int64(version))
}

// Snapshot implements snapshottypes.CommitSnapshotter. The snapshot output for a given format must be
// identical across nodes such that chunks from different sources fit together.
// Snapshot implements snapshottypes.CommitSnapshotter.
func (t *IavlTree) Snapshot(version uint64, protoWriter protoio.Writer) error {
if version == 0 {
return fmt.Errorf("the snapshot version must be greater than 0")
}

latestVersion := t.GetLatestVersion()
if version > latestVersion {
return fmt.Errorf("the snapshot version %d is greater than the latest version %d", version, latestVersion)
Expand Down Expand Up @@ -129,11 +130,12 @@ func (t *IavlTree) Snapshot(version uint64, protoWriter protoio.Writer) error {

for {
node, err := exporter.Next()
if err == iavl.ErrorExportDone {
if errors.Is(err, iavl.ErrorExportDone) {
break
} else if err != nil {
return fmt.Errorf("failed to get the next export node: %w", err)
}

if err = protoWriter.WriteMsg(&snapshottypes.SnapshotItem{
Item: &snapshottypes.SnapshotItem_IAVL{
IAVL: &snapshottypes.SnapshotIAVLItem{
Expand All @@ -152,15 +154,17 @@ func (t *IavlTree) Snapshot(version uint64, protoWriter protoio.Writer) error {
}

// Restore implements snapshottypes.CommitSnapshotter.
// returns next snapshot item and error.
func (t *IavlTree) Restore(version uint64, format uint32, protoReader protoio.Reader, chStorage chan<- *store.KVPair) (snapshottypes.SnapshotItem, error) {
var importer *iavl.Importer
var snapshotItem snapshottypes.SnapshotItem
var (
importer *iavl.Importer
snapshotItem snapshottypes.SnapshotItem
)

loop:
for {
snapshotItem = snapshottypes.SnapshotItem{}
err := protoReader.ReadMsg(&snapshotItem)
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
} else if err != nil {
return snapshottypes.SnapshotItem{}, fmt.Errorf("invalid protobuf message: %w", err)
Expand All @@ -179,7 +183,7 @@ loop:
if importer == nil {
return snapshottypes.SnapshotItem{}, fmt.Errorf("received IAVL node item before store item")
}
if item.IAVL.Height > math.MaxInt8 {
if item.IAVL.Height > int32(math.MaxInt8) {
return snapshottypes.SnapshotItem{}, fmt.Errorf("node height %v cannot exceed %v",
item.IAVL.Height, math.MaxInt8)
}
Expand Down Expand Up @@ -219,7 +223,6 @@ loop:
if err != nil {
return snapshottypes.SnapshotItem{}, fmt.Errorf("failed to commit importer: %w", err)
}
importer.Close()
}

_, err := t.tree.LoadVersion(int64(version))
Expand Down
4 changes: 0 additions & 4 deletions store/snapshots/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ func (m *Manager) GetSnapshotBlockRetentionHeights() int64 {

// Create creates a snapshot and returns its metadata.
func (m *Manager) Create(height uint64) (*types.Snapshot, error) {
if m == nil {
return nil, errorsmod.Wrap(store.ErrLogic, "no snapshot store configured")
}

err := m.begin(opSnapshot)
if err != nil {
return nil, err
Expand Down
7 changes: 5 additions & 2 deletions store/storage/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
reservedStoreKey = "_RESERVED_"
keyLatestHeight = "latest_height"

// TODO: it is a random number, need to be tuned
defaultBatchBufferSize = 100000

latestVersionStmt = `
Expand Down Expand Up @@ -241,8 +242,10 @@ func (db *Database) Restore(version uint64, chStorage <-chan *store.KVPair) erro
}
}

if err := b.Write(); err != nil {
return err
if b.Size() > 0 {
if err := b.Write(); err != nil {
return err
}
}

return db.SetLatestVersion(version)
Expand Down

0 comments on commit 0f462be

Please sign in to comment.