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/reduce mapping layers #403

Merged
merged 9 commits into from
Nov 9, 2023
Merged

Feat/reduce mapping layers #403

merged 9 commits into from
Nov 9, 2023

Conversation

amimart
Copy link
Member

@amimart amimart commented Oct 25, 2023

Closes #404

Proposed solution

To avoid unnecessary mapping layers, it is mandatory that the QueryEngine doesn't return already mapped data, its design is thus altered to provide a solutions iterator by exposing the resolved variables from the state directly.

As a common usage of the QueryEngine::select in the processing of DeleteData, Describe and Construct messages is to recompose triples or atoms from a set of patterns applied on resolved variables, I propose to introduce new iterator implementations (i.e. ResolvedTripleIterator & ResolvedAtomIterator) which based on a set of patterns and a solutions iterator offer those recomposition.

The StoreEngine has been in consequence updated to manage triple deletion directly from state::Triple, this way there is no mapping layer between the select query and the actual deletion.

Bug fixes

  • The StoreEngine now doesn't decrease counters (i.e. triple count & total byte size) when deleting non existing triples.
  • On DeleteData, Describe and Construct when a resolved triple pattern leads to an impossible triple construction (e.g. a variable from the object term representing a simple literal used as a predicate) it doesn't fail anymore, the solution is ignored.

Summary by CodeRabbit

Refactor:

  • Improved the execute and query modules in contract.rs for better code readability and efficiency.
  • Enhanced the functionality of the QueryEngine struct and related functions in engine.rs.
  • Reorganized the PlanBuilder struct and its methods in plan_builder.rs for better code structure.
  • Removed unused TriplePattern struct from atom.rs to clean up the codebase.

New Features:

  • Introduced mapper module in mod.rs for better code organization.
  • Added resolve_from_key method in namespaces.rs to resolve a Namespace from its internal key.

Bug Fixes:

  • Fixed typos and incorrect function usage in contract.rs for correct program execution.
  • Updated functions in mapper.rs to handle prefixed IRIs and resolve namespaces.

Chores:

  • Updated StoreEngine struct in engine.rs to use more appropriate data types and methods.
  • Removed unused imports and dependencies across multiple files.

@amimart amimart self-assigned this Oct 25, 2023
@coderabbitai
Copy link

coderabbitai bot commented Oct 25, 2023

Warning

Rate Limit Exceeded

@amimart has exceeded the limit for the number of files or commits that can be reviewed per hour. Please wait 6 minutes and 6 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per repository.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 42b07e3 and f08f7d2.

Walkthrough

The changes primarily focus on enhancing the functionality of the query engine and refactoring the code for better organization and readability. Key modifications include the introduction of new structs and methods, changes to function parameters and return types, and the removal of unused code. The updates aim to improve the handling of triples and atoms, namespace resolution, and error handling.

Changes

File(s) Summary
contracts/okp4-cognitarium/src/contract.rs Modified the execute and query modules, updating several functions to use new methods for handling triples and atoms.
contracts/okp4-cognitarium/src/querier/engine.rs
contracts/okp4-cognitarium/src/querier/mapper.rs
contracts/okp4-cognitarium/src/querier/mod.rs
contracts/okp4-cognitarium/src/querier/plan_builder.rs
Introduced changes to the QueryEngine struct and related functions, added a new "mapper" module, and refactored the "engine" and "plan_builder" modules.
contracts/okp4-cognitarium/src/rdf/atom.rs
contracts/okp4-cognitarium/src/rdf/mapper.rs
Removed unused TriplePattern struct and related test cases, and removed TryFrom trait implementations for certain types.
contracts/okp4-cognitarium/src/state/namespaces.rs Updated the NamespaceBatchService struct, adding a new method and modifying existing ones for better namespace handling.
contracts/okp4-cognitarium/src/storer/engine.rs Made several changes to the StoreEngine struct and its methods, including parameter and return type modifications, and improved handling of triples and namespaces.

Poem

As the leaves fall, so does old code, 🍂

Making way for the new, a sight to behold. 🌟

With each change, we hop and twirl, 🐇

In this digital world, we continue to whirl. 💫

