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

Create packed binary format #444

Merged
merged 1 commit into from
Dec 20, 2024
Merged

Create packed binary format #444

merged 1 commit into from
Dec 20, 2024

Conversation

jodastephen
Copy link
Member

@jodastephen jodastephen commented Dec 20, 2024

  • Define the input/output classes for the new binary format
  • Design is inspired by MessagePack, and partly compatible
  • First commit just introduces the format, without the reader/writer

Summary by CodeRabbit

  • New Features

    • Introduced classes for BeanPack serialization and deserialization, including BeanPack, BeanPackInput, BeanPackOutput, and BeanPackVisualizer.
    • Added methods for handling various data types, including primitives and complex types, enhancing data processing capabilities.
  • Bug Fixes

    • Enhanced test coverage to ensure mutable beans that should not support cloning are correctly identified.
  • Documentation

    • Improved method visibility for test methods to enhance encapsulation.

Copy link

coderabbitai bot commented Dec 20, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces a comprehensive set of classes for binary serialization in the Joda Beans library, specifically focusing on MsgPack-based serialization. The new classes BeanPack, BeanPackInput, BeanPackOutput, and BeanPackVisualizer provide a robust framework for handling binary data serialization, input processing, output generation, and data visualization. Additionally, two test classes were modified to enhance testing coverage for cloning and meta bean provider functionality.

Changes

File Change Summary
src/main/java/org/joda/beans/ser/bin/BeanPack.java Added abstract class with constants for MsgPack binary serialization, including type codes for various data structures, primitives, and collections.
src/main/java/org/joda/beans/ser/bin/BeanPackInput.java Added abstract input processing class for reading and parsing BeanPack data streams.
src/main/java/org/joda/beans/ser/bin/BeanPackOutput.java Added final class for serializing data types into binary format with methods for writing primitives, arrays, maps, and complex types.
src/main/java/org/joda/beans/ser/bin/BeanPackVisualizer.java Added final class for visualizing BeanPack data, with methods to format and display different data types.
src/test/java/org/joda/beans/TestClone.java Added new test method test_noclone_on_mutable_bean_option() to verify cloning behaviour.
src/test/java/org/joda/beans/TestMetaBeans.java Changed method visibility of test_metaBeanProviderAnnotation() from public to package-private.

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request. We would love to hear your feedback on Discord.


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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
src/test/java/org/joda/beans/TestClone.java (1)

Line range hint 64-70: Consider enhancing the test robustness and clarity

While the test correctly verifies the absence of a clone method, consider these improvements:

  1. The test should also check for inherited methods using getMethods() instead of just getDeclaredMethods().
  2. The assertion message could be more descriptive about the test's purpose.
  3. The test name could better reflect that it's verifying the absence of clone functionality.

Here's a suggested improvement:

