Skip to content

Commit

Permalink
feat: make ChangeSet and KVPair protobuf serializable (#726)
Browse files Browse the repository at this point in the history
(cherry picked from commit 61be28a)

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
yihuang authored and mergify[bot] committed Apr 10, 2023
1 parent 4e62b2e commit 2c3dee9
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 21 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

<<<<<<< HEAD
=======
### Improvements

- [#703](https://github.com/cosmos/iavl/pull/703) New APIs `NewCompressExporter`/`NewCompressImporter` to support more compact snapshot format.
- [#726](https://github.com/cosmos/iavl/pull/726) Make `KVPair` and `ChangeSet` serializable with protobuf.

>>>>>>> 61be28a (feat: make ChangeSet and KVPair protobuf serializable (#726))
### Breaking Changes

- [#646](https://github.com/cosmos/iavl/pull/646) Remove the `orphans` from the storage
Expand Down
9 changes: 9 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: v1
managed:
enabled: true
go_package_prefix:
default: github.com/cosmos/iavl/proto
plugins:
- plugin: buf.build/protocolbuffers/go
out: proto
opt: paths=source_relative
16 changes: 6 additions & 10 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ package iavl

import (
"bytes"
)

// ChangeSet represents the state changes extracted from diffing iavl versions.
type ChangeSet struct {
Pairs []KVPair
}
"github.com/cosmos/iavl/proto"
)

type KVPair struct {
Delete bool
Key []byte
Value []byte
}
type (
KVPair = proto.KVPair
ChangeSet = proto.ChangeSet
)

// KVPairReceiver is callback parameter of method `extractStateChanges` to receive stream of `KVPair`s.
type KVPairReceiver func(pair *KVPair) error
Expand Down
20 changes: 10 additions & 10 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,34 @@ func TestDiffRoundTrip(t *testing.T) {
tree, err := NewMutableTree(db, 0, true)
require.NoError(t, err)
for i := range changeSets {
v, err := tree.SaveChangeSet(&changeSets[i])
v, err := tree.SaveChangeSet(changeSets[i])
require.NoError(t, err)
require.Equal(t, int64(i+1), v)
}

// extract change sets from db
var extractChangeSets []ChangeSet
var extractChangeSets []*ChangeSet
tree2 := NewImmutableTree(db, 0, true)
err = tree2.TraverseStateChanges(0, math.MaxInt64, func(version int64, changeSet *ChangeSet) error {
extractChangeSets = append(extractChangeSets, *changeSet)
extractChangeSets = append(extractChangeSets, changeSet)
return nil
})
require.NoError(t, err)
require.Equal(t, changeSets, extractChangeSets)
}

func genChangeSets(r *rand.Rand, n int) []ChangeSet {
var changeSets []ChangeSet
func genChangeSets(r *rand.Rand, n int) []*ChangeSet {
var changeSets []*ChangeSet

for i := 0; i < n; i++ {
items := make(map[string]KVPair)
items := make(map[string]*KVPair)
start, count, step := r.Int63n(1000), r.Int63n(1000), r.Int63n(10)
for i := start; i < start+count*step; i += step {
value := make([]byte, 8)
binary.LittleEndian.PutUint64(value, uint64(i))

key := fmt.Sprintf("test-%d", i)
items[key] = KVPair{
items[key] = &KVPair{
Key: []byte(key),
Value: value,
}
Expand All @@ -65,7 +65,7 @@ func genChangeSets(r *rand.Rand, n int) []ChangeSet {
if pair.Delete {
continue
}
items[string(pair.Key)] = KVPair{
items[string(pair.Key)] = &KVPair{
Key: pair.Key,
Delete: true,
}
Expand All @@ -77,7 +77,7 @@ func genChangeSets(r *rand.Rand, n int) []ChangeSet {
i := r.Int63n(int64(len(lastChangeSet.Pairs)))
pair := lastChangeSet.Pairs[i]
if !pair.Delete {
items[string(pair.Key)] = KVPair{
items[string(pair.Key)] = &KVPair{
Key: pair.Key,
Value: pair.Value,
}
Expand All @@ -96,7 +96,7 @@ func genChangeSets(r *rand.Rand, n int) []ChangeSet {
cs.Pairs = append(cs.Pairs, items[key])
}

changeSets = append(changeSets, cs)
changeSets = append(changeSets, &cs)
}
return changeSets
}
2 changes: 1 addition & 1 deletion nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ func (ndb *nodeDB) traverseStateChanges(startVersion, endVersion int64, fn func(

var changeSet ChangeSet
receiveKVPair := func(pair *KVPair) error {
changeSet.Pairs = append(changeSet.Pairs, *pair)
changeSet.Pairs = append(changeSet.Pairs, pair)
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions proto/buf.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by buf. DO NOT EDIT.
version: v1
3 changes: 3 additions & 0 deletions proto/buf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## IAVL

Defines messages used by IAVL library, currently only `ChangeSet` and `KVPair`.
10 changes: 10 additions & 0 deletions proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: v1
name: buf.build/cosmos/iavl
breaking:
use:
- FILE
lint:
use:
- DEFAULT
- COMMENTS
- FILE_LOWER_SNAKE_CASE
230 changes: 230 additions & 0 deletions proto/changeset.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions proto/changeset.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";
package iavl;

option go_package = "proto";

message KVPair {
bool delete = 1;
bytes key = 2;
bytes value = 3;
}

message ChangeSet {
repeated KVPair pairs = 1;
}

0 comments on commit 2c3dee9

Please sign in to comment.