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

refactor(torii): update query logic for them to not block #2397

Merged
merged 1 commit into from
Sep 13, 2024

Conversation

lambda-0x
Copy link
Collaborator

@lambda-0x lambda-0x commented Sep 8, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced query handling with the introduction of a QueryType for better categorization during execution.
    • Streamlined SQL operations for inserting and updating entities, improving efficiency and clarity.
    • Refactored database interaction into a dedicated asynchronous function for improved modularity.
  • Bug Fixes

    • Removed redundant code to reduce complexity and potential performance issues.
  • Refactor

    • Improved code organization by encapsulating database logic and enhancing separation of concerns.

Copy link

coderabbitai bot commented Sep 8, 2024

Walkthrough

Ohayo, sensei! The changes enhance the QueryQueue struct by incorporating a new QueryType enum to manage different query types effectively. The execute_all method is updated to process these query types, differentiating between SetEntity and other queries. The SQL handling logic is refined to improve entity insertion and retrieval, ensuring better management of updates in the query queue. Additionally, the set_entity functionality in the Relay implementation is refactored into a separate asynchronous function for improved modularity.

Changes

Files Change Summary
crates/torii/core/src/query_queue.rs Added QueryType enum and modified enqueue, push_front, and execute_all methods to handle different query types.
crates/torii/core/src/sql.rs Updated insertion logic to streamline entity updates and retrieval. Added QueryType for query management. Removed redundant code related to entity fetching.
crates/torii/libp2p/src/server/mod.rs Refactored set_entity functionality into a separate asynchronous function for improved modularity and readability.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant QueryQueue
    participant SQL
    participant Broker

    Client->>QueryQueue: Enqueue query
    QueryQueue->>SQL: Execute queries based on QueryType
    SQL->>QueryQueue: Return results
    QueryQueue->>SQL: Retrieve updated entity if SetEntity
    SQL->>QueryQueue: Return updated entity
    QueryQueue->>Broker: Send BrokerMessage(EntityUpdated)
Loading

Tip

OpenAI O1 model for chat
  • We have deployed OpenAI's latest O1 model for chat.
  • OpenAI claims that this model has superior reasoning capabilities than their GPT-4o model.
  • Please share any feedback with us in the discussions post.

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>.
    • 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 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 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 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: 2

crates/torii/core/src/sql.rs Outdated Show resolved Hide resolved
crates/torii/core/src/sql.rs Outdated Show resolved Hide resolved
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

Outside diff range, codebase verification and nitpick comments (2)
crates/torii/core/src/query_queue.rs (1)

68-75: Well-implemented method for adding publish queries.

The push_publish_query method correctly handles the addition of new publish queries to the queue. Consider adding a brief comment above the method to explain its purpose for future maintainability.

crates/torii/libp2p/src/server/mod.rs (1)

522-534: Ohayo, sensei! The new set_entity function is a masterstroke in modularity.

Encapsulating the database interaction logic into this function improves readability and maintainability. It would be beneficial to add detailed error handling documentation to aid in debugging and maintenance.

Copy link

codecov bot commented Sep 9, 2024

Codecov Report

Attention: Patch coverage is 83.20000% with 21 lines in your changes missing coverage. Please review.

Project coverage is 67.93%. Comparing base (ebb3b70) to head (cb290a8).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/torii/core/src/sql.rs 71.42% 18 Missing ⚠️
crates/torii/core/src/query_queue.rs 94.73% 2 Missing ⚠️
crates/torii/libp2p/src/server/mod.rs 95.83% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2397      +/-   ##
==========================================
+ Coverage   67.89%   67.93%   +0.03%     
==========================================
  Files         364      364              
  Lines       47773    47832      +59     
==========================================
+ Hits        32435    32494      +59     
  Misses      15338    15338              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lambda-0x lambda-0x changed the title refactor(torii): queries for them to not block refactor(torii): update query logic for them to not block Sep 9, 2024
Copy link
Collaborator

@glihm glihm left a comment

Choose a reason for hiding this comment

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

Looks good to have write and read separated in preparation for multithreading and also allows the batching of inserts.

@Larkooo any additional comment on this one?

@lambda-0x lambda-0x force-pushed the spr/main/1b6c03a5 branch 3 times, most recently from 4085f11 to 3871fa8 Compare September 13, 2024 08:52
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