Today we celebrate, with hot cocoa in hand, ☕️

The beauty of code, isn't it just grand? 🎉

So here's to the season, full of change and delight, 🍁

Happy coding to all, and to all a good night! 🌙


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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 help to get help.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.

Note: For conversation with the bot, please use the review comments on code diffs or files.

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.json

@bot-anik
Copy link
Member

bot-anik commented Oct 25, 2023

size-limit report 📦

Path Size
target/wasm32-unknown-unknown/release/okp4_objectarium.wasm 431.54 KB (0%)
target/wasm32-unknown-unknown/release/okp4_law_stone.wasm 645.23 KB (0%)
target/wasm32-unknown-unknown/release/okp4_cognitarium.wasm 824.93 KB (+1.97% 🔺)

@amimart amimart added bug Something isn't working enhancement New feature or request labels Oct 25, 2023
@codecov
Copy link

codecov bot commented Oct 25, 2023

Codecov Report

Merging #403 (f08f7d2) into main (a6f5dd0) will decrease coverage by 0.83%.
Report is 21 commits behind head on main.
The diff coverage is 95.40%.

@@            Coverage Diff             @@
##             main     #403      +/-   ##
==========================================
- Coverage   97.76%   96.93%   -0.83%     
==========================================
  Files          42       48       +6     
  Lines       10243     9858     -385     
==========================================
- Hits        10014     9556     -458     
- Misses        229      302      +73     
Files Coverage Δ
contracts/okp4-cognitarium/src/contract.rs 99.54% <100.00%> (+0.16%) ⬆️
contracts/okp4-cognitarium/src/querier/mapper.rs 100.00% <100.00%> (ø)
...racts/okp4-cognitarium/src/querier/plan_builder.rs 99.47% <100.00%> (-0.07%) ⬇️
contracts/okp4-cognitarium/src/rdf/atom.rs 97.97% <ø> (-2.03%) ⬇️
contracts/okp4-cognitarium/src/rdf/mapper.rs 44.44% <ø> (-55.56%) ⬇️
contracts/okp4-cognitarium/src/state/namespaces.rs 94.64% <86.36%> (+2.82%) ⬆️
contracts/okp4-cognitarium/src/storer/engine.rs 96.65% <91.52%> (-1.34%) ⬇️
contracts/okp4-cognitarium/src/querier/engine.rs 96.59% <94.61%> (-0.69%) ⬇️

... and 5 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@amimart amimart requested a review from ccamel November 2, 2023 23:01
@amimart amimart marked this pull request as ready for review November 2, 2023 23:01
@amimart
Copy link
Member Author

amimart commented Nov 2, 2023

@ccamel I've open to PR to review to take benefits from your eventual remarks and those from @coderabbitai while I'm adding some tests 😎

@amimart amimart force-pushed the feat/reduce-mapping-layers branch 2 times, most recently from bdc737a to cc94b6d Compare November 2, 2023 23:05
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 42b07e3 and bdc737a.
Files ignored due to filter (2)
  • Cargo.lock
  • contracts/okp4-cognitarium/Cargo.toml
Files selected for processing (9)
  • contracts/okp4-cognitarium/src/contract.rs (6 hunks)
  • contracts/okp4-cognitarium/src/querier/engine.rs (10 hunks)
  • contracts/okp4-cognitarium/src/querier/mapper.rs (1 hunks)
  • contracts/okp4-cognitarium/src/querier/mod.rs (1 hunks)
  • contracts/okp4-cognitarium/src/querier/plan_builder.rs (5 hunks)
  • contracts/okp4-cognitarium/src/rdf/atom.rs (3 hunks)
  • contracts/okp4-cognitarium/src/rdf/mapper.rs (2 hunks)
  • contracts/okp4-cognitarium/src/state/namespaces.rs (3 hunks)
  • contracts/okp4-cognitarium/src/storer/engine.rs (5 hunks)
Files skipped from review due to trivial changes (3)
  • contracts/okp4-cognitarium/src/querier/mod.rs
  • contracts/okp4-cognitarium/src/rdf/atom.rs
  • contracts/okp4-cognitarium/src/rdf/mapper.rs
