@@ -39,8 +39,7 @@ use spacetimedb_data_structures::error_stream::ErrorStream;
3939use spacetimedb_data_structures:: map:: { HashCollectionExt as _, IntMap } ;
4040use spacetimedb_datastore:: error:: DatastoreError ;
4141use spacetimedb_datastore:: execution_context:: { ExecutionContext , ReducerContext , Workload , WorkloadType } ;
42- use spacetimedb_datastore:: locking_tx_datastore:: datastore:: TxMetrics ;
43- use spacetimedb_datastore:: locking_tx_datastore:: { MutTxId , TxId } ;
42+ use spacetimedb_datastore:: locking_tx_datastore:: MutTxId ;
4443use spacetimedb_datastore:: system_tables:: { ST_CLIENT_ID , ST_CONNECTION_CREDENTIALS_ID , ST_VIEW_SUB_ID } ;
4544use spacetimedb_datastore:: traits:: { IsolationLevel , Program , TxData } ;
4645use spacetimedb_durability:: DurableOffset ;
@@ -1120,7 +1119,7 @@ impl ModuleHost {
11201119 // Decrement the number of subscribers for each view this caller is subscribed to
11211120 let dec_view_subscribers = |tx : & mut MutTxId | {
11221121 if drop_view_subscribers {
1123- if let Err ( err) = tx. dec_st_view_subscribers ( caller_identity) {
1122+ if let Err ( err) = tx. unsubscribe_views ( caller_identity) {
11241123 log:: error!( "`call_identity_disconnected`: failed to delete client view data: {err}" ) ;
11251124 }
11261125 }
@@ -1489,16 +1488,19 @@ impl ModuleHost {
14891488 . await ?
14901489 }
14911490
1492- /// Downgrade this mutable `tx` after:
1493- /// 1. Collecting view ids from `view_collector` and
1494- /// 2. Materializing them if necessary
1495- pub async fn materialize_views_and_downgrade_tx (
1491+ /// Materializes the views return by the `view_collector`, if not already materialized,
1492+ /// and updates `st_view_sub` accordingly.
1493+ ///
1494+ /// Passing [`Workload::Sql`] will update `st_view_sub.last_called`.
1495+ /// Passing [`Workload::Subscribe`] will also increment `st_view_sub.num_subscribers`,
1496+ /// in addition to updating `st_view_sub.last_called`.
1497+ pub async fn materialize_views (
14961498 & self ,
14971499 mut tx : MutTxId ,
14981500 view_collector : & impl CollectViews ,
14991501 sender : Identity ,
15001502 workload : Workload ,
1501- ) -> Result < ( TxData , TxMetrics , TxId ) , ViewCallError > {
1503+ ) -> Result < MutTxId , ViewCallError > {
15021504 use FunctionArgs :: * ;
15031505 let mut view_ids = HashSet :: new ( ) ;
15041506 view_collector. collect_views ( & mut view_ids) ;
@@ -1507,9 +1509,16 @@ impl ModuleHost {
15071509 if !tx. is_view_materialized ( view_id, ArgId :: SENTINEL , sender) ? {
15081510 tx = self . call_view ( tx, & name, Nullary , sender, None ) . await ?. tx ;
15091511 }
1510- tx. st_view_sub_update_or_insert_last_called ( view_id, ArgId :: SENTINEL , sender) ?;
1512+ // If this is a sql call, we only update this view's "last called" timestamp
1513+ if let Workload :: Sql = workload {
1514+ tx. update_view_timestamp ( view_id, ArgId :: SENTINEL , sender) ?;
1515+ }
1516+ // If this is a subscribe call, we also increment this view's subscriber count
1517+ if let Workload :: Subscribe = workload {
1518+ tx. subscribe_view ( view_id, ArgId :: SENTINEL , sender) ?;
1519+ }
15111520 }
1512- Ok ( tx. commit_downgrade ( workload ) )
1521+ Ok ( tx)
15131522 }
15141523
15151524 pub async fn call_view (
0 commit comments