Skip to content

Commit e26b56c

Browse files
committed
feat: v0.2.0: mvcc style API
1 parent 637437b commit e26b56c

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "state-machine-api"
3-
version = "0.1.1"
3+
version = "0.2.1"
44
authors = ["drmingdrmer <[email protected]>"]
55
license = "Apache-2.0"
66
publish = true
@@ -9,10 +9,16 @@ edition = "2021"
99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010

1111
[dependencies]
12+
async-trait = { version = "0.1.88" }
1213
display-more = { git = "https://github.com/databendlabs/display-more", tag = "v0.2.0" }
13-
map-api = { git = "https://github.com/databendlabs/map-api", tag = "v0.2.7" }
14+
map-api = { version = "0.3.0" }
1415
serde = { version = "1.0", features = ["derive"] }
1516

1617
[dev-dependencies]
1718
anyhow = "1.0"
1819
serde_json = "1.0"
20+
21+
[patch.crates-io]
22+
map-api = { git = "https://github.com/databendlabs/map-api", tag = "v0.3.0" }
23+
# map-api = { path = "../map-api" }
24+
# seq-marked = { path = "../seq-marked" }

src/state_machine_api.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::io;
1516
use std::time::Duration;
1617

17-
use map_api::MapApi;
18+
use map_api::mvcc;
1819

1920
use crate::ExpireKey;
21+
use crate::MetaValue;
2022
use crate::SeqV;
2123
use 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]
2730
pub 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
///

src/user_key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::ops::Deref;
1717

1818
use map_api::MapKey;
1919

20-
use crate::KVMeta;
20+
use crate::MetaValue;
2121

2222
/// Key for the user data in state machine
2323
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
@@ -53,7 +53,7 @@ impl fmt::Display for UserKey {
5353
}
5454

5555
impl MapKey for UserKey {
56-
type V = (Option<KVMeta>, Vec<u8>);
56+
type V = MetaValue;
5757
}
5858

5959
impl UserKey {

0 commit comments

Comments
 (0)