-
Notifications
You must be signed in to change notification settings - Fork 194
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(dojo-core): add support to read/write the same member from multiple models #2939
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
241da68
feat(model-storage): enhance model storage with new read/write method…
bengineer42 c4bd769
chamged read-members
bengineer42 5f8bb06
Removed unused code
bengineer42 b344e23
feat(model): add read/write methods for multiple members and tests
bengineer42 bb9d34a
refactor(model-storage): rename read/write methods for clarity and co…
bengineer42 df4658c
merge main
glihm 851d1ad
tests: regenerate test db
glihm 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
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
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 |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
|
||
use core::panic_with_felt252; | ||
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait, Resource}; | ||
use dojo::model::{Model, ModelIndex, ModelValueKey, ModelValue, ModelStorage, ModelPtr}; | ||
use dojo::model::{ | ||
Model, ModelIndex, ModelValueKey, ModelValue, ModelStorage, ModelPtr, ModelPtrsTrait, | ||
}; | ||
use dojo::event::{Event, EventStorage}; | ||
use dojo::meta::Layout; | ||
use dojo::utils::{ | ||
|
@@ -192,6 +194,22 @@ pub impl ModelStorageWorldStorageImpl<M, +Model<M>, +Drop<M>> of ModelStorage<Wo | |
), | ||
) | ||
} | ||
|
||
fn read_members<T, +Serde<T>, +Drop<T>>( | ||
self: @WorldStorage, ptrs: Span<ModelPtr<M>>, field_selector: felt252, | ||
) -> Array<T> { | ||
let mut values: Array<T> = array![]; | ||
for entity in IWorldDispatcherTrait::entities( | ||
*self.dispatcher, | ||
Model::<M>::selector(*self.namespace_hash), | ||
ptrs.to_member_indexes(field_selector), | ||
field_layout_unwrap::<M>(field_selector), | ||
) { | ||
values.append(deserialize_unwrap(*entity)); | ||
}; | ||
values | ||
} | ||
|
||
fn write_member<T, +Serde<T>, +Drop<T>>( | ||
ref self: WorldStorage, ptr: ModelPtr<M>, field_selector: felt252, value: T, | ||
) { | ||
|
@@ -204,6 +222,22 @@ pub impl ModelStorageWorldStorageImpl<M, +Model<M>, +Drop<M>> of ModelStorage<Wo | |
); | ||
} | ||
|
||
fn write_members<T, +Serde<T>, +Drop<T>>( | ||
ref self: WorldStorage, ptrs: Span<ModelPtr<M>>, field_selector: felt252, values: Span<T>, | ||
) { | ||
let mut serialized_values = ArrayTrait::<Span<felt252>>::new(); | ||
for value in values { | ||
serialized_values.append(serialize_inline(value)); | ||
}; | ||
IWorldDispatcherTrait::set_entities( | ||
self.dispatcher, | ||
Model::<M>::selector(self.namespace_hash), | ||
ptrs.to_member_indexes(field_selector), | ||
serialized_values.span(), | ||
field_layout_unwrap::<M>(field_selector), | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Ohayo sensei! Consider these essential improvements. The implementation would benefit from two important changes:
Here's the suggested implementation: fn write_members<T, +Serde<T>, +Drop<T>>(
ref self: WorldStorage, ptrs: Span<ModelPtr<M>>, field_selector: felt252, values: Span<T>,
) {
+ assert(ptrs.len() == values.len(), 'Length mismatch');
- let mut serialized_values = ArrayTrait::<Span<felt252>>::new();
- for value in values {
- serialized_values.append(serialize_inline(value));
- };
IWorldDispatcherTrait::set_entities(
self.dispatcher,
Model::<M>::selector(self.namespace_hash),
ptrs.to_member_indexes(field_selector),
- serialized_values.span(),
+ values.map(|v| serialize_inline(v)),
field_layout_unwrap::<M>(field_selector),
);
} This implementation:
|
||
|
||
fn erase_models_ptrs(ref self: WorldStorage, ptrs: Span<ModelPtr<M>>) { | ||
let mut indexes: Array<ModelIndex> = array![]; | ||
for ptr in ptrs { | ||
|
Oops, something went wrong.
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.
Would rename to
read_member_models
, since we're reading only one member. Would you mind applying this change to all functions added to this PR?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.
I think
read_models_member
is better, thoughts?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.
Wanted to keep the
s
at then end since we return an array. Sounds good for you?