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 8, 2023
1 parent 4f09420 commit 82bca96
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
11 changes: 11 additions & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: v1
managed:
enabled: true
go_package_prefix:
default: github.com/cosmos/iavl/gen
except:
- buf.build/cosmos/gogo-proto
plugins:
- plugin: buf.build/protocolbuffers/go
out: gen
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
}
v1 "github.com/cosmos/iavl/gen/v1"
)

type KVPair struct {
Delete bool
Key []byte
Value []byte
}
type (
KVPair = v1.KVPair
ChangeSet = v1.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
230 changes: 230 additions & 0 deletions gen/v1/changeset.pb.go

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

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
12 changes: 12 additions & 0 deletions proto/v1/changeset.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";
package iavl;

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

message ChangeSet {
repeated KVPair pairs = 1;
}

0 comments on commit 82bca96

Please sign in to comment.