diff --git a/crates/bindings-csharp/Runtime/Internal/Autogen/RawViewDefV9.g.cs b/crates/bindings-csharp/Runtime/Internal/Autogen/RawViewDefV9.g.cs index e7fb7c3f5ad..738eeb82367 100644 --- a/crates/bindings-csharp/Runtime/Internal/Autogen/RawViewDefV9.g.cs +++ b/crates/bindings-csharp/Runtime/Internal/Autogen/RawViewDefV9.g.cs @@ -15,6 +15,8 @@ public sealed partial class RawViewDefV9 { [DataMember(Name = "name")] public string Name; + [DataMember(Name = "index")] + public uint Index; [DataMember(Name = "is_public")] public bool IsPublic; [DataMember(Name = "is_anonymous")] @@ -26,6 +28,7 @@ public sealed partial class RawViewDefV9 public RawViewDefV9( string Name, + uint Index, bool IsPublic, bool IsAnonymous, List Params, @@ -33,6 +36,7 @@ SpacetimeDB.BSATN.AlgebraicType ReturnType ) { this.Name = Name; + this.Index = Index; this.IsPublic = IsPublic; this.IsAnonymous = IsAnonymous; this.Params = Params; diff --git a/crates/bindings/src/rt.rs b/crates/bindings/src/rt.rs index 8e159ce716c..9a37e605709 100644 --- a/crates/bindings/src/rt.rs +++ b/crates/bindings/src/rt.rs @@ -735,7 +735,9 @@ where register_describer(|module| { let params = A::schema::(&mut module.inner); let return_type = I::return_type(&mut module.inner).unwrap(); - module.inner.add_view(I::NAME, true, false, params, return_type); + module + .inner + .add_view(I::NAME, module.views.len(), true, false, params, return_type); module.views.push(I::INVOKE); }) } @@ -750,7 +752,9 @@ where register_describer(|module| { let params = A::schema::(&mut module.inner); let return_type = I::return_type(&mut module.inner).unwrap(); - module.inner.add_view(I::NAME, true, true, params, return_type); + module + .inner + .add_view(I::NAME, module.views_anon.len(), true, true, params, return_type); module.views_anon.push(I::INVOKE); }) } diff --git a/crates/core/src/db/relational_db.rs b/crates/core/src/db/relational_db.rs index 5ac4f781dcd..bd43f409990 100644 --- a/crates/core/src/db/relational_db.rs +++ b/crates/core/src/db/relational_db.rs @@ -2208,6 +2208,7 @@ pub mod tests_utils { builder.add_view( name, + 0, true, is_anonymous, ProductType::unit(), diff --git a/crates/core/src/host/wasm_common/module_host_actor.rs b/crates/core/src/host/wasm_common/module_host_actor.rs index 1db46c8aebb..64731baf0f6 100644 --- a/crates/core/src/host/wasm_common/module_host_actor.rs +++ b/crates/core/src/host/wasm_common/module_host_actor.rs @@ -791,11 +791,11 @@ impl InstanceCommon { return_type, timestamp, view_db_id, - .. + is_anonymous, } = params; let info = self.info.clone(); - let view_def = info.module_def.view_by_id(view_id); + let view_def = info.module_def.view_by_id(view_id, is_anonymous); let view_name = &*view_def.name; let _outer_span = start_call_function_span(view_name, &caller_identity, caller_connection_id); diff --git a/crates/expr/src/statement.rs b/crates/expr/src/statement.rs index 291282c48a2..8a3aa3a8d70 100644 --- a/crates/expr/src/statement.rs +++ b/crates/expr/src/statement.rs @@ -613,7 +613,7 @@ mod tests { let product_type = AlgebraicType::from(columns.into()); let type_ref = builder.add_algebraic_type([], name, product_type, true); let return_type = AlgebraicType::array(AlgebraicType::Ref(type_ref)); - builder.add_view(name, true, is_anonymous, ProductType::unit(), return_type); + builder.add_view(name, 0, true, is_anonymous, ProductType::unit(), return_type); } let mut builder = RawModuleDefV9Builder::new(); diff --git a/crates/lib/src/db/raw_def/v9.rs b/crates/lib/src/db/raw_def/v9.rs index d094c1a9a84..bc6fc5ba77f 100644 --- a/crates/lib/src/db/raw_def/v9.rs +++ b/crates/lib/src/db/raw_def/v9.rs @@ -454,6 +454,9 @@ pub struct RawViewDefV9 { /// The name of the view function as defined in the module pub name: RawIdentifier, + /// The index of the view in the module's list of views. + pub index: u32, + /// Is this a public or a private view? /// Currently only public views are supported. /// Private views may be supported in the future. @@ -732,6 +735,7 @@ impl RawModuleDefV9Builder { pub fn add_view( &mut self, name: impl Into, + index: usize, is_public: bool, is_anonymous: bool, params: ProductType, @@ -739,6 +743,7 @@ impl RawModuleDefV9Builder { ) { self.module.misc_exports.push(RawMiscModuleExportV9::View(RawViewDefV9 { name: name.into(), + index: index as u32, is_public, is_anonymous, params, diff --git a/crates/schema/src/auto_migrate.rs b/crates/schema/src/auto_migrate.rs index b6e179ef205..9952d187211 100644 --- a/crates/schema/src/auto_migrate.rs +++ b/crates/schema/src/auto_migrate.rs @@ -1110,6 +1110,7 @@ mod tests { let view_return_ty_ref = builder.add_algebraic_type([], "my_view_return", view_return_ty, true); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32), ("y", AlgebraicType::U32)]), @@ -1206,6 +1207,7 @@ mod tests { let view_return_ty_ref = builder.add_algebraic_type([], "my_view_return", view_return_ty, true); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -1765,6 +1767,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -1793,6 +1796,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -1835,6 +1839,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -1850,6 +1855,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -1868,6 +1874,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -1883,6 +1890,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -1937,6 +1945,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -1952,6 +1961,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, false, ProductType::from([("x", AlgebraicType::U32)]), @@ -1970,6 +1980,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -1985,6 +1996,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32), ("y", AlgebraicType::U32)]), @@ -2003,6 +2015,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32), ("y", AlgebraicType::U32)]), @@ -2018,6 +2031,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2036,6 +2050,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32), ("y", AlgebraicType::U32)]), @@ -2051,6 +2066,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("y", AlgebraicType::U32), ("x", AlgebraicType::U32)]), @@ -2069,6 +2085,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2084,6 +2101,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2102,6 +2120,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2117,6 +2136,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2135,6 +2155,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2150,6 +2171,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2168,6 +2190,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2183,6 +2206,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2201,6 +2225,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), @@ -2216,6 +2241,7 @@ mod tests { ); builder.add_view( "my_view", + 0, true, true, ProductType::from([("x", AlgebraicType::U32)]), diff --git a/crates/schema/src/def.rs b/crates/schema/src/def.rs index 79bdcd6e38d..d04fcd061b2 100644 --- a/crates/schema/src/def.rs +++ b/crates/schema/src/def.rs @@ -248,7 +248,7 @@ impl ModuleDef { /// Convenience method to look up a view, possibly by a string, returning its id as well. pub fn view_full>(&self, name: &K) -> Option<(ViewId, &ViewDef)> { // If the string IS a valid identifier, we can just look it up. - self.views.get_full(name).map(|(idx, _, def)| (idx.into(), def)) + self.views.get(name).map(|def| (def.index, def)) } /// Convenience method to look up a reducer, possibly by a string. @@ -296,8 +296,12 @@ impl ModuleDef { } /// Look up a view by its id, panicking if it doesn't exist. - pub fn view_by_id(&self, id: ViewId) -> &ViewDef { - &self.views[id.idx()] + pub fn view_by_id(&self, id: ViewId, is_anonymoys: bool) -> &ViewDef { + self.views + .iter() + .find(|(_, def)| def.index == id && def.is_anonymous == is_anonymoys) + .expect("view id not found") + .1 } /// Looks up a lifecycle reducer defined in the module. @@ -1111,6 +1115,11 @@ pub struct ViewDef { /// This type does not have access to the `Identity` of the caller. pub is_anonymous: bool, + /// It represents the unique index of this view within the module. + /// Module contains separate list for anonymous and non-anonymous views, + /// so `is_anonymous` is needed to fully identify the view along with this index. + pub index: ViewId, + /// The parameters of the view. /// /// This `ProductType` need not be registered in the module's `Typespace`. @@ -1171,10 +1180,12 @@ impl From for RawViewDefV9 { is_public, params, return_type, + index, .. } = val; RawViewDefV9 { name: name.into(), + index: index.into(), is_anonymous, is_public, params, diff --git a/crates/schema/src/def/validate/v9.rs b/crates/schema/src/def/validate/v9.rs index 85e6c14cc88..dde4eee7a6e 100644 --- a/crates/schema/src/def/validate/v9.rs +++ b/crates/schema/src/def/validate/v9.rs @@ -456,6 +456,7 @@ impl ModuleValidator<'_> { is_anonymous, params, return_type, + index, } = view_def; let invalid_return_type = || { @@ -541,6 +542,7 @@ impl ModuleValidator<'_> { is_anonymous, is_public, params, + index: index.into(), params_for_generate: ProductTypeDef { elements: params_for_generate, recursive: false, // A `ProductTypeDef` not stored in a `Typespace` cannot be recursive.