11use dojo :: model :: model_value :: ModelValueKey ;
22
3+ // TODO: define the right interface for member accesses.
4+
5+ /// A pointer to a model, which can be expressed by an entity id.
6+ /// It's different from `ModelIndex` which is used for low level accesses.
7+ #[derive(Copy , Drop , Serde , Debug , PartialEq )]
8+ pub enum ModelPtr <M > {
9+ // The id of the model.
10+ Id : felt252 ,
11+ // The keys of the model as span.
12+ Keys : Span <felt252 >,
13+ }
14+
315/// A `ModelStorage` trait that abstracts where the storage is.
416///
517/// Currently it's only world storage, but this will be useful when we have other
@@ -8,41 +20,29 @@ pub trait ModelStorage<S, M> {
820 /// Sets a model of type `M`.
921 fn write_model (ref self : S , model : @ M );
1022
23+ /// Sets multiple models of type `M`.
24+ fn write_models (ref self : S , models : Span <@ M >);
25+
1126 /// Retrieves a model of type `M` using the provided key of type `K`.
1227 fn read_model <K , + Drop <K >, + Serde <K >>(self : @ S , key : K ) -> M ;
1328
29+ /// Retrieves multiple models of type `M` using the provided keys of type `K`.
30+ /// Returnes an array to ensure the user can consume the models, even if the type is not
31+ /// copiable.
32+ fn read_models <K , + Drop <K >, + Serde <K >>(self : @ S , keys : Span <K >) -> Array <M >;
33+
1434 /// Deletes a model of type `M`.
1535 fn erase_model (ref self : S , model : @ M );
1636
17- /// Deletes a model of type `M` using the provided key of type `K `.
18- fn erase_model_from_key < K , + Drop < K >, + Serde < K >> (ref self : S , key : K );
37+ /// Deletes multiple models of type `M`.
38+ fn erase_models (ref self : S , models : Span < @ M > );
1939
2040 /// Deletes a model of type `M` using the provided entity id.
21- fn erase_model_from_id (ref self : S , entity_id : felt252 );
22-
23- /// Retrieves a member of type `T` from a model of type `M` using the provided member id and key
24- /// of type `K`.
25- fn read_member <T , K , + ModelMemberStorage <S , M , T >, + Drop <T >, + Drop <K >, + Serde <K >>(
26- self : @ S , key : K , member_id : felt252
27- ) -> T ;
28-
29- /// Updates a member of type `T` within a model of type `M` using the provided member id, key of
30- /// type `K`, and new value of type `T`.
31- fn write_member <T , K , + ModelMemberStorage <S , M , T >, + Drop <T >, + Drop <K >, + Serde <K >>(
32- ref self : S , key : K , member_id : felt252 , value : T
33- );
34-
35- /// Returns the current namespace hash.
36- fn namespace_hash (self : @ S ) -> felt252 ;
37- }
38-
39- /// A `ModelMemberStorage` trait that abstracts where the storage is.
40- pub trait ModelMemberStorage <S , M , T > {
41- /// Retrieves a member of type `T` for the given entity id and member id.
42- fn read_member_from_id (self : @ S , entity_id : felt252 , member_id : felt252 ) -> T ;
41+ /// The ptr is mostly used for type inferrence.
42+ fn erase_model_ptr (ref self : S , ptr : ModelPtr <M >);
4343
44- /// Updates a member of type `T` for the given entity id and member id .
45- fn write_member_from_id (ref self : S , entity_id : felt252 , member_id : felt252 , value : T );
44+ /// Deletes multiple models of type `M` using the provided entity ids .
45+ fn erase_models_ptrs (ref self : S , ptrs : Span < ModelPtr < M >> );
4646
4747 /// Returns the current namespace hash.
4848 fn namespace_hash (self : @ S ) -> felt252 ;
@@ -51,18 +51,32 @@ pub trait ModelMemberStorage<S, M, T> {
5151/// A `ModelValueStorage` trait that abstracts where the storage is.
5252pub trait ModelValueStorage <S , V > {
5353 /// Retrieves a model value of type `V` using the provided key of type `K`.
54- fn read_model_value <K , + Drop <K >, + Serde <K >, + ModelValueKey <V , K >>(self : @ S , key : K ) -> V ;
54+ fn read_value <K , + Drop <K >, + Serde <K >, + ModelValueKey <V , K >>(self : @ S , key : K ) -> V ;
55+
56+ /// Retrieves multiple model values of type `V` using the provided keys of type `K`.
57+ fn read_values <K , + Drop <K >, + Serde <K >, + ModelValueKey <V , K >>(
58+ self : @ S , keys : Span <K >
59+ ) -> Array <V >;
5560
5661 /// Retrieves a model value of type `V` using the provided entity id.
57- fn read_model_value_from_id (self : @ S , entity_id : felt252 ) -> V ;
62+ fn read_value_from_id (self : @ S , entity_id : felt252 ) -> V ;
63+
64+ /// Retrieves multiple model values of type `V` using the provided entity ids.
65+ fn read_values_from_ids (self : @ S , entity_ids : Span <felt252 >) -> Array <V >;
5866
5967 /// Updates a model value of type `V`.
60- fn write_model_value <K , + Drop <K >, + Serde <K >, + ModelValueKey <V , K >>(
61- ref self : S , key : K , value : @ V
68+ fn write_value <K , + Drop <K >, + Serde <K >, + ModelValueKey <V , K >>(ref self : S , key : K , value : @ V );
69+
70+ /// Updates multiple model values of type `V`.
71+ fn write_values <K , + Drop <K >, + Serde <K >, + ModelValueKey <V , K >>(
72+ ref self : S , keys : Span <K >, values : Span <@ V >
6273 );
6374
6475 /// Updates a model value of type `V`.
65- fn write_model_value_from_id (ref self : S , entity_id : felt252 , value : @ V );
76+ fn write_value_from_id (ref self : S , entity_id : felt252 , value : @ V );
77+
78+ /// Updates multiple model values of type `V`.
79+ fn write_values_from_ids (ref self : S , entity_ids : Span <felt252 >, values : Span <@ V >);
6680}
6781
6882/// A `ModelStorage` trait that abstracts where the storage is.
@@ -72,15 +86,31 @@ pub trait ModelValueStorage<S, V> {
7286pub trait ModelStorageTest <S , M > {
7387 /// Sets a model of type `M`.
7488 fn write_model_test (ref self : S , model : @ M );
89+ /// Sets multiple models of type `M`.
90+ fn write_models_test (ref self : S , models : Span <@ M >);
7591 /// Deletes a model of type `M`.
7692 fn erase_model_test (ref self : S , model : @ M );
93+ /// Deletes multiple models of type `M`.
94+ fn erase_models_test (ref self : S , models : Span <@ M >);
95+ /// Deletes a model of type `M` using the provided entity id.
96+ fn erase_model_ptr_test (ref self : S , ptr : ModelPtr <M >);
97+ /// Deletes multiple models of type `M` using the provided entity ids.
98+ fn erase_models_ptrs_test (ref self : S , ptrs : Span <ModelPtr <M >>);
7799}
78100
79101/// A `ModelValueStorageTest` trait that abstracts where the storage is and bypass the permission
80102/// checks.
81103pub trait ModelValueStorageTest <S , V > {
82104 /// Updates a model value of type `V`.
83- fn write_model_value_test (ref self : S , entity_id : felt252 , value : @ V );
84- /// Deletes a model value of type `V`.
85- fn erase_model_value_test (ref self : S , entity_id : felt252 );
105+ fn write_value_test <K , + Drop <K >, + Serde <K >, + ModelValueKey <V , K >>(
106+ ref self : S , key : K , value : @ V
107+ );
108+ /// Updates multiple model values of type `V`.
109+ fn write_values_test <K , + Drop <K >, + Serde <K >, + ModelValueKey <V , K >>(
110+ ref self : S , keys : Span <K >, values : Span <@ V >
111+ );
112+ /// Updates a model value of type `V`.
113+ fn write_value_from_id_test (ref self : S , entity_id : felt252 , value : @ V );
114+ /// Updates multiple model values of type `V`.
115+ fn write_values_from_ids_test (ref self : S , entity_ids : Span <felt252 >, values : Span <@ V >);
86116}
0 commit comments