-    void test_noclone_on_mutable_bean_option() {
+    void test_clone_method_should_not_exist_on_mutable_bean() {
         // test that clone() was not code-generated
         Class<?> c = NoClone.class;
-        Method[] noCloneMethods = c.getDeclaredMethods();
+        Method[] noCloneMethods = c.getMethods();
 
         for (Method method : noCloneMethods) {
-            assertThat("clone").isNotEqualTo(method.getName());
+            assertThat(method.getName())
+                .as("Mutable bean NoClone should not have a clone method")
+                .isNotEqualTo("clone");
         }
     }
src/main/java/org/joda/beans/ser/bin/BeanPackOutput.java (2)

93-102: Consider simplifying negative zero handling.
The check Double.compare(value, -0d) != 0 is correct for excluding -0.0. However, you might consider if special handling of negative zero is needed in this serialisation scenario. If not explicitly required, removing the check could simplify the logic.


174-203: Potential duplication with int/long logic.
The logic for serialising longs largely duplicates the int logic. Consider extracting a shared helper function if you need frequent updates or want to reduce code size.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a0ba88f and d49af86.

📒 Files selected for processing (6)
  • src/main/java/org/joda/beans/ser/bin/BeanPack.java (1 hunks)
  • src/main/java/org/joda/beans/ser/bin/BeanPackInput.java (1 hunks)
  • src/main/java/org/joda/beans/ser/bin/BeanPackOutput.java (1 hunks)
  • src/main/java/org/joda/beans/ser/bin/BeanPackVisualizer.java (1 hunks)
  • src/test/java/org/joda/beans/TestClone.java (1 hunks)
  • src/test/java/org/joda/beans/TestMetaBeans.java (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • src/main/java/org/joda/beans/ser/bin/BeanPack.java
🧰 Additional context used
📓 Path-based instructions (5)
src/test/java/org/joda/beans/TestClone.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/test/java/**/*.java: For test code, focus on:

  • Correctness of test assertions
  • Test coverage of edge cases
  • Clear test naming and documentation
  • Encourage test methods to be package-scoped where possible
  • Be more lenient with code style and minor optimisations
src/test/java/org/joda/beans/TestMetaBeans.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/test/java/**/*.java: For test code, focus on:

  • Correctness of test assertions
  • Test coverage of edge cases
  • Clear test naming and documentation
  • Encourage test methods to be package-scoped where possible
  • Be more lenient with code style and minor optimisations
src/main/java/org/joda/beans/ser/bin/BeanPackInput.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
src/main/java/org/joda/beans/ser/bin/BeanPackOutput.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
src/main/java/org/joda/beans/ser/bin/BeanPackVisualizer.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
🔇 Additional comments (3)
src/test/java/org/joda/beans/TestMetaBeans.java (1)

31-34: Excellent change to package-private visibility!

The change aligns with our coding guidelines for test methods. The test is well-structured with:

  • Clear naming that describes the test purpose
  • Focused assertion that verifies the meta bean lookup behaviour
  • Modern testing practices using JUnit Jupiter and AssertJ
src/main/java/org/joda/beans/ser/bin/BeanPackOutput.java (1)

144-165: No major issues spotted.
The int serialisation logic is well structured and accounts for positive/negative ranges. The code is consistent with Java 21 best practices.

src/main/java/org/joda/beans/ser/bin/BeanPackVisualizer.java (1)

193-206: Check indexing logic for chunked doubles.
The loop increments i inside a nested block, causing i to increment up to twice per iteration. While this may be intentional for chunking lines of output, ensure it aligns with the intended behaviour. Otherwise, consider a clearer approach to chunk the output.

Comment on lines 307 to 313
private Duration parseDuration() throws IOException {
var seconds = input.readLong();
var nanos = input.readInt();
var duration = Duration.ofSeconds(seconds, nanos);
handleDuration(duration);
return parseDuration();
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Infinite recursion in parseDuration.
The method parseDuration(Duration duration) calls handleDuration(duration) then, on line 312, it returns parseDuration() again instead of returning the duration. This causes endless recursion and will crash at runtime.

Suggested fix:

  handleDuration(duration);
- return parseDuration();
+ return duration;
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private Duration parseDuration() throws IOException {
var seconds = input.readLong();
var nanos = input.readInt();
var duration = Duration.ofSeconds(seconds, nanos);
handleDuration(duration);
return parseDuration();
}
private Duration parseDuration() throws IOException {
var seconds = input.readLong();
var nanos = input.readInt();
var duration = Duration.ofSeconds(seconds, nanos);
handleDuration(duration);
return duration;
}

* Define the input/output classes for the new binary format
* Design is inspired by MessagePack, and partly compatible
* First commit just introduces the format, without the reader/writer
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
src/main/java/org/joda/beans/ser/bin/BeanPackVisualizer.java (3)

31-36: Consider alternative approach to building output text.
The current approach with a single shared StringBuilder is perfectly fine for moderate usage. However, if you expect large or deeply nested structures in future usage scenarios, a more specialised approach (e.g., streaming data to a file or a custom writer) could be considered for enhanced performance and memory usage.


61-83: Indent manipulation is correct, but watch out for boundary conditions.
Each path appends or trims two characters from “indent”. If an unexpected path changes “indent” by more or fewer than two characters, the substring operation could cause an exception. Currently, the code is consistent, yet it might be safer to guard the substring usage in case of any future code modifications.


Line range hint 331-333: Consider adjusting the parameter type.
The parameter for toHex is an int, immediately narrowed with a cast to byte, which might be slightly misleading. If you really intend each input to be a single 8-bit value, consider changing the signature to “toHex(byte b)” for clarity.

src/main/java/org/joda/beans/ser/bin/BeanPack.java (1)

27-333: Potential future refactor to enums or record.
A large set of integer constants is normal practice for a binary format specification. If future evolution demands grouping or variation, reconsider an approach using enumerations or separate record structures to organise them more robustly. For present usage, this approach is clear and straightforward.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d49af86 and 1c4a693.

📒 Files selected for processing (6)
  • src/main/java/org/joda/beans/ser/bin/BeanPack.java (1 hunks)
  • src/main/java/org/joda/beans/ser/bin/BeanPackInput.java (1 hunks)
  • src/main/java/org/joda/beans/ser/bin/BeanPackOutput.java (1 hunks)
  • src/main/java/org/joda/beans/ser/bin/BeanPackVisualizer.java (1 hunks)
  • src/test/java/org/joda/beans/TestClone.java (1 hunks)
  • src/test/java/org/joda/beans/TestMetaBeans.java (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/test/java/org/joda/beans/TestClone.java
  • src/test/java/org/joda/beans/TestMetaBeans.java
  • src/main/java/org/joda/beans/ser/bin/BeanPackInput.java
  • src/main/java/org/joda/beans/ser/bin/BeanPackOutput.java
🧰 Additional context used
📓 Path-based instructions (2)
src/main/java/org/joda/beans/ser/bin/BeanPackVisualizer.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
src/main/java/org/joda/beans/ser/bin/BeanPack.java (2)

Pattern **/*.java: - Review code using Java 21 standards, taking into account the rules defined by src/main/checkstyle/checkstyle.xml.

  • Validate that code indentation uses spaces, not tabs, with an indent of multiple of 4.
  • Validate that immutable local variables are not annotated with final unless the variable is required for use in an inner class.
  • Favour use of var keyword for type declarations. var may also be used when the value is a cast null.
  • Use a coding standard where multi-line expressions have operators and tenary separators at the end of line.
  • Propose changes that only use the Java 21 API, not the API of Guava.
  • The pattern matching instanceof expression safely handles null, returning false.

Pattern **/main/java/**/*.java: - This project is mature and must provide a stable backwards-compatible public Java API.

  • In the 'Walkthrough' section, you must always provide a list of up to 25 changes to the public Java API that will affect end users.
    If there are no changes, you must explicitly state that there are no changes to the public Java API in this PR.
    The public Java API is defined as public and protected methods on public classes, plus the file module-info.java.
    Provide the list by deeply analysing code flow, which incudes analysing code flow through private methods and calls to Guava and Java 21.
    Changes to be reported on include:
    • New or removed methods in the public Java API
    • Changes to method return types or parameter types in the public Java API
    • Changes to method behaviour in the public Java API that might affect consumers
  • This project uses System.out.println instead of logging
  • This project tends to prefer for loops to streams for performance reasons, however either form is acceptable.
    Do not make suggestions to change between streams and for loops or vice versa.
🔇 Additional comments (4)
src/main/java/org/joda/beans/ser/bin/BeanPackVisualizer.java (3)

27-27: No immediate concerns regarding class declaration and scope.
This class is package-private and final, so it should not leak into the public API. Its usage is presumably limited to internal operations, ensuring stable backwards compatibility.


51-58: Minimal error handling in visualisation.
When an error occurs in reading, you append “!!ERROR!!” to the buffer and return it. This is a straightforward approach, but depending on the usage context, you may want finer-grained exception details, partial results, or structured error reporting.


94-97: Replacing “-” and “=” in the indent could alter the alignment.
This line selectively replaces indentation characters to produce more readable output. This technique is clear, but be aware that if hyphens or equals signs appear in future expansions of the indentation scheme, they may inadvertently be removed.

src/main/java/org/joda/beans/ser/bin/BeanPack.java (1)

26-26: Use of an abstract class for constants is acceptable.
You have placed these constants in an abstract class with package-private scope. This arrangement prevents them from becoming part of the public API, which is advisable for internal usage.

@jodastephen jodastephen merged commit 6eaf782 into main Dec 20, 2024
5 checks passed
@jodastephen jodastephen deleted the beanpack branch December 20, 2024 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant