Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make ChangeSet and KVPair protobuf serializable #726

Merged
merged 3 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### 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.

### Breaking Changes

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 @@ -833,7 +833,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;
}