Skip to content

Commit

Permalink
make ChangeSet and KVPair protobuf serializable
Browse files Browse the repository at this point in the history
Implement protobuf messages, and setup buf.build

```
$ buf generate proto
```
  • Loading branch information
yihuang committed Apr 10, 2023
1 parent 4f09420 commit 7a6e578
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 15 deletions.
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
8 changes: 4 additions & 4 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ 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 Down
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 7a6e578

Please sign in to comment.