Additional comments: 33
contracts/okp4-cognitarium/src/querier/mapper.rs (1)
  • 1-84: The changes in this hunk are mostly about adding a new parameter prefixes to the functions node_as_subject, node_as_predicate, node_as_object, literal_as_object, and iri_as_node. This parameter is used to resolve namespaces and handle prefixed IRIs. The function iri_as_node now returns a StdResult<state::Node> instead of state::Node, which allows for better error handling. The changes seem to be consistent and logical. However, ensure that all calls to these functions throughout the codebase have been updated to match the new signatures.
contracts/okp4-cognitarium/src/contract.rs (7)
  • 99-115: The delete_all method is used here to delete all triples. Ensure that this is the intended behavior and that it won't accidentally delete necessary data. Also, make sure that the delete_all method handles errors properly and doesn't fail silently if it encounters an issue.

  • 140-147: The query module has been modified. Ensure that all changes are compatible with the rest of the codebase and that all necessary updates have been made.

  • 173-176: The select function now uses the map_select_solutions function. Ensure that this function is correctly implemented and that it handles errors properly.

  • 222-231: The describe function now uses the resolve_atoms method. Ensure that this method is correctly implemented and that it handles errors properly.

  • 276-285: The construct function now uses the resolve_atoms method. Ensure that this method is correctly implemented and that it handles errors properly.

  • 321-322: The as_select_variables function is used here. Ensure that this function is correctly implemented and that it handles errors properly.

  • 341-370: The map_select_solutions function is used here. Ensure that this function is correctly implemented and that it handles errors properly.

contracts/okp4-cognitarium/src/storer/engine.rs (5)
  • 4-9: The import of blake3::Hash has been removed. Ensure that it is not used elsewhere in the code.

  • 52-63: The triple_size function now returns a StdResult<usize> instead of usize. This change is likely due to the fact that the function now performs operations that can fail and return an error. This is a good practice as it allows for better error handling.

  • 108-111: The delete_all function now takes a slice of Triple structs instead of rdf::Atom. This change is likely due to the fact that the function now operates directly on Triple structs instead of rdf::Atom. This is a good practice as it simplifies the function's implementation and makes it more efficient.

  • 115-149: The delete_triple function has been modified to use the triples().may_load and triples().replace functions instead of triples().remove. This change is likely due to the fact that the function now needs to check if the triple exists before deleting it and replace it with None if it does. This is a good practice as it prevents the function from attempting to delete non-existing triples and allows for better error handling.

  • 241-276: The triple_size, subject_size, node_size, and object_size functions now take a mutable reference to self. This change is likely due to the fact that these functions now modify the StoreEngine struct's state. This is a good practice as it allows these functions to modify the struct's state and ensures that the struct's state is always up-to-date.

contracts/okp4-cognitarium/src/state/namespaces.rs (3)
  • 194-205: The resolve_from_key method is a good addition to the NamespaceBatchService struct. It provides a way to resolve a Namespace from its internal key, which can be useful in various scenarios. The implementation looks correct and it uses the resolve_from_key method from the NamespaceResolver struct, which is the correct way to do it.

  • 238-261: The free_ref method has been updated to take a key parameter instead of a storage parameter. This is a good change as it simplifies the method signature and makes it more intuitive to use. The implementation of the method has also been updated to decrement the reference count of the namespace and update the ns_count_diff field accordingly. This is a logical change and it seems to be implemented correctly. However, it's important to ensure that the free_ref method is called correctly throughout the codebase with the new signature.

  • 286-288: The allocate method is a private method used to allocate a new Namespace with a given value. The implementation looks correct and it properly updates the ns_key_inc and ns_count_diff fields. However, it's important to note that the allocate method does not check if a Namespace with the same value already exists. If this is a possibility, it might be a good idea to add a check for this to avoid creating duplicate Namespace instances.