Outside diff range and nitpick comments (1)
crates/torii/core/src/sql.rs (1)

Line range hint 1050-1186: Ohayo sensei! The model query building function looks good, but could use more comments.

The function handles various scenarios for different model types and correctly builds the create table and insert queries based on the model type and member details. The logic seems to be sound and there are no major issues.

However, given the complexity of the function, it would be beneficial to add more comments explaining the different scenarios and the reasoning behind the query building logic. This will greatly improve the maintainability and readability of the code.

Consider adding more comments to explain the different scenarios and the query building logic for each model type. This will make it easier for future developers to understand and maintain this complex function.

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3871fa8 and cb290a8.

Files ignored due to path filters (2)
  • spawn-and-move-db.tar.gz is excluded by !**/*.gz
  • types-test-db.tar.gz is excluded by !**/*.gz
Files selected for processing (3)
  • crates/torii/core/src/query_queue.rs (3 hunks)
  • crates/torii/core/src/sql.rs (21 hunks)
  • crates/torii/libp2p/src/server/mod.rs (2 hunks)
Additional comments not posted (12)
crates/torii/core/src/query_queue.rs (7)

34-34: Ohayo, sensei! The change to the queue field looks good.

The update to store tuples with the new QueryType parameter will enable more effective handling of different query types during execution. Nice work!


40-44: The new QueryType enum is a great addition, sensei!

Defining the SetEntity(Ty) and Other variants will enable more nuanced handling of different query types. This will definitely improve the control flow and functionality of the QueryQueue. Keep up the good work!


51-57: Updating the enqueue method to accept the query_type parameter is spot on, sensei!

This change is essential to leverage the new QueryType parameter in the execute_all method for determining how to handle each query during execution. You're on the right track!


60-66: The update to the push_front method looks great, sensei!

Accepting the query_type parameter here is crucial for leveraging the new QueryType in the execute_all method. You've got the right idea!


Line range hint 73-116: Ohayo, sensei! The enhancements to the execute_all method are fantastic!

The introduction of the match statement to direct the execution path based on the query type significantly improves the control flow and overall functionality of the QueryQueue. Fetching a single row for SetEntity queries and publishing an EntityUpdated message is a great addition.

Changing the return type to Result<()> aligns well with the new focus on handling messages rather than counting affected rows.

These changes really elevate the functionality of the QueryQueue. Excellent work, sensei!


111-111: Using the send_broker_message helper function here is a nice touch, sensei!

Centralizing the logic for sending broker messages in a separate function definitely improves code organization and readability. Good thinking!


118-123: The send_broker_message helper function is a great addition, sensei!

Centralizing the logic for sending different types of broker messages in this helper function definitely improves code organization and readability. It's a clean and effective way to handle sending broker messages. Well done!

crates/torii/libp2p/src/server/mod.rs (2)

522-534: Ohayo sensei! The refactoring looks great! 👍

Moving the database interaction logic into a separate set_entity function improves code modularity and readability. It encapsulates the concerns well and makes the code easier to understand and maintain. The error handling is preserved, and the function returns a result to indicate success or failure.

This refactoring aligns with best practices and should facilitate easier testing and future enhancements. Excellent work!


352-368: The refactored function call looks perfect, sensei!

You've correctly replaced the inline database call with the new set_entity function. The parameters are passed correctly, and the error handling remains intact.

This change completes the refactoring and ensures that the new modular function is utilized properly. Great job!

crates/torii/core/src/sql.rs (3)

184-194: Ohayo sensei! The set_entity function looks great! 👍

The changes incorporate the new QueryType::SetEntity to categorize the insert query based on the entity type. This aligns perfectly with the PR objective of using the QueryType enum to manage different query types effectively.


300-311: Skipping comment as the past review is still applicable.

The past review comment on this change is still valid:

coderabbitai[bot]: Update query for setting model members.

The update query for setting model members now includes a clearer separation of concerns by explicitly handling the update timestamps and event IDs. This change improves the clarity of the SQL operations and ensures that updates are handled more effectively.


Line range hint 875-900: Ohayo sensei! The recursive delete query building looks solid!

The function correctly handles the recursive deletion of an entity and its nested members. By using push_front, it ensures that the delete queries are executed before any other queries in the queue, which is crucial for maintaining data integrity.

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.

3 participants