12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ use std:: io;
15
16
use std:: time:: Duration ;
16
17
17
- use map_api:: MapApi ;
18
+ use map_api:: mvcc ;
18
19
19
20
use crate :: ExpireKey ;
21
+ use crate :: MetaValue ;
20
22
use crate :: SeqV ;
21
23
use crate :: UserKey ;
22
24
23
25
/// The API a state machine implements.
24
26
///
25
27
/// The state machine is responsible for managing the application's persistent state,
26
28
/// including application kv data and expired key data.
29
+ #[ async_trait:: async_trait]
27
30
pub trait StateMachineApi < SysData > : Send + Sync {
28
31
/// The map that stores application data.
29
- type UserMap : MapApi < UserKey > + ' static ;
32
+ type UserMap : mvcc :: ScopedView < UserKey , MetaValue > + Send + Sync + ' static ;
30
33
31
34
/// Returns a reference to the map that stores application data.
32
35
///
@@ -43,7 +46,7 @@ pub trait StateMachineApi<SysData>: Send + Sync {
43
46
fn user_map_mut ( & mut self ) -> & mut Self :: UserMap ;
44
47
45
48
/// The map that stores expired key data.
46
- type ExpireMap : MapApi < ExpireKey > + ' static ;
49
+ type ExpireMap : mvcc :: ScopedView < ExpireKey , String > + Send + Sync + ' static ;
47
50
48
51
/// Returns a reference to the map that stores expired key data.
49
52
fn expire_map ( & self ) -> & Self :: ExpireMap ;
@@ -56,20 +59,27 @@ pub trait StateMachineApi<SysData>: Send + Sync {
56
59
/// The timestamp is the duration since the Unix epoch.
57
60
/// Applications should save this timestamp when using storage with tombstones
58
61
/// 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
+ }
60
68
61
69
/// 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
+ }
67
75
68
- /// Returns a mutable reference to the system data.
76
+ /// Access the system data.
69
77
///
70
- /// This method provides read-write access to the system data, which includes
78
+ /// This method provides access to the system data, which includes
71
79
/// 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 > ;
73
83
74
84
/// Notify subscribers of a key-value change applied to the state machine.
75
85
///
0 commit comments