fn allocate(&mut self, value: String) -> Namespace {
    if self.ns_resolver.by_val.contains_key(&value) {
        panic!("Namespace with the same value already exists");
    }

    let ns = Namespace {
        value,
        key: self.ns_key_inc,
        counter: 0u128,
    };

    self.ns_key_inc += 1;
    self.ns_count_diff += 1;

    self.ns_resolver.insert(ns).borrow().clone()
}
contracts/okp4-cognitarium/src/querier/engine.rs (11)
  • 1-24: The QueryEngine and SelectResults structs are defined. The QueryEngine struct has a storage field, and the SelectResults struct has head and solutions fields. The QueryEngine struct has a new method that takes a storage argument and returns a new QueryEngine instance.

  • 47-55: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [34-52]

The select method of the QueryEngine struct is defined. It takes a QueryPlan and a vector of SelectItem as arguments, and returns a SelectResults instance. The head field of the SelectResults instance is a vector of the keys of the bindings map, and the solutions field is a SolutionsIterator instance.

  • 372-418: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [375-439]

The SolutionsIterator struct and its methods are defined. The SolutionsIterator struct has iter and bindings fields. The new method takes a ResolvedVariablesIterator and a BTreeMap of bindings as arguments, and returns a new SolutionsIterator instance. The resolve_triples and resolve_atoms methods take several arguments and return a vector of Triple and Atom instances, respectively.

  • 441-535: The ResolvedTripleIterator struct and its methods are defined. The ResolvedTripleIterator struct has iter, templates, and buffer fields. The try_new method takes several arguments and returns a new ResolvedTripleIterator instance. The next method returns the next Triple instance from the buffer.

  • 502-631: The TripleTemplate struct and its methods are defined. The TripleTemplate struct has subject, predicate, and object fields. The try_new method takes several arguments and returns a new TripleTemplate instance. The resolve method takes a BTreeMap of variables as an argument and returns a Triple instance.

  • 633-856: The ResolvedAtomIterator struct and its methods are defined. The ResolvedAtomIterator struct has ns_resolver, storage, iter, templates, and buffer fields. The try_new method takes several arguments and returns a new ResolvedAtomIterator instance. The next method returns the next Atom instance from the buffer.

  • 986-1082: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [858-1079]

The test module is defined. It contains several test cases for the select method of the QueryEngine struct.

  • 926-937: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [918-934]

This test case expects an error because the selected variable is not found in the query.

  • 946-966: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [935-963]

This test case expects a successful result with a single variable in the result set.

  • 986-1082: > Note: This review was outside of the patch, so it was mapped to the patch with the greatest overlap. Original lines [964-1066]

This test case expects a successful result with multiple variables in the result set.

  • 1068-1079: The test cases are executed and the results are asserted.
contracts/okp4-cognitarium/src/querier/plan_builder.rs (6)
  • 1-11: Imports have been updated to reflect the changes in the codebase. Ensure that these changes do not break any functionality.

  • 1-11: > Note: This review was outside of the patch, but no patch was found that overlapped with it. Original lines [12-80]

The PlanBuilder struct and its methods have been updated. The changes seem to be mostly refactoring and reorganizing the code. Ensure that these changes do not break any functionality.

  • 115-138: The build_subject_pattern and build_predicate_pattern methods have been updated to use the new node_as_subject and node_as_predicate functions. Ensure that these changes do not break any functionality.

  • 142-159: The build_object_pattern method has been updated to use the new node_as_object and literal_as_object functions. Ensure that these changes do not break any functionality.

  • 180-188: The test imports have been updated to reflect the changes in the codebase. Ensure that these changes do not break any functionality.

  • 264-335: The build_named_node test has been commented out. If this test is no longer relevant due to the changes in the codebase, consider removing it entirely. If it is still relevant, consider updating it to reflect the changes in the codebase.

337-End:
The build_triple_pattern and build_plan tests have been updated to reflect the changes in the codebase. Ensure that these changes do not break any functionality.

Copy link
Member

@ccamel ccamel left a comment

Choose a reason for hiding this comment

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

That's awesome! Great job. 👍

@amimart amimart merged commit ce6b05f into main Nov 9, 2023
16 of 18 checks passed
@amimart amimart deleted the feat/reduce-mapping-layers branch November 9, 2023 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🤯 Reduce mapping layers
3 participants