-
Notifications
You must be signed in to change notification settings - Fork 268
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: more compact exported snapshot #703
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c9c9d0
more compact exported snapshot
yihuang cccba32
Merge branch 'master' into compress-snapshot
yihuang fe8bb13
Merge branch 'master' into compress-snapshot
yihuang 4bf5fae
Merge branch 'master' into compress-snapshot
cool-develope b838b5d
Merge branch 'master' into compress-snapshot
tac0turtle 89ca034
Merge branch 'master' into compress-snapshot
tac0turtle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package iavl | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
) | ||
|
||
type NodeExporter interface { | ||
Next() (*ExportNode, error) | ||
} | ||
|
||
type NodeImporter interface { | ||
Add(*ExportNode) error | ||
} | ||
|
||
// CompressExporter wraps the normal exporter to apply some compressions on `ExportNode`: | ||
// - branch keys are skipped | ||
// - leaf keys are encoded with delta compared with the previous leaf | ||
// - branch node's version are encoded with delta compared with the max version in it's children | ||
type CompressExporter struct { | ||
inner NodeExporter | ||
lastKey []byte | ||
versionStack []int64 | ||
} | ||
|
||
var _ NodeExporter = (*CompressExporter)(nil) | ||
|
||
func NewCompressExporter(exporter NodeExporter) NodeExporter { | ||
return &CompressExporter{inner: exporter} | ||
} | ||
|
||
func (e *CompressExporter) Next() (*ExportNode, error) { | ||
n, err := e.inner.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if n.Height == 0 { | ||
// apply delta encoding to leaf keys | ||
n.Key, e.lastKey = deltaEncode(n.Key, e.lastKey), n.Key | ||
|
||
e.versionStack = append(e.versionStack, n.Version) | ||
} else { | ||
// branch keys can be derived on the fly when import, safe to skip | ||
n.Key = nil | ||
|
||
// delta encode the version | ||
maxVersion := maxInt64(e.versionStack[len(e.versionStack)-1], e.versionStack[len(e.versionStack)-2]) | ||
e.versionStack = e.versionStack[:len(e.versionStack)-1] | ||
e.versionStack[len(e.versionStack)-1] = n.Version | ||
n.Version -= maxVersion | ||
} | ||
|
||
return n, nil | ||
} | ||
|
||
// CompressImporter wraps the normal importer to do de-compressions before hand. | ||
type CompressImporter struct { | ||
inner NodeImporter | ||
lastKey []byte | ||
minKeyStack [][]byte | ||
versionStack []int64 | ||
} | ||
|
||
var _ NodeImporter = (*CompressImporter)(nil) | ||
|
||
func NewCompressImporter(importer NodeImporter) NodeImporter { | ||
return &CompressImporter{inner: importer} | ||
} | ||
|
||
func (i *CompressImporter) Add(node *ExportNode) error { | ||
if node.Height == 0 { | ||
key, err := deltaDecode(node.Key, i.lastKey) | ||
if err != nil { | ||
return err | ||
} | ||
node.Key = key | ||
i.lastKey = key | ||
|
||
i.minKeyStack = append(i.minKeyStack, key) | ||
i.versionStack = append(i.versionStack, node.Version) | ||
} else { | ||
// use the min-key in right branch as the node key | ||
node.Key = i.minKeyStack[len(i.minKeyStack)-1] | ||
// leave the min-key in left branch in the stack | ||
i.minKeyStack = i.minKeyStack[:len(i.minKeyStack)-1] | ||
|
||
// decode branch node version | ||
maxVersion := maxInt64(i.versionStack[len(i.versionStack)-1], i.versionStack[len(i.versionStack)-2]) | ||
node.Version += maxVersion | ||
i.versionStack = i.versionStack[:len(i.versionStack)-1] | ||
i.versionStack[len(i.versionStack)-1] = node.Version | ||
} | ||
|
||
return i.inner.Add(node) | ||
} | ||
|
||
func deltaEncode(key, lastKey []byte) []byte { | ||
var sizeBuf [binary.MaxVarintLen64]byte | ||
shared := diffOffset(lastKey, key) | ||
n := binary.PutUvarint(sizeBuf[:], uint64(shared)) | ||
return append(sizeBuf[:n], key[shared:]...) | ||
} | ||
|
||
func deltaDecode(key, lastKey []byte) ([]byte, error) { | ||
shared, n := binary.Uvarint(key) | ||
if n <= 0 { | ||
return nil, fmt.Errorf("uvarint parse failed %d", n) | ||
} | ||
|
||
key = key[n:] | ||
if shared == 0 { | ||
return key, nil | ||
} | ||
|
||
newKey := make([]byte, shared+uint64(len(key))) | ||
copy(newKey, lastKey[:shared]) | ||
copy(newKey[shared:], key) | ||
return newKey, nil | ||
} | ||
|
||
// diffOffset returns the index of first byte that's different in two bytes slice. | ||
func diffOffset(a, b []byte) int { | ||
var off int | ||
var l int | ||
if len(a) < len(b) { | ||
l = len(a) | ||
} else { | ||
l = len(b) | ||
} | ||
for ; off < l; off++ { | ||
if a[off] != b[off] { | ||
break | ||
} | ||
} | ||
return off | ||
} | ||
cool-develope marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func maxInt64(a, b int64) int64 { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious about the efficiency of version delta encode?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#703 (comment)