1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ use std:: io;
1516use std:: time:: Duration ;
1617
17- use map_api:: MapApi ;
18+ use map_api:: mvcc ;
1819
1920use crate :: ExpireKey ;
21+ use crate :: MetaValue ;
2022use crate :: SeqV ;
2123use crate :: UserKey ;
2224
2325/// The API a state machine implements.
2426///
2527/// The state machine is responsible for managing the application's persistent state,
2628/// including application kv data and expired key data.
29+ #[ async_trait:: async_trait]
2730pub trait StateMachineApi < SysData > : Send + Sync {
2831 /// The map that stores application data.
29- type UserMap : MapApi < UserKey > + ' static ;
32+ type UserMap : mvcc :: ScopedView < UserKey , MetaValue > + Send + Sync + ' static ;
3033
3134 /// Returns a reference to the map that stores application data.
3235 ///
@@ -43,7 +46,7 @@ pub trait StateMachineApi<SysData>: Send + Sync {
4346 fn user_map_mut ( & mut self ) -> & mut Self :: UserMap ;
4447
4548 /// The map that stores expired key data.
46- type ExpireMap : MapApi < ExpireKey > + ' static ;
49+ type ExpireMap : mvcc :: ScopedView < ExpireKey , String > + Send + Sync + ' static ;
4750
4851 /// Returns a reference to the map that stores expired key data.
4952 fn expire_map ( & self ) -> & Self :: ExpireMap ;
@@ -56,20 +59,27 @@ pub trait StateMachineApi<SysData>: Send + Sync {
5659 /// The timestamp is the duration since the Unix epoch.
5760 /// Applications should save this timestamp when using storage with tombstones
5861 /// that persist across cleanup rounds.
59- fn cleanup_start_timestamp ( & self ) -> Duration ;
62+ fn with_cleanup_start_timestamp < T > ( & self , f : impl FnOnce ( & mut Duration ) -> T ) -> T ;
63+
64+ /// Returns the timestamp since which to start cleaning expired keys.
65+ fn cleanup_start_timestamp ( & self ) -> Duration {
66+ self . with_cleanup_start_timestamp ( |ts| * ts)
67+ }
6068
6169 /// Set the timestamp since which to start cleaning expired keys.
62- ///
63- /// The timestamp is the duration since the Unix epoch.
64- /// Applications should save this timestamp when using storage with tombstones
65- /// that persist across cleanup rounds.
66- fn set_cleanup_start_timestamp ( & mut self , timestamp : Duration ) ;
70+ fn set_cleanup_start_timestamp ( & mut self , timestamp : Duration ) {
71+ self . with_cleanup_start_timestamp ( |ts| {
72+ * ts = timestamp ;
73+ } ) ;
74+ }
6775
68- /// Returns a mutable reference to the system data.
76+ /// Access the system data.
6977 ///
70- /// This method provides read-write access to the system data, which includes
78+ /// This method provides access to the system data, which includes
7179 /// metadata about the state machine and its configuration.
72- fn sys_data_mut ( & mut self ) -> & mut SysData ;
80+ fn with_sys_data < T > ( & self , f : impl FnOnce ( & mut SysData ) -> T ) -> T ;
81+
82+ async fn commit ( self ) -> Result < ( ) , io:: Error > ;
7383
7484 /// Notify subscribers of a key-value change applied to the state machine.
7585 ///
0 commit comments