Skip to content
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: optimize the fast key format and encoding #923

Merged
merged 2 commits into from
Mar 27, 2024
Merged

Conversation

cool-develope
Copy link
Collaborator

@cool-develope cool-develope commented Mar 26, 2024

ref: #877

Some advanced features are missed in the master branch.

@cool-develope cool-develope requested a review from a team as a code owner March 26, 2024 21:41
Copy link

coderabbitai bot commented Mar 26, 2024

Walkthrough

The recent updates aim to improve code efficiency, readability, and maintainability. Key enhancements include introducing new encoding functions, a fast prefix formatter, and refining node key and hash handling. The restructuring of tree modification processes and adjustments in logging and key management strategies contribute to streamlining operations and reducing complexity.

Changes

File(s) Change Summary
internal/encoding/encoding.go - Added comment and simplified return in DecodeBytes.
- Introduced Encode32BytesHash function and an init for hash length initialization.
keyformat/prefix_formatter.go Introduced FastPrefixFormatter for efficient key prefixing with methods for key creation, formatting, scanning, and length retrieval.
mutable_tree.go Refactored recursiveSet to use recursiveSetLeaf for leaf nodes, enhancing readability and maintainability.
node.go, node_test.go - Updated Node struct field names, comments, and hash encoding methods.
- Modified test hash values in node_test.go.
nodedb.go - Switched to NewFastPrefixFormatter for node keys.
- Adjusted key range and retrieval methods.
- Moved deletion of the last version for legacy nodes.
- Added and commented out debug logs.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

Note: Auto-reply has been disabled for this repository by the repository owner. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Comment on lines +25 to +26
func (f *FastPrefixFormatter) Scan(key []byte, a interface{}) {
scan(a, key[1:])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Scan method lacks error handling or validation for the input parameters. Specifically, it assumes that the key slice has at least one element without checking, which could lead to a runtime panic if called with an empty slice.

Consider adding validation to ensure that the key slice is not empty before accessing its elements to prevent potential runtime panics.

@@ -620,7 +624,7 @@ func (node *Node) writeBytes(w io.Writer) error {
return fmt.Errorf("writing mode, %w", err)
}
if mode&ModeLegacyLeftNode != 0 { // legacy leftNodeKey
err = encoding.EncodeBytes(w, node.leftNodeKey)
err = encoding.Encode32BytesHash(w, node.leftNodeKey)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attempt to encode the legacy left node key using Encode32BytesHash in the writeBytes method seems incorrect. The method Encode32BytesHash is designed for encoding 32-byte hashes, not for encoding node keys.

- err = encoding.Encode32BytesHash(w, node.leftNodeKey)
+ err = encoding.EncodeBytes(w, node.leftNodeKey)

Consider using EncodeBytes or an appropriate method for encoding node keys to ensure correct behavior.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
err = encoding.Encode32BytesHash(w, node.leftNodeKey)
err = encoding.EncodeBytes(w, node.leftNodeKey)

@@ -639,7 +643,7 @@
return ErrRightNodeKeyEmpty
}
if mode&ModeLegacyRightNode != 0 { // legacy rightNodeKey
err = encoding.EncodeBytes(w, node.rightNodeKey)
err = encoding.Encode32BytesHash(w, node.rightNodeKey)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the previous comment, using Encode32BytesHash for encoding the legacy right node key is incorrect. The method is intended for hash encoding, not node key encoding.

- err = encoding.Encode32BytesHash(w, node.rightNodeKey)
+ err = encoding.EncodeBytes(w, node.rightNodeKey)

Use EncodeBytes or a suitable method for encoding node keys to ensure the correct encoding process.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
err = encoding.Encode32BytesHash(w, node.rightNodeKey)
err = encoding.EncodeBytes(w, node.rightNodeKey)

nodedb.go Outdated Show resolved Hide resolved
@tac0turtle tac0turtle merged commit 8967633 into master Mar 27, 2024
7 checks passed
@tac0turtle tac0turtle deleted the feat/fast_key branch March 27, 2024 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants