Skip to content

Commit 8307d7c

Browse files
authored
fix: view index (#3596)
# Description of Changes `call_view` and `call_view_anon` maintains separate `Vec` list for view inside Module but `ModuleDef` used to have single `Vec`. Hence, Views index is not same across `ModuleDef` and module itself. Added a `index` field inside `ViewDef` which stores it index inside module. # API and ABI breaking changes NA How complicated do you think these changes are? Grade on a scale from 1 to 5, 2, there could be some existing code relying on poistion in `Vec` and may not appear as compiler error # Testing Future smoketests should cover this.
1 parent 6096150 commit 8307d7c

File tree

9 files changed

+61
-8
lines changed

9 files changed

+61
-8
lines changed

crates/bindings-csharp/Runtime/Internal/Autogen/RawViewDefV9.g.cs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings/src/rt.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,9 @@ where
735735
register_describer(|module| {
736736
let params = A::schema::<I>(&mut module.inner);
737737
let return_type = I::return_type(&mut module.inner).unwrap();
738-
module.inner.add_view(I::NAME, true, false, params, return_type);
738+
module
739+
.inner
740+
.add_view(I::NAME, module.views.len(), true, false, params, return_type);
739741
module.views.push(I::INVOKE);
740742
})
741743
}
@@ -750,7 +752,9 @@ where
750752
register_describer(|module| {
751753
let params = A::schema::<I>(&mut module.inner);
752754
let return_type = I::return_type(&mut module.inner).unwrap();
753-
module.inner.add_view(I::NAME, true, true, params, return_type);
755+
module
756+
.inner
757+
.add_view(I::NAME, module.views_anon.len(), true, true, params, return_type);
754758
module.views_anon.push(I::INVOKE);
755759
})
756760
}

crates/core/src/db/relational_db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,6 +2208,7 @@ pub mod tests_utils {
22082208

22092209
builder.add_view(
22102210
name,
2211+
0,
22112212
true,
22122213
is_anonymous,
22132214
ProductType::unit(),

crates/core/src/host/wasm_common/module_host_actor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,11 +791,11 @@ impl InstanceCommon {
791791
return_type,
792792
timestamp,
793793
view_db_id,
794-
..
794+
is_anonymous,
795795
} = params;
796796

797797
let info = self.info.clone();
798-
let view_def = info.module_def.view_by_id(view_id);
798+
let view_def = info.module_def.view_by_id(view_id, is_anonymous);
799799
let view_name = &*view_def.name;
800800

801801
let _outer_span = start_call_function_span(view_name, &caller_identity, caller_connection_id);

crates/expr/src/statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ mod tests {
613613
let product_type = AlgebraicType::from(columns.into());
614614
let type_ref = builder.add_algebraic_type([], name, product_type, true);
615615
let return_type = AlgebraicType::array(AlgebraicType::Ref(type_ref));
616-
builder.add_view(name, true, is_anonymous, ProductType::unit(), return_type);
616+
builder.add_view(name, 0, true, is_anonymous, ProductType::unit(), return_type);
617617
}
618618

619619
let mut builder = RawModuleDefV9Builder::new();

crates/lib/src/db/raw_def/v9.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ pub struct RawViewDefV9 {
454454
/// The name of the view function as defined in the module
455455
pub name: RawIdentifier,
456456

457+
/// The index of the view in the module's list of views.
458+
pub index: u32,
459+
457460
/// Is this a public or a private view?
458461
/// Currently only public views are supported.
459462
/// Private views may be supported in the future.
@@ -732,13 +735,15 @@ impl RawModuleDefV9Builder {
732735
pub fn add_view(
733736
&mut self,
734737
name: impl Into<RawIdentifier>,
738+
index: usize,
735739
is_public: bool,
736740
is_anonymous: bool,
737741
params: ProductType,
738742
return_type: AlgebraicType,
739743
) {
740744
self.module.misc_exports.push(RawMiscModuleExportV9::View(RawViewDefV9 {
741745
name: name.into(),
746+
index: index as u32,
742747
is_public,
743748
is_anonymous,
744749
params,

crates/schema/src/auto_migrate.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ mod tests {
11101110
let view_return_ty_ref = builder.add_algebraic_type([], "my_view_return", view_return_ty, true);
11111111
builder.add_view(
11121112
"my_view",
1113+
0,
11131114
true,
11141115
true,
11151116
ProductType::from([("x", AlgebraicType::U32), ("y", AlgebraicType::U32)]),
@@ -1206,6 +1207,7 @@ mod tests {
12061207
let view_return_ty_ref = builder.add_algebraic_type([], "my_view_return", view_return_ty, true);
12071208
builder.add_view(
12081209
"my_view",
1210+
0,
12091211
true,
12101212
true,
12111213
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1765,6 +1767,7 @@ mod tests {
17651767
);
17661768
builder.add_view(
17671769
"my_view",
1770+
0,
17681771
true,
17691772
true,
17701773
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1793,6 +1796,7 @@ mod tests {
17931796
);
17941797
builder.add_view(
17951798
"my_view",
1799+
0,
17961800
true,
17971801
true,
17981802
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1835,6 +1839,7 @@ mod tests {
18351839
);
18361840
builder.add_view(
18371841
"my_view",
1842+
0,
18381843
true,
18391844
true,
18401845
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1850,6 +1855,7 @@ mod tests {
18501855
);
18511856
builder.add_view(
18521857
"my_view",
1858+
0,
18531859
true,
18541860
true,
18551861
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1868,6 +1874,7 @@ mod tests {
18681874
);
18691875
builder.add_view(
18701876
"my_view",
1877+
0,
18711878
true,
18721879
true,
18731880
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1883,6 +1890,7 @@ mod tests {
18831890
);
18841891
builder.add_view(
18851892
"my_view",
1893+
0,
18861894
true,
18871895
true,
18881896
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1937,6 +1945,7 @@ mod tests {
19371945
);
19381946
builder.add_view(
19391947
"my_view",
1948+
0,
19401949
true,
19411950
true,
19421951
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1952,6 +1961,7 @@ mod tests {
19521961
);
19531962
builder.add_view(
19541963
"my_view",
1964+
0,
19551965
true,
19561966
false,
19571967
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1970,6 +1980,7 @@ mod tests {
19701980
);
19711981
builder.add_view(
19721982
"my_view",
1983+
0,
19731984
true,
19741985
true,
19751986
ProductType::from([("x", AlgebraicType::U32)]),
@@ -1985,6 +1996,7 @@ mod tests {
19851996
);
19861997
builder.add_view(
19871998
"my_view",
1999+
0,
19882000
true,
19892001
true,
19902002
ProductType::from([("x", AlgebraicType::U32), ("y", AlgebraicType::U32)]),
@@ -2003,6 +2015,7 @@ mod tests {
20032015
);
20042016
builder.add_view(
20052017
"my_view",
2018+
0,
20062019
true,
20072020
true,
20082021
ProductType::from([("x", AlgebraicType::U32), ("y", AlgebraicType::U32)]),
@@ -2018,6 +2031,7 @@ mod tests {
20182031
);
20192032
builder.add_view(
20202033
"my_view",
2034+
0,
20212035
true,
20222036
true,
20232037
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2036,6 +2050,7 @@ mod tests {
20362050
);
20372051
builder.add_view(
20382052
"my_view",
2053+
0,
20392054
true,
20402055
true,
20412056
ProductType::from([("x", AlgebraicType::U32), ("y", AlgebraicType::U32)]),
@@ -2051,6 +2066,7 @@ mod tests {
20512066
);
20522067
builder.add_view(
20532068
"my_view",
2069+
0,
20542070
true,
20552071
true,
20562072
ProductType::from([("y", AlgebraicType::U32), ("x", AlgebraicType::U32)]),
@@ -2069,6 +2085,7 @@ mod tests {
20692085
);
20702086
builder.add_view(
20712087
"my_view",
2088+
0,
20722089
true,
20732090
true,
20742091
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2084,6 +2101,7 @@ mod tests {
20842101
);
20852102
builder.add_view(
20862103
"my_view",
2104+
0,
20872105
true,
20882106
true,
20892107
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2102,6 +2120,7 @@ mod tests {
21022120
);
21032121
builder.add_view(
21042122
"my_view",
2123+
0,
21052124
true,
21062125
true,
21072126
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2117,6 +2136,7 @@ mod tests {
21172136
);
21182137
builder.add_view(
21192138
"my_view",
2139+
0,
21202140
true,
21212141
true,
21222142
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2135,6 +2155,7 @@ mod tests {
21352155
);
21362156
builder.add_view(
21372157
"my_view",
2158+
0,
21382159
true,
21392160
true,
21402161
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2150,6 +2171,7 @@ mod tests {
21502171
);
21512172
builder.add_view(
21522173
"my_view",
2174+
0,
21532175
true,
21542176
true,
21552177
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2168,6 +2190,7 @@ mod tests {
21682190
);
21692191
builder.add_view(
21702192
"my_view",
2193+
0,
21712194
true,
21722195
true,
21732196
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2183,6 +2206,7 @@ mod tests {
21832206
);
21842207
builder.add_view(
21852208
"my_view",
2209+
0,
21862210
true,
21872211
true,
21882212
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2201,6 +2225,7 @@ mod tests {
22012225
);
22022226
builder.add_view(
22032227
"my_view",
2228+
0,
22042229
true,
22052230
true,
22062231
ProductType::from([("x", AlgebraicType::U32)]),
@@ -2216,6 +2241,7 @@ mod tests {
22162241
);
22172242
builder.add_view(
22182243
"my_view",
2244+
0,
22192245
true,
22202246
true,
22212247
ProductType::from([("x", AlgebraicType::U32)]),

crates/schema/src/def.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl ModuleDef {
248248
/// Convenience method to look up a view, possibly by a string, returning its id as well.
249249
pub fn view_full<K: ?Sized + Hash + Equivalent<Identifier>>(&self, name: &K) -> Option<(ViewId, &ViewDef)> {
250250
// If the string IS a valid identifier, we can just look it up.
251-
self.views.get_full(name).map(|(idx, _, def)| (idx.into(), def))
251+
self.views.get(name).map(|def| (def.index, def))
252252
}
253253

254254
/// Convenience method to look up a reducer, possibly by a string.
@@ -296,8 +296,12 @@ impl ModuleDef {
296296
}
297297

298298
/// Look up a view by its id, panicking if it doesn't exist.
299-
pub fn view_by_id(&self, id: ViewId) -> &ViewDef {
300-
&self.views[id.idx()]
299+
pub fn view_by_id(&self, id: ViewId, is_anonymoys: bool) -> &ViewDef {
300+
self.views
301+
.iter()
302+
.find(|(_, def)| def.index == id && def.is_anonymous == is_anonymoys)
303+
.expect("view id not found")
304+
.1
301305
}
302306

303307
/// Looks up a lifecycle reducer defined in the module.
@@ -1111,6 +1115,11 @@ pub struct ViewDef {
11111115
/// This type does not have access to the `Identity` of the caller.
11121116
pub is_anonymous: bool,
11131117

1118+
/// It represents the unique index of this view within the module.
1119+
/// Module contains separate list for anonymous and non-anonymous views,
1120+
/// so `is_anonymous` is needed to fully identify the view along with this index.
1121+
pub index: ViewId,
1122+
11141123
/// The parameters of the view.
11151124
///
11161125
/// This `ProductType` need not be registered in the module's `Typespace`.
@@ -1171,10 +1180,12 @@ impl From<ViewDef> for RawViewDefV9 {
11711180
is_public,
11721181
params,
11731182
return_type,
1183+
index,
11741184
..
11751185
} = val;
11761186
RawViewDefV9 {
11771187
name: name.into(),
1188+
index: index.into(),
11781189
is_anonymous,
11791190
is_public,
11801191
params,

crates/schema/src/def/validate/v9.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ impl ModuleValidator<'_> {
456456
is_anonymous,
457457
params,
458458
return_type,
459+
index,
459460
} = view_def;
460461

461462
let invalid_return_type = || {
@@ -541,6 +542,7 @@ impl ModuleValidator<'_> {
541542
is_anonymous,
542543
is_public,
543544
params,
545+
index: index.into(),
544546
params_for_generate: ProductTypeDef {
545547
elements: params_for_generate,
546548
recursive: false, // A `ProductTypeDef` not stored in a `Typespace` cannot be recursive.

0 commit comments

Comments
 (0)