Skip to content

Commit

Permalink
Add log
Browse files Browse the repository at this point in the history
  • Loading branch information
yzang2019 committed Oct 7, 2023
1 parent e600427 commit 156e7eb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
22 changes: 8 additions & 14 deletions changeset/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"fmt"
"io"
"math"
"math/bits"
"os"
"path/filepath"
Expand Down Expand Up @@ -122,6 +123,12 @@ func dumpChangesetSegment(outputDir string, tree *iavl.ImmutableTree, start int6
fmt.Printf("Error: %v\n", err)
return err
}
var endingHeader [16]byte
binary.LittleEndian.PutUint64(endingHeader[:], math.MaxUint64)
binary.LittleEndian.PutUint64(endingHeader[8:], uint64(0))
if _, err := zstdWriter.Write(endingHeader[:]); err != nil {
return err
}

return zstdWriter.Flush()
}
Expand All @@ -135,13 +142,12 @@ func WriteChangeSet(writer io.Writer, version int64, cs iavl.ChangeSet) error {
return nil
}

fmt.Printf("Version: %d\n", version)
fmt.Printf("Writing Version: %d\n", version)
var size int
items := make([][]byte, 0, len(cs.Pairs))
for _, pair := range cs.Pairs {
buf, err := encodeKVPair(pair)
if err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
size += len(buf)
Expand All @@ -153,15 +159,11 @@ func WriteChangeSet(writer io.Writer, version int64, cs iavl.ChangeSet) error {
binary.LittleEndian.PutUint64(versionHeader[:], uint64(version))
binary.LittleEndian.PutUint64(versionHeader[8:], uint64(size))

fmt.Printf("Total Size: %d\n", size)

if _, err := writer.Write(versionHeader[:]); err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
for _, item := range items {
if _, err := writer.Write(item); err != nil {
fmt.Printf("Error: %v\n", err)
return err
}
}
Expand All @@ -173,15 +175,12 @@ func WriteChangeSet(writer io.Writer, version int64, cs iavl.ChangeSet) error {
// returns error if key/value length overflows.
func encodeKVPair(pair *iavl.KVPair) ([]byte, error) {
buf := make([]byte, encodedSizeOfKVPair(pair))
fmt.Printf("encodedSizeOfKVPair: %d\n", encodedSizeOfKVPair(pair))

offset := 1
keyLen := len(pair.Key)
fmt.Printf("offset: %d\n", offset)

written := binary.PutUvarint(buf[offset:], uint64(keyLen))
offset += written
fmt.Printf("written: %d, offset: %d\n", written, offset)

copy(buf[offset:], pair.Key)
if pair.Delete {
Expand All @@ -190,19 +189,14 @@ func encodeKVPair(pair *iavl.KVPair) ([]byte, error) {
} else {
buf[0] = 0
}
fmt.Printf("key: %X\n", pair.Key)

offset += keyLen
fmt.Printf("keyLen: %d, offset: %d\n", keyLen, offset)

valueLen := len(pair.Value)
written = binary.PutUvarint(buf[offset:], uint64(valueLen))
offset += written
fmt.Printf("written: %d, offset: %d\n", written, offset)

copy(buf[offset:], pair.Value)
offset += valueLen
fmt.Printf("valueLen: %d, offset: %d\n", valueLen, offset)

return buf, nil
}
Expand Down
19 changes: 11 additions & 8 deletions changeset/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/DataDog/zstd"
"github.com/cosmos/iavl"
"io"
"math"
"os"
)

Expand Down Expand Up @@ -58,12 +59,12 @@ func iterateChangeSet(reader Reader, fn func(version int64, changeset *iavl.Chan
for true {
offset, version, changeSet, err := readNextChangeset(reader)
if err != nil {
if err == io.EOF {
// Reaching here means we are done reading everything in the file
break
} else {
return lastOffset, err
}
return lastOffset, err
}

if offset == -1 || version == -1 {
// this means we are done
break
}
shouldStop, err := fn(version, changeSet)
lastOffset += offset
Expand All @@ -83,9 +84,11 @@ func readNextChangeset(reader Reader) (int64, int64, *iavl.ChangeSet, error) {
}
// Read header
version := binary.LittleEndian.Uint64(versionHeader[:8])
fmt.Printf("Version: %d\n", version)
fmt.Printf("Reading Version: %d\n", version)
if version == math.MaxUint64 {
return -1, -1, nil, nil
}
size := int64(binary.LittleEndian.Uint64(versionHeader[8:16]))
fmt.Printf("Size: %d\n", size)
if size <= 0 {
return 16, int64(version), nil, nil
}
Expand Down

0 comments on commit 156e7eb

Please sign in to comment.