Skip to content

Commit

Permalink
feat: make ChangeSet and KVPair protobuf serializable (backport: #726) (
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang authored Apr 10, 2023
1 parent aac157e commit e1e212a
Show file tree
Hide file tree
Showing 10 changed files with 645 additions and 39 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## UNRELEASED

### Improvements

- [#726](https://github.com/cosmos/iavl/pull/726) Make `KVPair` and `ChangeSet` serializable with protobuf.

## 0.19.5 (February 2, 2023)

### Breaking Changes
Expand Down
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 @@ -51,14 +51,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 @@ -74,7 +74,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 @@ -86,7 +86,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 @@ -1078,7 +1078,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
Loading

0 comments on commit e1e212a

Please sign in to comment.